mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-02 00:10:45 +02:00
config: add metrics_basic_auth option (#1917)
* config: add metrics_basic_auth option * remove println * use constant time compare
This commit is contained in:
parent
03d8ffaee2
commit
8b42eb5ebd
8 changed files with 309 additions and 170 deletions
|
@ -1,12 +1,14 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMetricsManager(t *testing.T) {
|
||||
|
@ -54,3 +56,32 @@ func TestMetricsManager(t *testing.T) {
|
|||
assert.Equal(t, 500, getStatusCode(addr1))
|
||||
assert.Equal(t, 200, getStatusCode(addr2))
|
||||
}
|
||||
|
||||
func TestMetricsManagerBasicAuth(t *testing.T) {
|
||||
li1, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
addr1 := li1.Addr().String()
|
||||
li1.Close()
|
||||
|
||||
src := NewStaticSource(&Config{
|
||||
Options: &Options{
|
||||
MetricsAddr: addr1,
|
||||
MetricsBasicAuth: base64.StdEncoding.EncodeToString([]byte("x:y")),
|
||||
},
|
||||
})
|
||||
mgr := NewMetricsManager(src)
|
||||
defer mgr.Close()
|
||||
|
||||
res, err := http.Get(fmt.Sprintf("http://%s/metrics", addr1))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
|
||||
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/metrics", addr1), nil)
|
||||
require.NoError(t, err)
|
||||
req.SetBasicAuth("x", "y")
|
||||
res, err = http.DefaultClient.Do(req)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, http.StatusOK, res.StatusCode)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue