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

Support Bearer Token #145

Merged
merged 1 commit into from
Sep 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/remotewrite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Config struct {
// Password is the Password for the Basic Auth.
Password null.String `json:"password"`

// BearerToken if set is the token used for the `Authorization` header.
BearerToken null.String `json:"bearerToken"`

// PushInterval defines the time between flushes. The Output will wait the set time
// before push a new set of time series to the endpoint.
PushInterval types.NullDuration `json:"pushInterval"`
Expand Down Expand Up @@ -95,6 +98,14 @@ func (conf Config) RemoteConfig() (*remote.HTTPConfig, error) {
hc.Headers.Add(k, v)
}
}

if conf.BearerToken.String != "" {
if hc.Headers == nil {
hc.Headers = make(http.Header)
}
hc.Headers.Set("Authorization", "Bearer "+conf.BearerToken.String)
}

return &hc, nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/remotewrite/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ func TestConfigRemoteConfig(t *testing.T) {
Password: null.StringFrom("mypass"),
Headers: map[string]string{
"X-MYCUSTOM-HEADER": "val1",
// it asserts that Authz header is overwritten if the token is set
"Authorization": "pre-set-token",
},
BearerToken: null.StringFrom("my-fake-token"),
}

headers := http.Header{}
headers.Set("X-MYCUSTOM-HEADER", "val1")
headers.Set("Authorization", "Bearer my-fake-token")
exprcc := &remote.HTTPConfig{
Timeout: 5 * time.Second,
TLSConfig: &tls.Config{
Expand Down