Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
node: a router that refuses a refreshed credential is asked again before it is dropped
Antonio Ojea91a1955a029b
diff --git a/internal/node/node.go b/internal/node/node.goindex 34128de9..8be81b97 100644--- a/internal/node/node.go+++ b/internal/node/node.go@@ -1302,13 +1302,19 @@ func (n *AgentMeshNode) authenticateRouterSession(ctx context.Context, router pereturn nil}+// readmitRetryDelays spaces the retries of a re-admission a router refused.+// A credential refreshed right after a key rotation is signed by a key the+// router may not have synced yet; it has within a few seconds, so the node+// asks again instead of waiting a monitor interval with no router.+var readmitRetryDelays = []time.Duration{500 * time.Millisecond, time.Second, 2 * time.Second}+// readmitRouters presents the current credential to every router this node// holds an authenticated session with. A router admits a peer until the// biscuit it was shown expires, whatever the connection does, so a refreshed// credential must be shown on the live session or relay circuits to this-// node are refused once the old one lapses. A router that refuses is-// dropped from the authenticated set; the connection monitor then dials and-// handshakes afresh.+// node are refused once the old one lapses. A router that still refuses+// after the retries is dropped from the authenticated set; the connection+// monitor then dials and handshakes afresh.func (n *AgentMeshNode) readmitRouters(ctx context.Context) {if n.Host == nil {return@@ -1318,16 +1324,36 @@ func (n *AgentMeshNode) readmitRouters(ctx context.Context) {return}for _, router := range n.authenticatedRouterIDs() {+ n.readmitRouter(ctx, router, biscuitBytes)+ }+}++func (n *AgentMeshNode) readmitRouter(ctx context.Context, router peer.ID, biscuitBytes []byte) {+ var err error+ for attempt := 0; ; attempt++ {routerCtx, cancel := context.WithTimeout(ctx, n.config.RouterConnectTimeout)- err := n.authenticateRouterSession(routerCtx, router, biscuitBytes)+ err = n.authenticateRouterSession(routerCtx, router, biscuitBytes)cancel()- if err != nil {- logger.Warnf("[AuthN] Router %s did not accept the refreshed credential; the connection monitor will re-handshake: %v", router, err)- n.mu.Lock()- delete(n.authenticatedRouters, router)- n.mu.Unlock()+ if err == nil {+ if attempt > 0 {+ logger.Infow("[AuthN] Router accepted the refreshed credential on retry", "router", router, "attempt", attempt+1)+ }+ return+ }+ if attempt >= len(readmitRetryDelays) {+ break+ }+ logger.Warnw("[AuthN] Router did not accept the refreshed credential; retrying", "router", router, "attempt", attempt+1, "retry_in", readmitRetryDelays[attempt], "error", err)+ select {+ case <-ctx.Done():+ return+ case <-time.After(readmitRetryDelays[attempt]):}}+ logger.Warnw("[AuthN] Router did not accept the refreshed credential; the connection monitor will re-handshake", "router", router, "attempts", len(readmitRetryDelays)+1, "error", err)+ n.mu.Lock()+ delete(n.authenticatedRouters, router)+ n.mu.Unlock()}func (n *AgentMeshNode) performRouterAuthHandshake(s network.Stream, biscuitBytes []byte, expectedRouter peer.ID) (bool, error) {diff --git a/internal/node/readmit_test.go b/internal/node/readmit_test.gonew file mode 100644index 00000000..8f55fa59--- /dev/null+++ b/internal/node/readmit_test.go@@ -0,0 +1,145 @@+// Copyright 2026 Google LLC+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package node++import (+ "context"+ "crypto/ed25519"+ "sync/atomic"+ "testing"+ "time"++ "github.com/eclipse-biscuit/biscuit-go/v2"+ "github.com/libp2p/go-libp2p"+ "github.com/libp2p/go-libp2p/core/host"+ "github.com/libp2p/go-libp2p/core/network"+ "github.com/libp2p/go-libp2p/core/peer"+ "github.com/libp2p/go-msgio"+ "google.golang.org/protobuf/proto"++ "github.com/google/agentmesh/api"+)++// A router that refuses the first handshakes, as one does when a node shows a+// credential under a key the router has not synced yet, then accepts.+func startRefusingRouter(t *testing.T, cpPriv ed25519.PrivateKey, refusals int32) (host.Host, *atomic.Int32) {+ t.Helper()+ h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = h.Close() })+ builder := biscuit.NewBuilder(cpPriv)+ for _, f := range []biscuit.Fact{+ {Predicate: biscuit.Predicate{Name: api.FactNode, IDs: []biscuit.Term{biscuit.String(h.ID().String())}}},+ {Predicate: biscuit.Predicate{Name: api.FactRole, IDs: []biscuit.Term{biscuit.String(api.RoleRouter)}}},+ {Predicate: biscuit.Predicate{Name: api.FactExpiration, IDs: []biscuit.Term{biscuit.Date(time.Now().Add(24 * time.Hour))}}},+ api.MarkerFact(api.FactTargetUnrestricted),+ } {+ if err := builder.AddAuthorityFact(f); err != nil {+ t.Fatal(err)+ }+ }+ tok, err := builder.Build()+ if err != nil {+ t.Fatal(err)+ }+ routerBiscuit, err := tok.Serialize()+ if err != nil {+ t.Fatal(err)+ }+ var handshakes atomic.Int32+ h.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) {+ defer func() { _ = s.Close() }()+ reader := msgio.NewVarintReaderSize(s, 1024*64)+ msg, err := reader.ReadMsg()+ if err != nil {+ return+ }+ reader.ReleaseMsg(msg)+ resp := &api.AuthResponse{Success: true, Biscuit: routerBiscuit}+ if handshakes.Add(1) <= refusals {+ resp = &api.AuthResponse{Success: false, Error: "no valid key found for verification"}+ }+ data, err := proto.Marshal(resp)+ if err != nil {+ return+ }+ _ = msgio.NewVarintWriter(s).WriteMsg(data)+ })+ return h, &handshakes+}++// newReadmitTestNode is a node holding an authenticated session with router,+// connected to it, with a refreshed credential to show.+func newReadmitTestNode(t *testing.T, cpPub ed25519.PublicKey, router host.Host) *AgentMeshNode {+ t.Helper()+ h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { _ = h.Close() })+ if err := h.Connect(t.Context(), peer.AddrInfo{ID: router.ID(), Addrs: router.Addrs()}); err != nil {+ t.Fatal(err)+ }+ n := &AgentMeshNode{+ Host: h,+ BiscuitTimeout: 2 * time.Second,+ authenticatedRouters: map[peer.ID]bool{router.ID(): true},+ trustedKeys: []TrustedKey{{Key: cpPub, ReceivedAt: time.Now()}},+ }+ n.config.RouterConnectTimeout = 2 * time.Second+ n.cachedIdentity.Store([]byte("refreshed-credential"))+ return n+}++// TestReadmitRetriesARouterThatHasNotSyncedTheNewKey pins the window after a+// key rotation: the node refreshes under the new key and shows it to its+// routers at once; a router that has not synced the key yet refuses. The+// node asks again within seconds and keeps the session, instead of dropping+// the router and serving 503 until the connection monitor's next minute.+func TestReadmitRetriesARouterThatHasNotSyncedTheNewKey(t *testing.T) {+ cpPub, cpPriv, _ := ed25519.GenerateKey(nil)+ router, handshakes := startRefusingRouter(t, cpPriv, 2)+ n := newReadmitTestNode(t, cpPub, router)++ start := time.Now()+ n.readmitRouters(t.Context())+ if got := handshakes.Load(); got != 3 {+ t.Fatalf("router saw %d handshakes, want 3: two refusals and the one that succeeded", got)+ }+ if !n.IsConnected() {+ t.Fatal("the router accepted the third handshake, but the node dropped it from the authenticated set")+ }+ if elapsed := time.Since(start); elapsed > 4*time.Second {+ t.Errorf("re-admission took %s; the retries should fit in a few seconds", elapsed)+ }+}++// A router that keeps refusing is dropped after the retries, so the monitor+// dials and handshakes afresh, as before.+func TestReadmitDropsARouterThatKeepsRefusing(t *testing.T) {+ cpPub, cpPriv, _ := ed25519.GenerateKey(nil)+ router, handshakes := startRefusingRouter(t, cpPriv, 100)+ n := newReadmitTestNode(t, cpPub, router)++ n.readmitRouters(context.Background())+ if got, want := handshakes.Load(), int32(len(readmitRetryDelays)+1); got != want {+ t.Fatalf("router saw %d handshakes, want %d", got, want)+ }+ if n.IsConnected() {+ t.Fatal("a router that refused every handshake is still in the authenticated set")+ }+}diff --git a/tests/integration/rotation_test.go b/tests/integration/rotation_test.goindex 59c4344b..7c62fe2c 100644--- a/tests/integration/rotation_test.go+++ b/tests/integration/rotation_test.go@@ -211,7 +211,7 @@ roles:"-args", "{}",)if err != nil {- t.Fatalf("failed to query node: %v\nstdout: %s\nstderr: %s", err, stdout, stderr)+ t.Fatalf("failed to query node: %v\nstdout: %s\nstderr: %s\n--- node.log ---\n%s", err, stdout, stderr, readLog(t, nodeLogPath))}if !strings.Contains(stdout, "attached_routers") {t.Fatalf("unexpected node response: %s", stdout)