Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
feat(node): sync the valid control plane key set from /keys
Antonio Ojea2ae686a63f67
diff --git a/internal/node/controlplane.go b/internal/node/controlplane.goindex 959fcb75..e6a3c37d 100644--- a/internal/node/controlplane.go+++ b/internal/node/controlplane.go@@ -15,7 +15,9 @@package nodeimport (+ "bytes""context"+ "crypto/ed25519""encoding/base64""fmt""io"@@ -67,6 +69,69 @@ func FetchControlPlaneInfo(ctx context.Context, controlPlaneURL string) (*api.Coreturn &info, nil}+// FetchControlPlaneKeys retrieves the full set of currently valid control+// plane public keys from the /keys endpoint — the same catch-up path routers+// use. Enrollment only hands out the newest key, so this is how a node+// learns keys still in their rotation grace period, or rotations it missed+// while offline.+func FetchControlPlaneKeys(ctx context.Context, controlPlaneURL string) ([]ed25519.PublicKey, error) {+ if !strings.HasPrefix(controlPlaneURL, "http://") && !strings.HasPrefix(controlPlaneURL, "https://") {+ controlPlaneURL = "https://" + controlPlaneURL+ }+ controlPlaneURL = strings.TrimSuffix(controlPlaneURL, "/")++ req, err := http.NewRequestWithContext(ctx, "GET", controlPlaneURL+"/keys", nil)+ if err != nil {+ return nil, fmt.Errorf("failed to create HTTP request: %w", err)+ }++ client := &http.Client{Timeout: 10 * time.Second}+ resp, err := client.Do(req)+ if err != nil {+ return nil, fmt.Errorf("HTTP request failed: %w", err)+ }+ defer resp.Body.Close() //nolint:errcheck++ body, err := io.ReadAll(io.LimitReader(resp.Body, 1024*1024))+ if err != nil {+ return nil, fmt.Errorf("failed to read response body: %w", err)+ }+ if resp.StatusCode != http.StatusOK {+ return nil, fmt.Errorf("control plane returned status %s: %s", resp.Status, string(body))+ }++ var keysResp api.KeysResponse+ if err := proto.Unmarshal(body, &keysResp); err != nil {+ return nil, fmt.Errorf("failed to decode /keys response: %w", err)+ }++ var keys []ed25519.PublicKey+ for _, kb := range keysResp.PublicKeys {+ if len(kb) == ed25519.PublicKeySize {+ keys = append(keys, ed25519.PublicKey(kb))+ }+ }+ return keys, nil+}++// mergeTrustedKeys replaces the stored trust set with the authoritative set+// from /keys, preserving ReceivedAt for keys already known so local grace+// pruning keeps working across syncs.+func mergeTrustedKeys(existing []TrustedKey, fetched []ed25519.PublicKey, now time.Time) []TrustedKey {+ merged := make([]TrustedKey, 0, len(fetched))+ for _, key := range fetched {+ tk := TrustedKey{Key: key, ReceivedAt: now}+ for _, old := range existing {+ if bytes.Equal(old.Key, key) {+ tk.ReceivedAt = old.ReceivedAt+ break+ }+ }+ merged = append(merged, tk)+ }+ return merged+}+// SyncMeshConfig loads the mesh configuration from the store, attempts to refresh it// via HTTP from the control plane, and updates the store if successful.// It returns the control plane public key and the latest multiaddresses.@@ -118,6 +183,20 @@ func SyncMeshConfig(ctx context.Context, s *Store) ([]byte, []multiaddr.Multiadd}}}++ // Catch up on the valid key set: an empty result would wipe the trust+ // set, so it is ignored like any fetch failure.+ if keys, keysErr := FetchControlPlaneKeys(ctx, controlPlaneURL); keysErr != nil {+ logger.Warnf("Failed to fetch control plane keys via HTTP (using cached): %v", keysErr)+ } else if len(keys) > 0 {+ existing, loadErr := s.LoadTrustedKeys()+ if loadErr != nil {+ logger.Warnf("Failed to load stored trusted keys, replacing: %v", loadErr)+ }+ if saveErr := s.SaveTrustedKeys(mergeTrustedKeys(existing, keys, time.Now())); saveErr != nil {+ logger.Errorf("Failed to save trusted keys to store: %v", saveErr)+ }+ }}return pubKey, routerAddrs, nildiff --git a/internal/node/controlplane_test.go b/internal/node/controlplane_test.goindex 1ef342ad..04b6cc08 100644--- a/internal/node/controlplane_test.go+++ b/internal/node/controlplane_test.go@@ -16,16 +16,47 @@ package nodeimport ("context"+ "crypto/ed25519""net/http""net/http/httptest""reflect""strings""testing"+ "time""github.com/google/sam/api""google.golang.org/protobuf/proto")+func TestMergeTrustedKeys(t *testing.T) {+ now := time.Now()+ earlier := now.Add(-2 * time.Hour)+ keyA, _, _ := ed25519.GenerateKey(nil)+ keyB, _, _ := ed25519.GenerateKey(nil)+ keyC, _, _ := ed25519.GenerateKey(nil)++ existing := []TrustedKey{+ {Key: keyA, ReceivedAt: earlier},+ {Key: keyC, ReceivedAt: earlier}, // expired at the CP: absent from /keys+ }+ got := mergeTrustedKeys(existing, []ed25519.PublicKey{keyA, keyB}, now)++ if len(got) != 2 {+ t.Fatalf("expected 2 keys, got %d", len(got))+ }+ if !got[0].Key.Equal(keyA) || !got[0].ReceivedAt.Equal(earlier) {+ t.Errorf("known key must keep its ReceivedAt: got %+v", got[0])+ }+ if !got[1].Key.Equal(keyB) || !got[1].ReceivedAt.Equal(now) {+ t.Errorf("new key must get the sync time: got %+v", got[1])+ }+ for _, tk := range got {+ if tk.Key.Equal(keyC) {+ t.Error("key expired at the control plane must be dropped")+ }+ }+}+func TestFetchControlPlaneInfo(t *testing.T) {expectedInfo := &api.ControlPlaneInfoResponse{RouterAddresses: []string{"/ip4/127.0.0.1/tcp/4001"},@@ -106,8 +137,19 @@ func TestSyncMeshConfig(t *testing.T) {t.Fatalf("Failed to marshal info: %v", err)}+ cpPub, _, _ := ed25519.GenerateKey(nil)+ gracePub, _, _ := ed25519.GenerateKey(nil)+ keysBody, err := proto.Marshal(&api.KeysResponse{PublicKeys: [][]byte{cpPub, gracePub}})+ if err != nil {+ t.Fatalf("Failed to marshal keys: %v", err)+ }+server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusOK)+ if r.URL.Path == "/keys" {+ _, _ = w.Write(keysBody)+ return+ }_, _ = w.Write(body)}))defer server.Close()@@ -159,6 +201,18 @@ func TestSyncMeshConfig(t *testing.T) {if string(savedPubKey) != string(testPubKey) {t.Errorf("Expected saved pubKey %s, got %s", testPubKey, savedPubKey)}++ // The full valid key set from /keys must have been persisted+ trusted, err := store.LoadTrustedKeys()+ if err != nil {+ t.Fatalf("LoadTrustedKeys: %v", err)+ }+ if len(trusted) != 2 {+ t.Fatalf("expected 2 trusted keys from /keys, got %d", len(trusted))+ }+ if !trusted[0].Key.Equal(cpPub) || !trusted[1].Key.Equal(gracePub) {+ t.Errorf("persisted keys do not match /keys response")+ }if len(savedAddrsStr) != 1 || savedAddrsStr[0] != expectedInfo.RouterAddresses[0] {t.Errorf("Expected saved addrs %v, got %v", expectedInfo.RouterAddresses, savedAddrsStr)}diff --git a/internal/node/node.go b/internal/node/node.goindex 99fbdc23..4634f05a 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -302,10 +302,29 @@ func NewSamNode(cfg Options) (*SamNode, error) {// Start initializes the libp2p host, DHT, connects to the routers, and starts runtime components.func (n *SamNode) Start(ctx context.Context) error {if biscuitBytes := n.GetIdentity(); len(biscuitBytes) > 0 {- controlPlanePubKeyBytes, _, err := n.Store.LoadMeshConfig()- if err == nil && len(controlPlanePubKeyBytes) > 0 {- if err := identity.VerifyBiscuitRole(biscuitBytes, ed25519.PublicKey(controlPlanePubKeyBytes), n.config.RequiredRole, n.BiscuitTimeout); err != nil {- return fmt.Errorf("loaded identity fails role requirement %q: %w", n.config.RequiredRole, err)+ // The identity may be signed by any currently valid control plane+ // key, not only the newest: after a rotation the biscuit's key can+ // legitimately be in its grace period.+ n.keysMu.RLock()+ candidates := make([]ed25519.PublicKey, 0, len(n.trustedKeys))+ for _, tk := range n.trustedKeys {+ candidates = append(candidates, tk.Key)+ }+ n.keysMu.RUnlock()+ if len(candidates) == 0 {+ if pubKeyBytes, _, err := n.Store.LoadMeshConfig(); err == nil && len(pubKeyBytes) > 0 {+ candidates = append(candidates, ed25519.PublicKey(pubKeyBytes))+ }+ }+ if len(candidates) > 0 {+ var roleErr error+ for _, key := range candidates {+ if roleErr = identity.VerifyBiscuitRole(biscuitBytes, key, n.config.RequiredRole, n.BiscuitTimeout); roleErr == nil {+ break+ }+ }+ if roleErr != nil {+ return fmt.Errorf("loaded identity fails role requirement %q: %w", n.config.RequiredRole, roleErr)}}}