NATS output plug-in use internal.GetTLSConfig to instrument TLS configuration.

This commit is contained in:
Paulo Pires 2016-09-06 11:03:35 +01:00
parent cabcb43228
commit 715ff4f5ef
1 changed files with 9 additions and 19 deletions

View File

@ -1,14 +1,12 @@
package nats package nats
import ( import (
"crypto/tls"
"crypto/x509"
"fmt" "fmt"
"io/ioutil"
nats_client "github.com/nats-io/nats" nats_client "github.com/nats-io/nats"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/serializers" "github.com/influxdata/telegraf/plugins/serializers"
) )
@ -58,6 +56,7 @@ func (n *NATS) SetSerializer(serializer serializers.Serializer) {
} }
func (n *NATS) Connect() error { func (n *NATS) Connect() error {
var err error
// set NATS connection options // set NATS connection options
opts := nats_client.DefaultOptions opts := nats_client.DefaultOptions
opts.Servers = n.Servers opts.Servers = n.Servers
@ -67,27 +66,18 @@ func (n *NATS) Connect() error {
} }
// is TLS enabled? // is TLS enabled?
var tlsConfig tls.Config tlsConfig, err := internal.GetTLSConfig(
tlsConfig.InsecureSkipVerify = n.InsecureSkipVerify "", "", n.CAFile, n.InsecureSkipVerify)
if n.CAFile != "" { if err != nil {
rootPEM, err := ioutil.ReadFile(n.CAFile) return err
if err != nil || rootPEM == nil { }
return fmt.Errorf("FAILED to connect to NATS (can't read root certificate): %s", err) if tlsConfig != nil {
}
pool := x509.NewCertPool()
ok := pool.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
return fmt.Errorf("FAILED to connect to NATS (can't parse root certificate): %s", err)
}
tlsConfig.RootCAs = pool
// set NATS connection TLS options // set NATS connection TLS options
opts.Secure = true opts.Secure = true
opts.TLSConfig = &tlsConfig opts.TLSConfig = tlsConfig
} }
// try and connect // try and connect
var err error
n.conn, err = opts.Connect() n.conn, err = opts.Connect()
return err return err