fix: bound SSH failure diagnostics
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
101ea19f1c
commit
4b8a5547d4
7 files changed
+213
-124
No files matched your search
@@ -96,9 +96,10 @@ describe("SSH environment IPC", () => {
|
||||
it("presents legacy process causes without weakening structured errors", () => {
|
||||
const cause = new Error("ssh executable was not found");
|
||||
const structured = new SshCommandSpawnError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount: 0,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: "devbox",
|
||||
cause,
|
||||
});
|
||||
|
||||
@@ -34,30 +34,35 @@ describe("ssh auth", () => {
|
||||
it("only follows causes from SSH process wrappers", () => {
|
||||
const authFailure = new Error("Permission denied (publickey,password).");
|
||||
const commandFailure = new SshErrors.SshCommandSpawnError({
|
||||
command: ["ssh", "devbox"],
|
||||
command: "ssh",
|
||||
argumentCount: 1,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: "devbox",
|
||||
cause: authFailure,
|
||||
});
|
||||
assert.equal(isSshAuthFailure(commandFailure), true);
|
||||
|
||||
const helperFailure = new SshErrors.SshAuthenticationHelperError({
|
||||
command: ["ssh", "devbox"],
|
||||
command: "ssh",
|
||||
argumentCount: 1,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: "devbox",
|
||||
cause: authFailure,
|
||||
});
|
||||
assert.equal(isSshAuthFailure(helperFailure), false);
|
||||
|
||||
const readinessFailure = new SshErrors.SshReadinessTimeoutError({
|
||||
baseUrl: "http://127.0.0.1:41773/",
|
||||
requestUrl: "http://127.0.0.1:41773/ready",
|
||||
baseTarget: "http://127.0.0.1:41773/",
|
||||
baseUrlLength: 23,
|
||||
requestTarget: "http://127.0.0.1:41773/ready",
|
||||
requestUrlLength: 28,
|
||||
timeoutMs: 1_000,
|
||||
attempts: 1,
|
||||
cause: new SshErrors.SshReadinessProbeError({
|
||||
requestUrl: "http://127.0.0.1:41773/ready",
|
||||
requestTarget: "http://127.0.0.1:41773/ready",
|
||||
requestUrlLength: 28,
|
||||
cause: new Error("HTTP authentication failed."),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -145,7 +145,7 @@ describe("ssh command", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("includes stdout in non-zero command failures when stderr is empty", () => {
|
||||
it.effect("records bounded output diagnostics for non-zero command failures", () => {
|
||||
const spawner = ChildProcessSpawner.make(() =>
|
||||
Effect.succeed(makeFailedProcess({ stdout: "Pairing token creation failed\n" })),
|
||||
);
|
||||
@@ -168,16 +168,24 @@ describe("ssh command", () => {
|
||||
assert.isTrue(Result.isFailure(result));
|
||||
if (Result.isFailure(result)) {
|
||||
assert.instanceOf(result.failure, SshCommandExitError);
|
||||
assert.equal(result.failure.message, "Pairing token creation failed");
|
||||
assert.equal(result.failure.stdout, "Pairing token creation failed\n");
|
||||
assert.equal(result.failure.stderr, "");
|
||||
assert.equal(result.failure.message, "SSH command failed for julius@devbox (exit 1).");
|
||||
assert.equal(result.failure.command, "ssh");
|
||||
assert.equal(result.failure.argumentCount, 9);
|
||||
assert.equal(
|
||||
result.failure.stdoutBytes,
|
||||
encoder.encode("Pairing token creation failed\n").length,
|
||||
);
|
||||
assert.equal(result.failure.stderrBytes, 0);
|
||||
assert.isFalse("stdout" in result.failure);
|
||||
assert.isFalse("stderr" in result.failure);
|
||||
}
|
||||
|
||||
const spawnCause = new Error("ssh executable was not found");
|
||||
const spawnError = new SshCommandSpawnError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount: 0,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: "devbox",
|
||||
cause: spawnCause,
|
||||
});
|
||||
@@ -187,7 +195,7 @@ describe("ssh command", () => {
|
||||
}).pipe(Effect.provide(processLayer));
|
||||
});
|
||||
|
||||
it.effect("redacts credentials from stdout in non-zero command failures", () => {
|
||||
it.effect("does not expose credentials from non-zero command output", () => {
|
||||
const spawner = ChildProcessSpawner.make(() =>
|
||||
Effect.succeed(makeFailedProcess({ stdout: '{"credential":"pairing-secret"}\n' })),
|
||||
);
|
||||
@@ -210,8 +218,12 @@ describe("ssh command", () => {
|
||||
assert.isTrue(Result.isFailure(result));
|
||||
if (Result.isFailure(result)) {
|
||||
assert.instanceOf(result.failure, SshCommandExitError);
|
||||
assert.equal(result.failure.message, '{"credential":"[redacted]"}');
|
||||
assert.equal(result.failure.stdout, '{"credential":"[redacted]"}\n');
|
||||
assert.notInclude(result.failure.message, "pairing-secret");
|
||||
assert.equal(
|
||||
result.failure.stdoutBytes,
|
||||
encoder.encode('{"credential":"pairing-secret"}\n').length,
|
||||
);
|
||||
assert.isFalse("stdout" in result.failure);
|
||||
}
|
||||
}).pipe(Effect.provide(processLayer));
|
||||
});
|
||||
|
||||
+39
-30
@@ -27,7 +27,6 @@ import {
|
||||
|
||||
const PUBLISHABLE_T3_VERSION_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/u;
|
||||
const DEFAULT_SSH_COMMAND_TIMEOUT_MS = 60_000;
|
||||
const MAX_SSH_ERROR_OUTPUT_LENGTH = 4_000;
|
||||
|
||||
/**
|
||||
* ssh is a real executable everywhere (`ssh.exe` on Windows), so it is always
|
||||
@@ -41,6 +40,10 @@ export const resolveSshCommand = Effect.map(HostProcessPlatform, sshCommandForPl
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
export function utf8ByteLength(value: string): number {
|
||||
return encoder.encode(value).byteLength;
|
||||
}
|
||||
|
||||
export interface SshCommandResult {
|
||||
readonly stdout: string;
|
||||
readonly stderr: string;
|
||||
@@ -145,16 +148,6 @@ export const collectProcessOutput = <E>(
|
||||
),
|
||||
);
|
||||
|
||||
function redactSshErrorOutput(output: string): string {
|
||||
const redacted = output.replace(
|
||||
/("(?:access_token|bearerToken|credential|pairingToken|token)"\s*:\s*")[^"]+(")/giu,
|
||||
"$1[redacted]$2",
|
||||
);
|
||||
return redacted.length > MAX_SSH_ERROR_OUTPUT_LENGTH
|
||||
? `${redacted.slice(0, MAX_SSH_ERROR_OUTPUT_LENGTH)}\n[truncated]`
|
||||
: redacted;
|
||||
}
|
||||
|
||||
function sshTargetLogFields(target: DesktopSshEnvironmentTarget) {
|
||||
return {
|
||||
alias: target.alias,
|
||||
@@ -185,9 +178,11 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshAuthenticationHelperError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount: 0,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: hostSpec,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
@@ -204,7 +199,8 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
const sshCommand = yield* resolveSshCommand;
|
||||
yield* Effect.logDebug("ssh.command.start", {
|
||||
...sshTargetLogFields(target),
|
||||
command: [sshCommand, ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
hasStdin: input.stdin !== undefined,
|
||||
timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
|
||||
});
|
||||
@@ -224,9 +220,10 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshCommandSpawnError({
|
||||
command: [sshCommand, ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: hostSpec,
|
||||
cause,
|
||||
}),
|
||||
@@ -244,9 +241,10 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshCommandExecutionError({
|
||||
command: ["ssh", ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: hostSpec,
|
||||
cause,
|
||||
}),
|
||||
@@ -254,26 +252,28 @@ const runSshCommandInScope = Effect.fn("ssh/command.runSshCommand.inScope")(func
|
||||
);
|
||||
|
||||
if (exitCode !== 0) {
|
||||
const diagnosticStdout = redactSshErrorOutput(stdout);
|
||||
yield* Effect.logWarning("ssh.command.failed", {
|
||||
...sshTargetLogFields(target),
|
||||
command: ["ssh", ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode,
|
||||
stdout: diagnosticStdout,
|
||||
stderr,
|
||||
stdoutBytes: utf8ByteLength(stdout),
|
||||
stderrBytes: utf8ByteLength(stderr),
|
||||
});
|
||||
return yield* new SshCommandExitError({
|
||||
command: ["ssh", ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode,
|
||||
stdout: diagnosticStdout,
|
||||
stderr,
|
||||
stdoutBytes: utf8ByteLength(stdout),
|
||||
stderrBytes: utf8ByteLength(stderr),
|
||||
target: hostSpec,
|
||||
});
|
||||
}
|
||||
|
||||
yield* Effect.logDebug("ssh.command.succeeded", {
|
||||
...sshTargetLogFields(target),
|
||||
command: ["ssh", ...args],
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
});
|
||||
return { stdout, stderr };
|
||||
});
|
||||
@@ -298,14 +298,23 @@ export const runSshCommand = Effect.fn("ssh/command.runSshCommand")(function* (
|
||||
yield* Effect.logWarning("ssh.command.timedOut", {
|
||||
...sshTargetLogFields(target),
|
||||
timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
|
||||
remoteCommandArgs: input.remoteCommandArgs ?? [],
|
||||
preHostArgs: input.preHostArgs ?? [],
|
||||
argumentCount:
|
||||
baseSshArgs(target).length +
|
||||
(input.preHostArgs?.length ?? 0) +
|
||||
1 +
|
||||
(input.remoteCommandArgs?.length ?? 0),
|
||||
hasStdin: input.stdin !== undefined,
|
||||
});
|
||||
return yield* new SshCommandTimeoutError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount:
|
||||
baseSshArgs(target).length +
|
||||
(input.preHostArgs?.length ?? 0) +
|
||||
1 +
|
||||
(input.remoteCommandArgs?.length ?? 0),
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: target.alias || target.hostname,
|
||||
timeoutMs: input.timeoutMs ?? DEFAULT_SSH_COMMAND_TIMEOUT_MS,
|
||||
});
|
||||
}),
|
||||
|
||||
+23
-26
@@ -42,10 +42,11 @@ export const SshInvalidTargetError = Schema.Union([
|
||||
export type SshInvalidTargetError = typeof SshInvalidTargetError.Type;
|
||||
|
||||
const SshCommandErrorFields = {
|
||||
command: Schema.Array(Schema.String),
|
||||
command: Schema.String,
|
||||
argumentCount: Schema.Number,
|
||||
exitCode: Schema.NullOr(Schema.Number),
|
||||
stderr: Schema.String,
|
||||
stdout: Schema.optional(Schema.String),
|
||||
stderrBytes: Schema.Number,
|
||||
stdoutBytes: Schema.optional(Schema.Number),
|
||||
target: Schema.optional(Schema.String),
|
||||
timeoutMs: Schema.optional(Schema.Number),
|
||||
};
|
||||
@@ -95,11 +96,7 @@ export class SshCommandExitError extends Schema.TaggedErrorClass<SshCommandExitE
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return (
|
||||
this.stderr.trim() ||
|
||||
this.stdout?.trim() ||
|
||||
`SSH command failed${targetSuffix(this.target)}${exitCodeSuffix(this.exitCode)}.`
|
||||
);
|
||||
return `SSH command failed${targetSuffix(this.target)}${exitCodeSuffix(this.exitCode)}.`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,11 +151,7 @@ export class SshTunnelExitError extends Schema.TaggedErrorClass<SshTunnelExitErr
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return (
|
||||
this.stderr.trim() ||
|
||||
this.stdout?.trim() ||
|
||||
`SSH tunnel exited unexpectedly${targetSuffix(this.target)}${exitCodeSuffix(this.exitCode)}.`
|
||||
);
|
||||
return `SSH tunnel exited unexpectedly${targetSuffix(this.target)}${exitCodeSuffix(this.exitCode)}.`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,7 +171,7 @@ export type SshCommandError = typeof SshCommandError.Type;
|
||||
export class SshLaunchPortMissingError extends Schema.TaggedErrorClass<SshLaunchPortMissingError>()(
|
||||
"SshLaunchPortMissingError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
@@ -189,7 +182,7 @@ export class SshLaunchPortMissingError extends Schema.TaggedErrorClass<SshLaunch
|
||||
export class SshLaunchOutputParseError extends Schema.TaggedErrorClass<SshLaunchOutputParseError>()(
|
||||
"SshLaunchOutputParseError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
@@ -201,7 +194,7 @@ export class SshLaunchOutputParseError extends Schema.TaggedErrorClass<SshLaunch
|
||||
export class SshLaunchInvalidPortError extends Schema.TaggedErrorClass<SshLaunchInvalidPortError>()(
|
||||
"SshLaunchInvalidPortError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
remotePort: Schema.optional(Schema.Number),
|
||||
},
|
||||
) {
|
||||
@@ -220,7 +213,7 @@ export type SshLaunchError = typeof SshLaunchError.Type;
|
||||
export class SshPairingCredentialMissingError extends Schema.TaggedErrorClass<SshPairingCredentialMissingError>()(
|
||||
"SshPairingCredentialMissingError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
@@ -231,7 +224,7 @@ export class SshPairingCredentialMissingError extends Schema.TaggedErrorClass<Ss
|
||||
export class SshPairingOutputParseError extends Schema.TaggedErrorClass<SshPairingOutputParseError>()(
|
||||
"SshPairingOutputParseError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
@@ -243,7 +236,7 @@ export class SshPairingOutputParseError extends Schema.TaggedErrorClass<SshPairi
|
||||
export class SshPairingInvalidCredentialError extends Schema.TaggedErrorClass<SshPairingInvalidCredentialError>()(
|
||||
"SshPairingInvalidCredentialError",
|
||||
{
|
||||
stdout: Schema.String,
|
||||
stdoutBytes: Schema.Number,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
@@ -299,40 +292,44 @@ export type SshHttpBridgeError = typeof SshHttpBridgeError.Type;
|
||||
export class SshReadinessProbeError extends Schema.TaggedErrorClass<SshReadinessProbeError>()(
|
||||
"SshReadinessProbeError",
|
||||
{
|
||||
requestUrl: Schema.String,
|
||||
requestTarget: Schema.String,
|
||||
requestUrlLength: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Backend readiness probe failed at ${this.requestUrl}.`;
|
||||
return `Backend readiness probe failed at ${this.requestTarget}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class SshReadinessProbeTimeoutError extends Schema.TaggedErrorClass<SshReadinessProbeTimeoutError>()(
|
||||
"SshReadinessProbeTimeoutError",
|
||||
{
|
||||
requestUrl: Schema.String,
|
||||
requestTarget: Schema.String,
|
||||
requestUrlLength: Schema.Number,
|
||||
timeoutMs: Schema.Number,
|
||||
attempt: Schema.Number,
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Backend readiness probe exceeded ${this.timeoutMs}ms at ${this.requestUrl}.`;
|
||||
return `Backend readiness probe exceeded ${this.timeoutMs}ms at ${this.requestTarget}.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class SshReadinessTimeoutError extends Schema.TaggedErrorClass<SshReadinessTimeoutError>()(
|
||||
"SshReadinessTimeoutError",
|
||||
{
|
||||
baseUrl: Schema.String,
|
||||
requestUrl: Schema.String,
|
||||
baseTarget: Schema.String,
|
||||
baseUrlLength: Schema.Number,
|
||||
requestTarget: Schema.String,
|
||||
requestUrlLength: Schema.Number,
|
||||
timeoutMs: Schema.Number,
|
||||
attempts: Schema.Number,
|
||||
cause: Schema.optional(Schema.Defect()),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Timed out waiting ${this.timeoutMs}ms for backend readiness at ${this.baseUrl}.`;
|
||||
return `Timed out waiting ${this.timeoutMs}ms for backend readiness at ${this.baseTarget}.`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
SshHttpBridgeInvalidUrlError,
|
||||
SshHttpBridgeMissingUrlError,
|
||||
SshHttpBridgeNonLoopbackUrlError,
|
||||
SshPairingOutputParseError,
|
||||
SshReadinessProbeTimeoutError,
|
||||
SshReadinessTimeoutError,
|
||||
} from "./errors.ts";
|
||||
@@ -259,8 +260,8 @@ describe("ssh tunnel scripts", () => {
|
||||
const fiber = yield* Effect.forkChild(
|
||||
Effect.result(
|
||||
SshTunnel.waitForHttpReady({
|
||||
baseUrl: "http://127.0.0.1:41773/",
|
||||
path: "/ready",
|
||||
baseUrl: "http://127.0.0.1:41773/?token=base-secret#fragment",
|
||||
path: "/ready?credential=request-secret#fragment",
|
||||
timeoutMs: 1_000,
|
||||
intervalMs: 100,
|
||||
probeTimeoutMs: 250,
|
||||
@@ -276,8 +277,13 @@ describe("ssh tunnel scripts", () => {
|
||||
if (Result.isFailure(result)) {
|
||||
assert.instanceOf(result.failure, SshReadinessTimeoutError);
|
||||
const timeoutError = result.failure as SshReadinessTimeoutError;
|
||||
assert.equal(timeoutError.baseUrl, "http://127.0.0.1:41773/");
|
||||
assert.equal(timeoutError.requestUrl, "http://127.0.0.1:41773/ready");
|
||||
assert.equal(timeoutError.baseTarget, "http://127.0.0.1:41773/");
|
||||
assert.equal(timeoutError.requestTarget, "http://127.0.0.1:41773/ready");
|
||||
assert.isAbove(timeoutError.baseUrlLength, timeoutError.baseTarget.length);
|
||||
assert.isAbove(timeoutError.requestUrlLength, timeoutError.requestTarget.length);
|
||||
assert.isFalse("baseUrl" in timeoutError);
|
||||
assert.isFalse("requestUrl" in timeoutError);
|
||||
assert.notInclude(timeoutError.message, "secret");
|
||||
assert.equal(
|
||||
timeoutError.message,
|
||||
"Timed out waiting 1000ms for backend readiness at http://127.0.0.1:41773/.",
|
||||
@@ -325,7 +331,7 @@ describe("ssh tunnel scripts", () => {
|
||||
}),
|
||||
);
|
||||
|
||||
it("preserves primitive readiness reason values in diagnostic output", () => {
|
||||
it("describes readiness causes without copying arbitrary messages", () => {
|
||||
assert.deepEqual(
|
||||
SshTunnel.describeReadinessCause({
|
||||
_tag: "HttpClientError",
|
||||
@@ -335,9 +341,8 @@ describe("ssh tunnel scripts", () => {
|
||||
}),
|
||||
{
|
||||
_tag: "HttpClientError",
|
||||
message: "Backend readiness probe failed.",
|
||||
reason: "authentication failed",
|
||||
cause: "upstream closed",
|
||||
reason: { type: "string" },
|
||||
cause: { type: "string" },
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -346,14 +351,16 @@ describe("ssh tunnel scripts", () => {
|
||||
assert.deepEqual(
|
||||
SshTunnel.describeReadinessCause(
|
||||
new SshReadinessProbeTimeoutError({
|
||||
requestUrl: "http://127.0.0.1:41773/ready",
|
||||
requestTarget: "http://127.0.0.1:41773/ready",
|
||||
requestUrlLength: 28,
|
||||
timeoutMs: 250,
|
||||
attempt: 3,
|
||||
}),
|
||||
),
|
||||
{
|
||||
_tag: "SshReadinessProbeTimeoutError",
|
||||
requestUrl: "http://127.0.0.1:41773/ready",
|
||||
requestTarget: "http://127.0.0.1:41773/ready",
|
||||
requestUrlLength: 28,
|
||||
timeoutMs: 250,
|
||||
attempt: 3,
|
||||
message: "Backend readiness probe exceeded 250ms at http://127.0.0.1:41773/ready.",
|
||||
@@ -416,6 +423,33 @@ describe("ssh tunnel scripts", () => {
|
||||
}).pipe(Effect.provide(processLayer));
|
||||
});
|
||||
|
||||
it.effect("does not copy pairing command output into parse errors", () => {
|
||||
const target = {
|
||||
alias: "devbox",
|
||||
hostname: "devbox.example.com",
|
||||
username: "julius",
|
||||
port: 2222,
|
||||
} as const;
|
||||
const output = '{"credential":"pairing-secret"';
|
||||
const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeSuccessfulProcess(output)));
|
||||
const processLayer = Layer.merge(
|
||||
NodeServices.layer,
|
||||
Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const result = yield* Effect.result(SshTunnel.issueRemotePairingToken(target));
|
||||
assert.isTrue(Result.isFailure(result));
|
||||
if (Result.isFailure(result)) {
|
||||
assert.instanceOf(result.failure, SshPairingOutputParseError);
|
||||
assert.equal(result.failure.stdoutBytes, new TextEncoder().encode(output).length);
|
||||
assert.isFalse("stdout" in result.failure);
|
||||
assert.notInclude(result.failure.message, "pairing-secret");
|
||||
assert.exists(result.failure.cause);
|
||||
}
|
||||
}).pipe(Effect.provide(processLayer));
|
||||
});
|
||||
|
||||
it.effect("retries authentication when a spawn error wraps an SSH auth failure", () => {
|
||||
const target = {
|
||||
alias: "devbox",
|
||||
|
||||
+71
-40
@@ -35,6 +35,7 @@ import {
|
||||
resolveSshTarget,
|
||||
runSshCommand,
|
||||
targetConnectionKey,
|
||||
utf8ByteLength,
|
||||
} from "./command.ts";
|
||||
import {
|
||||
SshAuthenticationHelperError,
|
||||
@@ -256,23 +257,25 @@ export function describeReadinessCause(cause: unknown): unknown {
|
||||
if (cause instanceof Error) {
|
||||
return {
|
||||
name: cause.name,
|
||||
message: cause.message,
|
||||
...(cause.cause === undefined ? {} : { cause: describeReadinessCause(cause.cause) }),
|
||||
};
|
||||
}
|
||||
if (typeof cause !== "object" || cause === null) {
|
||||
return cause;
|
||||
return { type: cause === null ? "null" : typeof cause };
|
||||
}
|
||||
|
||||
const record = cause as Readonly<Record<string, unknown>>;
|
||||
return {
|
||||
...(typeof record._tag === "string" ? { _tag: record._tag } : {}),
|
||||
...(typeof record.message === "string" ? { message: record.message } : {}),
|
||||
...(record.reason === undefined ? {} : { reason: describeReadinessCause(record.reason) }),
|
||||
...(record.cause === undefined ? {} : { cause: describeReadinessCause(record.cause) }),
|
||||
};
|
||||
}
|
||||
|
||||
function readinessUrlTarget(url: URL): string {
|
||||
return `${url.origin}${url.pathname}`;
|
||||
}
|
||||
|
||||
export const REMOTE_PICK_PORT_SCRIPT = `const fs = require("node:fs");
|
||||
const net = require("node:net");
|
||||
const filePath = process.argv[2] ?? "";
|
||||
@@ -748,21 +751,21 @@ export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemo
|
||||
});
|
||||
if (!getLastNonEmptyOutputLine(result.stdout)) {
|
||||
return yield* new SshLaunchPortMissingError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
});
|
||||
}
|
||||
const parsed = yield* decodeRemoteLaunchOutput(result.stdout).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshLaunchOutputParseError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (!Number.isInteger(parsed.remotePort)) {
|
||||
return yield* new SshLaunchInvalidPortError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
remotePort: parsed.remotePort,
|
||||
});
|
||||
}
|
||||
@@ -803,21 +806,21 @@ export const issueRemotePairingToken = Effect.fn("ssh/tunnel.issueRemotePairingT
|
||||
});
|
||||
if (!getLastNonEmptyOutputLine(result.stdout)) {
|
||||
return yield* new SshPairingCredentialMissingError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
});
|
||||
}
|
||||
const parsed = yield* decodeRemotePairingOutput(result.stdout).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshPairingOutputParseError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
if (parsed.credential.trim().length === 0) {
|
||||
return yield* new SshPairingInvalidCredentialError({
|
||||
stdout: result.stdout,
|
||||
stdoutBytes: utf8ByteLength(result.stdout),
|
||||
});
|
||||
}
|
||||
yield* Effect.logDebug("ssh.remoteServer.pairingToken.created", {
|
||||
@@ -886,14 +889,22 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
const retryPolicy = Schedule.spaced(Duration.millis(intervalMs)).pipe(
|
||||
Schedule.take(Math.max(0, Math.ceil(timeoutMs / intervalMs))),
|
||||
);
|
||||
const requestUrl = new URL(input.path ?? "/", input.baseUrl).toString();
|
||||
const baseUrl = new URL(input.baseUrl);
|
||||
const request = new URL(input.path ?? "/", baseUrl);
|
||||
const requestUrl = request.toString();
|
||||
const baseTarget = readinessUrlTarget(baseUrl);
|
||||
const requestTarget = readinessUrlTarget(request);
|
||||
const baseUrlLength = utf8ByteLength(input.baseUrl);
|
||||
const requestUrlLength = utf8ByteLength(requestUrl);
|
||||
const client = yield* HttpClient.HttpClient;
|
||||
const lastProbeFailure = yield* Ref.make<unknown>(null);
|
||||
let attempt = 0;
|
||||
|
||||
yield* Effect.logDebug("ssh.tunnel.httpReady.start", {
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
baseTarget,
|
||||
baseUrlLength,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
timeoutMs,
|
||||
intervalMs,
|
||||
probeTimeoutMs,
|
||||
@@ -909,7 +920,8 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshReadinessProbeError({
|
||||
requestUrl,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
@@ -919,7 +931,8 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
onNone: () =>
|
||||
Effect.fail(
|
||||
new SshReadinessProbeTimeoutError({
|
||||
requestUrl,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
timeoutMs: probeTimeoutMs,
|
||||
attempt,
|
||||
}),
|
||||
@@ -930,7 +943,8 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
isSshReadinessError(cause)
|
||||
? cause
|
||||
: new SshReadinessProbeError({
|
||||
requestUrl,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
@@ -946,7 +960,8 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
isSshReadinessError(cause)
|
||||
? cause
|
||||
: new SshReadinessProbeError({
|
||||
requestUrl,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
@@ -956,16 +971,20 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
return yield* Option.match(result, {
|
||||
onSome: () =>
|
||||
Effect.logDebug("ssh.tunnel.httpReady.succeeded", {
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
baseTarget,
|
||||
baseUrlLength,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
attempts: attempt,
|
||||
}),
|
||||
onNone: () =>
|
||||
Effect.gen(function* () {
|
||||
const lastFailure = yield* Ref.get(lastProbeFailure);
|
||||
yield* Effect.logWarning("ssh.tunnel.httpReady.timedOut", {
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
baseTarget,
|
||||
baseUrlLength,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
timeoutMs,
|
||||
intervalMs,
|
||||
probeTimeoutMs,
|
||||
@@ -973,8 +992,10 @@ export const waitForHttpReady = Effect.fn("ssh/tunnel.waitForHttpReady")(functio
|
||||
lastFailure: describeReadinessCause(lastFailure),
|
||||
});
|
||||
return yield* new SshReadinessTimeoutError({
|
||||
baseUrl: input.baseUrl,
|
||||
requestUrl,
|
||||
baseTarget,
|
||||
baseUrlLength,
|
||||
requestTarget,
|
||||
requestUrlLength,
|
||||
timeoutMs,
|
||||
attempts: attempt,
|
||||
...(lastFailure === null ? {} : { cause: lastFailure }),
|
||||
@@ -1045,9 +1066,11 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshAuthenticationHelperError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount: 0,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: hostSpec,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
@@ -1069,12 +1092,12 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
hostSpec,
|
||||
];
|
||||
const sshCommand = yield* resolveSshCommand;
|
||||
const tunnelCommand = [sshCommand, ...args];
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const scope = yield* Scope.Scope;
|
||||
yield* Effect.logDebug("ssh.tunnel.spawn.start", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
remoteServerKind: input.remoteServerKind,
|
||||
@@ -1095,9 +1118,10 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshTunnelSpawnError({
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: input.resolvedTarget.alias,
|
||||
cause,
|
||||
}),
|
||||
@@ -1105,7 +1129,8 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
);
|
||||
yield* Effect.logDebug("ssh.tunnel.spawn.succeeded", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
pid: child.pid,
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
@@ -1129,29 +1154,32 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SshTunnelMonitorError({
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: input.resolvedTarget.alias,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
Effect.flatMap(([stderr, exitCode]) => {
|
||||
const error = new SshTunnelExitError({
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
exitCode,
|
||||
stderr,
|
||||
stderrBytes: utf8ByteLength(stderr),
|
||||
target: input.resolvedTarget.alias,
|
||||
});
|
||||
return Effect.logWarning("ssh.tunnel.process.exited", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
pid: child.pid,
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
httpBaseUrl: input.httpBaseUrl,
|
||||
exitCode,
|
||||
stderr,
|
||||
stderrBytes: utf8ByteLength(stderr),
|
||||
}).pipe(Effect.andThen(Effect.fail(error)));
|
||||
}),
|
||||
);
|
||||
@@ -1165,7 +1193,8 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
Effect.tap(() =>
|
||||
Effect.logInfo("ssh.tunnel.ready", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
pid: child.pid,
|
||||
localPort: input.localPort,
|
||||
remotePort: input.remotePort,
|
||||
@@ -1191,7 +1220,8 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
: null;
|
||||
yield* Effect.logWarning("ssh.tunnel.ready.failed", {
|
||||
...sshTargetLogFields(input.resolvedTarget),
|
||||
command: tunnelCommand,
|
||||
command: sshCommand,
|
||||
argumentCount: args.length,
|
||||
pid: child.pid,
|
||||
processRunning,
|
||||
...(Exit.isSuccess(processRunningExit)
|
||||
@@ -1204,7 +1234,7 @@ const startSshTunnel = Effect.fn("ssh/tunnel.startSshTunnel")(function* (input:
|
||||
...(Exit.isSuccess(localPortAvailableExit)
|
||||
? {}
|
||||
: { localPortProbeError: localPortAvailableExit.cause }),
|
||||
...(remoteLogTail === null ? {} : { remoteLogTail }),
|
||||
...(remoteLogTail === null ? {} : { remoteLogTailBytes: utf8ByteLength(remoteLogTail) }),
|
||||
...(Exit.isSuccess(remoteLogTailExit)
|
||||
? {}
|
||||
: { remoteLogTailError: remoteLogTailExit.cause }),
|
||||
@@ -1267,9 +1297,10 @@ export const make = Effect.fn("ssh/tunnel.SshEnvironmentManager.make")(function*
|
||||
yield* Deferred.fail(
|
||||
pending,
|
||||
new SshCommandCancelledError({
|
||||
command: ["ssh"],
|
||||
command: "ssh",
|
||||
argumentCount: 0,
|
||||
exitCode: null,
|
||||
stderr: "",
|
||||
stderrBytes: 0,
|
||||
target: target.alias || target.hostname,
|
||||
}),
|
||||
).pipe(Effect.ignore);
|
||||
|
||||
Reference in new issue
Block a user