From 66beeb6523b3fa988b8c4420e7f2fd2b9cc653ff Mon Sep 17 00:00:00 2001 From: Greg <2653109+glinton@users.noreply.github.com> Date: Mon, 15 Jul 2019 17:41:29 -0600 Subject: [PATCH] Add basic auth support to elasticsearch input (#6122) --- plugins/inputs/elasticsearch/README.md | 4 +++ plugins/inputs/elasticsearch/elasticsearch.go | 28 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/plugins/inputs/elasticsearch/README.md b/plugins/inputs/elasticsearch/README.md index a9d4db0c8fe8f..445e6f82efab9 100644 --- a/plugins/inputs/elasticsearch/README.md +++ b/plugins/inputs/elasticsearch/README.md @@ -40,6 +40,10 @@ or [cluster-stats](https://www.elastic.co/guide/en/elasticsearch/reference/curre ## "breaker". Per default, all stats are gathered. # node_stats = ["jvm", "http"] + ## HTTP Basic Authentication username and password. + # username = "" + # password = "" + ## Optional TLS Config # tls_ca = "/etc/telegraf/ca.pem" # tls_cert = "/etc/telegraf/cert.pem" diff --git a/plugins/inputs/elasticsearch/elasticsearch.go b/plugins/inputs/elasticsearch/elasticsearch.go index 70377320fb733..71ef2a01a4b19 100644 --- a/plugins/inputs/elasticsearch/elasticsearch.go +++ b/plugins/inputs/elasticsearch/elasticsearch.go @@ -119,6 +119,10 @@ const sampleConfig = ` ## "breaker". Per default, all stats are gathered. # node_stats = ["jvm", "http"] + ## HTTP Basic Authentication username and password. + # username = "" + # password = "" + ## Optional TLS Config # tls_ca = "/etc/telegraf/ca.pem" # tls_cert = "/etc/telegraf/cert.pem" @@ -138,6 +142,8 @@ type Elasticsearch struct { ClusterStats bool ClusterStatsOnlyFromMaster bool NodeStats []string + Username string `toml:"username"` + Password string `toml:"password"` tls.ClientConfig client *http.Client @@ -455,7 +461,16 @@ func (e *Elasticsearch) gatherClusterStats(url string, acc telegraf.Accumulator) } func (e *Elasticsearch) getCatMaster(url string) (string, error) { - r, err := e.client.Get(url) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return "", err + } + + if e.Username != "" || e.Password != "" { + req.SetBasicAuth(e.Username, e.Password) + } + + r, err := e.client.Do(req) if err != nil { return "", err } @@ -478,7 +493,16 @@ func (e *Elasticsearch) getCatMaster(url string) (string, error) { } func (e *Elasticsearch) gatherJsonData(url string, v interface{}) error { - r, err := e.client.Get(url) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return err + } + + if e.Username != "" || e.Password != "" { + req.SetBasicAuth(e.Username, e.Password) + } + + r, err := e.client.Do(req) if err != nil { return err }