2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-03-24 21:40:10 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-07 13:41:01 +00:00
|
|
|
"errors"
|
2021-03-24 21:40:10 +00:00
|
|
|
"fmt"
|
2021-08-05 22:38:06 +00:00
|
|
|
"math"
|
2021-08-04 17:26:36 +00:00
|
|
|
"strings"
|
2021-03-24 21:40:10 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/armon/go-metrics/prometheus"
|
2021-07-07 13:41:01 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2021-08-04 17:05:10 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/connect"
|
|
|
|
"github.com/hashicorp/consul/logging"
|
2021-03-24 21:40:10 +00:00
|
|
|
)
|
|
|
|
|
2021-07-07 13:41:01 +00:00
|
|
|
var metricsKeyMeshRootCAExpiry = []string{"mesh", "active-root-ca", "expiry"}
|
|
|
|
var metricsKeyMeshActiveSigningCAExpiry = []string{"mesh", "active-signing-ca", "expiry"}
|
|
|
|
|
2021-10-19 20:49:23 +00:00
|
|
|
var LeaderCertExpirationGauges = []prometheus.GaugeDefinition{
|
2021-03-24 21:40:10 +00:00
|
|
|
{
|
|
|
|
Name: metricsKeyMeshRootCAExpiry,
|
2021-07-07 13:41:01 +00:00
|
|
|
Help: "Seconds until the service mesh root certificate expires. Updated every hour",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: metricsKeyMeshActiveSigningCAExpiry,
|
|
|
|
Help: "Seconds until the service mesh signing certificate expires. Updated every hour",
|
2021-03-24 21:40:10 +00:00
|
|
|
},
|
2021-10-19 20:49:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 17:05:10 +00:00
|
|
|
func rootCAExpiryMonitor(s *Server) CertExpirationMonitor {
|
|
|
|
return CertExpirationMonitor{
|
2021-10-20 15:54:11 +00:00
|
|
|
Key: metricsKeyMeshRootCAExpiry,
|
2021-03-24 21:40:10 +00:00
|
|
|
Logger: s.logger.Named(logging.Connect),
|
|
|
|
Query: func() (time.Duration, error) {
|
2021-07-07 13:41:01 +00:00
|
|
|
return getRootCAExpiry(s)
|
2021-03-24 21:40:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-07 13:41:01 +00:00
|
|
|
func getRootCAExpiry(s *Server) (time.Duration, error) {
|
|
|
|
state := s.fsm.State()
|
|
|
|
_, root, err := state.CARootActive(nil)
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return 0, fmt.Errorf("failed to retrieve root CA: %w", err)
|
|
|
|
case root == nil:
|
|
|
|
return 0, fmt.Errorf("no active root CA")
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Until(root.NotAfter), nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 17:05:10 +00:00
|
|
|
func signingCAExpiryMonitor(s *Server) CertExpirationMonitor {
|
|
|
|
return CertExpirationMonitor{
|
2021-10-20 15:54:11 +00:00
|
|
|
Key: metricsKeyMeshActiveSigningCAExpiry,
|
2021-08-04 17:05:10 +00:00
|
|
|
Logger: s.logger.Named(logging.Connect),
|
|
|
|
Query: func() (time.Duration, error) {
|
2021-11-23 17:49:43 +00:00
|
|
|
if s.caManager.isIntermediateUsedToSignLeaf() {
|
|
|
|
return getActiveIntermediateExpiry(s)
|
|
|
|
}
|
|
|
|
return getRootCAExpiry(s)
|
2021-08-04 17:05:10 +00:00
|
|
|
},
|
2021-07-07 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getActiveIntermediateExpiry(s *Server) (time.Duration, error) {
|
|
|
|
state := s.fsm.State()
|
|
|
|
_, root, err := state.CARootActive(nil)
|
2021-08-04 20:34:01 +00:00
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return 0, fmt.Errorf("failed to retrieve root CA: %w", err)
|
|
|
|
case root == nil:
|
|
|
|
return 0, fmt.Errorf("no active root CA")
|
2021-07-07 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the CA used in a secondary DC is the active intermediate,
|
|
|
|
// which is the last in the IntermediateCerts stack
|
|
|
|
if len(root.IntermediateCerts) == 0 {
|
|
|
|
return 0, errors.New("no intermediate available")
|
|
|
|
}
|
|
|
|
cert, err := connect.ParseCert(root.IntermediateCerts[len(root.IntermediateCerts)-1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return time.Until(cert.NotAfter), nil
|
|
|
|
}
|
|
|
|
|
2021-08-04 17:05:10 +00:00
|
|
|
type CertExpirationMonitor struct {
|
2021-10-20 15:54:11 +00:00
|
|
|
Key []string
|
|
|
|
// Labels to be emitted along with the metric. It is very important that these
|
|
|
|
// labels be included in the pre-declaration as well. Otherwise, if
|
|
|
|
// telemetry.prometheus_retention_time is less than certExpirationMonitorInterval
|
|
|
|
// then the metrics will expire before they are emitted again.
|
2021-03-24 21:40:10 +00:00
|
|
|
Labels []metrics.Label
|
|
|
|
Logger hclog.Logger
|
|
|
|
// Query is called at each interval. It should return the duration until the
|
|
|
|
// certificate expires, or an error if the query failed.
|
|
|
|
Query func() (time.Duration, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const certExpirationMonitorInterval = time.Hour
|
|
|
|
|
2021-08-04 17:05:10 +00:00
|
|
|
func (m CertExpirationMonitor) Monitor(ctx context.Context) error {
|
2021-03-24 21:40:10 +00:00
|
|
|
ticker := time.NewTicker(certExpirationMonitorInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2021-08-04 17:26:36 +00:00
|
|
|
logger := m.Logger.With("metric", strings.Join(m.Key, "."))
|
|
|
|
|
2021-10-20 15:54:11 +00:00
|
|
|
emitMetric := func() {
|
2021-08-04 20:34:01 +00:00
|
|
|
d, err := m.Query()
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to emit certificate expiry metric", "error", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d < 24*time.Hour {
|
|
|
|
logger.Warn("certificate will expire soon",
|
|
|
|
"time_to_expiry", d, "expiration", time.Now().Add(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
expiry := d / time.Second
|
|
|
|
metrics.SetGaugeWithLabels(m.Key, float32(expiry), m.Labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit the metric immediately so that if a cert was just updated the
|
|
|
|
// new metric will be updated to the new expiration time.
|
2021-10-20 15:54:11 +00:00
|
|
|
emitMetric()
|
2021-08-04 20:34:01 +00:00
|
|
|
|
2021-03-24 21:40:10 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2021-08-05 22:38:06 +00:00
|
|
|
// "Zero-out" the metric on exit so that when prometheus scrapes this
|
|
|
|
// metric from a non-leader, it does not get a stale value.
|
2021-10-20 15:54:11 +00:00
|
|
|
metrics.SetGaugeWithLabels(m.Key, float32(math.NaN()), m.Labels)
|
2021-03-24 21:40:10 +00:00
|
|
|
return nil
|
|
|
|
case <-ticker.C:
|
2021-10-20 15:54:11 +00:00
|
|
|
emitMetric()
|
2021-03-24 21:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-04 17:05:10 +00:00
|
|
|
|
2021-10-21 22:09:30 +00:00
|
|
|
// initLeaderMetrics sets all metrics that are emitted only on leaders to a NaN
|
|
|
|
// value so that they don't incorrectly report 0 when a server starts as a
|
|
|
|
// follower.
|
|
|
|
func initLeaderMetrics() {
|
|
|
|
for _, g := range LeaderCertExpirationGauges {
|
|
|
|
metrics.SetGaugeWithLabels(g.Name, float32(math.NaN()), g.ConstLabels)
|
|
|
|
}
|
|
|
|
}
|