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

feat(common.cookie): Allow usage of secrets for header #15640

Merged
merged 2 commits into from
Aug 6, 2024
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
14 changes: 11 additions & 3 deletions plugins/common/cookie/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CookieAuthConfig struct {
URL string `toml:"cookie_auth_url"`
Method string `toml:"cookie_auth_method"`

Headers map[string]string `toml:"cookie_auth_headers"`
Headers map[string]*config.Secret `toml:"cookie_auth_headers"`
powersj marked this conversation as resolved.
Show resolved Hide resolved

// HTTP Basic Auth Credentials
Username string `toml:"cookie_auth_username"`
Expand Down Expand Up @@ -98,11 +98,19 @@ func (c *CookieAuthConfig) auth() error {
}

for k, v := range c.Headers {
secret, err := v.Get()
if err != nil {
return err
}

headerVal := secret.String()
if strings.EqualFold(k, "host") {
req.Host = v
req.Host = headerVal
} else {
req.Header.Add(k, v)
req.Header.Add(k, headerVal)
}

secret.Destroy()
}

resp, err := c.client.Do(req)
Expand Down
6 changes: 4 additions & 2 deletions plugins/common/cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var fakeCookie = &http.Cookie{
Value: "this is an auth cookie",
}

var reqHeaderValSecret = config.NewSecret([]byte(reqHeaderVal))

type fakeServer struct {
*httptest.Server
*int32
Expand Down Expand Up @@ -123,7 +125,7 @@ func TestAuthConfig_Start(t *testing.T) {
Username string
Password string
Body string
Headers map[string]string
Headers map[string]*config.Secret
}
type args struct {
renewal time.Duration
Expand Down Expand Up @@ -157,7 +159,7 @@ func TestAuthConfig_Start(t *testing.T) {
endpoint: authEndpointWithHeader,
},
fields: fields{
Headers: map[string]string{reqHeaderKey: reqHeaderVal},
Headers: map[string]*config.Secret{reqHeaderKey: &reqHeaderValSecret},
},
firstAuthCount: 1,
lastAuthCount: 3,
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Secret-store support

This plugin supports secrets from secret-stores for the `username`, `password`,
`token` and `headers` option.
`token`, `headers`, and `cookie_auth_headers` option.
See the [secret-store documentation][SECRETSTORE] for more details on how
to use them.

Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Secret-store support

This plugin supports secrets from secret-stores for the `username`, `password`
and `headers` option.
`headers`, and `cookie_auth_headers` option.
See the [secret-store documentation][SECRETSTORE] for more details on how
to use them.

Expand Down
Loading