mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-25 18:16:32 +00:00
417 lines
12 KiB
TypeScript
417 lines
12 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { applyHostAuthMethodSelection, hasBridgeSshCredentials, hasRequiredHostAuthCredential, resolveBridgeKeyAuth, resolveBridgeSshAgentAuth, resolveHostAuth, resolveHostAuthMethodSelection, resolveHostAutofillPassword } from "./sshAuth.ts";
|
|
import { applyGroupDefaults } from "./groupConfig.ts";
|
|
import type { Host, Identity, SSHKey } from "./models.ts";
|
|
|
|
const referenceKey: SSHKey = {
|
|
id: "key-1",
|
|
label: "Reference key",
|
|
type: "ED25519",
|
|
privateKey: "",
|
|
source: "reference",
|
|
category: "key",
|
|
created: 1,
|
|
filePath: "/Users/alice/.ssh/id_ed25519",
|
|
};
|
|
|
|
test("resolveBridgeKeyAuth passes reference keys as identity file paths", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeKeyAuth({
|
|
key: referenceKey,
|
|
fallbackIdentityFilePaths: ["/legacy/key"],
|
|
passphrase: "saved-passphrase",
|
|
}),
|
|
{
|
|
privateKey: undefined,
|
|
identityFilePaths: ["/Users/alice/.ssh/id_ed25519"],
|
|
passphrase: "saved-passphrase",
|
|
},
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeKeyAuth ignores undecryptable passphrase placeholders", () => {
|
|
assert.equal(
|
|
resolveBridgeKeyAuth({
|
|
key: {
|
|
...referenceKey,
|
|
passphrase: "enc:v1:djEwAAAA",
|
|
},
|
|
}).passphrase,
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeKeyAuth ignores undecryptable private key placeholders", () => {
|
|
assert.equal(
|
|
resolveBridgeKeyAuth({
|
|
key: {
|
|
...referenceKey,
|
|
source: "imported",
|
|
filePath: undefined,
|
|
privateKey: "enc:v1:djEwAAAA",
|
|
},
|
|
}).privateKey,
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeKeyAuth preserves imported key material", () => {
|
|
const importedKey: SSHKey = {
|
|
...referenceKey,
|
|
source: "imported",
|
|
privateKey: "PRIVATE KEY",
|
|
filePath: undefined,
|
|
};
|
|
|
|
assert.deepEqual(
|
|
resolveBridgeKeyAuth({
|
|
key: importedKey,
|
|
fallbackIdentityFilePaths: ["/legacy/key"],
|
|
}),
|
|
{
|
|
privateKey: "PRIVATE KEY",
|
|
identityFilePaths: ["/legacy/key"],
|
|
passphrase: undefined,
|
|
},
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth carries system agent settings without private material", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({
|
|
...autofillBaseHost,
|
|
useSshAgent: true,
|
|
identityAgent: "$SSH_AUTH_SOCK",
|
|
identitiesOnly: true,
|
|
addKeysToAgent: "yes",
|
|
useKeychain: true,
|
|
}),
|
|
{
|
|
useSshAgent: true,
|
|
identityAgent: "$SSH_AUTH_SOCK",
|
|
identitiesOnly: true,
|
|
addKeysToAgent: "yes",
|
|
useKeychain: true,
|
|
},
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth keeps certificate authentication independent", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({
|
|
...autofillBaseHost,
|
|
useSshAgent: true,
|
|
}, { certificate: "ssh-ed25519-cert-v01@openssh.com AAAATEST" }),
|
|
{ useSshAgent: false },
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth forwards a selected vault public key", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({
|
|
...autofillBaseHost,
|
|
useSshAgent: true,
|
|
identitiesOnly: true,
|
|
}, { publicKey: "ssh-ed25519 AAAATEST" }),
|
|
{
|
|
useSshAgent: true,
|
|
identityAgent: undefined,
|
|
identitiesOnly: true,
|
|
addKeysToAgent: undefined,
|
|
useKeychain: undefined,
|
|
agentPublicKeys: ["ssh-ed25519 AAAATEST"],
|
|
},
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth preserves an explicit agent opt-out", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({
|
|
...autofillBaseHost,
|
|
useSshAgent: false,
|
|
identityAgent: "none",
|
|
}),
|
|
{ useSshAgent: false },
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth treats an unset agent toggle as disabled", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth(autofillBaseHost),
|
|
{ useSshAgent: false },
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth leaves the ambient agent available in automatic mode", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth(autofillBaseHost, undefined, "auto"),
|
|
{},
|
|
);
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({ ...autofillBaseHost, useSshAgent: false }, undefined, "auto"),
|
|
{ useSshAgent: false },
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth lets explicit auth override a stale agent toggle", () => {
|
|
const staleAgentHost = { ...autofillBaseHost, useSshAgent: true };
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth(staleAgentHost, undefined, "password"),
|
|
{ useSshAgent: false },
|
|
);
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth(staleAgentHost, undefined, "certificate"),
|
|
{ useSshAgent: false },
|
|
);
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth({ ...staleAgentHost, authMethod: "key" }, undefined, "key"),
|
|
{ useSshAgent: false },
|
|
);
|
|
});
|
|
|
|
test("resolveBridgeSshAgentAuth restricts explicit agent-backed key auth to the selected key", () => {
|
|
assert.deepEqual(
|
|
resolveBridgeSshAgentAuth(
|
|
{ ...autofillBaseHost, useSshAgent: true, identitiesOnly: false },
|
|
{ publicKey: "ssh-ed25519 AAAASELECTED" },
|
|
"key",
|
|
),
|
|
{
|
|
useSshAgent: true,
|
|
identityAgent: undefined,
|
|
identitiesOnly: true,
|
|
addKeysToAgent: undefined,
|
|
useKeychain: undefined,
|
|
agentPublicKeys: ["ssh-ed25519 AAAASELECTED"],
|
|
},
|
|
);
|
|
});
|
|
|
|
test("hasRequiredHostAuthCredential rejects empty explicit key and certificate selections", () => {
|
|
assert.equal(hasRequiredHostAuthCredential({
|
|
host: { ...autofillBaseHost, authMethod: "key" },
|
|
keys: [],
|
|
}), false);
|
|
assert.equal(hasRequiredHostAuthCredential({
|
|
host: { ...autofillBaseHost, authMethod: "certificate" },
|
|
keys: [],
|
|
}), false);
|
|
assert.equal(hasRequiredHostAuthCredential({
|
|
host: { ...autofillBaseHost, authMethod: "key", identityFilePaths: ["~/.ssh/id_work"] },
|
|
keys: [],
|
|
}), true);
|
|
assert.equal(hasRequiredHostAuthCredential({
|
|
host: { ...autofillBaseHost, protocol: "telnet", authMethod: "key" },
|
|
keys: [],
|
|
}), true);
|
|
});
|
|
|
|
test("hasBridgeSshCredentials accepts an agent-only host", () => {
|
|
assert.equal(hasBridgeSshCredentials({ useSshAgent: true }), true);
|
|
assert.equal(hasBridgeSshCredentials({}), false);
|
|
});
|
|
|
|
test("hasBridgeSshCredentials accepts automatic local authentication", () => {
|
|
assert.equal(hasBridgeSshCredentials({ authMethod: "auto" }), true);
|
|
});
|
|
|
|
test("resolveHostAuth respects password auth over stale key selections", () => {
|
|
const host: Host = {
|
|
id: "host-1",
|
|
label: "Host",
|
|
hostname: "example.com",
|
|
username: "root",
|
|
authMethod: "password",
|
|
identityFileId: "key-1",
|
|
};
|
|
|
|
const resolved = resolveHostAuth({
|
|
host,
|
|
keys: [referenceKey],
|
|
identities: [],
|
|
});
|
|
|
|
assert.equal(resolved.authMethod, "password");
|
|
assert.equal(resolved.key, undefined);
|
|
assert.equal(resolved.keyId, undefined);
|
|
});
|
|
|
|
test("resolveHostAuth infers key auth from imported IdentityFile paths", () => {
|
|
const resolved = resolveHostAuth({
|
|
host: {
|
|
...autofillBaseHost,
|
|
identityFilePaths: ["~/.ssh/id_work"],
|
|
},
|
|
keys: [],
|
|
});
|
|
|
|
assert.equal(resolved.authMethod, "key");
|
|
});
|
|
|
|
test("resolveHostAuth treats a legacy host without credentials as automatic", () => {
|
|
const resolved = resolveHostAuth({
|
|
host: autofillBaseHost,
|
|
keys: [],
|
|
});
|
|
|
|
assert.equal(resolved.authMethod, "auto");
|
|
});
|
|
|
|
test("resolveHostAuth keeps a legacy saved password in password-only mode", () => {
|
|
const resolved = resolveHostAuth({
|
|
host: { ...autofillBaseHost, password: "saved-secret" },
|
|
keys: [],
|
|
});
|
|
|
|
assert.equal(resolved.authMethod, "password");
|
|
});
|
|
|
|
test("resolveHostAuthMethodSelection gives legacy hosts a visible mode", () => {
|
|
assert.equal(resolveHostAuthMethodSelection(autofillBaseHost), "auto");
|
|
assert.equal(resolveHostAuthMethodSelection({ ...autofillBaseHost, password: "secret" }), "password");
|
|
assert.equal(resolveHostAuthMethodSelection({ ...autofillBaseHost, identityFilePaths: ["~/.ssh/id_work"] }), "key");
|
|
assert.equal(resolveHostAuthMethodSelection({ ...autofillBaseHost, identityFilePaths: ["~/.ssh/id_work"], useSshAgent: true }), "auto");
|
|
});
|
|
|
|
test("resolveHostAuth preserves legacy agent plus identity-file hosts as automatic", () => {
|
|
const resolved = resolveHostAuth({
|
|
host: {
|
|
...autofillBaseHost,
|
|
identityFilePaths: ["~/.ssh/id_work"],
|
|
useSshAgent: true,
|
|
},
|
|
keys: [],
|
|
});
|
|
assert.equal(resolved.authMethod, "auto");
|
|
});
|
|
|
|
test("applyHostAuthMethodSelection clears incompatible per-host credentials", () => {
|
|
const keyedHost = {
|
|
...autofillBaseHost,
|
|
authMethod: "key",
|
|
identityId: "identity-1",
|
|
identityFileId: "key-1",
|
|
identityFilePaths: ["~/.ssh/id_work"],
|
|
useSshAgent: true,
|
|
} as Host;
|
|
|
|
assert.deepEqual(applyHostAuthMethodSelection(keyedHost, "certificate"), {
|
|
...keyedHost,
|
|
authMethod: "certificate",
|
|
identityId: "",
|
|
identityFileId: undefined,
|
|
identityFilePaths: undefined,
|
|
useSshAgent: false,
|
|
});
|
|
assert.deepEqual(applyHostAuthMethodSelection(keyedHost, "auto"), {
|
|
...keyedHost,
|
|
authMethod: "auto",
|
|
identityId: "",
|
|
identityFileId: undefined,
|
|
identityFilePaths: undefined,
|
|
useSshAgent: undefined,
|
|
});
|
|
});
|
|
|
|
test("per-host auth selection opts out of an inherited group identity", () => {
|
|
const selected = applyHostAuthMethodSelection({
|
|
...autofillBaseHost,
|
|
group: "Production",
|
|
identityId: "group-identity",
|
|
} as Host, "password");
|
|
const effective = applyGroupDefaults(selected, { identityId: "group-identity" });
|
|
const resolved = resolveHostAuth({
|
|
host: effective,
|
|
keys: [referenceKey],
|
|
identities: [{
|
|
id: "group-identity",
|
|
label: "Group key",
|
|
username: "deploy",
|
|
authMethod: "key",
|
|
keyId: referenceKey.id,
|
|
created: 1,
|
|
}],
|
|
});
|
|
|
|
assert.equal(effective.identityId, "");
|
|
assert.equal(resolved.authMethod, "password");
|
|
assert.equal(resolved.key, undefined);
|
|
});
|
|
|
|
test("switching to automatic keeps visible custom agent settings active", () => {
|
|
const selected = applyHostAuthMethodSelection({
|
|
...autofillBaseHost,
|
|
authMethod: "password",
|
|
useSshAgent: false,
|
|
identityAgent: "/tmp/custom-agent.sock",
|
|
identitiesOnly: true,
|
|
} as Host, "auto");
|
|
|
|
assert.equal(selected.useSshAgent, true);
|
|
assert.deepEqual(resolveBridgeSshAgentAuth(selected, undefined, "auto"), {
|
|
useSshAgent: true,
|
|
identityAgent: "/tmp/custom-agent.sock",
|
|
identitiesOnly: true,
|
|
addKeysToAgent: undefined,
|
|
useKeychain: undefined,
|
|
});
|
|
});
|
|
|
|
const autofillBaseHost = {
|
|
id: "h1",
|
|
label: "Host",
|
|
hostname: "h.example.test",
|
|
username: "alice",
|
|
} as Host;
|
|
|
|
test("resolveHostAutofillPassword uses the host's own saved password", () => {
|
|
assert.equal(
|
|
resolveHostAutofillPassword({ host: { ...autofillBaseHost, password: "direct-secret" }, keys: [] }),
|
|
"direct-secret",
|
|
);
|
|
});
|
|
|
|
test("resolveHostAutofillPassword resolves a referenced keychain identity's password", () => {
|
|
// host stores no password of its own; the credential lives in a Keychain
|
|
// identity it references (host.identityId) — the #1284 scenario.
|
|
const identity = {
|
|
id: "id-1",
|
|
label: "alice@prod",
|
|
username: "alice",
|
|
authMethod: "password",
|
|
password: "identity-secret",
|
|
created: 1,
|
|
} as Identity;
|
|
assert.equal(
|
|
resolveHostAutofillPassword({
|
|
host: { ...autofillBaseHost, password: undefined, identityId: "id-1" },
|
|
keys: [],
|
|
identities: [identity],
|
|
}),
|
|
"identity-secret",
|
|
);
|
|
});
|
|
|
|
test("resolveHostAutofillPassword returns undefined when the host opts out of saving", () => {
|
|
assert.equal(
|
|
resolveHostAutofillPassword({ host: { ...autofillBaseHost, password: "x", savePassword: false }, keys: [] }),
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("resolveHostAutofillPassword returns undefined when no password is available", () => {
|
|
assert.equal(
|
|
resolveHostAutofillPassword({ host: { ...autofillBaseHost, password: undefined }, keys: [] }),
|
|
undefined,
|
|
);
|
|
});
|
|
|
|
test("resolveHostAutofillPassword ignores undecryptable password placeholders", () => {
|
|
assert.equal(
|
|
resolveHostAutofillPassword({ host: { ...autofillBaseHost, password: "enc:v1:djEwAAAA" }, keys: [] }),
|
|
undefined,
|
|
);
|
|
});
|