Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit c3c0795

Browse files
committed
Fix the Bearer token parsing
1 parent cbd5b9f commit c3c0795

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

pkg/remotewrite/config.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ func (conf Config) Apply(applied Config) Config {
145145
conf.Password = applied.Password
146146
}
147147

148+
if applied.BearerToken.Valid {
149+
conf.BearerToken = applied.BearerToken
150+
}
151+
148152
if applied.PushInterval.Valid {
149153
conf.PushInterval = applied.PushInterval
150154
}
@@ -276,6 +280,10 @@ func parseEnvs(env map[string]string) (Config, error) {
276280
c.ClientCertificateKey = null.StringFrom(clientCertificateKey)
277281
}
278282

283+
if token, tokenDefined := env["K6_PROMETHEUS_RW_BEARER_TOKEN"]; tokenDefined {
284+
c.BearerToken = null.StringFrom(token)
285+
}
286+
279287
envHeaders := envMap(env, "K6_PROMETHEUS_RW_HEADERS_")
280288
for k, v := range envHeaders {
281289
c.Headers[k] = v

pkg/remotewrite/config_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,40 @@ func TestOptionBasicAuth(t *testing.T) {
382382
}
383383
}
384384

385+
func TestOptionBearerToken(t *testing.T) {
386+
t.Parallel()
387+
388+
cases := map[string]struct {
389+
arg string
390+
env map[string]string
391+
jsonRaw json.RawMessage
392+
}{
393+
"JSON": {jsonRaw: json.RawMessage(`{"bearerToken":"my-bearer-token"}`)},
394+
"Env": {env: map[string]string{"K6_PROMETHEUS_RW_BEARER_TOKEN": "my-bearer-token"}},
395+
}
396+
397+
expconfig := Config{
398+
ServerURL: null.StringFrom("http://localhost:9090/api/v1/write"),
399+
InsecureSkipTLSVerify: null.BoolFrom(false),
400+
BearerToken: null.StringFrom("my-bearer-token"),
401+
PushInterval: types.NullDurationFrom(5 * time.Second),
402+
Headers: make(map[string]string),
403+
TrendStats: []string{"p(99)"},
404+
StaleMarkers: null.BoolFrom(false),
405+
}
406+
407+
for name, tc := range cases {
408+
tc := tc
409+
t.Run(name, func(t *testing.T) {
410+
t.Parallel()
411+
c, err := GetConsolidatedConfig(
412+
tc.jsonRaw, tc.env, tc.arg)
413+
require.NoError(t, err)
414+
assert.Equal(t, expconfig, c)
415+
})
416+
}
417+
}
418+
385419
func TestOptionClientCertificate(t *testing.T) {
386420
t.Parallel()
387421

0 commit comments

Comments
 (0)