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

Adds the ability to read an authorization token from a file #7304

Merged
merged 4 commits into from
Apr 14, 2020
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
4 changes: 4 additions & 0 deletions plugins/inputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
## compress body or "identity" to apply no encoding.
# content_encoding = "identity"

## Optional file with Bearer token
## file content is added as an Authorization header
# bearer_token = "/path/to/file"

## Optional HTTP Basic Auth Credentials
# username = "username"
# password = "pa$$word"
Expand Down
16 changes: 16 additions & 0 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type HTTP struct {
Password string `toml:"password"`
tls.ClientConfig

// Absolute path to file with Bearer token
BearerToken string `toml:"bearer_token"`

SuccessStatusCodes []int `toml:"success_status_codes"`

Timeout internal.Duration `toml:"timeout"`
Expand All @@ -52,6 +55,10 @@ var sampleConfig = `
## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}

## Optional file with Bearer token
## file content is added as an Authorization header
# bearer_token = "/path/to/file"

## Optional HTTP Basic Auth Credentials
# username = "username"
# password = "pa$$word"
Expand Down Expand Up @@ -160,6 +167,15 @@ func (h *HTTP) gatherURL(
return err
}

if h.BearerToken != "" {
token, err := ioutil.ReadFile(h.BearerToken)
if err != nil {
return err
}
bearer := "Bearer " + strings.Trim(string(token), "\n")
request.Header.Set("Authorization", bearer)
}

if h.ContentEncoding == "gzip" {
request.Header.Set("Content-Encoding", "gzip")
}
Expand Down