Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add exclude_root_certs option to x509_cert plugin #9822

Merged
merged 4 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions plugins/inputs/x509_cert/x509_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const sampleConfig = `
## example: server_name = "myhost.example.org"
# server_name = ""

## Don't include root or intermediate certificates in output
# exclude_root_certs = false

## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
Expand All @@ -46,10 +49,11 @@ const description = "Reads metrics from a SSL certificate"

// X509Cert holds the configuration of the plugin.
type X509Cert struct {
Sources []string `toml:"sources"`
Timeout config.Duration `toml:"timeout"`
ServerName string `toml:"server_name"`
tlsCfg *tls.Config
Sources []string `toml:"sources"`
Timeout config.Duration `toml:"timeout"`
ServerName string `toml:"server_name"`
ExcludeRootCerts bool `toml:"exclude_root_certs"`
tlsCfg *tls.Config
_tls.ClientConfig
locations []*url.URL
globpaths []*globpath.GlobPath
Expand Down Expand Up @@ -334,6 +338,9 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error {
}

acc.AddFields("x509_cert", fields, tags)
if c.ExcludeRootCerts {
break
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions plugins/inputs/x509_cert/x509_cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,35 @@ func TestTags(t *testing.T) {
_, validSerialNumber := serialNumber.SetString(acc.TagValue("x509_cert", "serial_number"), 16)
require.Truef(t, validSerialNumber, "Expected a valid Hex serial number but got %s", acc.TagValue("x509_cert", "serial_number"))
require.Equal(t, big.NewInt(1), serialNumber)

// expect root/intermediate certs (more than one cert)
require.Greater(t, acc.NMetrics(), uint64(1))
}

func TestGatherExcludeRootCerts(t *testing.T) {
cert := fmt.Sprintf("%s\n%s", pki.ReadServerCert(), pki.ReadCACert())

f, err := os.CreateTemp("", "x509_cert")
require.NoError(t, err)

_, err = f.Write([]byte(cert))
require.NoError(t, err)

require.NoError(t, f.Close())

defer os.Remove(f.Name())

sc := X509Cert{
Sources: []string{f.Name()},
ExcludeRootCerts: true,
}
require.NoError(t, sc.Init())

acc := testutil.Accumulator{}
require.NoError(t, sc.Gather(&acc))

require.True(t, acc.HasMeasurement("x509_cert"))
require.Equal(t, acc.NMetrics(), uint64(1))
}

func TestGatherChain(t *testing.T) {
Expand Down