feat(inputs.http): Rework token options (#13622)
This commit is contained in:
parent
50b6a63b98
commit
0ba463817c
|
|
@ -17,8 +17,8 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||||
|
|
||||||
## Secret-store support
|
## Secret-store support
|
||||||
|
|
||||||
This plugin supports secrets from secret-stores for the `username` and
|
This plugin supports secrets from secret-stores for the `username`, `password`
|
||||||
`password` option.
|
and `token` option.
|
||||||
See the [secret-store documentation][SECRETSTORE] for more details on how
|
See the [secret-store documentation][SECRETSTORE] for more details on how
|
||||||
to use them.
|
to use them.
|
||||||
|
|
||||||
|
|
@ -47,9 +47,10 @@ to use them.
|
||||||
## compress body or "identity" to apply no encoding.
|
## compress body or "identity" to apply no encoding.
|
||||||
# content_encoding = "identity"
|
# content_encoding = "identity"
|
||||||
|
|
||||||
## Optional file with Bearer token
|
## Optional Bearer token settings to use for the API calls.
|
||||||
## file content is added as an Authorization header
|
## Use either the token itself or the token file if you need a token.
|
||||||
# bearer_token = "/path/to/file"
|
# token = "eyJhbGc...Qssw5c"
|
||||||
|
# token_file = "/path/to/file"
|
||||||
|
|
||||||
## Optional HTTP Basic Auth Credentials
|
## Optional HTTP Basic Auth Credentials
|
||||||
# username = "username"
|
# username = "username"
|
||||||
|
|
|
||||||
|
|
@ -28,17 +28,17 @@ type HTTP struct {
|
||||||
Body string `toml:"body"`
|
Body string `toml:"body"`
|
||||||
ContentEncoding string `toml:"content_encoding"`
|
ContentEncoding string `toml:"content_encoding"`
|
||||||
|
|
||||||
Headers map[string]string `toml:"headers"`
|
// Basic authentication
|
||||||
|
|
||||||
// HTTP Basic Auth Credentials
|
|
||||||
Username config.Secret `toml:"username"`
|
Username config.Secret `toml:"username"`
|
||||||
Password config.Secret `toml:"password"`
|
Password config.Secret `toml:"password"`
|
||||||
|
|
||||||
// Absolute path to file with Bearer token
|
// Bearer authentication
|
||||||
BearerToken string `toml:"bearer_token"`
|
BearerToken string `toml:"bearer_token" deprecated:"1.28.0;use 'token_file' instead"`
|
||||||
|
Token config.Secret `toml:"token"`
|
||||||
|
TokenFile string `toml:"token_file"`
|
||||||
|
|
||||||
|
Headers map[string]string `toml:"headers"`
|
||||||
SuccessStatusCodes []int `toml:"success_status_codes"`
|
SuccessStatusCodes []int `toml:"success_status_codes"`
|
||||||
|
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
httpconfig.HTTPClientConfig
|
httpconfig.HTTPClientConfig
|
||||||
|
|
@ -52,12 +52,24 @@ func (*HTTP) SampleConfig() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTTP) Init() error {
|
func (h *HTTP) Init() error {
|
||||||
|
// For backward compatibility
|
||||||
|
if h.TokenFile != "" && h.BearerToken != "" && h.TokenFile != h.BearerToken {
|
||||||
|
return fmt.Errorf("conflicting settings for 'bearer_token' and 'token_file'")
|
||||||
|
} else if h.TokenFile == "" && h.BearerToken != "" {
|
||||||
|
h.TokenFile = h.BearerToken
|
||||||
|
}
|
||||||
|
|
||||||
|
// We cannot use multiple sources for tokens
|
||||||
|
if h.TokenFile != "" && !h.Token.Empty() {
|
||||||
|
return fmt.Errorf("either use 'token_file' or 'token' not both")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the client
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
client, err := h.HTTPClientConfig.CreateClient(ctx, h.Log)
|
client, err := h.HTTPClientConfig.CreateClient(ctx, h.Log)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
h.client = client
|
h.client = client
|
||||||
|
|
||||||
// Set default as [200]
|
// Set default as [200]
|
||||||
|
|
@ -110,7 +122,14 @@ func (h *HTTP) gatherURL(
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.BearerToken != "" {
|
if !h.Token.Empty() {
|
||||||
|
token, err := h.Token.Get()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
bearer := "Bearer " + strings.TrimSpace(string(token))
|
||||||
|
request.Header.Set("Authorization", bearer)
|
||||||
|
} else if h.TokenFile != "" {
|
||||||
token, err := os.ReadFile(h.BearerToken)
|
token, err := os.ReadFile(h.BearerToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@
|
||||||
## compress body or "identity" to apply no encoding.
|
## compress body or "identity" to apply no encoding.
|
||||||
# content_encoding = "identity"
|
# content_encoding = "identity"
|
||||||
|
|
||||||
## Optional file with Bearer token
|
## Optional Bearer token settings to use for the API calls.
|
||||||
## file content is added as an Authorization header
|
## Use either the token itself or the token file if you need a token.
|
||||||
# bearer_token = "/path/to/file"
|
# token = "eyJhbGc...Qssw5c"
|
||||||
|
# token_file = "/path/to/file"
|
||||||
|
|
||||||
## Optional HTTP Basic Auth Credentials
|
## Optional HTTP Basic Auth Credentials
|
||||||
# username = "username"
|
# username = "username"
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@
|
||||||
## compress body or "identity" to apply no encoding.
|
## compress body or "identity" to apply no encoding.
|
||||||
# content_encoding = "identity"
|
# content_encoding = "identity"
|
||||||
|
|
||||||
## Optional file with Bearer token
|
## Optional Bearer token settings to use for the API calls.
|
||||||
## file content is added as an Authorization header
|
## Use either the token itself or the token file if you need a token.
|
||||||
# bearer_token = "/path/to/file"
|
# token = "eyJhbGc...Qssw5c"
|
||||||
|
# token_file = "/path/to/file"
|
||||||
|
|
||||||
## Optional HTTP Basic Auth Credentials
|
## Optional HTTP Basic Auth Credentials
|
||||||
# username = "username"
|
# username = "username"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue