Skip to content

Agent Mesh

control-plane: export the signing key set

Antonio Ojea6ba43f2fcd7d
diff --git a/internal/controlplane/metrics.go b/internal/controlplane/metrics.go
index cce00825..97f51586 100644
--- a/internal/controlplane/metrics.go
+++ b/internal/controlplane/metrics.go
@@ -118,6 +118,11 @@ type meshSnapshot struct {
tokensByState map[string]int
routers []storage.RouterLease
meshConnectedPeers int
+ // signingKeys is how many keys verify a credential today: one, or two
+ // during the grace period after a rotation. retiringKeyExpiry is when
+ // the retiring one stops verifying; zero when there is none.
+ signingKeys int
+ retiringKeyExpiry time.Time
}
// meshStateCollector exports the control plane's view of the mesh: who is
@@ -144,6 +149,8 @@ type meshStateCollector struct {
routerDHTDesc *prometheus.Desc
routerLeaseDesc *prometheus.Desc
meshPeersDesc *prometheus.Desc
+ signingKeysDesc *prometheus.Desc
+ retiringKeyDesc *prometheus.Desc
scrapeOKDesc *prometheus.Desc
scrapeSampleDesc *prometheus.Desc
}
@@ -190,6 +197,14 @@ func newMeshStateCollector(store storage.Store) *meshStateCollector {
"sam_control_plane_mesh_connected_peers",
"Distinct non-router peers connected to at least one active router",
nil, nil),
+ signingKeysDesc: prometheus.NewDesc(
+ "sam_control_plane_signing_keys",
+ "Signing keys a credential verifies against: 1, or 2 during the grace period after a rotation",
+ nil, nil),
+ retiringKeyDesc: prometheus.NewDesc(
+ "sam_control_plane_retiring_key_expiry_timestamp_seconds",
+ "Unix time the retiring signing key stops verifying; absent when no rotation is in its grace period",
+ nil, nil),
scrapeOKDesc: prometheus.NewDesc(
"sam_control_plane_mesh_state_scrape_success",
"1 if the mesh state was read from the store, 0 if the last read failed",
@@ -205,7 +220,7 @@ func (c *meshStateCollector) Describe(ch chan<- *prometheus.Desc) {
for _, d := range []*prometheus.Desc{
c.nodesDesc, c.usersDesc, c.requestsDesc, c.tokensDesc, c.routersDesc,
c.routerPeersDesc, c.routerDHTDesc, c.routerLeaseDesc, c.meshPeersDesc,
- c.scrapeOKDesc, c.scrapeSampleDesc,
+ c.signingKeysDesc, c.retiringKeyDesc, c.scrapeOKDesc, c.scrapeSampleDesc,
} {
ch <- d
}
@@ -248,6 +263,10 @@ func (c *meshStateCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(c.routerLeaseDesc, prometheus.GaugeValue, float64(r.LastRenewal.Unix()), r.PeerID)
}
ch <- prometheus.MustNewConstMetric(c.meshPeersDesc, prometheus.GaugeValue, float64(s.meshConnectedPeers))
+ ch <- prometheus.MustNewConstMetric(c.signingKeysDesc, prometheus.GaugeValue, float64(s.signingKeys))
+ if !s.retiringKeyExpiry.IsZero() {
+ ch <- prometheus.MustNewConstMetric(c.retiringKeyDesc, prometheus.GaugeValue, float64(s.retiringKeyExpiry.Unix()))
+ }
}
// refresh replaces the snapshot from the store. A failed read keeps the last
@@ -312,6 +331,18 @@ func readMeshSnapshot(ctx context.Context, store storage.Store, now time.Time) (
snap.routers = routers
snap.meshConnectedPeers = countMeshPeers(routers)
+ keys, err := store.GetAllValidKeys(ctx)
+ if err != nil {
+ return snap, err
+ }
+ snap.signingKeys = len(keys)
+ for _, k := range keys {
+ // The current key has no expiration; a retiring one expires at the end of its grace period.
+ if !k.Expiration.IsZero() && (snap.retiringKeyExpiry.IsZero() || k.Expiration.Before(snap.retiringKeyExpiry)) {
+ snap.retiringKeyExpiry = k.Expiration
+ }
+ }
+
return snap, nil
}
diff --git a/internal/controlplane/metrics_test.go b/internal/controlplane/metrics_test.go
index e37c2da0..1b998212 100644
--- a/internal/controlplane/metrics_test.go
+++ b/internal/controlplane/metrics_test.go
@@ -16,6 +16,8 @@ package controlplane
import (
"context"
+ "crypto/ed25519"
+ "crypto/rand"
"io"
"net/http"
"net/http/httptest"
@@ -155,6 +157,23 @@ func seedMeshState(t *testing.T, store storage.Store, now time.Time) {
t.Fatalf("request %s: %v", reqs[i].ID, err)
}
}
+
+ // A rotation in its grace period: the first key retires in an hour, the
+ // second signs now.
+ for i, grace := range []time.Duration{0, time.Hour} {
+ pub, priv, err := ed25519.GenerateKey(rand.Reader)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if i == 0 {
+ err = store.SaveInitialKey(ctx, priv, pub)
+ } else {
+ err = store.RotateKeys(ctx, priv, pub, grace)
+ }
+ if err != nil {
+ t.Fatalf("key %d: %v", i, err)
+ }
+ }
}
func TestMeshStateCollectorExportsStoreState(t *testing.T) {
@@ -196,6 +215,16 @@ func TestMeshStateCollectorExportsStoreState(t *testing.T) {
// node-a counted once, routers excluded, node-z behind a dead lease ignored.
g.want(t, "sam_control_plane_mesh_connected_peers", 3)
+
+ // Both keys verify during the grace period; the retiring one's expiry is exported.
+ g.want(t, "sam_control_plane_signing_keys", 2)
+ retiring, ok := g["sam_control_plane_retiring_key_expiry_timestamp_seconds"]
+ if !ok {
+ t.Fatal("retiring key expiry not exported during the grace period")
+ }
+ if until := time.Until(time.Unix(int64(retiring), 0)); until < 55*time.Minute || until > 65*time.Minute {
+ t.Errorf("retiring key expires in %v, want about an hour", until)
+ }
}
func TestMeshStateCollectorCachesWithinTTL(t *testing.T) {