-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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 #9924 from hashicorp/dnephin/cert-expiration-metric
connect: emit a metric for the seconds until root CA expiry
- Loading branch information
Showing
6 changed files
with
82 additions
and
0 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,4 @@ | ||
```release-note:improvement | ||
telemetry: add a new `mesh.active-root-ca.expiry` metric for tracking when the root certificate expires. | ||
``` | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package consul | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/armon/go-metrics" | ||
"github.com/armon/go-metrics/prometheus" | ||
"github.com/hashicorp/go-hclog" | ||
|
||
"github.com/hashicorp/consul/logging" | ||
) | ||
|
||
var CertExpirationGauges = []prometheus.GaugeDefinition{ | ||
{ | ||
Name: metricsKeyMeshRootCAExpiry, | ||
Help: "Seconds until the service mesh root certificate expires.", | ||
}, | ||
} | ||
|
||
var metricsKeyMeshRootCAExpiry = []string{"mesh", "active-root-ca", "expiry"} | ||
|
||
func rootCAExpiryMonitor(s *Server) certExpirationMonitor { | ||
return certExpirationMonitor{ | ||
Key: metricsKeyMeshRootCAExpiry, | ||
Labels: []metrics.Label{ | ||
{Name: "datacenter", Value: s.config.Datacenter}, | ||
}, | ||
Logger: s.logger.Named(logging.Connect), | ||
Query: func() (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 | ||
}, | ||
} | ||
} | ||
|
||
type certExpirationMonitor struct { | ||
Key []string | ||
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 | ||
|
||
func (m certExpirationMonitor) monitor(ctx context.Context) error { | ||
ticker := time.NewTicker(certExpirationMonitorInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
d, err := m.Query() | ||
if err != nil { | ||
m.Logger.Warn("failed to emit certificate expiry metric", "error", err) | ||
} | ||
expiry := d / time.Second | ||
metrics.SetGaugeWithLabels(m.Key, float32(expiry), m.Labels) | ||
} | ||
} | ||
} |
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
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