Skip to content

Commit 4e1e6f3

Browse files
committed
inputs.x509_cert: Reset c.tlsCfg.ServerName between certs. Without
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.
1 parent 448eac0 commit 4e1e6f3

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

plugins/inputs/x509_cert/x509_cert.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@ func (c *X509Cert) getCert(u *url.URL, timeout time.Duration) ([]*x509.Certifica
131131

132132
hsErr := conn.Handshake()
133133
if hsErr != nil {
134+
c.tlsCfg.ServerName = "" // reset SNI, otherwise we reuse c.tlsCfg.ServerName
134135
return nil, hsErr
135136
}
136137

137138
certs := conn.ConnectionState().PeerCertificates
139+
c.tlsCfg.ServerName = "" // reset SNI, otherwise we reuse c.tlsCfg.ServerName
138140

139141
return certs, nil
140142
case "file":
@@ -248,6 +250,16 @@ func (c *X509Cert) collectCertURLs() ([]*url.URL, error) {
248250
// Gather adds metrics into the accumulator.
249251
func (c *X509Cert) Gather(acc telegraf.Accumulator) error {
250252
now := time.Now()
253+
254+
if c.tlsCfg.ServerName != "" && c.ServerName == "" {
255+
// Save SNI from c.tlsCfg.ServerName to c.ServerName and reset c.tlsCfg.ServerName.
256+
// We need to reset c.tlsCfg.ServerName for each certificate when there's
257+
// no explicit SNI (c.tlsCfg.ServerName or c.ServerName) otherwise we'll always use
258+
// first uri HostName for all certs (see issue 8914)
259+
c.ServerName = c.tlsCfg.ServerName
260+
c.tlsCfg.ServerName = ""
261+
}
262+
251263
collectedUrls, err := c.collectCertURLs()
252264
if err != nil {
253265
acc.AddError(fmt.Errorf("cannot get file: %s", err.Error()))

0 commit comments

Comments
 (0)