Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Bearer token parsing #155

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading