logicsrc/packages/cli/src/vault-clean.test.ts
sh1pt-actions-fleet[bot] ae4615e61d
feat(teams): clean archives stale keys to <project>--<env>-archive (CLI 0.11.0) (#262)
logicsrc teams clean <team> <project> <env> [--key K] [--dead] [--approve]
moves keys out of a live vault into its archive vault. Nothing is deleted:
a key leaves the live vault only after its archived copy reads back with
the same fingerprint. --dead finds URL values whose host no longer resolves
and Supabase JWTs whose project ref is such a host. A different value
already archived under a name is kept; the new copy gets a __<UTC stamp>
suffix. Each archived key has an __ARCHIVE__<NAME> entry (from, why, when,
who). Dry run unless --approve.

Co-authored-by: Anthony Ettinger <anthony@profullstack.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 18:32:11 +00:00

69 lines
2.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { archiveSlot, archiveVaultName, findDead, jwtHosts, urlHosts } from "./vault-clean.js";
const jwt = (payload: object) =>
`eyJhbGciOiJIUzI1NiJ9.${Buffer.from(JSON.stringify(payload)).toString("base64url")}.c2ln`;
describe("urlHosts", () => {
it("finds hosts in URLs, connection strings and multi-URL values", () => {
expect(urlHosts("https://abc.supabase.co")).toEqual(["abc.supabase.co"]);
expect(urlHosts("postgres://u:p@db.abc.supabase.co:5432/postgres?sslmode=require")).toEqual(["db.abc.supabase.co"]);
expect(urlHosts("a=https://One.example.com/x b=redis://two.example.org")).toEqual(["one.example.com", "two.example.org"]);
});
it("ignores values without a URL", () => {
expect(urlHosts("sk_live_abc123")).toEqual([]);
expect(urlHosts("http://localhost:3000")).toEqual([]);
});
});
describe("jwtHosts", () => {
it("maps a Supabase key's ref to its project host", () => {
expect(jwtHosts(jwt({ ref: "odhaoehucfyrqhanthyq", role: "anon" }))).toEqual(["odhaoehucfyrqhanthyq.supabase.co"]);
});
it("reads the issuer host", () => {
expect(jwtHosts(jwt({ iss: "https://auth.example.com/v1" }))).toEqual(["auth.example.com"]);
});
it("returns nothing for non-JWTs and self-hosted keys without a ref", () => {
expect(jwtHosts("not.a.jwt")).toEqual([]);
expect(jwtHosts(jwt({ iss: "supabase", role: "service_role" }))).toEqual([]);
});
});
describe("findDead", () => {
it("flags URL and token keys bound to a host that does not resolve, checking each host once", async () => {
const asked: string[] = [];
const check = async (h: string) => {
asked.push(h);
return h !== "goneabcdefghijklmnop.supabase.co";
};
const dead = await findDead(
new Map([
["SUPABASE_URL", "https://goneabcdefghijklmnop.supabase.co"],
["ANON_KEY", jwt({ ref: "goneabcdefghijklmnop" })],
["LIVE_URL", "https://supabase.example.com"],
["PLAIN", "hunter2"]
]),
check
);
expect([...dead.keys()].sort()).toEqual(["ANON_KEY", "SUPABASE_URL"]);
expect(asked.filter((h) => h === "goneabcdefghijklmnop.supabase.co")).toHaveLength(1);
});
});
describe("archiveSlot", () => {
const now = new Date("2026-10-28T16:40:05.123Z");
it("keeps the key's name when the archive has none", () => {
expect(archiveSlot("K", "fp1", new Map(), now)).toBe("K");
});
it("skips a value the archive already holds", () => {
expect(archiveSlot("K", "fp1", new Map([["K", "fp1"]]), now)).toBeNull();
});
it("never overwrites a different archived value", () => {
expect(archiveSlot("K", "fp2", new Map([["K", "fp1"]]), now)).toBe("K__20261028T164005");
expect(archiveSlot("K", "fp3", new Map([["K", "fp1"], ["K__20261028T164005", "fp2"]]), now)).toBe("K__20261028T164005_2");
});
});
it("names the archive vault after the live one", () => {
expect(archiveVaultName("threatcrush", "prod")).toBe("threatcrush--prod-archive");
});