Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/client-runtime/src/connection/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2064,9 +2064,24 @@ describe("EnvironmentRegistry routes", () => {

it.effect("re-pairing the same address replaces that route rather than adding another", () =>
Effect.gen(function* () {
// Borrows the login saved under the old route id, which goes away with that route.
const learnedId = `learned:${LAN_TARGET.environmentId}:http://100.101.102.103:3773@${LAN_TARGET.connectionId}`;
const harness = yield* makeHarness(
[LAN_TARGET, RELAY_TARGET],
[LAN_PROFILE],
[
LAN_TARGET,
new BearerConnectionTarget({ ...LAN_TARGET, connectionId: learnedId }),
RELAY_TARGET,
],
[
LAN_PROFILE,
new BearerConnectionProfile({
...LAN_PROFILE,
connectionId: learnedId,
httpBaseUrl: "http://100.101.102.103:3773/",
wsBaseUrl: "ws://100.101.102.103:3773/",
learned: true,
}),
],
[[LAN_TARGET.connectionId, BEARER_CREDENTIAL]],
);
yield* Effect.gen(function* () {
Expand Down
11 changes: 6 additions & 5 deletions packages/client-runtime/src/connection/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,12 @@ export const make = Effect.gen(function* () {
if (previous !== undefined) {
const existing = connectionRoutes(previous);
const sameAddress = findRouteToSameAddress(existing, route);
const routes = existing.filter(
(existing) =>
existing !== sameAddress ||
connectionRouteId(existing.target) === connectionRouteId(route.target),
);
// An address saved under another id is replaced, along with the routes borrowing its login.
const routes =
sameAddress === undefined ||
connectionRouteId(sameAddress.target) === connectionRouteId(route.target)
? existing
: routesAfterRemoving(existing, connectionRouteId(sameAddress.target));
const next = withRoutes(previous, upsertRoute(routes, route));
if (next.addressesChanged) {
yield* forgetRoutingTrust(environmentId, "register-connection");
Expand Down
38 changes: 32 additions & 6 deletions packages/client-runtime/src/connection/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,17 +340,43 @@ export function mergeLearnedRoutes(input: {
// A paired token goes to a plain HTTP address outside the tailnet only when
// it was paired that way, so a token paired over HTTPS, loopback, or the
// tailnet keeps to the transports it was issued for.
function pairedOverPlainHttp(route: ConnectionRoute): boolean {
const kind = connectionRouteKind(route);
return (
routeHttpBaseUrl(route)?.startsWith("http:") === true && (kind === "lan" || kind === "public")
);
}

function tokenAllowsPlainHttp(
routes: ReadonlyArray<ConnectionRoute>,
credentialId: string,
): boolean {
const credentialRoute = routes.find((route) => connectionRouteId(route.target) === credentialId);
return (
credentialRoute !== undefined &&
routeHttpBaseUrl(credentialRoute)?.startsWith("http:") === true &&
(connectionRouteKind(credentialRoute) === "lan" ||
connectionRouteKind(credentialRoute) === "public")
);
return credentialRoute !== undefined && pairedOverPlainHttp(credentialRoute);
}

/**
* The paired addresses of `paired`'s environment that take the login just issued over it. The
* server accepts a login at every address, and learned routes borrow one of these. A plain HTTP
* address gets it only under the rule learned routes follow.
*/
export function routesSharingLogin(
routes: ReadonlyArray<ConnectionRoute>,
paired: ConnectionRoute,
): ReadonlyArray<ConnectionRoute> {
const inClear = pairedOverPlainHttp(paired);
return routes.filter((route) => {
if (route.target._tag !== "BearerConnectionTarget" || isLearned(route)) return false;
if (connectionRouteId(route.target) === connectionRouteId(paired.target)) return true;
const profile = Option.getOrNull(route.profile);
if (profile?._tag !== "BearerConnectionProfile") return false;
return (
inClear ||
!profile.httpBaseUrl.startsWith("http:") ||
profile.network === "tailscale" ||
connectionRouteKind(route) === "loopback"
);
});
}

/**
Expand Down
61 changes: 61 additions & 0 deletions packages/client-runtime/src/platform/storageDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,67 @@ describe("ConnectionCatalogDocument", () => {
expect(bearerOnly.remoteDpopTokens).toEqual([]);
});

it("a new pairing replaces the login of the environment's other paired addresses", () => {
const route = (connectionId: string, httpBaseUrl: string, learned = false) => ({
target: new BearerConnectionTarget({ ...BEARER_TARGET, connectionId }),
profile: new BearerConnectionProfile({
...BEARER_PROFILE,
connectionId,
httpBaseUrl,
wsBaseUrl: httpBaseUrl.replace(/^http/, "ws"),
...(learned ? { learned: true } : {}),
}),
});
const expired = new BearerConnectionCredential({ token: "expired" });
const https = route("https", "https://remote.example.test/");
const lan = route("lan", "http://192.168.1.10:3773/");
const learned = route("learned:tailnet@https", "http://100.101.102.103:3773/", true);
const ssh = new SshConnectionTarget({
environmentId: ENVIRONMENT_ID,
label: "Remote",
connectionId: "ssh",
});
const saved: ConnectionCatalogDocument = {
...EMPTY_CONNECTION_CATALOG_DOCUMENT,
targets: [https.target, lan.target, learned.target, ssh],
profiles: [https.profile, lan.profile, learned.profile],
credentials: [https, lan, { target: ssh }].map(({ target }) => ({
connectionId: target.connectionId,
credential: expired,
})),
};
const tokens = (document: ConnectionCatalogDocument) =>
Object.fromEntries(
document.credentials.map((value) => [value.connectionId, value.credential.token]),
);
const pair = (paired: ReturnType<typeof route>, credential = BEARER_CREDENTIAL) =>
registerConnectionInCatalog(
saved,
new BearerConnectionRegistration({ ...paired, credential }),
[paired.target, ...saved.targets],
);

// Paired over HTTPS: the plain HTTP LAN address keeps to the login it was paired with.
const overHttps = pair(route("https-2", "https://other.example.test/"));
expect(tokens(overHttps)).toEqual({
ssh: "expired",
lan: "expired",
https: "bearer-token",
"https-2": "bearer-token",
});
// Removing the newly paired address leaves the others on the working login.
expect(tokens(setRoutesInCatalog(overHttps, ENVIRONMENT_ID, saved.targets))).toMatchObject({
https: "bearer-token",
});

expect(tokens(pair(route("lan-2", "http://10.0.0.5:3773/")))).toMatchObject({
lan: "bearer-token",
https: "bearer-token",
});
// Editing an address registers the login it already has.
expect(tokens(pair(lan, expired))).toEqual(tokens(saved));
});

it("keeps an environment's position in the catalog when its routes change", () => {
const other = new RelayConnectionTarget({
environmentId: EnvironmentId.make("environment-2"),
Expand Down
40 changes: 39 additions & 1 deletion packages/client-runtime/src/platform/storageDocument.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EnvironmentId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";

import {
Expand All @@ -8,6 +9,7 @@ import {
ConnectionProfile,
} from "../connection/catalog.ts";
import { type ConnectionTarget, PersistedConnectionTarget } from "../connection/model.ts";
import { routesSharingLogin } from "../connection/routes.ts";
import * as TokenStore from "../authorization/tokenStore.ts";
import { StoredPullRequestRoutingPermission } from "../connection/pullRequestRoutingPermissions.ts";

Expand Down Expand Up @@ -134,7 +136,43 @@ export function registerConnectionInCatalog(
switch (registration._tag) {
case "RelayConnectionRegistration":
return next;
case "BearerConnectionRegistration":
case "BearerConnectionRegistration": {
const { connectionId } = registration.target;
const profiles = replaceCatalogValue(
next.profiles,
(value) => value.connectionId,
registration.profile,
);
// An edit registers the login it already has, which must not overwrite the other addresses'.
const isNewLogin =
document.credentials.find((value) => value.connectionId === connectionId)?.credential
.token !== registration.credential.token;
const owners = new Set([
connectionId,
...(isNewLogin
? routesSharingLogin(
routes.map((target) => ({
target,
profile: Option.fromUndefinedOr(
profiles.find((profile) => profile.connectionId === connectionIdOf(target)),
),
})),
{ target: registration.target, profile: Option.some(registration.profile) },
).flatMap((route) => connectionIdOf(route.target) ?? [])
: []),
]);
return {
...next,
profiles,
credentials: [
...next.credentials.filter((value) => !owners.has(value.connectionId)),
...Array.from(owners, (owner) => ({
connectionId: owner,
credential: registration.credential,
})),
],
};
}
case "SshConnectionRegistration":
return {
...next,
Expand Down
Loading