mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Tools › Start SSH Session ran the system `ssh -t` in a terminal, which never used the password saved in the SSH configuration and, on Windows, could not take a typed password either. The session now opens a login shell over the IDE's SSH connection: the terminal manager can attach a channel (here an SSH shell) next to local PTYs, with the same output batching, UTF-8 decoding and exit events. A password or passphrase that isn't saved is asked for in a dialog, and an unknown host key asks to be trusted, before reconnecting. Closing the tab hangs the shell up. Also: a remote process closed by this side now ends without waiting for a close that russh never forwards, and each connection attempt gets its own IPC channel, since a failed attempt invalidates the one it was given. Fixes #2874
127 lines
5.8 KiB
TypeScript
127 lines
5.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { DEFAULT_SSH_CONFIG, type SshConfigValid } from "@/platform/remoteSettings";
|
|
import { answerAskpass, askpassAnswer, dropAskpass, enqueueAskpass, useAskpass } from "./askpass";
|
|
import { type ConnectDeps, openSshTerminal, sessionItems, sshRemedy } from "./session";
|
|
|
|
describe("askpass queue", () => {
|
|
beforeEach(() => useAskpass.setState({ queue: [] }));
|
|
|
|
it("queues prompts in arrival order, once each", () => {
|
|
enqueueAskpass({ id: 1, prompt: "Password:", kind: "secret", remember: false });
|
|
enqueueAskpass({ id: 2, prompt: "Continue connecting (yes/no)?", kind: "confirm", remember: false });
|
|
enqueueAskpass({ id: 1, prompt: "Password:", kind: "secret", remember: false });
|
|
expect(useAskpass.getState().queue.map((p) => p.id)).toEqual([1, 2]);
|
|
});
|
|
|
|
it("drops a prompt answered elsewhere", () => {
|
|
enqueueAskpass({ id: 1, prompt: "a", kind: "text", remember: false });
|
|
enqueueAskpass({ id: 2, prompt: "b", kind: "text", remember: false });
|
|
dropAskpass(1);
|
|
dropAskpass(9);
|
|
expect(useAskpass.getState().queue.map((p) => p.id)).toEqual([2]);
|
|
});
|
|
|
|
it("answers the backend and shows the next prompt", () => {
|
|
const send = vi.fn((_id: number, _answer: string | null, _remember: boolean) => Promise.resolve());
|
|
enqueueAskpass({ id: 1, prompt: "a", kind: "secret", remember: true });
|
|
enqueueAskpass({ id: 2, prompt: "b", kind: "secret", remember: false });
|
|
answerAskpass({ id: 1, prompt: "a", kind: "secret", remember: true }, "hunter2", true, send);
|
|
expect(send).toHaveBeenCalledWith(1, "hunter2", true);
|
|
expect(useAskpass.getState().queue.map((p) => p.id)).toEqual([2]);
|
|
});
|
|
|
|
it("maps choices to answers", () => {
|
|
expect(askpassAnswer("secret", { text: "pw" })).toBe("pw");
|
|
expect(askpassAnswer("text", { text: "" })).toBe("");
|
|
expect(askpassAnswer("confirm", { confirm: true })).toBe("yes");
|
|
expect(askpassAnswer("confirm", { confirm: false })).toBe("no");
|
|
expect(askpassAnswer("secret", null)).toBeNull();
|
|
expect(askpassAnswer("confirm", null)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("Start SSH Session chooser", () => {
|
|
const config = (patch: Partial<SshConfigValid>): SshConfigValid => ({ ...DEFAULT_SSH_CONFIG, ...patch });
|
|
|
|
it("lists configurations, then a way to edit them", () => {
|
|
const start = vi.fn();
|
|
const configure = vi.fn();
|
|
const items = sessionItems(
|
|
[
|
|
config({ id: "a", name: "Dev box", host: "box", user: "dev", port: 2222 }),
|
|
config({ id: "b", host: "bastion" }),
|
|
config({ id: "c", name: "Empty" }),
|
|
],
|
|
start,
|
|
configure,
|
|
);
|
|
expect(items.map((i) => [i.label, i.detail])).toEqual([
|
|
["Dev box", "dev@box:2222"],
|
|
["bastion", undefined],
|
|
["Empty", undefined],
|
|
["Edit SSH Configurations…", undefined],
|
|
]);
|
|
expect(items[2]!.disabled).toBeTruthy();
|
|
void items[0]!.run();
|
|
expect(start).toHaveBeenCalledWith("a");
|
|
void items[3]!.run();
|
|
expect(configure).toHaveBeenCalled();
|
|
});
|
|
|
|
it("offers to add one when there are none", () => {
|
|
const items = sessionItems([], vi.fn(), vi.fn());
|
|
expect(items.map((i) => i.label)).toEqual(["No SSH configurations. Add one…"]);
|
|
});
|
|
});
|
|
|
|
describe("Start SSH Session connection", () => {
|
|
const config: SshConfigValid = { ...DEFAULT_SSH_CONFIG, id: "box", host: "box", auth: "password", saveSecret: true };
|
|
const output = {} as Parameters<typeof openSshTerminal>[3];
|
|
const deps = (errors: string[], patch: Partial<ConnectDeps> = {}): ConnectDeps => ({
|
|
open: vi.fn(async () => {
|
|
const error = errors.shift();
|
|
if (error) throw new Error(error);
|
|
return { id: 7, title: "box", pid: null };
|
|
}),
|
|
confirmTrust: vi.fn(async () => true),
|
|
askSecret: vi.fn(async () => "pw"),
|
|
trustHost: vi.fn(async () => {}),
|
|
setSecret: vi.fn(async () => {}),
|
|
...patch,
|
|
});
|
|
|
|
it("classifies what a failed connection needs", () => {
|
|
expect(sshRemedy("Unknown SSH host key for box:22 (SHA256:x)", "password")).toBe("trustHost");
|
|
expect(sshRemedy("SSH password required for dev@box", "password")).toBe("secret");
|
|
expect(sshRemedy("SSH key passphrase required for /k/id", "keyPair")).toBe("secret");
|
|
expect(sshRemedy("SSH authentication failed for dev@box", "password")).toBe("secret");
|
|
expect(sshRemedy("SSH authentication failed for box", "openSsh")).toBeNull();
|
|
expect(sshRemedy("connection refused", "password")).toBeNull();
|
|
});
|
|
|
|
it("opens with the saved credentials without asking", async () => {
|
|
const d = deps([]);
|
|
expect(await openSshTerminal(config, 80, 24, output, d)).toMatchObject({ id: 7 });
|
|
expect(d.open).toHaveBeenCalledWith("box", 80, 24, output);
|
|
expect(d.askSecret).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("trusts a new host key and asks for a missing password, then connects", async () => {
|
|
const d = deps(["Unknown SSH host key for box:22 (SHA256:x)", "SSH password required for dev@box"]);
|
|
await openSshTerminal(config, 80, 24, output, d);
|
|
expect(d.trustHost).toHaveBeenCalledWith("box");
|
|
expect(d.setSecret).toHaveBeenCalledWith("box", "pw", true);
|
|
expect(d.open).toHaveBeenCalledTimes(3);
|
|
});
|
|
|
|
it("stops when a prompt is declined or the error has no remedy", async () => {
|
|
const declined = deps(["SSH password required for dev@box"], { askSecret: vi.fn(async () => null) });
|
|
await expect(openSshTerminal(config, 80, 24, output, declined)).rejects.toThrow("SSH password required");
|
|
expect(declined.setSecret).not.toHaveBeenCalled();
|
|
const refused = deps(["connection refused"]);
|
|
await expect(openSshTerminal(config, 80, 24, output, refused)).rejects.toThrow("connection refused");
|
|
const changed = deps(["Unknown SSH host key a", "Unknown SSH host key b"]);
|
|
await expect(openSshTerminal(config, 80, 24, output, changed)).rejects.toThrow("Unknown SSH host key b");
|
|
});
|
|
});
|