From 27f09758ba1555967ead775222a24bc1618c32f5 Mon Sep 17 00:00:00 2001 From: M0rdecay <50422107+M0rdecay@users.noreply.github.com> Date: Tue, 14 Apr 2020 22:31:26 +0300 Subject: [PATCH] Add reading bearer token from a file to http input (#7304) --- plugins/inputs/http/README.md | 4 ++++ plugins/inputs/http/http.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/plugins/inputs/http/README.md b/plugins/inputs/http/README.md index 9cd136bd0..59abd8256 100644 --- a/plugins/inputs/http/README.md +++ b/plugins/inputs/http/README.md @@ -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" diff --git a/plugins/inputs/http/http.go b/plugins/inputs/http/http.go index 13c9cd170..8290a6f66 100644 --- a/plugins/inputs/http/http.go +++ b/plugins/inputs/http/http.go @@ -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"` @@ -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" @@ -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") }