logicsrc/packages/cli/src/propagate.test.ts
sh1pt-actions-fleet[bot] 3254b2706b
feat(rotation): provider rotators, consumer propagation, weekly job, runbook (CLI 0.14.0) (#265)
- Catalog covers every key in the team plan (unknown 275 -> 0): new methods
  `provider` and `not-secret`, plus family/owner/never/disruptive on every
  spec. Wallets, treasury, Lightning, exchanges, Stripe, npm, GitHub App keys,
  signing identities and the personal vault are `never` and owner-only; an
  __ROTATE__ entry cannot make them automatic.
- Two-phase provider rotators (rotators.ts): resend, cloudflare-token,
  coinpay-api-key, coinpay-webhook-secret, vapid, postgres-role (SCRAM
  verifier over stdin, refuses self-hosted Supabase).
- rotate-keys --propagate [root]: finds consumer env files by value, backs
  up (.bak-rotate-<date>, 600), rewrites, restarts with the site's deploy.env
  through deploy-gate 3 at a time, health-checks local + public URL, revokes
  the old credential only after; any failure restores vaults, files and the
  provider. --scheduled, --max, --max-sites, --exclude-site, --report.
- scripts/logicsrc-rotate: weekly job, dry run unless LOGICSRC_ROTATE_LIVE=1,
  mails rotated / skipped / manual-by-owner report (never values).
- docs/rotation.md (logicsrc.com/docs/rotation) + docs/ROTATION.md pointer,
  including the wallets and signing keys section.

Co-authored-by: Anthony Ettinger <anthony@profullstack.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:43:55 +00:00

74 lines
3.3 KiB
TypeScript

import { mkdtempSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync, readdirSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { envFilesIn, findConsumers, inBatches, replaceInValue, restoreBackup, rewriteEnvFile, strayCopies } from "./propagate.js";
const dirs: string[] = [];
function www(): string {
const d = mkdtempSync(join(tmpdir(), "logicsrc-propagate-"));
dirs.push(d);
return d;
}
afterEach(() => {
for (const d of dirs.splice(0)) rmSync(d, { recursive: true, force: true });
});
const OLD = "old-secret-0123456789";
const NEW = "new-secret-abcdefghij";
describe("propagate", () => {
it("finds consumers by value, skipping backups, and flags build-time keys", () => {
const root = www();
mkdirSync(join(root, "a.com"));
mkdirSync(join(root, "b.com"));
writeFileSync(join(root, "a.com", "app.env"), `X=1\nCRON_SECRET=${OLD}\nRENAMED="${OLD}"\n`);
writeFileSync(join(root, "a.com", "app.env.bak-001"), `CRON_SECRET=${OLD}\n`);
writeFileSync(join(root, "a.com", "app.bak-002.env"), `CRON_SECRET=${OLD}\n`);
writeFileSync(join(root, "b.com", "deploy.env"), `NEXT_PUBLIC_THING=${OLD}\n`);
expect(envFilesIn(join(root, "a.com")).map((f) => f.split("/").pop())).toEqual(["app.env"]);
const hits = findConsumers(root, [{ oldValue: OLD, newValue: NEW }]);
expect(hits.map((h) => [h.site, h.keys, h.needsBuild])).toEqual([
["a.com", ["CRON_SECRET", "RENAMED"], false],
["b.com", ["NEXT_PUBLIC_THING"], true]
]);
});
it("rewrites in place with a 600 backup, keeps mode and quotes, and restores", () => {
const root = www();
const f = join(root, "app.env");
writeFileSync(f, `A=${OLD}\nB='${OLD}'\nDATABASE_URL=postgres://u:${OLD}@h:5432/d\nC=keep\n`, { mode: 0o600 });
const { backup, keys } = rewriteEnvFile(f, [{ oldValue: OLD, newValue: NEW }], "20261029");
expect(keys).toEqual(["A", "B", "DATABASE_URL"]);
expect(readFileSync(f, "utf8")).toBe(`A=${NEW}\nB='${NEW}'\nDATABASE_URL=postgres://u:${NEW}@h:5432/d\nC=keep\n`);
expect(statSync(f).mode & 0o777).toBe(0o600);
expect(statSync(backup).mode & 0o777).toBe(0o600);
expect(backup).toMatch(/app\.env\.bak-rotate-20261029$/);
restoreBackup(f, backup);
expect(readFileSync(f, "utf8")).toContain(`A=${OLD}`);
const second = rewriteEnvFile(f, [{ oldValue: OLD, newValue: NEW }], "20261029");
expect(second.backup).toMatch(/-2$/);
expect(readdirSync(root).filter((n) => n.includes("rotate-tmp"))).toEqual([]);
});
it("ignores short values that could collide with unrelated text", () => {
expect(replaceInValue("abc", [{ oldValue: "abc", newValue: "x" }])).toBeNull();
});
it("finds stray copies (crontab, scripts)", () => {
const root = www();
writeFileSync(join(root, "s"), `curl -H "x: ${OLD}"`);
expect(strayCopies([join(root, "s"), join(root, "missing")], [{ oldValue: OLD, newValue: NEW }])).toEqual([join(root, "s")]);
});
it("runs at most 3 at a time", async () => {
let running = 0;
let peak = 0;
await inBatches([1, 2, 3, 4, 5, 6, 7], 3, async () => {
peak = Math.max(peak, ++running);
await new Promise((r) => setTimeout(r, 5));
running--;
});
expect(peak).toBe(3);
});
});