Skip to content

Commit

Permalink
Add server_name override for x509_cert plugin (influxdata#6917)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackery authored and danielnelson committed Jan 22, 2020
1 parent 81b5427 commit a89e268
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/x509_cert/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ file or network connection.
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Pass a different name into the TLS request (Server Name Indication)
# server_name = "myhost.example.org"
```


Expand Down
22 changes: 17 additions & 5 deletions plugins/inputs/x509_cert/x509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ const sampleConfig = `
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.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"

// X509Cert holds the configuration of the plugin.
type X509Cert struct {
Sources []string `toml:"sources"`
Timeout internal.Duration `toml:"timeout"`
tlsCfg *tls.Config
Sources []string `toml:"sources"`
Timeout internal.Duration `toml:"timeout"`
ServerName string `toml:"server_name"`
tlsCfg *tls.Config
_tls.ClientConfig
}

Expand Down Expand Up @@ -78,7 +81,12 @@ func (c *X509Cert) getCert(u *url.URL, timeout time.Duration) ([]*x509.Certifica
}
defer ipConn.Close()

c.tlsCfg.ServerName = u.Hostname()
if c.ServerName == "" {
c.tlsCfg.ServerName = u.Hostname()
} else {
c.tlsCfg.ServerName = c.ServerName
}

c.tlsCfg.InsecureSkipVerify = true
conn := tls.Client(ipConn, c.tlsCfg)
defer conn.Close()
Expand Down Expand Up @@ -203,7 +211,11 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error {
Intermediates: x509.NewCertPool(),
}
if i == 0 {
opts.DNSName = u.Hostname()
if c.ServerName == "" {
opts.DNSName = u.Hostname()
} else {
opts.DNSName = c.ServerName
}
for j, cert := range certs {
if j != 0 {
opts.Intermediates.AddCert(cert)
Expand Down

0 comments on commit a89e268

Please sign in to comment.