Skip to content

Commit 06e3bfe

Browse files
t3dotggclaude
andcommitted
fix(client-runtime): re-registering keeps learned routes unless their credential is replaced
A label edit or rename registers the same credential again, so learned routes that borrow it stay. Only a deleted credential, or one whose kind changes (a pairing replacing a Tailscale sign-in), drops them; the next connect learns them again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5028c8b commit 06e3bfe

2 files changed

Lines changed: 109 additions & 11 deletions

File tree

‎packages/client-runtime/src/connection/registry.test.ts‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import {
3434
RelayConnectionRegistration,
3535
SshConnectionProfile,
3636
SshConnectionRegistration,
37+
TailnetConnectionCredential,
38+
TailnetConnectionRegistration,
3739
type ConnectionCredential,
3840
type ConnectionProfile,
3941
} from "./catalog.ts";
@@ -1965,6 +1967,86 @@ describe("EnvironmentRegistry routes", () => {
19651967
}),
19661968
);
19671969

1970+
it.effect(
1971+
"registering a route again with the same kind of credential keeps its learned routes",
1972+
() =>
1973+
Effect.gen(function* () {
1974+
const learnedId = `learned:${LAN_TARGET.environmentId}:http://100.88.1.2:3773@${LAN_TARGET.connectionId}`;
1975+
const learned = new BearerConnectionTarget({ ...LAN_TARGET, connectionId: learnedId });
1976+
const harness = yield* makeHarness(
1977+
[LAN_TARGET, learned, RELAY_TARGET],
1978+
[
1979+
LAN_PROFILE,
1980+
new BearerConnectionProfile({
1981+
connectionId: learnedId,
1982+
environmentId: LAN_TARGET.environmentId,
1983+
label: LAN_TARGET.label,
1984+
httpBaseUrl: "http://100.88.1.2:3773/",
1985+
wsBaseUrl: "ws://100.88.1.2:3773/",
1986+
learned: true,
1987+
network: "tailscale",
1988+
}),
1989+
],
1990+
[[LAN_TARGET.connectionId, BEARER_CREDENTIAL]],
1991+
);
1992+
yield* Effect.gen(function* () {
1993+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
1994+
yield* registry.register(lanRegistration());
1995+
1996+
expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual(
1997+
[LAN_TARGET, learned, RELAY_TARGET],
1998+
);
1999+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
2000+
}),
2001+
);
2002+
2003+
it.effect(
2004+
"renaming a route upgraded to Tailscale sign-in keeps its learned Tailscale route",
2005+
() =>
2006+
Effect.gen(function* () {
2007+
const learnedId = `learned:${LAN_TARGET.environmentId}:http://100.88.1.2:3773@${LAN_TARGET.connectionId}`;
2008+
const learned = new BearerConnectionTarget({ ...LAN_TARGET, connectionId: learnedId });
2009+
const session = new TailnetConnectionCredential({
2010+
signInBaseUrl: "http://100.88.1.2:3773/",
2011+
accessToken: "tailnet-token",
2012+
expiresAtEpochMs: 4_102_444_800_000,
2013+
dpopThumbprint: "thumbprint",
2014+
});
2015+
const harness = yield* makeHarness(
2016+
[LAN_TARGET, learned, RELAY_TARGET],
2017+
[
2018+
LAN_PROFILE,
2019+
new BearerConnectionProfile({
2020+
connectionId: learnedId,
2021+
environmentId: LAN_TARGET.environmentId,
2022+
label: LAN_TARGET.label,
2023+
httpBaseUrl: "http://100.88.1.2:3773/",
2024+
wsBaseUrl: "ws://100.88.1.2:3773/",
2025+
learned: true,
2026+
authorization: "tailnet",
2027+
network: "tailscale",
2028+
}),
2029+
],
2030+
[[LAN_TARGET.connectionId, session]],
2031+
);
2032+
const renamed = new BearerConnectionTarget({ ...LAN_TARGET, label: "Renamed" });
2033+
yield* Effect.gen(function* () {
2034+
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
2035+
yield* registry.register(
2036+
new TailnetConnectionRegistration({
2037+
target: renamed,
2038+
profile: new BearerConnectionProfile({ ...LAN_PROFILE, label: "Renamed" }),
2039+
credential: session,
2040+
}),
2041+
);
2042+
2043+
expect(routesOf(yield* Ref.get(harness.storedTargets), LAN_TARGET.environmentId)).toEqual(
2044+
[renamed, learned, RELAY_TARGET],
2045+
);
2046+
}).pipe(Effect.provide(harness.layer), Effect.scoped);
2047+
}),
2048+
);
2049+
19682050
it.effect("pairing at a saved address under a new id drops routes learned over the old one", () =>
19692051
Effect.gen(function* () {
19702052
const learnedId = `learned:${LAN_TARGET.environmentId}:http://100.88.1.2:3773@${LAN_TARGET.connectionId}`;

‎packages/client-runtime/src/connection/registry.ts‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ interface EnvironmentServiceScope {
209209
readonly scope: Scope.Closeable;
210210
}
211211

212+
const authorizationOf = (route: ConnectionRoute) =>
213+
Option.match(route.profile, {
214+
onNone: () => undefined,
215+
onSome: (profile) =>
216+
profile._tag === "BearerConnectionProfile" ? profile.authorization : undefined,
217+
});
218+
212219
/** @public */
213220
export const make = Effect.gen(function* () {
214221
const registryScope = yield* Scope.Scope;
@@ -594,20 +601,29 @@ export const make = Effect.gen(function* () {
594601
const existing = connectionRoutes(previous);
595602
const sameAddress = findRouteToSameAddress(existing, route);
596603
const id = connectionRouteId(route.target);
597-
// Registering replaces the route at the same address and the credential it held.
598-
const replacedIds = new Set([
599-
id,
600-
...(sameAddress === undefined ? [] : [connectionRouteId(sameAddress.target)]),
601-
]);
604+
const replacedId =
605+
sameAddress !== undefined && connectionRouteId(sameAddress.target) !== id
606+
? connectionRouteId(sameAddress.target)
607+
: undefined;
608+
// Learned routes borrow a credential. Drop them when it is deleted, or
609+
// when its kind changes (a pairing replacing a Tailscale sign-in); the
610+
// saved credential decides, since an upgraded LAN route keeps a LAN profile.
611+
const registeredAuthorization =
612+
registration._tag === "TailnetConnectionRegistration"
613+
? "tailnet"
614+
: authorizationOf(route);
615+
const dropsLearned = (candidate: ConnectionRoute) => {
616+
if (!isLearned(candidate)) return false;
617+
const borrowed = credentialConnectionId(connectionRouteId(candidate.target));
618+
return (
619+
borrowed === replacedId ||
620+
(borrowed === id && authorizationOf(candidate) !== registeredAuthorization)
621+
);
622+
};
602623
const routes = existing.filter(
603624
(existing) =>
604625
(existing !== sameAddress || connectionRouteId(existing.target) === id) &&
605-
// Routes learned over the old credential may carry its Tailscale
606-
// sign-in; the next connect learns them again with the new one.
607-
!(
608-
isLearned(existing) &&
609-
replacedIds.has(credentialConnectionId(connectionRouteId(existing.target)))
610-
),
626+
!dropsLearned(existing),
611627
);
612628
const next = withRoutes(previous, upsertRoute(routes, route));
613629
if (next.addressesChanged) {

0 commit comments

Comments
 (0)