Files
Netcatty/infrastructure/services/vaultImportBatch.test.ts
T
ea6fe18539 fix(vault): allow CSV import of same-endpoint hosts in different groups (#2739)
* fix(#2737): automated Cursor CLI fix

* fix: address Codex review on PR #2739

* fix(vault): harden destination re-dedupe passphrase and counts

Keep collapsed destination imports aligned with CSV merge policy for key
passphrases, and correct plugin importer added/duplicate counters when
hosts collapse into one group.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): preserve SecureCRT and plugin hosts under destination group

Skip endpoint collapse for SecureCRT imports, and keep plugin hosts that
differ by configuration when an import destination rewrites their group.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): skip plugin imports that collide after destination rewrite

After rewriting imported hosts into the destination group, drop ones that
now share a fingerprint with existing vault hosts.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): keep group case in host merge keys

Vault group paths are compared exactly, so Prod and prod must remain
distinct session identities during import/create dedupe.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): prune orphan credentials after destination collisions

When a destination rewrite drops colliding imported hosts, also remove
identities/keys that were only referenced by those skipped hosts.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): only prune newly imported credentials on collision

Pass existing identity/key counts into destination handling so remapped
duplicates of vault credentials are never deleted when a host is skipped.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): merge credential refs when collapsing destination hosts

Carry identityId/telnetIdentityId/identityFileId onto the retained host
so destination-group collapse does not drop referenced auth.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(vault): only graft credential refs onto bare collapsed hosts

Avoid copying identityId onto a retained host that already has key or
password auth, which would silently change resolveHostAuth preference.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: netcatty-bot <308658023+netcatty-bot@users.noreply.github.com>
Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 18:55:02 +08:00

162 lines
4.8 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { importVaultHostFiles } from "./vaultImportBatch.ts";
const secureCrtFile = (
relativePath: string,
hostname: string | null,
portHex = "00000016",
protocol = "SSH2",
) => {
const file = new File([[
hostname ? `S:"Hostname"=${hostname}` : "S:\"Username\"=nobody",
'S:"Username"=operator',
`S:"Protocol Name"=${protocol}`,
`D:"[SSH2] Port"=${portHex}`,
].join("\n")], relativePath.split("/").at(-1) ?? "session.ini");
Object.defineProperty(file, "webkitRelativePath", { value: relativePath });
return file;
};
test("SecureCRT directory import reads every session and preserves folder groups", async () => {
const result = await importVaultHostFiles({
format: "securecrt",
files: [
secureCrtFile("Sessions/Production/Web.ini", "web.example.com", "000008ae"),
secureCrtFile("Sessions/Staging/DB.ini", "db.example.com"),
secureCrtFile("Sessions/Archive/Web Copy.ini", "web.example.com", "000008ae"),
secureCrtFile("Sessions/Default.ini", "should-not-import.example.com"),
secureCrtFile("Sessions/Production/__FolderData__.ini", "should-not-import.example.com"),
secureCrtFile("Sessions/Broken.ini", null),
],
});
assert.deepEqual(result.stats, {
parsed: 3,
imported: 3,
skipped: 1,
duplicates: 0,
});
assert.deepEqual(
result.hosts.map(({ label, hostname, port, group }) => ({ label, hostname, port, group })),
[
{
label: "Web",
hostname: "web.example.com",
port: 2222,
group: "Production",
},
{
label: "DB",
hostname: "db.example.com",
port: 22,
group: "Staging",
},
{
label: "Web Copy",
hostname: "web.example.com",
port: 2222,
group: "Archive",
},
],
);
assert.deepEqual(result.groups, ["Production", "Staging", "Archive"]);
assert.match(result.issues[0]?.message ?? "", /Broken\.ini/);
});
test("SecureCRT keeps separate session files that point to the same endpoint", async () => {
const session = [
'S:"Hostname"=shared.example.com',
'S:"Username"=root',
'S:"Protocol Name"=SSH2',
].join("\n");
const result = await importVaultHostFiles({
format: "securecrt",
files: [
new File([session], "web.ini"),
new File([session], "web.ini"),
],
relativePaths: [
"Sessions/Prod/web.ini",
"Sessions/Staging/web.ini",
],
});
assert.equal(result.hosts.length, 2);
assert.deepEqual(result.hosts.map((host) => host.group), ["Prod", "Staging"]);
});
test("SecureCRT destination group keeps same-endpoint session files", async () => {
const { applyVaultImportDestination } = await import("../../domain/vaultImport");
const session = [
'S:"Hostname"=shared.example.com',
'S:"Username"=root',
'S:"Protocol Name"=SSH2',
].join("\n");
const imported = await importVaultHostFiles({
format: "securecrt",
files: [
new File([session], "web.ini"),
new File([session], "web.ini"),
],
relativePaths: [
"Sessions/Prod/web.ini",
"Sessions/Staging/web.ini",
],
});
const targeted = applyVaultImportDestination(
imported,
{ mode: "group", group: "Imported/SecureCRT" },
{ collapseDuplicateEndpoints: false },
);
assert.equal(targeted.hosts.length, 2);
assert.deepEqual(targeted.hosts.map((host) => host.group), [
"Imported/SecureCRT",
"Imported/SecureCRT",
]);
});
test("SecureCRT batch import does not count an unsupported session twice", async () => {
const result = await importVaultHostFiles({
format: "securecrt",
files: [secureCrtFile("Sessions/Local.ini", "localhost", "00000016", "Local")],
});
assert.equal(result.hosts.length, 0);
assert.equal(result.stats.parsed, 1);
assert.equal(result.stats.skipped, 1);
assert.equal(result.issues.length, 1);
});
test("SecureCRT folder paths transferred alongside files survive the worker boundary", async () => {
const file = new File([
[
'S:"Hostname"=transferred.example.com',
'S:"Username"=operator',
'S:"Protocol Name"=SSH2',
].join("\n"),
], "Transferred.ini");
const result = await importVaultHostFiles({
format: "securecrt",
files: [file],
relativePaths: ["Sessions/Production/Transferred.ini"],
});
assert.equal(result.hosts[0]?.group, "Production");
assert.deepEqual(result.groups, ["Production"]);
});
test("SecureCRT keeps a real nested Sessions folder when that folder was selected", async () => {
const result = await importVaultHostFiles({
format: "securecrt",
files: [secureCrtFile("Sessions/Sessions/Nested.ini", "nested.example.com")],
});
assert.equal(result.hosts[0]?.group, "Sessions");
assert.deepEqual(result.groups, ["Sessions"]);
});