Skip to content

Commit 008e29e

Browse files
authored
Merge pull request #459 from TrafeX/api-key-authorization
Add the ability to use ApiKey based authentication
2 parents 3d6277d + 41e0f4e commit 008e29e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ elasticsearch_exporter --help
5757
| es.client-cert | 1.0.2 | Path to PEM file that contains the corresponding cert for the private key to connect to Elasticsearch. | |
5858
| es.clusterinfo.interval | 1.1.0rc1 | Cluster info update interval for the cluster label | 5m |
5959
| es.ssl-skip-verify | 1.0.4rc1 | Skip SSL verification when connecting to Elasticsearch. | false |
60+
| es.apiKey | unreleased | API Key to use for authenticating against Elasticsearch. | |
6061
| web.listen-address | 1.0.2 | Address to listen on for web interface and telemetry. | :9114 |
6162
| web.telemetry-path | 1.0.2 | Path under which to expose metrics. | /metrics |
6263
| version | 1.0.2 | Show version info on stdout and exit. | |

main.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package main
1515

1616
import (
17+
"fmt"
1718
"net/http"
1819
"net/url"
1920
"os"
@@ -31,6 +32,16 @@ import (
3132
"gopkg.in/alecthomas/kingpin.v2"
3233
)
3334

35+
type transportWithApiKey struct {
36+
underlyingTransport http.RoundTripper
37+
apiKey string
38+
}
39+
40+
func (t *transportWithApiKey) RoundTrip(req *http.Request) (*http.Response, error) {
41+
req.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", t.apiKey))
42+
return t.underlyingTransport.RoundTrip(req)
43+
}
44+
3445
func main() {
3546
var (
3647
Name = "elasticsearch_exporter"
@@ -85,6 +96,9 @@ func main() {
8596
esInsecureSkipVerify = kingpin.Flag("es.ssl-skip-verify",
8697
"Skip SSL verification when connecting to Elasticsearch.").
8798
Default("false").Envar("ES_SSL_SKIP_VERIFY").Bool()
99+
esApiKey = kingpin.Flag("es.apiKey",
100+
"API Key to use for authenticating against Elasticsearch").
101+
Default("").Envar("ES_API_KEY").String()
88102
logLevel = kingpin.Flag("log.level",
89103
"Sets the loglevel. Valid levels are debug, info, warn, error").
90104
Default("info").Envar("LOG_LEVEL").String()
@@ -114,12 +128,25 @@ func main() {
114128
// returns nil if not provided and falls back to simple TCP.
115129
tlsConfig := createTLSConfig(*esCA, *esClientCert, *esClientPrivateKey, *esInsecureSkipVerify)
116130

131+
var httpTransport http.RoundTripper
132+
133+
httpTransport = &http.Transport{
134+
TLSClientConfig: tlsConfig,
135+
Proxy: http.ProxyFromEnvironment,
136+
}
137+
138+
if *esApiKey != "" {
139+
apiKey := *esApiKey
140+
141+
httpTransport = &transportWithApiKey{
142+
underlyingTransport: httpTransport,
143+
apiKey: apiKey,
144+
}
145+
}
146+
117147
httpClient := &http.Client{
118-
Timeout: *esTimeout,
119-
Transport: &http.Transport{
120-
TLSClientConfig: tlsConfig,
121-
Proxy: http.ProxyFromEnvironment,
122-
},
148+
Timeout: *esTimeout,
149+
Transport: httpTransport,
123150
}
124151

125152
// version metric

0 commit comments

Comments
 (0)