Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
sdk/js: authenticate on any connection to a listed router, release after a lookup
Antonio Ojea76d21c347a78
diff --git a/sdk/js/src/session.test.ts b/sdk/js/src/session.test.tsindex ac3b2aaa..227d9314 100644--- a/sdk/js/src/session.test.ts+++ b/sdk/js/src/session.test.ts@@ -405,6 +405,48 @@ test("a member on one router reaches a member on another", async () => {}});+test("a connection to a listed router is authenticated, and released after a lookup", async () => {+ // The DHT client connects to whichever listed router kademlia puts closest+ // to a key. A router serves its DHT only to a member that authenticated+ // on the connection, so the handshake runs on connection:open for any+ // listed router, held or not; a router met this way is not held, and the+ // connection is closed once the lookup ends.+ const routerB = await createLibp2p({+ addresses: { listen: ["/ip4/127.0.0.1/tcp/0"] },+ transports: [tcp()],+ connectionEncrypters: [tls()],+ streamMuxers: [yamux()],+ services: { identify: identify(), relay: circuitRelayServer() },+ });+ try {+ const handshakesB: string[] = [];+ await routerB.handle(AUTH_PROTOCOL, authStreamHandler({ ownBiscuit: () => mint(routerB.peerId.toString(), ROLE_ROUTER), trustedKeys: () => [cpKey], onAuthenticated: (p) => handshakesB.push(p) }));+ const routerBAddr = (routerB.getMultiaddrs()[0] as ReturnType<typeof multiaddr>).toString();+ const mesh = await AgentMesh.enroll({ controlPlaneUrl: "http://127.0.0.1:1", bootstrapToken: "sbt", fetch: fakeControlPlane([routerAddr, routerBAddr]) });+ const session = await mesh.join({ refreshLeadMs: 0, routerAddresses: [routerAddr], reserveRelay: false });+ try {+ assert.deepEqual(session.routers.map((r) => r.peerId), [router.peerId.toString()]);++ await session.node.dial(multiaddr(routerBAddr));+ const deadline = Date.now() + 10_000;+ while (handshakesB.length === 0 && Date.now() < deadline) {+ await new Promise((r) => setTimeout(r, 20));+ }+ assert.deepEqual(handshakesB, [mesh.peerId], "the member authenticated on connecting to a listed router");+ assert.deepEqual(session.routers.map((r) => r.peerId), [router.peerId.toString()], "a router met for a lookup is not held");++ await session.discover("mcp", undefined, { timeoutMs: 500 }).catch(() => {});+ const connected = session.node.getConnections().map((c) => c.remotePeer.toString());+ assert.ok(!connected.includes(routerB.peerId.toString()), "the lookup's connection to the router it does not hold is released");+ assert.ok(connected.includes(router.peerId.toString()), "the held router's connection stays");+ } finally {+ await session.close();+ }+ } finally {+ await routerB.stop();+ }+});+test("a resumed member adopts a rotated key before admitting routers", async () => {// The control plane rotated its signing key while this member was not// running: the routers already hold credentials under the new key, anddiff --git a/sdk/js/src/session.ts b/sdk/js/src/session.tsindex 91bb2b4b..63fc5852 100644--- a/sdk/js/src/session.ts+++ b/sdk/js/src/session.ts@@ -499,18 +499,64 @@ export class MeshSession {* A router forgets the admission with the connection it came on, and* js-libp2p reconnects to a relay on its own, so every new connection to a* router runs the handshake again: the relay's own renewal of the- * reservation then still passes the router's check.+ * reservation then still passes the router's check. A router serves its+ * DHT and gossip only to a member that authenticated on the connection,+ * so a connection the DHT client opens to any router the control plane+ * lists is authenticated the same way; the router holds the streams the+ * client opened meanwhile and serves them once the handshake is done.*/#keepRouterAdmissions(): void {this.node.addEventListener("connection:open", (evt) => {const conn = evt.detail;- if (this.#closed || !this.routers.some((r) => r.peerId === conn.remotePeer.toString())) {+ if (this.#closed || !this.#isRouter(conn.remotePeer.toString())) {return;}- void authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys).catch(() => {});+ void this.#authenticateRouter(conn).catch(() => {});});}+ /** Handshakes in flight by connection id: one per connection, whoever asks first. */+ readonly #routerHandshakes = new Map<string, Promise<VerifiedBiscuit>>();++ /**+ * Runs the handshake on a router connection once: the connection:open+ * listener and an explicit admission both call this, and the second waits+ * for the first instead of running a second handshake.+ */+ #authenticateRouter(conn: Connection): Promise<VerifiedBiscuit> {+ const pending = this.#routerHandshakes.get(conn.id);+ if (pending !== undefined) {+ return pending;+ }+ const run = authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys).finally(() => {+ this.#routerHandshakes.delete(conn.id);+ });+ this.#routerHandshakes.set(conn.id, run);+ return run;+ }++ /** Whether peerId is a router this member holds or one the mesh lists for it. */+ #isRouter(peerId: string): boolean {+ return (+ this.routers.some((r) => r.peerId === peerId) ||+ candidatesFromRouterInfos(this.mesh.routers, this.mesh.credential.routerAddresses).some((c) => c.peerId === peerId)+ );+ }++ /**+ * Closes the connections a lookup opened to routers this member does not+ * hold: the member keeps sessions with the routers it holds and with no+ * other, and the next lookup connects and authenticates again.+ */+ async #releaseLookupRouters(): Promise<void> {+ for (const conn of this.node.getConnections()) {+ const peerId = conn.remotePeer.toString();+ if (this.#isRouter(peerId) && !this.routers.some((r) => r.peerId === peerId)) {+ await this.node.hangUp(conn.remotePeer).catch(() => {});+ }+ }+ }+/*** Maintains held routers and relay reservations: when a held router's* connection or reservation drops, redials it first (2s, 4s, 8s by default)@@ -574,7 +620,7 @@ export class MeshSession {continue;}try {- const credential = await authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys);+ const credential = await this.#authenticateRouter(conn);requireRole(credential, ROLE_ROUTER);const addr = connectedAddress(conn, r.peerId);if (this.#reserveRelay) {@@ -625,7 +671,7 @@ export class MeshSession {tried.add(cand.peerId);try {const conn = await this.node.dial(cand.addresses, signal !== undefined ? { signal } : {});- const credential = await authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys);+ const credential = await this.#authenticateRouter(conn);requireRole(credential, ROLE_ROUTER);const addr = connectedAddress(conn, cand.peerId);if (this.#reserveRelay) {@@ -724,6 +770,8 @@ export class MeshSession {found = await this.node.peerRouting.findPeer(peerIdFromString(peerId), { signal: lookup });} catch {return [];+ } finally {+ await this.#releaseLookupRouters();}const out: Multiaddr[] = [];for (const ma of found.multiaddrs) {@@ -753,7 +801,7 @@ export class MeshSession {/** Dials a router, runs the handshake and, its role verified, adds it to the admitted set. */async #admitRouter(addr: Multiaddr, signal?: AbortSignal): Promise<AdmittedRouter> {const conn = await this.node.dial(addr, signal !== undefined ? { signal } : {});- const credential = await authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys);+ const credential = await this.#authenticateRouter(conn);requireRole(credential, ROLE_ROUTER);const peerId = conn.remotePeer.toString();const router = { peerId, addr: connectedAddress(conn, peerId), credential };@@ -837,6 +885,7 @@ export class MeshSession {}} finally {clearTimeout(settleTimer);+ await this.#releaseLookupRouters();}return [...found.values()];}@@ -909,7 +958,7 @@ export class MeshSession {return;}try {- await authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys);+ await this.#authenticateRouter(conn);} catch {// The router keeps the admission it has until the old credential lapses.}