Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
fix(node): never prune the newest trusted control plane key
Antonio Ojea2d3920414ebd
diff --git a/internal/node/node.go b/internal/node/node.goindex e0d20a0e..0f3f7aea 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -1246,6 +1246,29 @@ func (n *SamNode) handleKeyRotationEvent(event *api.MeshEvent) {n.trustedKeys = append(n.trustedKeys, TrustedKey{Key: ed25519.PublicKey(event.NewPublicKey), ReceivedAt: time.Now()})}+// pruneTrustedKeys drops keys older than gracePeriod but always keeps the+// most recently received one: the current signing key has no successor until+// a rotation event arrives, so ageing it out would empty the trust set and+// leave the node unable to verify any peer or router.+func pruneTrustedKeys(keys []TrustedKey, now time.Time, gracePeriod time.Duration) []TrustedKey {+ if len(keys) == 0 {+ return keys+ }+ newest := 0+ for i, tk := range keys {+ if tk.ReceivedAt.After(keys[newest].ReceivedAt) {+ newest = i+ }+ }+ var active []TrustedKey+ for i, tk := range keys {+ if i == newest || now.Sub(tk.ReceivedAt) <= gracePeriod {+ active = append(active, tk)+ }+ }+ return active+}+func (n *SamNode) startKeyPruning(ctx context.Context, gracePeriod time.Duration) {if gracePeriod <= 0 {return@@ -1258,14 +1281,7 @@ func (n *SamNode) startKeyPruning(ctx context.Context, gracePeriod time.Durationcase <-ticker.C:logger.Info("[KeyPruning] Pruning expired keys...")n.keysMu.Lock()- now := time.Now()- var activeKeys []TrustedKey- for _, tk := range n.trustedKeys {- if now.Sub(tk.ReceivedAt) <= gracePeriod {- activeKeys = append(activeKeys, tk)- }- }- n.trustedKeys = activeKeys+ n.trustedKeys = pruneTrustedKeys(n.trustedKeys, time.Now(), gracePeriod)n.keysMu.Unlock()case <-ctx.Done():returndiff --git a/internal/node/node_test.go b/internal/node/node_test.goindex d4b2030a..44eb151d 100644--- a/internal/node/node_test.go+++ b/internal/node/node_test.go@@ -145,6 +145,51 @@ func TestHandleKeyRotationEvent(t *testing.T) {}}+// TestPruneTrustedKeys covers the trust-set floor: a node that runs past the+// grace period without witnessing a rotation must not age out its only key.+func TestPruneTrustedKeys(t *testing.T) {+ now := time.Now()+ grace := 24 * time.Hour+ k := func(age time.Duration) TrustedKey {+ pub, _, err := ed25519.GenerateKey(nil)+ if err != nil {+ t.Fatal(err)+ }+ return TrustedKey{Key: pub, ReceivedAt: now.Add(-age)}+ }++ stale := k(48 * time.Hour)+ staler := k(72 * time.Hour)+ fresh := k(time.Hour)++ tests := []struct {+ name string+ in []TrustedKey+ want []TrustedKey+ }{+ {"sole stale key survives", []TrustedKey{stale}, []TrustedKey{stale}},+ {"stale dropped when fresher exists", []TrustedKey{staler, fresh}, []TrustedKey{fresh}},+ {"newest of two stale keys survives", []TrustedKey{staler, stale}, []TrustedKey{stale}},+ {"fresh keys all kept", []TrustedKey{fresh, k(2 * time.Hour)}, nil}, // want computed below+ {"empty", nil, nil},+ }+ tests[3].want = tests[3].in++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := pruneTrustedKeys(tt.in, now, grace)+ if len(got) != len(tt.want) {+ t.Fatalf("got %d keys, want %d", len(got), len(tt.want))+ }+ for i := range got {+ if !got[i].Key.Equal(tt.want[i].Key) {+ t.Errorf("key %d: got %x, want %x", i, got[i].Key, tt.want[i].Key)+ }+ }+ })+ }+}+// TestVerifyBiscuitRejectsExpiredToken covers the peer-admission half of #296:// HandleAuthHandshake admits a peer into authPeers, which the relay ACL then// trusts, so it must reject an expired token exactly like the dataplane does.