Skip to content

Commit cb4909e

Browse files
committed
Reports Certificate Serial number
Adds `serialnumber` to `probe_ssl_last_chain_info` Output looks like Test: `curl -s http://localhost:9115/probe\?target\=https://example.com\&module\=http_2xx` ``` probe_ssl_last_chain_info{fingerprint_sha256="efba26d8c1ce3779ac77630a90f82163a3d6892ed6afee408672cf19eba7a362",issuer="CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1,O=DigiCert Inc,C=US",serialnumber="075bcef30689c8addf13e51af4afe187",subject="CN=www.example.org,O=Internet Corporation for Assigned Names and Numbers,L=Los Angeles,ST=California,C=US",subjectalternative="www.example.org,example.net,example.edu,example.com,example.org,www.example.com,www.example.edu,www.example.net"} 1 ``` Relates to #1103
1 parent 7e25c6f commit cb4909e

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

prober/grpc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func ProbeGRPC(ctx context.Context, target string, module config.Module, registr
109109
Name: "probe_ssl_last_chain_info",
110110
Help: "Contains SSL leaf certificate information",
111111
},
112-
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative"},
112+
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative", "serialnumber"},
113113
)
114114
)
115115

@@ -206,7 +206,7 @@ func ProbeGRPC(ctx context.Context, target string, module config.Module, registr
206206
isSSLGauge.Set(float64(1))
207207
probeSSLEarliestCertExpiryGauge.Set(float64(getEarliestCertExpiry(&tlsInfo.State).Unix()))
208208
probeTLSVersion.WithLabelValues(getTLSVersion(&tlsInfo.State)).Set(1)
209-
probeSSLLastInformation.WithLabelValues(getFingerprint(&tlsInfo.State), getSubject(&tlsInfo.State), getIssuer(&tlsInfo.State), getDNSNames(&tlsInfo.State)).Set(1)
209+
probeSSLLastInformation.WithLabelValues(getFingerprint(&tlsInfo.State), getSubject(&tlsInfo.State), getIssuer(&tlsInfo.State), getDNSNames(&tlsInfo.State), getSerialNumber(&tlsInfo.State)).Set(1)
210210
} else {
211211
isSSLGauge.Set(float64(0))
212212
}

prober/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
273273
Name: "probe_ssl_last_chain_info",
274274
Help: "Contains SSL leaf certificate information",
275275
},
276-
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative"},
276+
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative", "serialnumber"},
277277
)
278278

279279
probeTLSVersion = prometheus.NewGaugeVec(
@@ -647,7 +647,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
647647
probeTLSVersion.WithLabelValues(getTLSVersion(resp.TLS)).Set(1)
648648
probeTLSCipher.WithLabelValues(getTLSCipher(resp.TLS)).Set(1)
649649
probeSSLLastChainExpiryTimestampSeconds.Set(float64(getLastChainExpiry(resp.TLS).Unix()))
650-
probeSSLLastInformation.WithLabelValues(getFingerprint(resp.TLS), getSubject(resp.TLS), getIssuer(resp.TLS), getDNSNames(resp.TLS)).Set(1)
650+
probeSSLLastInformation.WithLabelValues(getFingerprint(resp.TLS), getSubject(resp.TLS), getIssuer(resp.TLS), getDNSNames(resp.TLS), getSerialNumber(resp.TLS)).Set(1)
651651
if httpConfig.FailIfSSL {
652652
logger.Error("Final request was over SSL")
653653
success = false

prober/tcp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
113113
Name: "probe_ssl_last_chain_info",
114114
Help: "Contains SSL leaf certificate information",
115115
},
116-
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative"},
116+
[]string{"fingerprint_sha256", "subject", "issuer", "subjectalternative", "serialnumber"},
117117
)
118118
probeTLSVersion := prometheus.NewGaugeVec(
119119
probeTLSInfoGaugeOpts,
@@ -147,7 +147,7 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
147147
probeSSLEarliestCertExpiry.Set(float64(getEarliestCertExpiry(&state).Unix()))
148148
probeTLSVersion.WithLabelValues(getTLSVersion(&state)).Set(1)
149149
probeSSLLastChainExpiryTimestampSeconds.Set(float64(getLastChainExpiry(&state).Unix()))
150-
probeSSLLastInformation.WithLabelValues(getFingerprint(&state), getSubject(&state), getIssuer(&state), getDNSNames(&state)).Set(1)
150+
probeSSLLastInformation.WithLabelValues(getFingerprint(&state), getSubject(&state), getIssuer(&state), getDNSNames(&state), getSerialNumber(&state)).Set(1)
151151
}
152152
scanner := bufio.NewScanner(conn)
153153
for i, qr := range module.TCP.QueryResponse {
@@ -216,7 +216,7 @@ func ProbeTCP(ctx context.Context, target string, module config.Module, registry
216216
probeSSLEarliestCertExpiry.Set(float64(getEarliestCertExpiry(&state).Unix()))
217217
probeTLSVersion.WithLabelValues(getTLSVersion(&state)).Set(1)
218218
probeSSLLastChainExpiryTimestampSeconds.Set(float64(getLastChainExpiry(&state).Unix()))
219-
probeSSLLastInformation.WithLabelValues(getFingerprint(&state), getSubject(&state), getIssuer(&state), getDNSNames(&state)).Set(1)
219+
probeSSLLastInformation.WithLabelValues(getFingerprint(&state), getSubject(&state), getIssuer(&state), getDNSNames(&state), getSerialNumber(&state)).Set(1)
220220
}
221221
}
222222
return true

prober/tls.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"crypto/sha256"
1818
"crypto/tls"
1919
"encoding/hex"
20+
"fmt"
2021
"strings"
2122
"time"
2223
)
@@ -69,6 +70,17 @@ func getLastChainExpiry(state *tls.ConnectionState) time.Time {
6970
return lastChainExpiry
7071
}
7172

73+
func getSerialNumber(state *tls.ConnectionState) string {
74+
cert := state.PeerCertificates[0]
75+
// Actual serial number = 0B:FF:BC5:11:F1:90:7D:02:AF:71:9A:FC:D6:4F:B2:53
76+
// serialNumber := cert.SerialNumber.Text(16) // drops leading zeros outputs = BFFBC511F1907D02AF719AFCD64FB253 in lower case, telgraf follows this https://github.com/influxdata/telegraf/blob/a9c91f162ddbe453364f68a89799535c43328a3c/plugins/inputs/x509_cert/x509_cert.go#L218
77+
// https://github.com/atc0005/check-cert retains the leading zero with some aditional formatting
78+
79+
serialNumber := strings.ToLower(fmt.Sprintf("%X", cert.SerialNumber.Bytes()))
80+
81+
return serialNumber
82+
}
83+
7284
func getTLSVersion(state *tls.ConnectionState) string {
7385
switch state.Version {
7486
case tls.VersionTLS10:

0 commit comments

Comments
 (0)