Add server_name override for x509_cert plugin (#6917)

This commit is contained in:
James Beckett 2020-01-22 01:06:58 +00:00 committed by Daniel Nelson
parent 6a982da8b0
commit 916f39d285
2 changed files with 19 additions and 5 deletions

View File

@ -19,6 +19,8 @@ file or network connection.
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem" # tls_key = "/etc/telegraf/key.pem"
## Pass a different name into the TLS request (Server Name Indication)
# server_name = "myhost.example.org"
``` ```

View File

@ -30,6 +30,8 @@ const sampleConfig = `
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem" # tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem" # tls_key = "/etc/telegraf/key.pem"
## Pass a different name into the TLS request (Server Name Indication)
# server_name = "myhost.example.org"
` `
const description = "Reads metrics from a SSL certificate" const description = "Reads metrics from a SSL certificate"
@ -37,6 +39,7 @@ const description = "Reads metrics from a SSL certificate"
type X509Cert struct { type X509Cert struct {
Sources []string `toml:"sources"` Sources []string `toml:"sources"`
Timeout internal.Duration `toml:"timeout"` Timeout internal.Duration `toml:"timeout"`
ServerName string `toml:"server_name"`
tlsCfg *tls.Config tlsCfg *tls.Config
_tls.ClientConfig _tls.ClientConfig
} }
@ -78,7 +81,12 @@ func (c *X509Cert) getCert(u *url.URL, timeout time.Duration) ([]*x509.Certifica
} }
defer ipConn.Close() defer ipConn.Close()
if c.ServerName == "" {
c.tlsCfg.ServerName = u.Hostname() c.tlsCfg.ServerName = u.Hostname()
} else {
c.tlsCfg.ServerName = c.ServerName
}
c.tlsCfg.InsecureSkipVerify = true c.tlsCfg.InsecureSkipVerify = true
conn := tls.Client(ipConn, c.tlsCfg) conn := tls.Client(ipConn, c.tlsCfg)
defer conn.Close() defer conn.Close()
@ -203,7 +211,11 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error {
Intermediates: x509.NewCertPool(), Intermediates: x509.NewCertPool(),
} }
if i == 0 { if i == 0 {
if c.ServerName == "" {
opts.DNSName = u.Hostname() opts.DNSName = u.Hostname()
} else {
opts.DNSName = c.ServerName
}
for j, cert := range certs { for j, cert := range certs {
if j != 0 { if j != 0 {
opts.Intermediates.AddCert(cert) opts.Intermediates.AddCert(cert)