Skip to content

Commit

Permalink
inputs.x509_cert: Reset c.tlsCfg.ServerName between certs. Without
Browse files Browse the repository at this point in the history
this multiple remote sources (sources = [ "tcp://remote1.example.org:443",
"tcp://remote2.example.org:443" ]) reuse first SNI. Telegraf will
request wrong certificate and can fail validation when SAN/SNI doesn't match.
  • Loading branch information
jjh74 committed Jun 22, 2021
1 parent 0832ad4 commit d85ccd4
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions plugins/inputs/x509_cert/x509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func (c *X509Cert) getCert(u *url.URL, timeout time.Duration) ([]*x509.Certifica
conn := tls.Client(ipConn, c.tlsCfg)
defer conn.Close()

// reset SNI between requests
defer func() { c.tlsCfg.ServerName = "" }()

hsErr := conn.Handshake()
if hsErr != nil {
return nil, hsErr
Expand Down Expand Up @@ -311,6 +314,14 @@ func (c *X509Cert) Init() error {
tlsCfg = &tls.Config{}
}

if tlsCfg.ServerName != "" && c.ServerName == "" {
// Save SNI from tlsCfg.ServerName to c.ServerName and reset tlsCfg.ServerName.
// We need to reset c.tlsCfg.ServerName for each certificate when there's
// no explicit SNI (c.tlsCfg.ServerName or c.ServerName) otherwise we'll always (re)use
// first uri HostName for all certs (see issue 8914)
c.ServerName = tlsCfg.ServerName
tlsCfg.ServerName = ""
}
c.tlsCfg = tlsCfg

return nil
Expand Down

0 comments on commit d85ccd4

Please sign in to comment.