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>
This commit is contained in:
sh1pt-actions-fleet[bot] 2026-10-29 00:43:55 +00:00 • committed by GitHub
parent b5f875837a
commit 3254b2706b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 2609 additions and 91 deletions

View file

@ -1,6 +1,6 @@
import { spawnSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { chmodSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import {
decodeSshFile,
@ -25,6 +25,8 @@ import {
type PlannedRotation,
type RotationSpec
} from "./rotation-catalog.js";
import { ROTATORS, defaultRun, rewriteUrls, type Minted, type ProviderRotator, type RotatorContext } from "./rotators.js";
import { dev2Ops, findConsumers, inBatches, restoreBackup, rewriteEnvFile, type SiteOps } from "./propagate.js";
/**
* `logicsrc teams rotation` what rotating each secret takes (never values)
@ -146,6 +148,8 @@ export async function teamsRotationAction(slug: string, filter: RotationFilter,
key: p.key,
method: p.method,
auto: p.automatic ? "yes" : "no",
family: p.spec?.family ?? "",
owner: p.spec?.owner ?? "",
source: p.source,
vaults: p.vaults.length + ((p as { alsoIn?: string[] }).alsoIn?.length ?? 0),
since: String(p.oldestUpdate).slice(0, 10),
@ -153,7 +157,9 @@ export async function teamsRotationAction(slug: string, filter: RotationFilter,
}))
);
const auto = plan.filter((p) => p.automatic).length;
console.error(`\n${plan.length} value(s): ${auto} automatic, ${plan.length - auto} need a person or agent. Steps per key: --format json.`);
const unknown = plan.filter((p) => p.method === "unknown").length;
const never = plan.filter((p) => p.spec?.never).length;
console.error(`\n${plan.length} value(s): ${auto} automatic, ${plan.length - auto - unknown} manual (${never} never automated), ${unknown} without instructions. Steps per key: --format json.`);
console.error(`Rotate: logicsrc teams rotate-keys ${slug}${filter.project ? ` ${filter.project}` : ""}${filter.env ? ` ${filter.env}` : ""} [--key NAME] --approve`);
} else {
print(plan.map(({ fingerprint: _fp, ...p }) => p), options.format);
@ -215,6 +221,18 @@ export interface RotateKeysOptions {
approve: boolean;
setFromStdin?: boolean;
format: OutputFormat;
/** Rewrite + restart + health-check consumer sites under this root (dev2: ~/www). */
propagate?: string;
/** Unattended mode: only methods that are automatic, non-disruptive, and two-phase (or a plain random secret with known consumers). */
scheduled?: boolean;
/** Stop after this many live rotations. */
max?: number;
/** Write the JSON results (never values) to this file as well. */
report?: string;
/** Sites never rewritten or restarted: a value they hold is skipped. */
excludeSites?: string[];
/** Unattended mode: skip values read by more sites than this (default 5). */
maxSites?: number;
}
interface KeyPair {
@ -286,10 +304,20 @@ async function makeReplacement(p: PlannedRotation, oldValue: string, stdinValue?
}
}
export interface ConsumerReport {
site: string;
file: string;
keys: string[];
backup?: string;
restart?: string;
health?: string;
ok?: boolean;
}
export interface RotationResult {
key: string;
method: string;
status: "rotated" | "would-rotate" | "todo" | "failed" | "covered";
status: "rotated" | "would-rotate" | "todo" | "failed" | "rolled-back" | "skipped" | "covered";
/** Every vault:key holding the old value. */
vaults?: string[];
written: string[];
@ -297,6 +325,13 @@ export interface RotationResult {
steps?: string[];
url?: string;
consumers?: string[];
/** Sites found holding the value (with --propagate) and what happened to each. */
sites?: ConsumerReport[];
family?: string;
owner?: string;
never?: boolean;
disruptive?: string;
note?: string;
error?: string;
}
@ -306,9 +341,230 @@ async function readStdin(): Promise<string> {
return Buffer.concat(chunks).toString("utf8").replace(/\r?\n$/, "");
}
export async function teamsRotateKeysAction(slug: string, filter: RotationFilter, options: RotateKeysOptions): Promise<void> {
/** Why the unattended job leaves this one to a person, or null when it may rotate it. */
export function scheduledBlock(p: PlannedRotation): string | null {
const spec = p.spec;
if (!spec || !p.automatic) return "needs a person";
if (spec.never) return "never automated";
if (spec.disruptive) return `disruptive: ${spec.disruptive}`;
if (spec.method === "random") return null;
if (spec.method === "provider") {
const r = ROTATORS[spec.provider ?? ""];
if (!r) return `unknown rotator ${spec.provider}`;
return r.overlap ? null : `${r.title} has no overlap window`;
}
return `${spec.method} needs a person (public key install / command review)`;
}
const STRAY_PLACES = (): string[] => {
const home = homedir();
const dirs = [join(home, ".local/bin"), join(home, "scripts/bin")];
const files: string[] = [];
for (const d of dirs) {
try {
for (const n of readdirSync(d)) files.push(join(d, n));
} catch {
/* missing */
}
}
return files;
};
/** The crontab and small scripts under ~/.local/bin and ~/scripts/bin: places a secret hides outside env files. */
function defaultStrayText(): string[] {
const out = [crontabText()];
for (const f of STRAY_PLACES()) {
try {
if (statSync(f).isFile() && statSync(f).size < 1 << 20) out.push(readFileSync(f, "utf8"));
} catch {
/* unreadable */
}
}
return out;
}
function crontabText(): string {
const r = spawnSync("crontab", ["-l"], { encoding: "utf8" });
return r.status === 0 ? r.stdout : "";
}
export interface RotateDeps {
ops?: SiteOps;
fetch?: typeof fetch;
now?: Date;
env?: Record<string, string | undefined>;
/** Extra text to search for stray copies (tests). */
strayText?: () => string[];
}
interface VaultWrite {
vault: VaultState;
name: string;
oldValue: string;
newValue: string;
}
async function writeValues(team: Loaded, writes: VaultWrite[], which: "newValue" | "oldValue"): Promise<string[]> {
const written: string[] = [];
for (const vault of [...new Set(writes.map((w) => w.vault))]) {
const upserts = [];
for (const w of writes.filter((x) => x.vault === vault)) {
const value = w[which];
upserts.push({ name: w.name, ...(await encryptValue(value, await team.dek(vault))), fingerprint: fingerprintValue(value) });
}
const { applied } = await team.client.putSecrets(vault.id, upserts, []);
for (const name of applied) written.push(`${vault.name}:${name}`);
}
return written;
}
async function vaultValues(team: Loaded, v: VaultState): Promise<Record<string, string>> {
const out: Record<string, string> = {};
for (const s of v.secrets) {
if (isRotationEntry(s.name) || s.fingerprint === EMPTY_FINGERPRINT) continue;
out[s.name] = await readValue(team, v, s.name);
}
return out;
}
/**
* Rotate one value with a new one we can make ourselves (random, provider):
* mint -> vaults -> consumers -> health -> revoke, with a full rollback when
* any step after mint fails.
*/
async function rotateTwoPhase(
team: Loaded,
p: PlannedRotation,
targets: Array<{ vault: VaultState; key: string }>,
base: RotationResult,
options: RotateKeysOptions,
deps: Required<Pick<RotateDeps, "ops" | "fetch" | "now" | "env">> & { strayText: () => string[] }
): Promise<RotationResult> {
const spec = p.spec!;
const first = targets[0]!;
const oldValue = await readValue(team, first.vault, first.key);
const firstValues = await vaultValues(team, first.vault);
const stamp = deps.now.toISOString().slice(0, 10).replace(/-/g, "");
// Who reads it today. Found by value, so renamed copies count too.
const probe = [{ oldValue, newValue: oldValue }];
const hits = options.propagate ? findConsumers(options.propagate, probe) : [];
const sites: ConsumerReport[] = hits.map((h) => ({ site: h.site, file: h.file, keys: h.keys }));
const strays = deps.strayText().some((t) => oldValue.length >= 12 && t.includes(oldValue));
const excluded = hits.filter((h) => options.excludeSites?.includes(h.site));
if (excluded.length) return { ...base, status: "skipped", sites, error: `excluded site holds it: ${[...new Set(excluded.map((h) => h.site))].join(", ")}` };
if (options.scheduled) {
const n = new Set(hits.map((h) => h.site)).size;
if (n > (options.maxSites ?? 5)) return { ...base, status: "skipped", sites, error: `too many consumers for unattended rotation: ${n} sites (run it by hand with --propagate)` };
if (!options.propagate) return { ...base, status: "skipped", sites, error: "unattended rotation needs --propagate" };
if (!hits.length) return { ...base, status: "skipped", sites, error: "no consumer found on this box: rotate by hand where it is used" };
if (strays) return { ...base, status: "skipped", sites, error: "also in the crontab or a script under ~/.local/bin or ~/scripts/bin: update those by hand" };
}
if (spec.method === "provider") {
const r = ROTATORS[spec.provider ?? ""];
if (!r) return { ...base, status: "todo", sites, error: `unknown rotator ${spec.provider}` };
const why = await r.preflight({ key: p.key, oldValue, vaultValues: firstValues, state: spec.state ?? {}, env: deps.env, fetch: deps.fetch, run: defaultRun, now: deps.now });
if (why) return { ...base, status: "todo", sites, error: why };
}
if (!options.approve) return { ...base, status: "would-rotate", sites };
// A site that is already down would make the rotation look guilty.
const siteDirs = [...new Map(hits.map((h) => [h.dir, h])).values()];
const before = await inBatches(siteDirs, 3, (h) => deps.ops.health(h.dir, h.site));
const sick = before.filter((b) => !b.ok);
if (sick.length) return { ...base, status: "skipped", sites, error: `unhealthy before rotation: ${sick.map((s) => `${s.site} (${s.detail})`).join(", ")}` };
let rotator: ProviderRotator | undefined;
let minted: Minted;
if (spec.method === "provider") {
rotator = ROTATORS[spec.provider ?? ""];
if (!rotator) return { ...base, status: "todo", error: `unknown rotator ${spec.provider}` };
const ctx: RotatorContext = { key: p.key, oldValue, vaultValues: firstValues, state: spec.state ?? {}, env: deps.env, fetch: deps.fetch, run: defaultRun, now: deps.now };
minted = await rotator.mint(ctx);
} else {
const replacement = await makeReplacement(p, oldValue);
const value = replacement.valueFor(first.key, oldValue);
minted = { value, revokeOld: async () => undefined, rollback: async () => undefined };
}
// Vault writes: the value itself everywhere, plus companions (public halves, connection strings) where they live.
const writes: VaultWrite[] = [];
const companionReps: Array<{ oldValue: string; newValue: string }> = [];
for (const vault of [...new Set(targets.map((t) => t.vault))]) {
const values = vault === first.vault ? firstValues : await vaultValues(team, vault);
for (const t of targets.filter((x) => x.vault === vault)) writes.push({ vault, name: t.key, oldValue: values[t.key] ?? oldValue, newValue: minted.value });
const extra: Record<string, string> = { ...rewriteUrls(values, oldValue, minted.value) };
for (const [name, v] of Object.entries(minted.companions ?? {})) if (name in values && !rewriteUrls({ [name]: values[name]! }, oldValue, minted.value)[name]) extra[name] = v;
for (const [name, v] of Object.entries(extra)) {
if (writes.some((w) => w.vault === vault && w.name === name)) continue;
writes.push({ vault, name, oldValue: values[name]!, newValue: v });
if (values[name] !== v && values[name]!.length >= 12 && !values[name]!.includes(oldValue)) companionReps.push({ oldValue: values[name]!, newValue: v });
}
}
const undo = async (why: string, rewritten: Array<{ file: string; backup: string; dir: string; site: string }>): Promise<RotationResult> => {
const notes: string[] = [why];
try {
await writeValues(team, writes, "oldValue");
} catch (err) {
notes.push(`VAULTS NOT RESTORED: ${(err as Error).message}`);
}
for (const r of rewritten) restoreBackup(r.file, r.backup);
const restarted = await inBatches([...new Map(rewritten.map((r) => [r.dir, r])).values()], 3, (r) => deps.ops.restart(r.dir, r.site, false));
for (const r of restarted.filter((x) => !x.ok)) notes.push(`${r.site} did not come back on the old value: ${r.detail}`);
const rb = await minted.rollback().catch((e: Error) => e.message);
if (rb) notes.push(rb);
return { ...base, status: "rolled-back", sites, error: notes.join("; ") };
};
let written: string[];
try {
written = await writeValues(team, writes, "newValue");
} catch (err) {
return undo(`vault write failed: ${(err as Error).message}`, []);
}
// Consumers: rewrite env files (with backups), restart 3 at a time, health-check.
const reps = [{ oldValue, newValue: minted.value }, ...companionReps];
const rewritten: Array<{ file: string; backup: string; dir: string; site: string }> = [];
if (options.propagate) {
const now = findConsumers(options.propagate, reps);
for (const h of now) {
const { backup } = rewriteEnvFile(h.file, reps, stamp);
rewritten.push({ file: h.file, backup, dir: h.dir, site: h.site });
const rep = sites.find((s) => s.file === h.file);
if (rep) rep.backup = backup;
else sites.push({ site: h.site, file: h.file, keys: h.keys, backup });
}
const dirs = [...new Map(now.map((h) => [h.dir, { ...h, needsBuild: now.some((x) => x.dir === h.dir && x.needsBuild) }])).values()];
const results = await inBatches(dirs, 3, async (h) => {
const r = await deps.ops.restart(h.dir, h.site, h.needsBuild);
const hl = r.ok ? await deps.ops.health(h.dir, h.site) : { site: h.site, ok: false, detail: "not restarted" };
for (const s of sites.filter((x) => x.site === h.site)) Object.assign(s, { restart: r.detail, health: hl.detail, ok: r.ok && hl.ok });
return r.ok && hl.ok;
});
if (results.some((ok) => !ok)) return undo("a consumer failed after the switch; rolled back", rewritten);
}
const revokeNote = await minted.revokeOld().catch((e: Error) => `revoking the old credential failed (it is still live): ${e.message}`);
// Remember provider ids for next time (the old key's id is what revokeOld needs).
if (minted.state && Object.keys(minted.state).length) {
const entry = rotationEntryName(first.key);
const value = JSON.stringify({ ...spec, state: minted.state });
await team.client.putSecrets(first.vault.id, [{ name: entry, ...(await encryptValue(value, await team.dek(first.vault))), fingerprint: fingerprintValue(value) }], []);
}
return { ...base, status: "rotated", written, sites, ...(revokeNote ? { note: revokeNote } : {}) };
}
export async function teamsRotateKeysAction(slug: string, filter: RotationFilter, options: RotateKeysOptions, depsIn: RotateDeps = {}): Promise<RotationResult[]> {
const { team, plan, invalid } = await buildPlan(slug, filter);
if (plan.length === 0) throw new Error("Nothing to rotate: no matching keys. See: logicsrc teams rotation " + slug);
const deps = {
ops: depsIn.ops ?? dev2Ops,
fetch: depsIn.fetch ?? fetch,
now: depsIn.now ?? new Date(),
env: depsIn.env ?? process.env,
strayText: depsIn.strayText ?? defaultStrayText
};
let stdinValue: string | undefined;
if (options.setFromStdin) {
@ -320,6 +576,7 @@ export async function teamsRotateKeysAction(slug: string, filter: RotationFilter
const results: RotationResult[] = [];
// One value stored under two names is one rotation, not two.
const handled = new Map<string, string>();
let live = 0;
for (const p of plan) {
const earlier = handled.get(p.fingerprint);
if (earlier && (p.automatic || stdinValue !== undefined)) {
@ -327,18 +584,56 @@ export async function teamsRotateKeysAction(slug: string, filter: RotationFilter
continue;
}
handled.set(p.fingerprint, p.key);
const base = { key: p.key, method: stdinValue !== undefined ? "set" : p.method, steps: p.spec?.steps, url: p.spec?.url, consumers: p.spec?.consumers };
const spec = p.spec;
const base: RotationResult = {
key: p.key,
method: stdinValue !== undefined ? "set" : spec?.method === "provider" ? `provider:${spec.provider}` : p.method,
status: "todo",
written: [],
steps: spec?.steps,
url: spec?.url,
consumers: spec?.consumers,
family: spec?.family,
owner: spec?.owner ?? (spec?.method === "manual" ? "agent" : undefined),
never: spec?.never,
disruptive: spec?.disruptive,
note: spec?.note
};
const targets = siblings(team, p.fingerprint);
const where = targets.map((t) => `${t.vault.name}:${t.key}`);
base.vaults = targets.map((t) => `${t.vault.name}:${t.key}`);
if (stdinValue === undefined && !p.automatic) {
results.push({ ...base, status: "todo", vaults: where, written: [], steps: p.spec?.steps ?? ["No instructions: add some with logicsrc teams rotation-set."] });
results.push({ ...base, status: "todo", steps: spec?.steps ?? ["No instructions: add some with logicsrc teams rotation-set."] });
continue;
}
if (!options.approve) {
results.push({ ...base, status: "would-rotate", vaults: where, written: [] });
if (options.scheduled && stdinValue === undefined) {
const why = scheduledBlock(p);
if (why) {
results.push({ ...base, status: "skipped", error: why });
continue;
}
}
// A plain dry run only plans; with --propagate or --scheduled it also runs the read-only checks.
if (!options.approve && !options.propagate && !options.scheduled) {
results.push({ ...base, status: "would-rotate" });
continue;
}
if (options.approve && options.max !== undefined && live >= options.max) {
results.push({ ...base, status: "skipped", error: `--max ${options.max} reached` });
continue;
}
try {
if (stdinValue === undefined && (spec?.method === "random" || spec?.method === "provider")) {
const r = await rotateTwoPhase(team, p, targets, base, options, deps);
if (r.status === "rotated" || r.status === "rolled-back") live++;
results.push(r);
continue;
}
// ssh-keygen, command and --set-from-stdin: vault write only, consumers by hand.
if (!options.approve) {
results.push({ ...base, status: "would-rotate" });
continue;
}
live++;
const first = targets[0]!;
const oldValue = await readValue(team, first.vault, first.key);
const replacement = await makeReplacement(p, oldValue, stdinValue);
@ -368,22 +663,36 @@ export async function teamsRotateKeysAction(slug: string, filter: RotationFilter
const { applied } = await team.client.putSecrets(vault.id, upserts, []);
for (const name of applied) written.push(`${vault.name}:${name}`);
}
results.push({ ...base, status: "rotated", vaults: where, written, publicKey: replacement.publicKey });
results.push({ ...base, status: "rotated", written, publicKey: replacement.publicKey });
} catch (err) {
results.push({ ...base, status: "failed", written: [], error: (err as Error).message });
}
}
if (options.report) {
writeFileSync(options.report, JSON.stringify({ team: slug, at: deps.now.toISOString(), approve: options.approve, scheduled: Boolean(options.scheduled), results, locked: team.locked }, null, 2), { mode: 0o600 });
chmodSync(options.report, 0o600);
}
if (options.format === "table") {
printColumns(results.map((r) => ({ key: r.key, method: r.method, status: r.status, vaults: (r.status === "rotated" ? r.written : r.vaults ?? []).length, next: r.status === "todo" ? (r.url ?? "see --format json") : r.error ?? "" })));
printColumns(
results.map((r) => ({
key: r.key,
method: r.method,
status: r.status,
vaults: (r.status === "rotated" ? r.written : r.vaults ?? []).length,
sites: r.sites?.length ?? "",
next: r.status === "todo" ? (r.error ?? r.url ?? "see --format json") : r.error ?? r.note ?? ""
}))
);
for (const r of results.filter((x) => x.publicKey)) console.error(`\n${r.key} new public key (install it, remove the old one):\n${r.publicKey}`);
const todo = results.filter((r) => r.status === "todo").length;
if (!options.approve) console.error(`\nDry run. Apply with --approve.${todo ? ` ${todo} key(s) need a person or agent: --format json lists the steps.` : ""}`);
else console.error(`\nRestart or redeploy whatever reads the rotated values, then revoke the old ones where they were issued.`);
else if (!options.propagate) console.error(`\nRestart or redeploy whatever reads the rotated values (or pass --propagate ~/www), then revoke the old ones where they were issued.`);
} else {
print(results, options.format);
}
if (invalid.length) console.error(`Unreadable rotation entries (ignored): ${invalid.join("; ")}`);
if (team.locked.length) console.error(`Not covered (no access): ${team.locked.join(", ")}. Copies of these values there stay OLD.`);
if (results.some((r) => r.status === "failed")) process.exitCode = 1;
if (results.some((r) => r.status === "failed" || r.status === "rolled-back")) process.exitCode = 1;
return results;
}