Skip to content

Commit

Permalink
Fix the Bearer token parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Oct 9, 2023
1 parent cbd5b9f commit c3c0795
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ func (conf Config) Apply(applied Config) Config {
conf.Password = applied.Password
}

if applied.BearerToken.Valid {
conf.BearerToken = applied.BearerToken
}

if applied.PushInterval.Valid {
conf.PushInterval = applied.PushInterval
}
Expand Down Expand Up @@ -276,6 +280,10 @@ func parseEnvs(env map[string]string) (Config, error) {
c.ClientCertificateKey = null.StringFrom(clientCertificateKey)
}

if token, tokenDefined := env["K6_PROMETHEUS_RW_BEARER_TOKEN"]; tokenDefined {
c.BearerToken = null.StringFrom(token)
}

envHeaders := envMap(env, "K6_PROMETHEUS_RW_HEADERS_")
for k, v := range envHeaders {
c.Headers[k] = v
Expand Down
34 changes: 34 additions & 0 deletions pkg/remotewrite/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,40 @@ func TestOptionBasicAuth(t *testing.T) {
}
}

func TestOptionBearerToken(t *testing.T) {
t.Parallel()

cases := map[string]struct {
arg string
env map[string]string
jsonRaw json.RawMessage
}{
"JSON": {jsonRaw: json.RawMessage(`{"bearerToken":"my-bearer-token"}`)},
"Env": {env: map[string]string{"K6_PROMETHEUS_RW_BEARER_TOKEN": "my-bearer-token"}},
}

expconfig := Config{
ServerURL: null.StringFrom("http://localhost:9090/api/v1/write"),
InsecureSkipTLSVerify: null.BoolFrom(false),
BearerToken: null.StringFrom("my-bearer-token"),
PushInterval: types.NullDurationFrom(5 * time.Second),
Headers: make(map[string]string),
TrendStats: []string{"p(99)"},
StaleMarkers: null.BoolFrom(false),
}

for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
c, err := GetConsolidatedConfig(
tc.jsonRaw, tc.env, tc.arg)
require.NoError(t, err)
assert.Equal(t, expconfig, c)
})
}
}

func TestOptionClientCertificate(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit c3c0795

Please sign in to comment.