Skip to content

Commit 78d05b9

Browse files
fix(client-runtime): SSH reconnects keep the alias target and routing consent
Reconnect no longer overwrites the saved SSH profile with the target ssh config resolved on that connect, which pinned the port and revoked pull request routing consent. Picking a discovered alias in Add Environment now connects by alias with only the typed user/port overrides instead of the resolved config values. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9f07e62 commit 78d05b9

3 files changed

Lines changed: 71 additions & 120 deletions

File tree

‎apps/web/src/components/settings/AddEnvironmentDialog.tsx‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,21 @@ export function AddEnvironmentDialog({
270270
const handleSelectSshHostSuggestion = async (target: DesktopDiscoveredSshHost) => {
271271
if (isAddingSavedBackend || !desktopBridge) return;
272272

273-
setIsAddingSavedBackend(true);
274-
onErrorChange(null);
275273
setSavedBackendSshHost(target.alias);
276-
let resolved: DesktopSshEnvironmentTarget;
274+
// Connect by alias with only the typed overrides. Saving what ssh config resolves
275+
// today would pin its user and port, so later config edits never apply.
276+
let aliasTarget: DesktopSshEnvironmentTarget;
277277
try {
278-
resolved = await desktopBridge.resolveSshHost(target.alias);
278+
aliasTarget = parseManualDesktopSshTarget({
279+
host: target.alias,
280+
username: savedBackendSshUsername,
281+
port: savedBackendSshPort,
282+
});
279283
} catch (error) {
280284
onErrorChange(formatDesktopSshConnectionError(error));
281-
setIsAddingSavedBackend(false);
282285
return;
283286
}
284-
setSavedBackendSshUsername(resolved.username ?? "");
285-
setSavedBackendSshPort(resolved.port === null ? "" : String(resolved.port));
286-
await connectSavedBackendSshTarget(resolved);
287+
await connectSavedBackendSshTarget(aliasTarget);
287288
};
288289

289290
const handleSavedBackendSshHostKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {

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

Lines changed: 62 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ import {
2020
type ConnectionProfile,
2121
} from "./catalog.ts";
2222
import * as ConnectionCredentialStore from "./credentialStore.ts";
23-
import {
24-
ConnectionTransientError,
25-
PrimaryConnectionTarget,
26-
SshConnectionTarget,
27-
type ConnectionTarget,
28-
} from "./model.ts";
23+
import { PrimaryConnectionTarget, SshConnectionTarget, type ConnectionTarget } from "./model.ts";
2924
import * as ConnectionProfileStore from "./profileStore.ts";
3025
import * as RpcHttp from "../rpc/http.ts";
3126
import {
@@ -250,99 +245,74 @@ describe("ConnectionResolver", () => {
250245
}),
251246
);
252247

253-
it.effect.each(["unchanged", "changed", "revocation-failed"] as const)(
254-
"handles %s SSH routing consent before saving or authorizing",
255-
(scenario) =>
256-
Effect.gen(function* () {
257-
const calls: string[] = [];
258-
const target = new SshConnectionTarget({
259-
environmentId: ENVIRONMENT_ID,
260-
label: "SSH",
261-
connectionId: "ssh-1",
262-
});
263-
const profile = new SshConnectionProfile({
264-
connectionId: target.connectionId,
265-
environmentId: ENVIRONMENT_ID,
266-
label: "SSH",
267-
target: SSH_TARGET,
268-
});
269-
const entry = catalogEntry(target, Option.some(profile));
270-
const preparedTarget =
271-
scenario === "unchanged"
272-
? SSH_TARGET
273-
: { ...SSH_TARGET, hostname: "replacement.example.test" };
274-
const failure = new ConnectionTransientError({
275-
reason: "remote-unavailable",
276-
detail: "Could not persist routing consent.",
277-
});
278-
const permissions = yield* makePullRequestRoutingPermissions({
279-
read: Effect.succeed([
280-
{
281-
environmentId: ENVIRONMENT_ID,
282-
connectionKey: pullRequestRoutingConnectionKey(entry)!,
283-
permission: "read-write",
284-
},
285-
]),
286-
write: () =>
287-
Effect.sync(() => {
288-
calls.push("revoke");
289-
}).pipe(
290-
Effect.andThen(scenario === "revocation-failed" ? Effect.fail(failure) : Effect.void),
291-
),
292-
});
293-
let savedProfile: ConnectionProfile = profile;
294-
const layerBroker = yield* makeDependencies({
295-
profileStore: {
296-
get: () => Effect.sync(() => Option.some(savedProfile)),
297-
put: (value) =>
298-
Effect.sync(() => {
299-
calls.push("profile");
300-
savedProfile = value;
301-
}),
302-
remove: () => Effect.die("unused"),
248+
it.effect("keeps the SSH target as entered and its routing consent across reconnects", () =>
249+
Effect.gen(function* () {
250+
const target = new SshConnectionTarget({
251+
environmentId: ENVIRONMENT_ID,
252+
label: "SSH",
253+
connectionId: "ssh-1",
254+
});
255+
const enteredTarget = { ...SSH_TARGET, hostname: "development", port: null };
256+
const profile = new SshConnectionProfile({
257+
connectionId: target.connectionId,
258+
environmentId: ENVIRONMENT_ID,
259+
label: "SSH",
260+
target: enteredTarget,
261+
});
262+
const entry = catalogEntry(target, Option.some(profile));
263+
const permissions = yield* makePullRequestRoutingPermissions({
264+
read: Effect.succeed([
265+
{
266+
environmentId: ENVIRONMENT_ID,
267+
connectionKey: pullRequestRoutingConnectionKey(entry)!,
268+
permission: "read-write",
303269
},
304-
prepareSsh: () =>
305-
Effect.succeed({
270+
]),
271+
write: () => Effect.void,
272+
});
273+
const preparedTargets: Array<DesktopSshEnvironmentTarget> = [];
274+
// ssh config resolves the alias, and its port changes between connects.
275+
const configPorts = [24568, 24569];
276+
let savedProfile: ConnectionProfile = profile;
277+
const layerBroker = yield* makeDependencies({
278+
profileStore: {
279+
get: () => Effect.sync(() => Option.some(savedProfile)),
280+
put: (value) =>
281+
Effect.sync(() => {
282+
savedProfile = value;
283+
}),
284+
remove: () => Effect.die("unused"),
285+
},
286+
prepareSsh: (input) =>
287+
Effect.sync(() => {
288+
preparedTargets.push(input.target);
289+
return {
306290
bootstrap: {
307-
target: preparedTarget,
291+
target: {
292+
...input.target,
293+
hostname: "127.0.0.1",
294+
port: configPorts[preparedTargets.length - 1]!,
295+
},
308296
httpBaseUrl: "http://127.0.0.1:4010",
309297
wsBaseUrl: "ws://127.0.0.1:4010",
310298
pairingToken: null,
311299
},
312300
bearerToken: "ssh-bearer",
313-
}),
314-
authorizeBearer: (input) =>
315-
Effect.sync(() => {
316-
calls.push("authorize");
317-
return {
318-
environmentId: input.expectedEnvironmentId,
319-
label: "SSH",
320-
httpBaseUrl: input.httpBaseUrl,
321-
socketUrl: "ws://127.0.0.1:4010/ws?wsTicket=ssh",
322-
httpAuthorization: { _tag: "Bearer" as const, token: input.bearerToken },
323-
};
324-
}),
325-
});
326-
const broker = yield* ConnectionResolver.ConnectionResolver.pipe(
327-
Effect.provide(layerBroker),
328-
);
329-
const prepare = broker
330-
.prepare(entry)
301+
};
302+
}),
303+
});
304+
const broker = yield* ConnectionResolver.ConnectionResolver.pipe(Effect.provide(layerBroker));
305+
const prepare = (current: ConnectionCatalogEntry) =>
306+
broker
307+
.prepare(current)
331308
.pipe(Effect.provideService(PullRequestRoutingPermissions, permissions));
332-
if (scenario === "revocation-failed") {
333-
expect(yield* Effect.flip(prepare)).toBe(failure);
334-
expect(savedProfile).toBe(profile);
335-
expect(calls).toEqual(["revoke"]);
336-
} else {
337-
expect((yield* prepare).socketUrl).toContain("wsTicket=ssh");
338-
expect(savedProfile).toMatchObject({ target: preparedTarget });
339-
expect(calls).toEqual(
340-
scenario === "unchanged"
341-
? ["profile", "authorize"]
342-
: ["revoke", "profile", "authorize"],
343-
);
344-
}
345-
expect(yield* permissions.get(entry)).toBe(scenario === "changed" ? "off" : "read-write");
346-
}),
309+
310+
yield* prepare(entry);
311+
yield* prepare(catalogEntry(target, Option.some(savedProfile)));
312+
313+
expect(preparedTargets).toEqual([enteredTarget, enteredTarget]);
314+
expect(savedProfile).toBe(profile);
315+
expect(yield* permissions.get(entry)).toBe("read-write");
316+
}),
347317
);
348318
});

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import {
3030
mapRemoteEnvironmentError,
3131
profileMissingError,
3232
} from "./errors.ts";
33-
import {
34-
PullRequestRoutingPermissions,
35-
pullRequestRoutingConnectionKey,
36-
} from "./pullRequestRoutingPermissions.ts";
3733
import type {
3834
BearerConnectionTarget,
3935
ConnectionTarget,
@@ -43,7 +39,6 @@ import type {
4339
SshConnectionTarget,
4440
} from "./model.ts";
4541
import { ConnectionBlockedError, type ConnectionAttemptError } from "./model.ts";
46-
import * as ConnectionProfileStore from "./profileStore.ts";
4742
import {
4843
appendOrchestrationProtocol,
4944
orchestrationProtocolCompatibilityError,
@@ -204,7 +199,6 @@ const makeRelayBroker = Effect.fn("clientRuntime.connection.broker.makeRelay")(f
204199
});
205200

206201
const makeSshBroker = Effect.fn("clientRuntime.connection.broker.makeSsh")(function* () {
207-
const profiles = yield* ConnectionProfileStore.ConnectionProfileStore;
208202
const credentials = yield* ConnectionCredentialStore.ConnectionCredentialStore;
209203
const ssh = yield* ClientCapabilities.SshEnvironmentGateway;
210204
const remote = yield* RemoteEnvironmentAuthorization.RemoteEnvironmentAuthorization;
@@ -243,20 +237,6 @@ const makeSshBroker = Effect.fn("clientRuntime.connection.broker.makeSsh")(funct
243237
new BearerConnectionCredential({ token: prepared.bearerToken }),
244238
);
245239
}
246-
const preparedProfile = new SshConnectionProfile({
247-
connectionId: profile.connectionId,
248-
environmentId: profile.environmentId,
249-
label: profile.label,
250-
target: prepared.bootstrap.target,
251-
});
252-
if (
253-
pullRequestRoutingConnectionKey(entry) !==
254-
pullRequestRoutingConnectionKey({ ...entry, profile: Option.some(preparedProfile) })
255-
) {
256-
const permissions = yield* PullRequestRoutingPermissions;
257-
yield* permissions.forget(target.environmentId);
258-
}
259-
yield* profiles.put(preparedProfile);
260240
const authorized = yield* remote.authorizeBearer({
261241
expectedEnvironmentId: target.environmentId,
262242
httpBaseUrl: prepared.bootstrap.httpBaseUrl,

0 commit comments

Comments
 (0)