-
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.
Merge pull request #275 from mitalirawat/mitali/SECENG-7134
add certificate expiration metrics for client certificates used in handshake
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
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,54 @@ | ||
// 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 { | ||
certificateExpirationTimes.With(getPrometheusLabels(cert)).Set(float64(cert.NotAfter.Unix())) | ||
} | ||
} | ||
|
||
func getPrometheusLabels(cert *x509.Certificate) prometheus.Labels { | ||
hostnames := append([]string(nil), cert.DNSNames...) | ||
sort.Strings(hostnames) | ||
return prometheus.Labels{ | ||
"serial_no": cert.SerialNumber.String(), | ||
"cn": cert.Subject.CommonName, | ||
"hostnames": strings.Join(hostnames, ","), | ||
"ca": boolToBinaryString(cert.IsCA), | ||
"server": hasKeyUsageAsBinaryString(cert.ExtKeyUsage, x509.ExtKeyUsageServerAuth), | ||
"client": hasKeyUsageAsBinaryString(cert.ExtKeyUsage, x509.ExtKeyUsageClientAuth)} | ||
} | ||
|
||
func boolToBinaryString(val bool) string { | ||
if val { | ||
return "1" | ||
} | ||
return "0" | ||
} | ||
|
||
func hasKeyUsageAsBinaryString(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
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