-
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 certificate verification status to x509_cert input #6143
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
[[inputs.x509_cert]] | ||
sources = ["https://www.influxdata.com:443"] | ||
sources = ["https://expired.badssl.com:443", "https://wrong.host.badssl.com:443"] | ||
|
||
[[outputs.file]] | ||
files = ["stdout"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,16 +30,14 @@ const sampleConfig = ` | |
# tls_ca = "/etc/telegraf/ca.pem" | ||
# tls_cert = "/etc/telegraf/cert.pem" | ||
# tls_key = "/etc/telegraf/key.pem" | ||
|
||
## Use TLS but skip chain & host verification | ||
# insecure_skip_verify = false | ||
` | ||
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 | ||
_tls.ClientConfig | ||
} | ||
|
||
|
@@ -53,39 +51,36 @@ func (c *X509Cert) SampleConfig() string { | |
return sampleConfig | ||
} | ||
|
||
func (c *X509Cert) getCert(location string, timeout time.Duration) ([]*x509.Certificate, error) { | ||
func (c *X509Cert) locationToURL(location string) (*url.URL, error) { | ||
if strings.HasPrefix(location, "/") { | ||
location = "file://" + location | ||
} | ||
|
||
u, err := url.Parse(location) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse cert location - %s\n", err.Error()) | ||
return nil, fmt.Errorf("failed to parse cert location - %s", err.Error()) | ||
} | ||
|
||
return u, nil | ||
} | ||
|
||
func (c *X509Cert) getCert(u *url.URL, timeout time.Duration) ([]*x509.Certificate, error) { | ||
switch u.Scheme { | ||
case "https": | ||
u.Scheme = "tcp" | ||
fallthrough | ||
case "udp", "udp4", "udp6": | ||
fallthrough | ||
case "tcp", "tcp4", "tcp6": | ||
tlsCfg, err := c.ClientConfig.TLSConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ipConn, err := net.DialTimeout(u.Scheme, u.Host, timeout) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer ipConn.Close() | ||
|
||
if tlsCfg == nil { | ||
tlsCfg = &tls.Config{} | ||
} | ||
tlsCfg.ServerName = u.Hostname() | ||
conn := tls.Client(ipConn, tlsCfg) | ||
c.tlsCfg.ServerName = u.Hostname() | ||
c.tlsCfg.InsecureSkipVerify = true | ||
conn := tls.Client(ipConn, c.tlsCfg) | ||
defer conn.Close() | ||
|
||
hsErr := conn.Handshake() | ||
|
@@ -114,7 +109,7 @@ func (c *X509Cert) getCert(location string, timeout time.Duration) ([]*x509.Cert | |
|
||
return []*x509.Certificate{cert}, nil | ||
default: | ||
return nil, fmt.Errorf("unsuported scheme '%s' in location %s\n", u.Scheme, location) | ||
return nil, fmt.Errorf("unsuported scheme '%s' in location %s", u.Scheme, u.String()) | ||
} | ||
} | ||
|
||
|
@@ -164,22 +159,62 @@ func (c *X509Cert) Gather(acc telegraf.Accumulator) error { | |
now := time.Now() | ||
|
||
for _, location := range c.Sources { | ||
certs, err := c.getCert(location, c.Timeout.Duration*time.Second) | ||
u, err := c.locationToURL(location) | ||
if err != nil { | ||
acc.AddError(err) | ||
return nil | ||
} | ||
|
||
certs, err := c.getCert(u, c.Timeout.Duration*time.Second) | ||
if err != nil { | ||
acc.AddError(fmt.Errorf("cannot get SSL cert '%s': %s", location, err.Error())) | ||
} | ||
|
||
for _, cert := range certs { | ||
for i, cert := range certs { | ||
fields := getFields(cert, now) | ||
tags := getTags(cert.Subject, location) | ||
|
||
// The first certificate is the leaf/end-entity certificate which needs DNS | ||
// name validation against the URL hostname. | ||
opts := x509.VerifyOptions{} | ||
if i == 0 { | ||
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. Here would be a great place for a comment:
|
||
opts.DNSName = u.Hostname() | ||
} | ||
if c.tlsCfg.RootCAs != nil { | ||
opts.Roots = c.tlsCfg.RootCAs | ||
} | ||
|
||
_, err = cert.Verify(opts) | ||
if err == nil { | ||
tags["verification"] = "valid" | ||
fields["verification"] = 0 | ||
} else { | ||
tags["verification"] = "invalid" | ||
fields["verification"] = 1 | ||
fields["validation_error"] = err.Error() | ||
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.
|
||
} | ||
|
||
acc.AddFields("x509_cert", fields, tags) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *X509Cert) Init() error { | ||
tlsCfg, err := c.ClientConfig.TLSConfig() | ||
if err != nil { | ||
return err | ||
} | ||
if tlsCfg == nil { | ||
tlsCfg = &tls.Config{} | ||
} | ||
|
||
c.tlsCfg = tlsCfg | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("x509_cert", func() telegraf.Input { | ||
return &X509Cert{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Needs updated