Skip to content

Commit

Permalink
Remove boolean to enable TLS, refactor TLS part
Browse files Browse the repository at this point in the history
  • Loading branch information
phlipse committed Jan 30, 2018
1 parent de9d7fa commit ffc0690
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
1 change: 0 additions & 1 deletion plugins/outputs/prometheus_client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ This plugin starts a [Prometheus](https://prometheus.io/) Client, it exposes all
listen = ":9273"
# Use TLS
tls = true
tls_cert = "/etc/ssl/telegraf.crt"
tls_key = "/etc/ssl/telegraf.key"
Expand Down
70 changes: 32 additions & 38 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ type MetricFamily struct {

type PrometheusClient struct {
Listen string
TLS bool `toml:"tls"`
TLSCert string `toml:"tls_cert"`
TLSKey string `toml:"tls_key"`
BasicAuth bool `toml:"basic_auth"`
Expand All @@ -78,9 +77,8 @@ var sampleConfig = `
# listen = ":9273"
## Use TLS
# tls = true
tls_cert = "/etc/ssl/telegraf.crt"
tls_key = "/etc/ssl/telegraf.key"
#tls_cert = "/etc/ssl/telegraf.crt"
#tls_key = "/etc/ssl/telegraf.key"
## Use http basic authentication
# basic_auth = true
Expand All @@ -95,6 +93,20 @@ var sampleConfig = `
collectors_exclude = ["gocollector", "process"]
`

func (p *PrometheusClient) getTLSConfig() *tls.Config {
if p.TLSCert != "" && p.TLSKey != "" {
return &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
}
}

return nil
}

func (p *PrometheusClient) basicAuth(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if p.BasicAuth {
Expand Down Expand Up @@ -151,42 +163,24 @@ func (p *PrometheusClient) Start() error {
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,
},
},
}
p.server = &http.Server{
Addr: p.Listen,
Handler: mux,
TLSConfig: p.getTLSConfig(),
}

go func() {
if err := p.server.ListenAndServeTLS(p.TLSCert, 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() {
var err error
if p.server.TLSConfig != nil {
err = p.server.ListenAndServeTLS(p.TLSCert, p.TLSKey)
} else {
err = p.server.ListenAndServe()
}

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())
}
}
}()
}
if err != nil && err != http.ErrServerClosed {
log.Printf("E! Error creating prometheus metric endpoint, err: %s\n",
err.Error())
}
}()

return nil
}
Expand Down

0 comments on commit ffc0690

Please sign in to comment.