fix: show why SSH connection tests and runs fail (#7)

Test connection, key installation and runs now tell authentication
failures apart from unreachable hosts (refused, timeout, DNS, ...)
instead of a generic "SSH connection failed".

The web client keeps the server's deliberate error messages (4xx,
502, 503) and shows them in the hosts page; unexpected 500 errors
still show only the status. The reload hint appears only on 409
conflicts.
This commit is contained in:
Florian Schmidt
2026-11-16 10:31:11 +01:00
parent 83e4e3f15e
commit 324ad4b39a
9 changed files with 91 additions and 9 deletions
+2 -2
View File
@@ -17,7 +17,7 @@ import {
httpError,
SerialQueue,
} from "../workspace";
import { connect, exec, forgetHostKey, HostKeyError } from "../ssh";
import { connect, exec, forgetHostKey, HostKeyError, sshFailure } from "../ssh";
import { CredentialStore } from "./credentials";
import { validateHost } from "./validation";
const saves = new SerialQueue();
@@ -221,7 +221,7 @@ export async function hostsPlugin(
changed: e.changed,
},
};
throw httpError(502, "SSH connection or action failed");
throw httpError(502, sshFailure(e) ?? "SSH connection or action failed");
} finally {
client?.end();
}
+3 -2
View File
@@ -8,7 +8,7 @@ import type { FastifyInstance } from "fastify";
import type { Client } from "ssh2";
import type { ServerConfig } from "../config";
import { readHosts, readNotebook, safeFile, safeRelative, httpError, SerialQueue } from "../workspace";
import { connect, exec, localExec, scriptInput, sftp, sftpRequest, transfer, HostKeyError, type Output } from "../ssh";
import { connect, exec, localExec, scriptInput, sftp, sftpRequest, transfer, HostKeyError, sshFailure, type Output } from "../ssh";
import { EventsHub } from "./events";
import { runRoot, artifactFile, persistArtifacts, isSensitive, claimArtifact, sweepRuns } from "./artifacts";
export { EventsHub } from "./events";
@@ -232,7 +232,8 @@ export async function runsPlugin(app: FastifyInstance, { config, hub }: { config
function runFailure(error: unknown, fallback: string, timeoutSec: number): string {
const e = error as { message?: string; level?: string; code?: string };
if (e.message === "Execution timed out") return `Execution timed out after ${timeoutSec} s`;
if (e.level === "client-authentication") return "SSH authentication failed";
const ssh = sshFailure(error);
if (ssh) return ssh;
if (e.message === "Captured output exceeds 1 MiB") return e.message;
if (e.message?.startsWith("SFTP ") && e.message.endsWith(" timed out")) return "File transfer timed out";
return fallback;
+14
View File
@@ -162,3 +162,17 @@ export async function forgetHostKey(config: ServerConfig, host: HostInfo) {
await writeFile(file, lines.join("\n"), { mode: 0o600 });
});
}
const unreachable: Record<string, string> = {
ECONNREFUSED: "connection refused", EHOSTUNREACH: "host unreachable", ENETUNREACH: "network unreachable",
ENOTFOUND: "unknown host name", EAI_AGAIN: "host name lookup failed", ETIMEDOUT: "connection timed out",
ECONNRESET: "connection reset",
};
/** User-facing reason for a failed SSH connection; undefined when not a connection problem. Never includes secrets. */
export function sshFailure(error: unknown): string | undefined {
const e = error as { level?: string; code?: string } | null;
if (e?.level === "client-authentication") return "SSH authentication failed: check the user and the saved password or key";
if (e?.level === "client-timeout") return "Cannot reach the server: SSH handshake timed out";
if (e?.code && unreachable[e.code]) return `Cannot reach the server: ${unreachable[e.code]}`;
return undefined;
}