Files
Netcatty/infrastructure/services/portForwardingService.test.ts
T

573 lines
16 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import type { Host, PortForwardingRule, SSHKey } from "../../domain/models.ts";
import { startPortForward } from "./portForwardingService.ts";
const host = (overrides: Partial<Host> = {}): Host => ({
id: "host-1",
label: "Host",
hostname: "example.com",
username: "root",
tags: [],
os: "linux",
...overrides,
});
const rule = (overrides: Partial<PortForwardingRule> = {}): PortForwardingRule => ({
id: "rule-1",
name: "Rule",
type: "local",
localPort: 18080,
remoteHost: "127.0.0.1",
remotePort: 8080,
enabled: true,
status: "inactive",
...overrides,
});
const installBridgeStub = () => {
let started = false;
let capturedOptions: Record<string, unknown> | null = null;
Object.defineProperty(globalThis, "window", {
configurable: true,
value: {
netcatty: {
startPortForward: async (options: Record<string, unknown>) => {
started = true;
capturedOptions = options;
return { success: true };
},
onPortForwardStatus: () => undefined,
},
},
});
return {
wasStarted: () => started,
getOptions: () => capturedOptions,
};
};
test("startPortForward forwards system agent settings", async () => {
const bridge = installBridgeStub();
const result = await startPortForward(
rule({ id: "rule-agent" }),
host({
useSshAgent: true,
identityAgent: "$SSH_AUTH_SOCK",
identityFilePaths: ["~/.ssh/aws_root"],
identitiesOnly: true,
addKeysToAgent: "yes",
useKeychain: true,
}),
[],
[],
[],
() => undefined,
);
assert.equal(result.success, true);
assert.deepEqual(
bridge.getOptions() && {
useSshAgent: bridge.getOptions()?.useSshAgent,
identityAgent: bridge.getOptions()?.identityAgent,
identityFilePaths: bridge.getOptions()?.identityFilePaths,
identitiesOnly: bridge.getOptions()?.identitiesOnly,
addKeysToAgent: bridge.getOptions()?.addKeysToAgent,
useKeychain: bridge.getOptions()?.useKeychain,
},
{
useSshAgent: true,
identityAgent: "$SSH_AUTH_SOCK",
identityFilePaths: ["~/.ssh/aws_root"],
identitiesOnly: true,
addKeysToAgent: "yes",
useKeychain: true,
},
);
});
test("startPortForward drops stale identity paths for password-only auth", async () => {
const bridge = installBridgeStub();
const jumpHost = host({
id: "jump-1",
authMethod: "password",
password: "jump-secret",
useSshAgent: true,
identityFilePaths: ["~/.ssh/stale-jump-key"],
});
const result = await startPortForward(
rule({ id: "rule-password-only" }),
host({
authMethod: "password",
password: "secret",
useSshAgent: true,
identityFilePaths: ["~/.ssh/stale-key"],
hostChain: { hostIds: ["jump-1"] },
}),
[jumpHost],
[],
[],
() => undefined,
);
assert.equal(result.success, true);
assert.equal(bridge.getOptions()?.identityFilePaths, undefined);
assert.equal(bridge.getOptions()?.useSshAgent, false);
const jumpHosts = bridge.getOptions()?.jumpHosts as Array<Record<string, unknown>>;
assert.equal(jumpHosts[0]?.identityFilePaths, undefined);
assert.equal(jumpHosts[0]?.useSshAgent, false);
});
test("startPortForward uses the system agent when a synced key cannot be decrypted", async () => {
const bridge = installBridgeStub();
const key: SSHKey = {
id: "key-1",
label: "Synced key",
type: "ED25519",
publicKey: "ssh-ed25519 AAAASELECTED",
privateKey: "enc:v1:djEwAAAA",
source: "imported",
category: "key",
created: 1,
};
const result = await startPortForward(
rule({ id: "rule-agent-synced-key" }),
host({
authMethod: "key",
identityFileId: "key-1",
useSshAgent: true,
}),
[],
[key],
[],
() => undefined,
);
assert.equal(result.success, true);
assert.equal(bridge.getOptions()?.useSshAgent, true);
assert.deepEqual(bridge.getOptions()?.agentPublicKeys, ["ssh-ed25519 AAAASELECTED"]);
assert.equal(bridge.getOptions()?.privateKey, undefined);
});
test("startPortForward keeps automatic target discovery available with an unreadable saved password", async () => {
const bridge = installBridgeStub();
const result = await startPortForward(
rule({ id: "rule-auto-unreadable" }),
host({ authMethod: "auto", password: "enc:v1:djEwAAAA" }),
[],
[],
[],
() => undefined,
);
assert.equal(result.success, true);
assert.equal(bridge.getOptions()?.authMethod, "auto");
assert.equal(bridge.getOptions()?.password, undefined);
});
test("startPortForward keeps automatic jump discovery available with an unreadable saved password", async () => {
const bridge = installBridgeStub();
const jumpHost = host({
id: "jump-1",
label: "Jump",
authMethod: "auto",
password: "enc:v1:djEwAAAA",
});
const result = await startPortForward(
rule({ id: "rule-auto-jump-unreadable" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[],
() => undefined,
);
assert.equal(result.success, true);
const jumpOptions = bridge.getOptions()?.jumpHosts as Array<Record<string, unknown>> | undefined;
assert.equal(jumpOptions?.[0]?.authMethod, "auto");
assert.equal(jumpOptions?.[0]?.password, undefined);
});
test("startPortForward forwards target and jump-host timeouts", async () => {
const bridge = installBridgeStub();
const jumpHost = host({
id: "jump-1",
sshTcpConnectTimeoutSeconds: 75,
sshAuthReadyTimeoutSeconds: 360,
});
const result = await startPortForward(
rule({ id: "rule-timeouts" }),
host({
hostChain: { hostIds: ["jump-1"] },
sshTcpConnectTimeoutSeconds: 45,
sshAuthReadyTimeoutSeconds: 300,
}),
[jumpHost],
[],
[],
() => {},
);
assert.equal(result.success, true);
assert.equal(bridge.getOptions()?.sshTcpConnectTimeoutMs, 45_000);
assert.equal(bridge.getOptions()?.sshAuthReadyTimeoutMs, 300_000);
const jumpHosts = bridge.getOptions()?.jumpHosts as Array<Record<string, unknown>>;
assert.equal(jumpHosts[0]?.sshTcpConnectTimeoutMs, 75_000);
assert.equal(jumpHosts[0]?.sshAuthReadyTimeoutMs, 360_000);
});
test("startPortForward rejects missing proxy identities before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule(),
host({
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "missing-identity",
},
}),
[],
[],
[],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy identity for "Host" is missing/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy identity/);
});
test("startPortForward rejects missing saved proxy profiles before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule({ id: "rule-missing-profile" }),
host({ proxyProfileId: "missing-proxy" }),
[],
[],
[],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Saved proxy for host "Host" is missing/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Saved proxy/);
});
test("startPortForward rejects incomplete proxy identities before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule({ id: "rule-incomplete" }),
host({
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
}),
[],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "proxy-user",
authMethod: "password",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy identity for "Host" is incomplete/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy identity/);
});
test("startPortForward rejects proxy identities with blank usernames even when passwords are encrypted", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule({ id: "rule-blank-username" }),
host({
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
}),
[],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "",
authMethod: "password",
password: "enc:v1:djEwAAAA",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy identity for "Host" is incomplete/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy identity/);
});
test("startPortForward resolves target proxy credentials from an identity", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule({ id: "rule-resolved-target" }),
host({
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
}),
[],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "proxy-user",
authMethod: "password",
password: "proxy-secret",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, true);
assert.equal(bridge.wasStarted(), true);
assert.deepEqual(bridge.getOptions()?.proxy, {
type: "http",
host: "proxy.example.com",
port: 3128,
username: "proxy-user",
password: "proxy-secret",
});
assert.deepEqual(statuses, ["connecting"]);
});
test("startPortForward rejects target proxy identity passwords that cannot be decrypted", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const result = await startPortForward(
rule({ id: "rule-unreadable-target" }),
host({
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
}),
[],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "proxy-user",
authMethod: "password",
password: "enc:v1:djEwAAAA",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy credentials cannot be decrypted/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy credentials/);
});
test("startPortForward rejects missing jump host proxy identities before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const jumpHost = host({
id: "jump-1",
label: "Jump",
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "missing-identity",
},
});
const result = await startPortForward(
rule({ id: "rule-2" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy identity for "Jump" is missing/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy identity/);
});
test("startPortForward rejects missing saved proxy profiles on jump hosts before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const jumpHost = host({
id: "jump-1",
label: "Jump",
proxyProfileId: "missing-proxy",
});
const result = await startPortForward(
rule({ id: "rule-missing-jump-profile" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Saved proxy for jump host "Jump" is missing/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Saved proxy/);
});
test("startPortForward rejects incomplete jump host proxy identities before starting", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const jumpHost = host({
id: "jump-1",
label: "Jump",
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
});
const result = await startPortForward(
rule({ id: "rule-jump-incomplete" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "",
authMethod: "password",
password: "enc:v1:djEwAAAA",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy identity for "Jump" is incomplete/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy identity/);
});
test("startPortForward resolves jump host proxy credentials from an identity", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const jumpHost = host({
id: "jump-1",
label: "Jump",
proxyConfig: {
type: "socks5",
host: "jump-proxy.example.com",
port: 1080,
identityId: "identity-1",
},
});
const result = await startPortForward(
rule({ id: "rule-resolved-jump" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "proxy-user",
authMethod: "password",
password: "proxy-secret",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, true);
assert.equal(bridge.wasStarted(), true);
const jumpHosts = bridge.getOptions()?.jumpHosts as Array<Record<string, unknown>>;
assert.deepEqual(jumpHosts[0]?.proxy, {
type: "socks5",
host: "jump-proxy.example.com",
port: 1080,
username: "proxy-user",
password: "proxy-secret",
});
assert.deepEqual(statuses, ["connecting"]);
});
test("startPortForward rejects jump host proxy identity passwords that cannot be decrypted", async () => {
const bridge = installBridgeStub();
const statuses: string[] = [];
const jumpHost = host({
id: "jump-1",
label: "Jump",
proxyConfig: {
type: "http",
host: "proxy.example.com",
port: 3128,
identityId: "identity-1",
},
});
const result = await startPortForward(
rule({ id: "rule-unreadable-jump" }),
host({ hostChain: { hostIds: ["jump-1"] } }),
[jumpHost],
[],
[{
id: "identity-1",
label: "Proxy login",
username: "proxy-user",
authMethod: "password",
password: "enc:v1:djEwAAAA",
created: 1,
}],
(status, error) => statuses.push(error ? `${status}:${error}` : status),
);
assert.equal(result.success, false);
assert.match(result.error || "", /Proxy credentials for jump host "Jump" cannot be decrypted/);
assert.equal(bridge.wasStarted(), false);
assert.match(statuses.at(-1) || "", /error:Proxy credentials/);
});