2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-10-27 19:23:29 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics/prometheus"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/consul"
|
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var CertExpirationGauges = []prometheus.GaugeDefinition{
|
|
|
|
{
|
|
|
|
Name: metricsKeyAgentTLSCertExpiry,
|
|
|
|
Help: "Seconds until the agent tls certificate expires. Updated every hour",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var metricsKeyAgentTLSCertExpiry = []string{"agent", "tls", "cert", "expiry"}
|
|
|
|
|
|
|
|
// tlsCertExpirationMonitor returns a CertExpirationMonitor which will
|
|
|
|
// monitor the expiration of the certificate used for agent TLS.
|
|
|
|
func tlsCertExpirationMonitor(c *tlsutil.Configurator, logger hclog.Logger) consul.CertExpirationMonitor {
|
|
|
|
return consul.CertExpirationMonitor{
|
|
|
|
Key: metricsKeyAgentTLSCertExpiry,
|
|
|
|
Logger: logger,
|
2023-04-07 20:38:07 +00:00
|
|
|
Query: func() (time.Duration, time.Duration, error) {
|
2021-10-27 19:23:29 +00:00
|
|
|
raw := c.Cert()
|
|
|
|
if raw == nil {
|
2023-04-07 20:38:07 +00:00
|
|
|
return 0, 0, fmt.Errorf("tls not enabled")
|
2021-10-27 19:23:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(raw.Certificate[0])
|
|
|
|
if err != nil {
|
2023-04-07 20:38:07 +00:00
|
|
|
return 0, 0, fmt.Errorf("failed to parse agent tls cert: %w", err)
|
2021-10-27 19:23:29 +00:00
|
|
|
}
|
2023-04-07 20:38:07 +00:00
|
|
|
|
|
|
|
lifetime := time.Since(cert.NotBefore) + time.Until(cert.NotAfter)
|
|
|
|
return lifetime, time.Until(cert.NotAfter), nil
|
2021-10-27 19:23:29 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|