Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prometheus_client plugin: Add transport encryption via TLS and authentication via http basic_auth #3719

Merged
merged 6 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions plugins/outputs/prometheus_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ This plugin starts a [Prometheus](https://prometheus.io/) Client, it exposes all
# Address to listen on
listen = ":9273"

# Use TLS
tls = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this option, if other tls options are set then enable tls, see http_listener input for an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in ffc0690

tls_crt = "/etc/ssl/telegraf.crt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this tls_cert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in de9d7fa

tls_key = "/etc/ssl/telegraf.key"

# Use http basic authentication
basic_auth = true
username = "Foo"
password = "Bar"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For basic auth lets do it like in #3496, enable if either username or password is set and no enable flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in bfe91ac


# Path to publish the metrics on, defaults to /metrics
path = "/metrics"

Expand Down
88 changes: 75 additions & 13 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prometheus_client

import (
"context"
"crypto/tls"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -53,6 +54,12 @@ type MetricFamily struct {

type PrometheusClient struct {
Listen string
TLS bool `toml:"tls"`
TLSCrt string `toml:"tls_crt"`
TLSKey string `toml:"tls_key"`
BasicAuth bool `toml:"basic_auth"`
Username string `toml:"username"`
Password string `toml:"password"`
ExpirationInterval internal.Duration `toml:"expiration_interval"`
Path string `toml:"path"`
CollectorsExclude []string `toml:"collectors_exclude"`
Expand All @@ -70,6 +77,16 @@ var sampleConfig = `
## Address to listen on
# listen = ":9273"

## Use TLS
# tls = true
tls_crt = "/etc/ssl/telegraf.crt"
tls_key = "/etc/ssl/telegraf.key"

## Use http basic authentication
# basic_auth = true
username = "Foo"
password = "Bar"

## Interval to expire metrics and not deliver to prometheus, 0 == no expiration
# expiration_interval = "60s"

Expand All @@ -78,6 +95,27 @@ var sampleConfig = `
collectors_exclude = ["gocollector", "process"]
`

func (p *PrometheusClient) basicAuth(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p.BasicAuth {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)

username, password, ok := r.BasicAuth()
if !ok {
http.Error(w, "Not authorized", 401)
return
}

if username != p.Username || password != p.Password {
http.Error(w, "Not authorized", 401)
return
}
}

h.ServeHTTP(w, r)
})
}

func (p *PrometheusClient) Start() error {
defaultCollectors := map[string]bool{
"gocollector": true,
Expand Down Expand Up @@ -110,22 +148,46 @@ func (p *PrometheusClient) Start() error {
}

mux := http.NewServeMux()
mux.Handle(p.Path, promhttp.HandlerFor(
registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError}))

p.server = &http.Server{
Addr: p.Listen,
Handler: mux,
}
mux.Handle(p.Path, p.basicAuth(promhttp.HandlerFor(
registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})))

if p.TLS {
p.server = &http.Server{
Addr: p.Listen,
Handler: mux,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
},
}

go func() {
if err := p.server.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error())
go func() {
if err := p.server.ListenAndServeTLS(p.TLSCrt, p.TLSKey); err != nil {
if err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus tls secured metric endpoint, err: %s\n",
err.Error())
}
}
}()
} else {
p.server = &http.Server{
Addr: p.Listen,
Handler: mux,
}
}()

go func() {
if err := p.server.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error())
}
}
}()
}

return nil
}

Expand Down