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>
This commit is contained in:
sh1pt-actions-fleet[bot] 2026-10-28 18:32:11 +00:00 • committed by GitHub
parent ae6298851b
commit ae4615e61d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 308 additions and 1 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "@logicsrc/cli", "name": "@logicsrc/cli",
"version": "0.10.0", "version": "0.11.0",
"description": "LogicSRC CLI: every LogicSRC standard and tool as one command.", "description": "LogicSRC CLI: every LogicSRC standard and tool as one command.",
"type": "module", "type": "module",
"main": "./dist/index.js", "main": "./dist/index.js",

View file

@ -38,6 +38,7 @@ import { sshAgentAction, sshListAction, sshPullAction, sshPushAction } from "./s
import { credentialsRotateAction } from "./rotate.js"; import { credentialsRotateAction } from "./rotate.js";
import { teamsRotateKeysAction, teamsRotationAction, teamsRotationSetAction } from "./rotate-keys.js"; import { teamsRotateKeysAction, teamsRotationAction, teamsRotationSetAction } from "./rotate-keys.js";
import { teamsAuditAction, teamsKeyImportAction, teamsRekeyAction, teamsRotateIdentityAction } from "./hardening.js"; import { teamsAuditAction, teamsKeyImportAction, teamsRekeyAction, teamsRotateIdentityAction } from "./hardening.js";
import { teamsCleanAction } from "./vault-clean.js";
import { boards, tasks } from "./fixtures.js"; import { boards, tasks } from "./fixtures.js";
import { print, type OutputFormat } from "./format.js"; import { print, type OutputFormat } from "./format.js";
import { parsePositiveInteger } from "./numeric-options.js"; import { parsePositiveInteger } from "./numeric-options.js";
@ -749,6 +750,7 @@ Quick start (<team> is e.g. profullstack; see yours with "logicsrc teams list"):
logicsrc teams rekey <team> --all new vault keys, values re-encrypted logicsrc teams rekey <team> --all new vault keys, values re-encrypted
logicsrc teams rotation <team> how each secret is rotated (agents: --format json) logicsrc teams rotation <team> how each secret is rotated (agents: --format json)
logicsrc teams rotate-keys <team> [project] [env] rotate values (dry run; --approve to write) logicsrc teams rotate-keys <team> [project] [env] rotate values (dry run; --approve to write)
logicsrc teams clean <team> <project> <env> --dead archive dead keys to <project>--<env>-archive (dry run; --approve)
Deploy boxes and CI (a machine API key: one team, scoped vaults, read-only, its own identity): Deploy boxes and CI (a machine API key: one team, scoped vaults, read-only, its own identity):
logicsrc keys create <name> --team <team> --vault <project>--<env> --out deploy.key logicsrc keys create <name> --team <team> --vault <project>--<env> --out deploy.key
@ -970,6 +972,40 @@ After rotating: redeploy the consumers, install new SSH public keys, revoke the
}) })
); );
teams
.command("clean")
.argument("<slug>", "Team slug")
.argument("<project>", "Project name")
.argument("<env>", "Environment name (prod, staging, …)")
.option("-k, --key <names>", "archive these keys (comma-separated or repeated)", collectList)
.option("--dead", "also archive keys whose URL host (or Supabase token's project) no longer resolves")
.option("--reason <text>", "why the --key ones are archived, kept beside them")
.option("--approve", "move them (dry run by default)")
.option("--format <format>", "table, json, or markdown", "table")
.description("Move stale keys out of a vault into <project>--<env>-archive. Never deletes: a key leaves only after its archived copy reads back.")
.addHelpText(
"after",
`
Dry run first, then apply:
logicsrc teams clean profullstack threatcrush prod --dead
logicsrc teams clean profullstack threatcrush prod --dead --key SUPABASE_DB_PASSWORD --reason "old Supabase cloud project" --approve
Archived keys keep their names in the archive vault; a name already holding a
different value there gets a __<UTC stamp> suffix. Each one has an
__ARCHIVE__<NAME> entry saying where it came from, why, when and who.
Get them back: logicsrc teams pull profullstack threatcrush prod-archive --env archive.env
`
)
.action((slug, project, env, options) =>
teamsCleanAction(slug, project, env, {
keys: options.key,
dead: Boolean(options.dead),
approve: Boolean(options.approve),
reason: options.reason,
format: options.format as OutputFormat
})
);
teams teams
.command("key") .command("key")
.description("Print this machine's identity secret key, to read vault values in the web app.") .description("Print this machine's identity secret key, to read vault values in the web app.")

View file

@ -0,0 +1,69 @@
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");
});

View file

@ -0,0 +1,202 @@
import { lookup } from "node:dns/promises";
import { decryptValue, fingerprintValue, teamProvider, unwrapVaultKey, type RemoteSecret } from "@logicsrc/plugin-credential-sharing";
import { print, printColumns, type OutputFormat } from "./format.js";
import { authedClient, splitVaultName, teamEndpoint, vaultName } from "./teams.js";
/**
* `logicsrc teams clean` moves stale keys out of a vault into its archive vault
* (`<project>--<env>-archive`). Nothing is ever deleted outright: a key leaves
* the live vault only after its copy in the archive reads back with the same
* fingerprint. Dry run unless --approve.
*
* `--dead` finds keys that point somewhere that no longer exists: a URL whose
* host has no DNS record, or a Supabase JWT whose project ref is such a host.
*/
export const ARCHIVE_SUFFIX = "-archive";
/** Archive entries describing where an archived key came from: `__ARCHIVE__<NAME>`. */
export const ARCHIVE_ENTRY_PREFIX = "__ARCHIVE__";
const URL_HOST = /\b[a-z][a-z0-9+.-]*:\/\/(?:[^@/\s"']+@)?([a-z0-9.-]+\.[a-z]{2,})(?=[:/?#\s"']|$)/gi;
const JWT = /^eyJ[\w-]+\.([\w-]+)\.[\w-]*$/;
/** Hostnames of every URL in a value, lowercased, without duplicates. */
export function urlHosts(value: string): string[] {
const hosts = new Set<string>();
for (const m of value.matchAll(URL_HOST)) hosts.add(m[1]!.toLowerCase());
return [...hosts];
}
/** Hosts a JWT is bound to: a Supabase key's `<ref>.supabase.co` and its `iss` host. */
export function jwtHosts(value: string): string[] {
const m = JWT.exec(value.trim());
if (!m) return [];
let payload: { ref?: unknown; iss?: unknown };
try {
payload = JSON.parse(Buffer.from(m[1]!, "base64url").toString("utf8"));
} catch {
return [];
}
const hosts: string[] = [];
if (typeof payload.ref === "string" && /^[a-z0-9]{8,40}$/.test(payload.ref)) hosts.push(`${payload.ref}.supabase.co`);
if (typeof payload.iss === "string") hosts.push(...urlHosts(payload.iss));
return hosts;
}
export function archiveVaultName(project: string, env: string): string {
return vaultName(project, `${env}${ARCHIVE_SUFFIX}`);
}
/**
* The name a key is stored under in the archive: its own name, unless the
* archive already holds a different value there, then `<KEY>__<UTC stamp>`.
* `null` means the archive already holds this exact value under the key.
*/
export function archiveSlot(key: string, fingerprint: string, archived: Map<string, string>, now: Date): string | null {
const existing = archived.get(key);
if (existing === undefined) return key;
if (existing === fingerprint) return null;
const stamp = now.toISOString().replace(/[-:]/g, "").replace(/\..*$/, "");
let name = `${key}__${stamp}`;
for (let n = 2; archived.has(name); n++) name = `${key}__${stamp}_${n}`;
return name;
}
export type HostCheck = (host: string) => Promise<boolean>;
/** True when the host resolves; only a definite "no such name" counts as dead. */
export const resolves: HostCheck = async (host) => {
try {
await lookup(host);
return true;
} catch (err) {
const code = (err as NodeJS.ErrnoException).code;
return !(code === "ENOTFOUND" || code === "ENODATA");
}
};
/** Why each key is dead, from its value alone; keys that look fine are absent. */
export async function findDead(values: Map<string, string>, check: HostCheck = resolves): Promise<Map<string, string>> {
const cache = new Map<string, Promise<boolean>>();
const alive = (h: string) => {
if (!cache.has(h)) cache.set(h, check(h));
return cache.get(h)!;
};
const dead = new Map<string, string>();
for (const [key, value] of values) {
for (const host of urlHosts(value)) {
if (!(await alive(host))) {
dead.set(key, `url host ${host} does not resolve`);
break;
}
}
if (dead.has(key)) continue;
for (const host of jwtHosts(value)) {
if (!(await alive(host))) {
dead.set(key, `token for ${host}, which does not resolve`);
break;
}
}
}
return dead;
}
export interface CleanOptions {
keys?: string[];
dead: boolean;
approve: boolean;
reason?: string;
format: OutputFormat;
}
interface CleanRow {
key: string;
reason: string;
archivedAs: string;
status: "would-archive" | "archived" | "already-archived" | "failed";
error?: string;
}
export async function teamsCleanAction(slug: string, project: string, env: string, options: CleanOptions): Promise<void> {
if (env.endsWith(ARCHIVE_SUFFIX)) throw new Error(`${vaultName(project, env)} is an archive vault; clean the live vault instead.`);
if (!options.dead && !options.keys?.length) throw new Error("Say what to archive: --key NAME (repeatable) and/or --dead.");
const { client, identity } = authedClient();
const source = vaultName(project, env);
const archive = archiveVaultName(project, env);
const { vaults } = await client.listVaults(slug);
const src = vaults.find((v) => v.name === source);
if (!src) throw new Error(`Vault ${slug}/${source} not found.`);
if (!src.hasAccess) throw new Error(`You cannot decrypt ${slug}/${source}.`);
const dek = await unwrapVaultKey((await client.getMyGrant(src.id)).wrappedDek, identity.keys);
const { secrets } = await client.listSecrets(src.id);
const byName = new Map<string, RemoteSecret>(secrets.map((s) => [s.name, s]));
const values = new Map<string, string>();
for (const s of secrets) values.set(s.name, await decryptValue({ nonce: s.nonce, ciphertext: s.ciphertext }, dek));
const picked = new Map<string, string>();
for (const key of options.keys ?? []) {
if (!byName.has(key)) throw new Error(`${key} is not in ${slug}/${source}.`);
picked.set(key, options.reason ?? "named on the command line");
}
if (options.dead) for (const [key, why] of await findDead(values)) if (!picked.has(key)) picked.set(key, why);
if (picked.size === 0) {
console.error(`Nothing to archive in ${slug}/${source}.`);
print({ team: slug, vault: source, archive, keys: [] }, options.format);
return;
}
// What the archive holds already, so a second run never stores a value twice.
const arc = vaults.find((v) => v.name === archive);
const archived = new Map<string, string>();
if (arc?.hasAccess) for (const s of (await client.listSecrets(arc.id)).secrets) archived.set(s.name, s.fingerprint);
else if (arc) throw new Error(`${slug}/${archive} exists but you cannot decrypt it; ask a member for a grant first.`);
const now = new Date();
const rows: CleanRow[] = [];
const upserts: Record<string, string> = {};
for (const [key, reason] of picked) {
const slot = archiveSlot(key, byName.get(key)!.fingerprint, archived, now);
if (slot === null) {
rows.push({ key, reason, archivedAs: key, status: "already-archived" });
continue;
}
rows.push({ key, reason, archivedAs: slot, status: "would-archive" });
upserts[slot] = values.get(key)!;
upserts[`${ARCHIVE_ENTRY_PREFIX}${slot}`] = JSON.stringify({ from: source, key, reason, archivedAt: now.toISOString(), by: identity.email });
}
if (options.approve) {
try {
if (Object.keys(upserts).length) {
await teamProvider.write!({ endpoint: teamEndpoint(slug, archive), upserts, deletes: [], dryRun: false });
}
// Read the archive back: a key leaves the live vault only once its copy is there, byte for byte.
const after = (await client.listVaults(slug)).vaults.find((v) => v.name === archive);
const stored = new Map((after ? (await client.listSecrets(after.id)).secrets : []).map((s) => [s.name, s.fingerprint]));
const safe: string[] = [];
for (const row of rows) {
if (stored.get(row.archivedAs) === fingerprintValue(values.get(row.key)!)) {
safe.push(row.key);
if (row.status === "would-archive") row.status = "archived";
} else {
row.status = "failed";
row.error = "copy not found in the archive; left in place";
}
}
if (safe.length) await client.putSecrets(src.id, [], safe);
} catch (err) {
for (const row of rows) if (row.status === "would-archive") Object.assign(row, { status: "failed", error: (err as Error).message });
}
}
if (options.format === "table") {
printColumns(rows.map((r) => ({ key: r.key, status: r.status, "archived as": r.archivedAs, reason: r.error ?? r.reason })));
const at = splitVaultName(archive);
if (!options.approve) console.error(`\nDry run. Move them to ${slug}/${archive} with --approve.`);
else console.error(`\nArchived in ${slug}/${archive}. Get them back with: logicsrc teams pull ${slug} ${at?.project} ${at?.env} --env archive.env`);
} else {
print({ team: slug, vault: source, archive, keys: rows }, options.format);
}
if (rows.some((r) => r.status === "failed")) process.exitCode = 1;
}