feat(inputs.prometheus): Allow to use secrets for credentials (#15865)

This commit is contained in:
Sven Rebhan 2024-09-12 23:02:30 +02:00 committed by GitHub
parent 9988154e6c
commit 63ac48010f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View File

@ -12,6 +12,14 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Secret-store support
This plugin supports secrets from secret-stores for the `username`, `password`
and `bearer_token_string` option. See the
[secret-store documentation][SECRETSTORE] for more details on how to use them.
[SECRETSTORE]: ../../../docs/CONFIGURATION.md#secret-store-secrets
## Configuration
```toml @sample.conf

View File

@ -50,9 +50,9 @@ type PodID string
type Prometheus struct {
URLs []string `toml:"urls"`
BearerToken string `toml:"bearer_token"`
BearerTokenString string `toml:"bearer_token_string"`
Username string `toml:"username"`
Password string `toml:"password"`
BearerTokenString config.Secret `toml:"bearer_token_string"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
HTTPHeaders map[string]string `toml:"http_headers"`
ContentLengthLimit config.Size `toml:"content_length_limit"`
ContentTypeOverride string `toml:"content_type_override"`
@ -432,10 +432,25 @@ func (p *Prometheus) gatherURL(u URLAndAddress, acc telegraf.Accumulator) (map[s
return nil, nil, err
}
req.Header.Set("Authorization", "Bearer "+string(token))
} else if p.BearerTokenString != "" {
req.Header.Set("Authorization", "Bearer "+p.BearerTokenString)
} else if p.Username != "" || p.Password != "" {
req.SetBasicAuth(p.Username, p.Password)
} else if !p.BearerTokenString.Empty() {
token, err := p.BearerTokenString.Get()
if err != nil {
return nil, nil, fmt.Errorf("getting token secret failed: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token.String())
token.Destroy()
} else if !p.Username.Empty() || !p.Password.Empty() {
username, err := p.Username.Get()
if err != nil {
return nil, nil, fmt.Errorf("getting username secret failed: %w", err)
}
password, err := p.Password.Get()
if err != nil {
return nil, nil, fmt.Errorf("getting password secret failed: %w", err)
}
req.SetBasicAuth(username.String(), password.String())
username.Destroy()
password.Destroy()
}
for key, value := range p.HTTPHeaders {