diff --git a/app/envoy-scraper/digest_http_test.go b/app/envoy-scraper/digest_http_test.go new file mode 100644 index 0000000..5b038a9 --- /dev/null +++ b/app/envoy-scraper/digest_http_test.go @@ -0,0 +1,101 @@ +package main + +import ( + "net/http" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_getCnonce(t *testing.T) { + t.Run("getCnonce", func(t *testing.T) { + got := getCnonce() + require.Len(t, got, 16) + }) +} + +func Test_getMD5(t *testing.T) { + tests := []struct { + arg string + want string + }{ + {"foo", "acbd18db4cc2f85cedef654fccc4a4d8"}, + {"bar", "37b51d194a7513e45b56f6524f2d51f2"}, + {strings.Repeat("uhoh", 10), "18212ada675a1b66a3ea6e61d1560b4e"}, + } + for _, tt := range tests { + t.Run(tt.arg, func(t *testing.T) { + got := getMD5(tt.arg) + assert.Equal(t, tt.want, got) + }) + } +} + +func Test_getDigestParts(t *testing.T) { + tests := []struct { + name string + headers http.Header + want map[string]string + }{ + { + "no authenticate header", + http.Header{}, + map[string]string{}, + }, { + "header with nonce", + http.Header{"Www-Authenticate": []string{`Digest, nonce="foo"`}}, + map[string]string{"nonce": "foo"}, + }, { + "header with extra parts", + http.Header{"Www-Authenticate": []string{`Digest, nonce="foo", whatever="this"`}}, + map[string]string{"nonce": "foo"}, + }, { + "all wanted components", + http.Header{"Www-Authenticate": []string{`Digest, nonce="foo",realm="example.com",qop="auth"`}}, + map[string]string{"nonce": "foo", "realm": "example.com", "qop": "auth"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := getDigestParts(tt.headers) + assert.Equal(t, tt.want, got) + }) + } +} + +func Test_getDigestAuthorization(t *testing.T) { + type args struct { + nonce string + parts map[string]string + } + tests := []struct { + name string + args args + want string + }{ + { + "all parts", + args{ + "abcdef", + map[string]string{ + "username": "alice", + "realm": "example.com", + "password": "secret", + "method": "GET", + "uri": "/foo", + "nonce": "abcdef", + "qop": "auth", + }, + }, + `Digest username="alice", realm="example.com", nonce="abcdef", uri="/foo", cnonce="abcdef", nc="1", qop="auth", response="d1a132d3ab82343c98d18abe957b67d0"`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := getDigestAuthorization(tt.args.nonce, tt.args.parts) + assert.Equal(t, tt.want, got) + }) + } +} diff --git a/go.mod b/go.mod index 9133aac..139cec4 100644 --- a/go.mod +++ b/go.mod @@ -5,16 +5,20 @@ go 1.20 require ( github.com/gorilla/handlers v1.5.1 github.com/gorilla/mux v1.8.0 - github.com/influxdata/influxdb-client-go/v2 v2.12.2 + github.com/influxdata/influxdb-client-go/v2 v2.12.3 github.com/skandragon/gohealthcheck v1.0.3 + github.com/stretchr/testify v1.8.1 ) require ( github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/deepmap/oapi-codegen v1.12.4 // indirect github.com/felixge/httpsnoop v1.0.3 // indirect github.com/google/uuid v1.3.0 // indirect github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf // indirect github.com/pkg/errors v0.9.1 // indirect - golang.org/x/net v0.8.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/net v0.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f1d9fc1..225c2ea 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/deepmap/oapi-codegen v1.12.4 h1:pPmn6qI9MuOtCz82WY2Xaw46EQjgvxednXXrP7g5Q2s= github.com/deepmap/oapi-codegen v1.12.4/go.mod h1:3lgHGMu6myQ2vqbbTXH2H1o4eXFTGnFiDaOaKKl5yas= github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= @@ -15,8 +16,8 @@ github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/influxdata/influxdb-client-go/v2 v2.12.2 h1:uYABKdrEKlYm+++qfKdbgaHKBPmoWR5wpbmj6MBB/2g= -github.com/influxdata/influxdb-client-go/v2 v2.12.2/go.mod h1:YteV91FiQxRdccyJ2cHvj2f/5sq4y4Njqu1fQzsQCOU= +github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0IpXeMSkY/uJa/O/vC4= +github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0= github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf h1:7JTmneyiNEwVBOHSjoMxiWAqB992atOeepeFYegn5RU= github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= @@ -28,8 +29,17 @@ github.com/skandragon/gohealthcheck v1.0.3 h1:Q4gIYvX2eBfEuM9msyK6NapTPWK0zgpyuU github.com/skandragon/gohealthcheck v1.0.3/go.mod h1:fEu59f3K+54jXlAOs0YDHojk7XximptsxYzCBXd9+oI= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= -golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ= -golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=