From a7a95e65eda90a3027950669ea545463fc17688e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Almeida?= Date: Sun, 17 May 2026 18:54:35 +0100 Subject: [PATCH] test(e2e): probe SSH fingerprint via kandev, not ssh-keyscan, in K specs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit regenerateHostKey was returning the ssh-keyscan fingerprint, but ssh-keyscan and the Go ssh client can negotiate different host-key types — meaning the fingerprint kandev observes during a real probe does not always match what keyscan saw. The keyscan output was used verbatim as the expected value, which K1 / K3 then asserted against kandev's pinned-fingerprint result, so the specs flapped depending on which algorithm sshd advertised. Drop the return value from regenerateHostKey and have callers hit /api/v1/ssh/test to learn the canonical fingerprint, matching what the SSH fixture already does at seeding time. K2 was polling env.error_message which doesn't exist — the launch fails at SSH dial time before the executor is recorded on the task environment. Point the polling at the task-session error_message instead, where updateTaskSessionState actually persists the dial failure. --- apps/web/e2e/helpers/ssh.ts | 16 ++++++++++---- .../e2e/tests/ssh/hostkey-rotation.spec.ts | 21 ++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/apps/web/e2e/helpers/ssh.ts b/apps/web/e2e/helpers/ssh.ts index b6ca4aeb3d..9b662fbbbc 100644 --- a/apps/web/e2e/helpers/ssh.ts +++ b/apps/web/e2e/helpers/ssh.ts @@ -174,9 +174,12 @@ export function dumpRemoteLogs(handle: SSHServerHandle, label: string): string | * fingerprint surface the host-key-changed error verbatim — exactly the path * the host-key-rotation specs care about. * - * Returns the new fingerprint so the test can assert against it. + * Does NOT return the new fingerprint. ssh-keyscan and the Go ssh client + * negotiate different host-key types in some configurations, so the + * fingerprint reported by keyscan won't always match what kandev observes + * — callers must hit /api/v1/ssh/test to learn the canonical value. */ -export function regenerateHostKey(handle: SSHServerHandle): string { +export function regenerateHostKey(handle: SSHServerHandle): void { execInContainer(handle, [ "sh", "-c", @@ -184,7 +187,6 @@ export function regenerateHostKey(handle: SSHServerHandle): string { ]); // sshd needs a moment to re-bind after pkill -HUP. waitForTCPOpen(handle.host, handle.port); - return scanHostFingerprint(handle.host, handle.port); } /** @@ -291,7 +293,13 @@ function generateKeypair(identityFile: string): void { fs.chmodSync(identityFile, 0o600); } -function execInContainer(handle: SSHServerHandle, argv: string[]): string { +/** + * Run an arbitrary command inside the sshd container as root. Used by tests + * to seed/clear remote state that the SSH executor would normally manage — + * e.g. wiping ~/.kandev/bin/agentctl to force a re-upload on the next + * launch. Throws when the command exits non-zero. + */ +export function execInContainer(handle: SSHServerHandle, argv: string[]): string { const res = spawnSync("docker", ["exec", handle.containerName, ...argv], { encoding: "utf8", }); diff --git a/apps/web/e2e/tests/ssh/hostkey-rotation.spec.ts b/apps/web/e2e/tests/ssh/hostkey-rotation.spec.ts index 9d6e2002d2..8d831a41a7 100644 --- a/apps/web/e2e/tests/ssh/hostkey-rotation.spec.ts +++ b/apps/web/e2e/tests/ssh/hostkey-rotation.spec.ts @@ -16,8 +16,7 @@ test.describe("ssh executor — host key rotation", () => { seedData, }) => { const before = seedData.sshTarget.hostFingerprint; - const newFp = regenerateHostKey(seedData.sshTarget); - expect(newFp).not.toBe(before); + regenerateHostKey(seedData.sshTarget); const result = await apiClient.testSSHConnection({ name: "K1 after rekey", @@ -28,7 +27,8 @@ test.describe("ssh executor — host key rotation", () => { identity_file: seedData.sshTarget.identityFile, }); expect(result.success).toBe(true); - expect(result.fingerprint).toBe(newFp); + expect(result.fingerprint).toBeTruthy(); + expect(result.fingerprint).not.toBe(before); }); test("launching against a stale pinned fingerprint fails with host-key-changed", async ({ @@ -62,11 +62,14 @@ test.describe("ssh executor — host key rotation", () => { }, ); + // The launch fails at SSH dial time before any executor is recorded on + // the task environment — the host-key-changed error lands on the task + // session itself. await expect .poll( async () => { - const env = await apiClient.getTaskEnvironment(task.id); - return env?.error_message ?? null; + const { sessions } = await apiClient.listTaskSessions(task.id); + return sessions[0]?.error_message ?? null; }, { timeout: 60_000 }, ) @@ -75,8 +78,9 @@ test.describe("ssh executor — host key rotation", () => { test("re-trusting the new fingerprint restores function", async ({ apiClient, seedData }) => { test.setTimeout(240_000); - // Rotate, observe, re-trust. - const newFp = regenerateHostKey(seedData.sshTarget); + // Rotate, observe via kandev's probe (ssh-keyscan disagrees in some + // host-key configurations), re-trust. + regenerateHostKey(seedData.sshTarget); const test1 = await apiClient.testSSHConnection({ name: "K3 observe new", host: seedData.sshTarget.host, @@ -85,7 +89,8 @@ test.describe("ssh executor — host key rotation", () => { identity_source: "file", identity_file: seedData.sshTarget.identityFile, }); - expect(test1.fingerprint).toBe(newFp); + expect(test1.success).toBe(true); + const newFp = test1.fingerprint!; await apiClient.updateExecutor(seedData.sshExecutorId, { config: {