-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add TLS support to socket_writer and socket_listener plugins #4021
Changes from 4 commits
8013e8e
f1ed50c
1257911
8f68b2f
d416799
2559fff
7dbc96b
8e1c686
ff3d682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,15 @@ This is a sample configuration for the plugin. | |
## 0 (default) is unlimited. | ||
# read_timeout = "30s" | ||
|
||
## Optional SSL configuration. | ||
## Only applies to stream sockets (e.g. TCP). | ||
# ssl_cert = "/etc/telegraf/cert.pem" | ||
# ssl_key = "/etc/telegraf/key.pem" | ||
## Enable and require client certificate authentication. | ||
# ssl_client_auth = false | ||
## CAs used to verify client certificates. | ||
# ssl_ca = ["/etc/telegraf/ca.pem"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a little bit of prior art on the server side: #3191, I would prefer if we use the same variable names, but let me know if you think that is a problem. The biggest difference is that client_auth is enabled automatically if there are cacerts specified. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense -- definitely want to stay consistent. The config now matches those variable names. |
||
|
||
## Maximum socket buffer size in bytes. | ||
## For stream sockets, once the buffer fills up, the sender will start backing up. | ||
## For datagram sockets, once the buffer fills up, metrics will start dropping. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,20 @@ import ( | |
"net" | ||
"strings" | ||
|
||
"crypto/tls" | ||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/internal" | ||
"github.com/influxdata/telegraf/plugins/outputs" | ||
"github.com/influxdata/telegraf/plugins/serializers" | ||
) | ||
|
||
type SocketWriter struct { | ||
Address string | ||
KeepAlivePeriod *internal.Duration | ||
Address string | ||
KeepAlivePeriod *internal.Duration | ||
SSLCA string | ||
SSLCert string | ||
SSLKey string | ||
InsecureSkipVerify bool | ||
|
||
serializers.Serializer | ||
|
||
|
@@ -39,6 +44,13 @@ func (sw *SocketWriter) SampleConfig() string { | |
# address = "unix:///tmp/telegraf.sock" | ||
# address = "unixgram:///tmp/telegraf.sock" | ||
|
||
## Optional SSL Config | ||
# ssl_ca = "/etc/telegraf/ca.pem" | ||
# ssl_cert = "/etc/telegraf/cert.pem" | ||
# ssl_key = "/etc/telegraf/key.pem" | ||
## Use SSL but skip chain & host verification | ||
# insecure_skip_verify = false | ||
|
||
## Period between keep alive probes. | ||
## Only applies to TCP sockets. | ||
## 0 disables keep alive probes. | ||
|
@@ -58,12 +70,26 @@ func (sw *SocketWriter) SetSerializer(s serializers.Serializer) { | |
} | ||
|
||
func (sw *SocketWriter) Connect() error { | ||
var ( | ||
c net.Conn | ||
err error | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: it's idiomatic go to declare variables right before they're used, not at the top of the function. |
||
|
||
spl := strings.SplitN(sw.Address, "://", 2) | ||
if len(spl) != 2 { | ||
return fmt.Errorf("invalid address: %s", sw.Address) | ||
} | ||
|
||
c, err := net.Dial(spl[0], spl[1]) | ||
tlsCfg, err := internal.GetClientTLSConfig(sw.SSLCert, sw.SSLKey, sw.SSLCA, sw.InsecureSkipVerify) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if tlsCfg == nil { | ||
c, err = net.Dial(spl[0], spl[1]) | ||
} else { | ||
c, err = tls.Dial(spl[0], spl[1], tlsCfg) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just leave this as GetTLSConfig for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.