Skip to content

Agent Mesh

node: do not redial a dropped attached router from ensurePeerAuthenticated

Antonio Ojea8de8bd76ddfa
diff --git a/internal/node/auth_gate.go b/internal/node/auth_gate.go
index ee35d8a1..11a54d35 100644
--- a/internal/node/auth_gate.go
+++ b/internal/node/auth_gate.go
@@ -72,17 +72,18 @@ func (n *AgentMeshNode) isPeerAuthenticated(p peer.ID) bool {
return n.isAuthenticatedAndConnected(p) || n.isAdmitted(p)
}
-// ensurePeerAuthenticated returns once p is authenticated: for a router the
-// control plane lists, by running the handshake (one at a time per router,
-// shared with the one the connection notifee started). Any other peer
-// authenticates with this node when it calls it; until it has, nothing is
-// sent to it, and a lookup is not held waiting for a peer it cannot vouch
-// for.
+// ensurePeerAuthenticated returns once p is authenticated: for an unattached
+// router the control plane lists, by running the handshake (one at a time per
+// router). A router this node is already attached to whose session dropped is
+// left to redialRouter and the connection monitor rather than re-dialled by an
+// in-flight directory or gossip stream. Any other peer authenticates with this
+// node when it calls it; until it has, nothing is sent to it, and a lookup is
+// not held waiting for a peer it cannot vouch for.
func (n *AgentMeshNode) ensurePeerAuthenticated(ctx context.Context, p peer.ID) error {
if n.isPeerAuthenticated(p) {
return nil
}
- if !n.isCatalogRouter(p) {
+ if !n.isCatalogRouter(p) || n.isAttached(p) {
return fmt.Errorf("%s: %w", p, errNotAuthenticated)
}
biscuitBytes, err := n.loadIdentityForAuth()
diff --git a/internal/node/auth_gate_test.go b/internal/node/auth_gate_test.go
index 332fe540..4ee0d0ea 100644
--- a/internal/node/auth_gate_test.go
+++ b/internal/node/auth_gate_test.go
@@ -48,10 +48,13 @@ func TestNodeStreamsToARouterFollowItsHandshake(t *testing.T) {
r1 := newFakeRouter(t, "/ip4/127.0.0.1/tcp/0")
r2 := newFakeRouterWithKey(t, r1.cpPub, r1.cpPriv, "/ip4/127.0.0.1/tcp/0")
- node := r1.startNode(t, ctx, append(r1.routerAddrs, r2.routerAddrs...), func(o *Options) { o.Routers = 1 })
- other := r2
+ node := r1.startNode(t, ctx, append(r1.routerAddrs, r2.routerAddrs...), func(o *Options) {
+ o.Routers = 1
+ o.RouterRedialDelay = -1
+ })
+ attached, other := r1, r2
if node.attachedRouters()[0] == r2.h.ID() {
- other = r1
+ attached, other = r2, r1
}
other.h.SetStreamHandler(echoProtocol, echo)
gated := &meshHost{Host: node.Host, n: node}
@@ -68,6 +71,17 @@ func TestNodeStreamsToARouterFollowItsHandshake(t *testing.T) {
t.Errorf("the router saw %d handshakes, want 1", got)
}
+ // An attached router whose session dropped is left to redialRouter and
+ // the connection monitor; an in-flight stream on gated must not
+ // re-handshake it on its own.
+ dropFromRouter(t, attached.h, node)
+ if _, err := gated.NewStream(ctx, attached.h.ID(), echoProtocol); !errors.Is(err, errNotAuthenticated) {
+ t.Fatalf("NewStream to a dropped attached router: err = %v, want %v", err, errNotAuthenticated)
+ }
+ if got := attached.handshakes.Load(); got != 1 {
+ t.Fatalf("dropped attached router saw %d handshakes, want 1", got)
+ }
+
stranger, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
if err != nil {
t.Fatal(err)