Skip to content

Commit

Permalink
Fix x509_cert input plugin SNI support (#9289)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjh74 committed Jun 22, 2021
1 parent 81f882a commit ac9bf5a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions plugins/inputs/x509_cert/x509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func (c *X509Cert) SampleConfig() string {
func (c *X509Cert) sourcesToURLs() error {
for _, source := range c.Sources {
if strings.HasPrefix(source, "file://") ||
strings.HasPrefix(source, "/") ||
strings.Index(source, ":\\") != 1 {
strings.HasPrefix(source, "/") {
source = filepath.ToSlash(strings.TrimPrefix(source, "file://"))
g, err := globpath.Compile(source)
if err != nil {
Expand All @@ -82,7 +81,6 @@ func (c *X509Cert) sourcesToURLs() error {
if err != nil {
return fmt.Errorf("failed to parse cert location - %s", err.Error())
}

c.locations = append(c.locations, u)
}
}
Expand Down Expand Up @@ -127,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 @@ -313,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
10 changes: 10 additions & 0 deletions plugins/inputs/x509_cert/x509_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@ func TestGatherCertMustNotTimeout(t *testing.T) {
assert.True(t, acc.HasMeasurement("x509_cert"))
}

func TestSourcesToURLs(t *testing.T) {
m := &X509Cert{
Sources: []string{"https://www.influxdata.com:443", "tcp://influxdata.com:443", "file:///dummy_test_path_file.pem", "/tmp/dummy_test_path_glob*.pem"},
}
require.NoError(t, m.Init())

assert.Equal(t, len(m.globpaths), 2)
assert.Equal(t, len(m.locations), 2)
}

func TestServerName(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit ac9bf5a

Please sign in to comment.