diff --git a/packages/client-runtime/src/connection/registry.test.ts b/packages/client-runtime/src/connection/registry.test.ts index 0d0327c53af28..ebc6e68cdcfec 100644 --- a/packages/client-runtime/src/connection/registry.test.ts +++ b/packages/client-runtime/src/connection/registry.test.ts @@ -1895,15 +1895,45 @@ describe("EnvironmentRegistry routes", () => { }), ); - it.effect("re-pairing the same address replaces that route rather than adding another", () => + it.effect("re-pairing under a new id drops routes borrowing the replaced credential", () => Effect.gen(function* () { + const learned = new BearerConnectionTarget({ + ...LAN_TARGET, + connectionId: `learned:${LAN_TARGET.environmentId}:tailnet@${LAN_TARGET.connectionId}`, + }); + const relayLearned = new BearerConnectionTarget({ + ...LAN_TARGET, + connectionId: `learned:${LAN_TARGET.environmentId}:relay`, + }); const harness = yield* makeHarness( - [LAN_TARGET, RELAY_TARGET], - [LAN_PROFILE], + [LAN_TARGET, learned, RELAY_TARGET, relayLearned], + [ + LAN_PROFILE, + new BearerConnectionProfile({ + ...LAN_PROFILE, + connectionId: learned.connectionId, + httpBaseUrl: "http://100.64.0.9:3773", + learned: true, + }), + new BearerConnectionProfile({ + ...LAN_PROFILE, + connectionId: relayLearned.connectionId, + httpBaseUrl: "http://100.64.0.10:3773", + learned: true, + authorization: "t3-connect", + }), + ], [[LAN_TARGET.connectionId, BEARER_CREDENTIAL]], ); yield* Effect.gen(function* () { const registry = yield* EnvironmentRegistry.EnvironmentRegistry; + yield* registry.register(lanRegistration()); + expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual([ + LAN_TARGET, + learned, + RELAY_TARGET, + relayLearned, + ]); const repaired = new BearerConnectionTarget({ ...LAN_TARGET, connectionId: "bearer:lan-again", @@ -1918,6 +1948,13 @@ describe("EnvironmentRegistry routes", () => { expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual([ repaired, RELAY_TARGET, + relayLearned, + ]); + const entry = (yield* SubscriptionRef.get(registry.entries)).get(LAN_TARGET.environmentId); + expect(entry?.target).toEqual(repaired); + expect(entry?.alternateRoutes?.map((route) => route.target)).toEqual([ + RELAY_TARGET, + relayLearned, ]); }).pipe(Effect.provide(harness.layer), Effect.scoped); }), diff --git a/packages/client-runtime/src/connection/registry.ts b/packages/client-runtime/src/connection/registry.ts index 5fef64855e845..af8190f723be6 100644 --- a/packages/client-runtime/src/connection/registry.ts +++ b/packages/client-runtime/src/connection/registry.ts @@ -551,11 +551,11 @@ export const make = Effect.gen(function* () { const route: ConnectionRoute = { target: registered.target, profile: registered.profile }; const existing = connectionRoutes(previous); const sameAddress = findRouteToSameAddress(existing, route); - const routes = existing.filter( - (existing) => - existing !== sameAddress || - connectionRouteId(existing.target) === connectionRouteId(route.target), - ); + const routes = + sameAddress !== undefined && + connectionRouteId(sameAddress.target) !== connectionRouteId(route.target) + ? routesAfterRemoving(existing, connectionRouteId(sameAddress.target)) + : existing; const next = withRoutes(previous, upsertRoute(routes, route)); if (next.addressesChanged) { yield* forgetRoutingTrust(environmentId, "register-connection"); diff --git a/packages/client-runtime/src/platform/storageDocument.test.ts b/packages/client-runtime/src/platform/storageDocument.test.ts index 60f561e3dae53..69d643a59edac 100644 --- a/packages/client-runtime/src/platform/storageDocument.test.ts +++ b/packages/client-runtime/src/platform/storageDocument.test.ts @@ -195,6 +195,136 @@ describe("ConnectionCatalogDocument", () => { }), ); + it("re-pairing refreshes only direct bearer owners, including after removing the new route", () => { + const other = new BearerConnectionTarget({ + ...BEARER_TARGET, + environmentId: EnvironmentId.make("environment-2"), + connectionId: "bearer:other", + }); + const ssh = new SshConnectionTarget({ + environmentId: ENVIRONMENT_ID, + label: "SSH", + connectionId: "ssh:remote", + }); + const tailnet = new BearerConnectionTarget({ + ...BEARER_TARGET, + connectionId: "bearer:tailnet", + }); + const learned = new BearerConnectionTarget({ + ...BEARER_TARGET, + connectionId: `learned:${ENVIRONMENT_ID}:tailnet@${BEARER_TARGET.connectionId}`, + }); + const relayLearned = new BearerConnectionTarget({ + ...BEARER_TARGET, + connectionId: `learned:${ENVIRONMENT_ID}:relay`, + }); + const routes = [BEARER_TARGET, tailnet, learned, relayLearned, ssh, RELAY_TARGET]; + const document: ConnectionCatalogDocument = { + ...EMPTY_CONNECTION_CATALOG_DOCUMENT, + targets: [...routes, other], + profiles: [ + BEARER_PROFILE, + new BearerConnectionProfile({ ...BEARER_PROFILE, connectionId: tailnet.connectionId }), + new BearerConnectionProfile({ + ...BEARER_PROFILE, + connectionId: learned.connectionId, + learned: true, + }), + new BearerConnectionProfile({ + ...BEARER_PROFILE, + connectionId: relayLearned.connectionId, + learned: true, + authorization: "t3-connect", + }), + ], + credentials: [BEARER_TARGET, tailnet, ssh, other].map((target) => ({ + connectionId: target.connectionId, + credential: BEARER_CREDENTIAL, + })), + remoteDpopTokens: [REMOTE_TOKEN], + disabledEnvironmentIds: [ENVIRONMENT_ID], + }; + const paired = new BearerConnectionTarget({ ...BEARER_TARGET, connectionId: "bearer:new-lan" }); + const fresh = new BearerConnectionCredential({ token: "fresh-pairing-token" }); + const sameRoute = registerConnectionInCatalog( + document, + new BearerConnectionRegistration({ + target: BEARER_TARGET, + profile: BEARER_PROFILE, + credential: fresh, + }), + routes, + ); + expect( + sameRoute.credentials.find((value) => value.connectionId === tailnet.connectionId) + ?.credential, + ).toEqual(fresh); + const refreshed = registerConnectionInCatalog( + document, + new BearerConnectionRegistration({ + target: paired, + profile: new BearerConnectionProfile({ + ...BEARER_PROFILE, + connectionId: paired.connectionId, + httpBaseUrl: "https://new-lan.example.test", + }), + credential: fresh, + }), + [paired, ...routes], + ); + const schema = Schema.fromJsonString(ConnectionCatalogDocument); + const restored = Schema.decodeSync(schema)( + Schema.encodeSync(schema)(setRoutesInCatalog(refreshed, ENVIRONMENT_ID, routes)), + ); + + expect( + refreshed.credentials.find((value) => value.connectionId === paired.connectionId)?.credential, + ).toEqual(fresh); + expect(restored.credentials).toEqual( + expect.arrayContaining([ + { connectionId: BEARER_TARGET.connectionId, credential: fresh }, + { connectionId: tailnet.connectionId, credential: fresh }, + { connectionId: ssh.connectionId, credential: BEARER_CREDENTIAL }, + { connectionId: other.connectionId, credential: BEARER_CREDENTIAL }, + ]), + ); + expect(restored.credentials).toHaveLength(4); + expect(restored.targets).toEqual(document.targets); + expect(restored.profiles).toEqual(document.profiles); + expect(restored.remoteDpopTokens).toEqual(document.remoteDpopTokens); + expect(restored.disabledEnvironmentIds).toEqual(document.disabledEnvironmentIds); + }); + + it("editing a saved route does not copy its stale login to other routes", () => { + const other = new BearerConnectionTarget({ + ...BEARER_TARGET, + connectionId: "bearer:other-route", + }); + const fresh = new BearerConnectionCredential({ token: "fresh-token" }); + const document: ConnectionCatalogDocument = { + ...EMPTY_CONNECTION_CATALOG_DOCUMENT, + targets: [BEARER_TARGET, other], + profiles: [BEARER_PROFILE], + credentials: [ + { connectionId: BEARER_TARGET.connectionId, credential: BEARER_CREDENTIAL }, + { connectionId: other.connectionId, credential: fresh }, + ], + }; + const edited = registerConnectionInCatalog( + document, + new BearerConnectionRegistration({ + target: BEARER_TARGET, + profile: new BearerConnectionProfile({ ...BEARER_PROFILE, label: "Edited" }), + credential: BEARER_CREDENTIAL, + }), + document.targets, + ); + expect( + edited.credentials.find((value) => value.connectionId === other.connectionId)?.credential, + ).toEqual(fresh); + expect(edited.profiles[0]?.label).toBe("Edited"); + }); + it("replaces obsolete connection metadata without discarding a reusable DPoP token", () => { const bearer = registerConnectionInCatalog( { diff --git a/packages/client-runtime/src/platform/storageDocument.ts b/packages/client-runtime/src/platform/storageDocument.ts index 1af8e0e3d67aa..d9a2938f88084 100644 --- a/packages/client-runtime/src/platform/storageDocument.ts +++ b/packages/client-runtime/src/platform/storageDocument.ts @@ -134,7 +134,48 @@ export function registerConnectionInCatalog( switch (registration._tag) { case "RelayConnectionRegistration": return next; - case "BearerConnectionRegistration": + case "BearerConnectionRegistration": { + const profiles = replaceCatalogValue( + next.profiles, + (value) => value.connectionId, + registration.profile, + ); + const learnedIds = new Set( + profiles.flatMap((profile) => + profile._tag === "BearerConnectionProfile" && + (profile.learned === true || profile.authorization === "t3-connect") + ? [profile.connectionId] + : [], + ), + ); + const credentialChanged = + document.credentials.find( + (value) => value.connectionId === registration.target.connectionId, + )?.credential.token !== registration.credential.token; + // A pairing refreshes this environment's direct login. Learned routes + // borrow from these owners; SSH and T3 Connect keep their own auth. + const ownerIds = new Set([ + registration.target.connectionId, + ...(credentialChanged && !learnedIds.has(registration.target.connectionId) + ? catalogRoutes(next, registration.target.environmentId).flatMap((target) => + target._tag === "BearerConnectionTarget" && !learnedIds.has(target.connectionId) + ? [target.connectionId] + : [], + ) + : []), + ]); + return { + ...next, + profiles, + credentials: [ + ...next.credentials.filter((value) => !ownerIds.has(value.connectionId)), + ...Array.from(ownerIds, (connectionId) => ({ + connectionId, + credential: registration.credential, + })), + ], + }; + } case "SshConnectionRegistration": return { ...next,