Skip to content

Commit

Permalink
Add reading bearer token from a file to http input (influxdata#7304)
Browse files Browse the repository at this point in the history
  • Loading branch information
M0rdecay authored and Mathieu Lecarme committed Apr 17, 2020
1 parent 7f95db9 commit 7dd9305
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 7dd9305

Please sign in to comment.