Skip to content

Commit

Permalink
Merge pull request #459 from TrafeX/api-key-authorization
Browse files Browse the repository at this point in the history
Add the ability to use ApiKey based authentication
  • Loading branch information
sysadmind authored Jul 27, 2021
2 parents 3d6277d + 41e0f4e commit 008e29e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ elasticsearch_exporter --help
| es.client-cert | 1.0.2 | Path to PEM file that contains the corresponding cert for the private key to connect to Elasticsearch. | |
| es.clusterinfo.interval | 1.1.0rc1 | Cluster info update interval for the cluster label | 5m |
| es.ssl-skip-verify | 1.0.4rc1 | Skip SSL verification when connecting to Elasticsearch. | false |
| es.apiKey | unreleased | API Key to use for authenticating against Elasticsearch. | |
| web.listen-address | 1.0.2 | Address to listen on for web interface and telemetry. | :9114 |
| web.telemetry-path | 1.0.2 | Path under which to expose metrics. | /metrics |
| version | 1.0.2 | Show version info on stdout and exit. | |
Expand Down
37 changes: 32 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package main

import (
"fmt"
"net/http"
"net/url"
"os"
Expand All @@ -31,6 +32,16 @@ import (
"gopkg.in/alecthomas/kingpin.v2"
)

type transportWithApiKey struct {
underlyingTransport http.RoundTripper
apiKey string
}

func (t *transportWithApiKey) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("ApiKey %s", t.apiKey))
return t.underlyingTransport.RoundTrip(req)
}

func main() {
var (
Name = "elasticsearch_exporter"
Expand Down Expand Up @@ -85,6 +96,9 @@ func main() {
esInsecureSkipVerify = kingpin.Flag("es.ssl-skip-verify",
"Skip SSL verification when connecting to Elasticsearch.").
Default("false").Envar("ES_SSL_SKIP_VERIFY").Bool()
esApiKey = kingpin.Flag("es.apiKey",
"API Key to use for authenticating against Elasticsearch").
Default("").Envar("ES_API_KEY").String()
logLevel = kingpin.Flag("log.level",
"Sets the loglevel. Valid levels are debug, info, warn, error").
Default("info").Envar("LOG_LEVEL").String()
Expand Down Expand Up @@ -114,12 +128,25 @@ func main() {
// returns nil if not provided and falls back to simple TCP.
tlsConfig := createTLSConfig(*esCA, *esClientCert, *esClientPrivateKey, *esInsecureSkipVerify)

var httpTransport http.RoundTripper

httpTransport = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
}

if *esApiKey != "" {
apiKey := *esApiKey

httpTransport = &transportWithApiKey{
underlyingTransport: httpTransport,
apiKey: apiKey,
}
}

httpClient := &http.Client{
Timeout: *esTimeout,
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
},
Timeout: *esTimeout,
Transport: httpTransport,
}

// version metric
Expand Down

0 comments on commit 008e29e

Please sign in to comment.