-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emit certificate expiration metrics for client certificates used in h…
…andshake
- Loading branch information
Mitali Rawat
committed
Jul 8, 2020
1 parent
cdf0b58
commit cd1c948
Showing
2 changed files
with
56 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Package certmetrics will be used to register and emit metrics for certificates in memory | ||
package certmetrics | ||
|
||
import ( | ||
"crypto/x509" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
) | ||
|
||
var certificateExpirationTimes = promauto.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Name: "certificate_expiration_timestamp_seconds", | ||
Help: "Expiration times of gokeyless certs", | ||
}, | ||
[]string{"serial_no", "cn", "hostnames", "ca", "server", "client"}, | ||
) | ||
|
||
// Observe takes in a list of certs and emits its expiration times | ||
func Observe(certs ...*x509.Certificate) { | ||
for _, cert := range certs { | ||
hostnames := cert.DNSNames | ||
sort.Strings(hostnames) | ||
labels := prometheus.Labels{ | ||
"serial_no": cert.SerialNumber.String(), | ||
"cn": cert.Subject.CommonName, | ||
"hostnames": strings.Join(hostnames, ","), | ||
"ca": boolToBinaryString(cert.IsCA), | ||
"server": containsKeyUsage(cert.ExtKeyUsage, x509.ExtKeyUsageServerAuth), | ||
"client": containsKeyUsage(cert.ExtKeyUsage, x509.ExtKeyUsageClientAuth)} | ||
certificateExpirationTimes.With(labels).Set(float64(cert.NotAfter.Unix())) | ||
} | ||
} | ||
|
||
func boolToBinaryString(val bool) string { | ||
if val { | ||
return "1" | ||
} | ||
return "0" | ||
} | ||
|
||
func containsKeyUsage(a []x509.ExtKeyUsage, x x509.ExtKeyUsage) string { | ||
for _, e := range a { | ||
if e == x || e == x509.ExtKeyUsageAny { | ||
return "1" | ||
} | ||
} | ||
return "0" | ||
} |
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