-
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 new SSL input plugin #3829
Closed
Closed
Add new SSL input plugin #3829
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3301068
add inputs.ssl plugin
e8b7b80
add readme. update config
2a9aba7
fix
28ffbd5
change time to expiration value fron nanosec to sec
33665fe
add wildcard support
43ff3e1
implode domain and port in config
1758e71
fix
4b5a093
update readme
808576a
add tests
179d8bd
add input plugin for check ssl sert
9a47333
Merge branch 'check-ssl' of https://github.com/mgrabazey/telegraf int…
e0be44d
fix by gofmt
d2ea247
fix by gofmt
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Telegraf Plugin: SSL | ||
|
||
### Configuration: | ||
|
||
``` | ||
# Check expiration date and domains of ssl certificate | ||
[[inputs.ssl]] | ||
## Server to check | ||
[[inputs.ssl.servers]] | ||
host = "google.com:443" | ||
timeout = 5 | ||
## Server to check | ||
[[inputs.ssl.servers]] | ||
host = "github.com" | ||
timeout = 5 | ||
``` | ||
|
||
### Tags: | ||
|
||
- domain | ||
- port | ||
|
||
### Fields: | ||
|
||
- time_to_expiration(int) | ||
|
||
### Example Output: | ||
|
||
If ssl certificate is valid: | ||
|
||
``` | ||
* Plugin: inputs.ssl, Collection 1 | ||
> ssl,domain=example.com,port=443,host=host time_to_expiration=3907728i 1517213967000000000 | ||
``` | ||
|
||
If ssl certificate and domain mismatch: | ||
|
||
``` | ||
* Plugin: inputs.ssl, Collection 1 | ||
2018-01-29T08:20:33Z E! Error in plugin [inputs.ssl]: [example.com:443] cert and domain mismatch | ||
> ssl,domain=example.com,port=443,host=host time_to_expiration=0i 1517214033000000000 | ||
``` | ||
|
||
If ssl certificate has expired: | ||
|
||
``` | ||
* Plugin: inputs.ssl, Collection 1 | ||
2018-01-29T08:20:33Z E! Error in plugin [inputs.ssl]: [example.com:443] cert has expired | ||
> ssl,domain=example.com,port=443,host=host time_to_expiration=0i 1517214033000000000 | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package ssl | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
"github.com/pkg/errors" | ||
"net" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Ssl struct { | ||
Servers []Server | ||
} | ||
|
||
type Server struct { | ||
Host string | ||
Timeout int | ||
} | ||
|
||
var sampleConfig = ` | ||
## Server to check | ||
[[inputs.ssl.servers]] | ||
host = "google.com:443" | ||
timeout = 5 | ||
## Server to check | ||
[[inputs.ssl.servers]] | ||
host = "github.com" | ||
timeout = 5 | ||
` | ||
|
||
func (s *Ssl) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (s *Ssl) Description() string { | ||
return "Check expiration date and domains of ssl certificate" | ||
} | ||
|
||
func (s *Ssl) Gather(acc telegraf.Accumulator) error { | ||
for _, server := range s.Servers { | ||
slice := strings.Split(server.Host, ":") | ||
domain, port := slice[0], "443" | ||
if len(slice) > 1 { | ||
port = slice[1] | ||
} | ||
h := getServerAddress(domain, port) | ||
timeNow := time.Now() | ||
timeToExp := int64(0) | ||
fields := make(map[string]interface{}) | ||
tags := make(map[string]string) | ||
certs, err := getServerCertsChain(domain, port, server.Timeout) | ||
|
||
if err != nil { | ||
acc.AddError(err) | ||
} else { | ||
cert := certs[0] | ||
if cert.NotAfter.UnixNano() < timeNow.UnixNano() { | ||
acc.AddError(errors.New("[" + h + "] cert has expired")) | ||
} else { | ||
timeToExp = int64(cert.NotAfter.Sub(timeNow) / time.Second) | ||
} | ||
if !isDomainInCertDnsNames(domain, cert.DNSNames) { | ||
acc.AddError(errors.New("[" + h + "] cert and domain mismatch")) | ||
timeToExp = int64(0) | ||
} | ||
} | ||
fields["time_to_expiration"] = timeToExp | ||
tags["domain"] = domain | ||
tags["port"] = port | ||
|
||
acc.AddFields("ssl", fields, tags) | ||
} | ||
return nil | ||
} | ||
|
||
func getServerCertsChain(d string, p string, t int) ([]*x509.Certificate, error) { | ||
h := getServerAddress(d, p) | ||
ipConn, err := net.DialTimeout("tcp", h, time.Duration(t)*time.Second) | ||
if err != nil { | ||
return nil, errors.New("[" + h + "] " + err.Error()) | ||
} | ||
defer ipConn.Close() | ||
|
||
tlsConn := tls.Client(ipConn, &tls.Config{ServerName: d, InsecureSkipVerify: true}) | ||
defer tlsConn.Close() | ||
|
||
err = tlsConn.Handshake() | ||
if err != nil { | ||
return nil, errors.New("[" + h + "] " + err.Error()) | ||
} | ||
certs := tlsConn.ConnectionState().PeerCertificates | ||
if certs == nil || len(certs) < 1 { | ||
return nil, errors.New("[" + h + "] cert receive error") | ||
} | ||
return certs, nil | ||
} | ||
|
||
func getServerAddress(d string, p string) string { | ||
return d + ":" + p | ||
} | ||
|
||
func isDomainInCertDnsNames(domain string, certDnsNames []string) bool { | ||
for _, d := range certDnsNames { | ||
if domain == d { | ||
return true | ||
} | ||
if d[:1] == "*" && len(domain) >= len(d[2:]) { | ||
d = d[2:] | ||
if domain == d { | ||
return true | ||
} | ||
start := len(domain) - len(d) - 1 | ||
if start >= 0 && domain[start:] == "."+d { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func init() { | ||
inputs.Add("ssl", func() telegraf.Input { | ||
return &Ssl{} | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ssl | ||
|
||
import ( | ||
"github.com/influxdata/telegraf/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestGathering(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Skipping network-dependent test in short mode.") | ||
} | ||
var servers = []Server{ | ||
{ | ||
Host: "github.com:443", | ||
Timeout: 5, | ||
}, | ||
{ | ||
Host: "github.com", | ||
Timeout: 5, | ||
}, | ||
} | ||
var sslConfig = Ssl{ | ||
Servers: servers, | ||
} | ||
var acc testutil.Accumulator | ||
|
||
err := acc.GatherError(sslConfig.Gather) | ||
assert.NoError(t, err) | ||
metric, ok := acc.Get("ssl") | ||
require.True(t, ok) | ||
timeToExp, _ := metric.Fields["time_to_expiration"].(float64) | ||
|
||
assert.NotEqual(t, 0, timeToExp) | ||
} |
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.
If we're going to set
InsecureSkipVerify: true
, we should probably do a manual verification step and indicate the status. It's possible for an intermediate or root cert to be revoked (or expired in the case of a stupid CA), which would invalidate the cert.