-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add additional tags for x509 Input Plugin #6686
Changes from 1 commit
0ddeb0c
dca0395
dc22d65
1cae784
242a6bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ package x509_cert | |
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"fmt" | ||
"io/ioutil" | ||
|
@@ -26,6 +25,14 @@ const sampleConfig = ` | |
## Timeout for SSL connection | ||
# timeout = "5s" | ||
|
||
## Include Certificate Issuer information in tags | ||
# include_issuer = false | ||
|
||
## Include Certificate SAN in tag | ||
# include_san = false | ||
## Separator between each SAN in tag | ||
# san_separator = "," | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove all of these options, the plugin user can use tagexclude/taginclude instead. On the SAN separator we can just always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I have removed them. |
||
|
||
## Optional TLS Config | ||
# tls_ca = "/etc/telegraf/ca.pem" | ||
# tls_cert = "/etc/telegraf/cert.pem" | ||
|
@@ -35,9 +42,12 @@ 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"` | ||
IncludeIssuer bool `toml:"include_issuer"` | ||
IncludeSAN bool `toml:"include_san"` | ||
SANSeperator string `toml:"san_separator"` | ||
tlsCfg *tls.Config | ||
_tls.ClientConfig | ||
} | ||
|
||
|
@@ -129,10 +139,15 @@ func getFields(cert *x509.Certificate, now time.Time) map[string]interface{} { | |
return fields | ||
} | ||
|
||
func getTags(subject pkix.Name, location string) map[string]string { | ||
func (c *X509Cert) getTags(cert *x509.Certificate, location string) map[string]string { | ||
subject := cert.Subject | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, just get cert.Subject.CommonName on line 147 without creating a local variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
tags := map[string]string{ | ||
"source": location, | ||
"common_name": subject.CommonName, | ||
"source": location, | ||
"common_name": subject.CommonName, | ||
"serial_number": cert.SerialNumber.Text(16), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How large is the serial_number? Can you add unit tests with examples of all the data used by these changes? Also, how does this compare to the cert.Subject.SerialNumber? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The certificate serial number is at most 20 bytes and this will be at most 40 Hex characters. This is supposed to be unique per CA. On the other hand, based on what I know, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added a new test. |
||
"signature_algorithm": cert.SignatureAlgorithm.String(), | ||
"public_key_algorithm": cert.PublicKeyAlgorithm.String(), | ||
} | ||
|
||
if len(subject.Organization) > 0 { | ||
|
@@ -151,6 +166,23 @@ func getTags(subject pkix.Name, location string) map[string]string { | |
tags["locality"] = subject.Locality[0] | ||
} | ||
|
||
if c.IncludeIssuer { | ||
issuer := cert.Issuer | ||
tags["issuer_common_name"] = issuer.CommonName | ||
tags["issuer_serial_number"] = issuer.SerialNumber | ||
} | ||
|
||
if c.IncludeSAN { | ||
san := append(cert.DNSNames, cert.EmailAddresses...) | ||
for _, ip := range cert.IPAddresses { | ||
san = append(san, ip.String()) | ||
} | ||
for _, uri := range cert.URIs { | ||
san = append(san, uri.String()) | ||
} | ||
tags["san"] = strings.Join(san, c.SANSeperator) | ||
} | ||
|
||
return tags | ||
} | ||
|
||
|
@@ -172,7 +204,7 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error { | |
|
||
for i, cert := range certs { | ||
fields := getFields(cert, now) | ||
tags := getTags(cert.Subject, location) | ||
tags := c.getTags(cert, location) | ||
|
||
// The first certificate is the leaf/end-entity certificate which needs DNS | ||
// name validation against the URL hostname. | ||
|
@@ -225,8 +257,11 @@ func (c *X509Cert) Init() error { | |
func init() { | ||
inputs.Add("x509_cert", func() telegraf.Input { | ||
return &X509Cert{ | ||
Sources: []string{}, | ||
Timeout: internal.Duration{Duration: 5}, | ||
Sources: []string{}, | ||
Timeout: internal.Duration{Duration: 5}, | ||
IncludeIssuer: false, | ||
IncludeSAN: false, | ||
SANSeperator: ",", | ||
} | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you undo this file, we will take care of it afterwards. We usually just update this once before release to avoid conflicts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.