mirror of
https://github.com/profullstack/logicsrc.git
synced 2026-11-29 15:51:52 +00:00
Every automatic secret older than the team's max age (90 days default) is queued as one run in awaiting-approval. An owner/admin approves it on the Rotation page or with `teams rotation-approve`; machine keys can queue but never approve. logicsrc-rotate --queue (*/5 cron) runs approved runs with propagate + health checks + rollback. rotation_auto_approve per team is the paid automatic rotation add-on (off by default). Co-authored-by: Anthony Ettinger <anthony@profullstack.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
230 lines
11 KiB
Bash
Executable file
230 lines
11 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
#
|
|
# logicsrc-rotate: the weekly secret rotation job (runbook: logicsrc.com/docs/rotation).
|
|
#
|
|
# logicsrc-rotate run per the config (dry run unless LOGICSRC_ROTATE_LIVE=1)
|
|
# logicsrc-rotate --dry-run plan + report only, whatever the config says
|
|
# logicsrc-rotate --no-mail print the report instead of mailing it
|
|
# logicsrc-rotate --queue run the APPROVED queued runs now (a */5 cron), mail only if one ran
|
|
#
|
|
# 90-day policy: every weekly run first queues the automatic keys older than the
|
|
# team's max age (90 days) as one run awaiting approval, and the mail links to
|
|
# the Rotation page. An owner/admin clicks Approve; the --queue cron rotates it
|
|
# within five minutes (3 sites at a time, health-checked, rolled back on failure).
|
|
# Teams with the paid automatic rotation add-on skip the click.
|
|
#
|
|
# Rotates only what `rotate-keys --scheduled` allows unattended (non-disruptive
|
|
# random secrets and overlapping provider rotators with known, healthy
|
|
# consumers), rewrites + restarts + health-checks the consumers under ~/www,
|
|
# and mails a report: rotated, rolled back, skipped (why), and the manual list
|
|
# with steps grouped by who has to do it. No secret value is ever printed,
|
|
# logged or mailed: the CLI's report holds key names, vaults, sites, outcomes.
|
|
#
|
|
# Config (optional, mode 600): ~/.config/logicsrc-rotate.env
|
|
# LOGICSRC_ROTATE_LIVE=1 actually rotate (default: dry run)
|
|
# LOGICSRC_ROTATE_TEAM=profullstack
|
|
# LOGICSRC_ROTATE_MAX=10 live rotations per run
|
|
# LOGICSRC_ROTATE_TO=a@x,b@y report recipients
|
|
# LOGICSRC_ROTATE_ROOT=~/www where consumer sites live
|
|
# LOGICSRC_ROTATE_MAX_SITES=5 skip values read by more sites than this
|
|
# LOGICSRC_ROTATE_EXCLUDE=a.com,b.com never touch these sites (default: the client site)
|
|
# RESEND_ADMIN_API_KEY, CLOUDFLARE_ADMIN_API_TOKEN,
|
|
# COINPAY_ROTATE_TOKEN | COINPAY_ROTATE_EMAIL + COINPAY_ROTATE_PASSWORD
|
|
# admin creds for the provider rotators
|
|
set -euo pipefail
|
|
umask 077
|
|
|
|
CONF="${LOGICSRC_ROTATE_CONF:-$HOME/.config/logicsrc-rotate.env}"
|
|
if [ -f "$CONF" ]; then
|
|
[ "$(stat -c %a "$CONF")" = 600 ] || { echo "refusing $CONF: mode must be 600" >&2; exit 2; }
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
. "$CONF"
|
|
set +a
|
|
fi
|
|
|
|
TEAM="${LOGICSRC_ROTATE_TEAM:-profullstack}"
|
|
MAX="${LOGICSRC_ROTATE_MAX:-10}"
|
|
TO="${LOGICSRC_ROTATE_TO:-ettinger@gmail.com}"
|
|
ROOT="${LOGICSRC_ROTATE_ROOT:-$HOME/www}"
|
|
LIVE="${LOGICSRC_ROTATE_LIVE:-0}"
|
|
MAX_SITES="${LOGICSRC_ROTATE_MAX_SITES:-5}"
|
|
# Client sites are never part of a fleet sweep.
|
|
EXCLUDE="${LOGICSRC_ROTATE_EXCLUDE:-tomahawkedison.com}"
|
|
MAIL=1
|
|
QUEUE=0
|
|
for a in "$@"; do
|
|
case "$a" in
|
|
--dry-run) LIVE=0 ;;
|
|
--queue) QUEUE=1 ;;
|
|
--no-mail) MAIL=0 ;;
|
|
-h|--help) sed -n 2,33p "$0"; exit 0 ;;
|
|
*) echo "unknown option $a" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
STATE="${XDG_STATE_HOME:-$HOME/.local/state}/logicsrc-rotate"
|
|
mkdir -p "$STATE"
|
|
chmod 700 "$STATE"
|
|
STAMP="$(date -u +%Y%m%dT%H%M%SZ)"
|
|
REPORT="$STATE/report-$STAMP.json"
|
|
exec 9>"$STATE/lock"
|
|
flock -n 9 || { echo "another logicsrc-rotate is running" >&2; exit 0; }
|
|
|
|
# Leave room on the box: restarts rebuild nothing, but compose recreates still cost memory.
|
|
avail_gb=$(awk '/MemAvailable/ {print int($2/1048576)}' /proc/meminfo)
|
|
if [ "$LIVE" = 1 ] && [ "$avail_gb" -lt 6 ]; then
|
|
echo "only ${avail_gb} GB available: running as a dry run" >&2
|
|
LIVE=0
|
|
fi
|
|
|
|
if [ "$QUEUE" = 1 ]; then
|
|
# Low memory: leave approved runs queued for the next tick instead of restarting sites now.
|
|
[ "$avail_gb" -lt 6 ] && { echo "only ${avail_gb} GB available: approved runs wait for the next tick" >&2; exit 0; }
|
|
# Only runs an owner/admin approved in the app (or the paid auto-approve add-on queued) write anything.
|
|
QREPORT="$STATE/queue-$STAMP.json"
|
|
logicsrc teams rotate-keys "$TEAM" --from-queue --propagate "$ROOT" --approve --exclude-site "$EXCLUDE" --report "$QREPORT" --format json >/dev/null 2>"$STATE/queue-$STAMP.err" || status=$?
|
|
status="${status:-0}"
|
|
if [ -f "$QREPORT" ] && node -e 'process.exit(require(process.argv[1]).runs.length ? 0 : 1)' "$QREPORT"; then
|
|
QBODY="$STATE/queue-mail-$STAMP.txt"
|
|
node - "$QREPORT" >"$QBODY" <<'NODE'
|
|
const r = require(process.argv[2]);
|
|
const out = [`Approved rotation run(s) for team ${r.team}, ${r.at}`, ""];
|
|
for (const run of r.runs) {
|
|
out.push(`Run ${run.id}: ${run.status} (${run.ran}), ${run.keys.length} key(s): ${run.summary}`);
|
|
for (const x of run.results) out.push(` ${x.key} ${x.status}${x.sites && x.sites.length ? ` sites: ${[...new Set(x.sites.map((s) => s.site))].join(" ")}` : ""}${x.error ? ` ${x.error}` : ""}`);
|
|
}
|
|
console.log(out.join("\n"));
|
|
NODE
|
|
QSUBJECT="logicsrc rotation: approved run $(node -e 'const r=require(process.argv[1]);console.log(r.runs.map(x=>x.status).join(", "))' "$QREPORT")"
|
|
if [ "$MAIL" = 1 ]; then env -u RESEND_API_KEY mail send --via resend --to "$TO" --subject "$QSUBJECT" --file "$QBODY"; else cat "$QBODY"; fi
|
|
else
|
|
rm -f "$QREPORT" "$STATE/queue-$STAMP.err"
|
|
fi
|
|
exit "${status:-0}"
|
|
fi
|
|
|
|
# 90-day policy: queue the overdue automatic keys as one run awaiting approval (never rotates by itself).
|
|
DUE="$STATE/due-$STAMP.json"
|
|
logicsrc teams rotate-keys "$TEAM" --queue-due --format json >"$DUE" 2>>"$STATE/run-$STAMP.err" || echo '{}' >"$DUE"
|
|
|
|
args=(teams rotate-keys "$TEAM" --scheduled --propagate "$ROOT" --max "$MAX" --max-sites "$MAX_SITES" --exclude-site "$EXCLUDE" --report "$REPORT" --format json)
|
|
[ "$LIVE" = 1 ] && args+=(--approve)
|
|
status=0
|
|
logicsrc "${args[@]}" >/dev/null 2>"$STATE/run-$STAMP.err" || status=$?
|
|
# stderr holds vault names and counts only; keep it for the report.
|
|
|
|
BODY="$STATE/mail-$STAMP.txt"
|
|
node - "$REPORT" "$LIVE" "$status" "$STATE/run-$STAMP.err" "$DUE" "$TEAM" >"$BODY" <<'NODE'
|
|
const fs = require("fs");
|
|
const [file, live, status, errFile, dueFile, team] = process.argv.slice(2);
|
|
let due = {};
|
|
try { due = JSON.parse(fs.readFileSync(dueFile, "utf8")); } catch {}
|
|
const dueLines = [];
|
|
if (due.run) {
|
|
dueLines.push(`${due.maxAgeDays}-DAY POLICY: ${due.run.keys.length} automatic key(s) older than ${due.maxAgeDays} days, run ${due.run.id} is ${due.run.status}.`);
|
|
if (due.run.status === "awaiting-approval") dueLines.push(` Approve it: https://app.logicsrc.com/teams/${team}/rotation (it rotates within 5 minutes of the click)`);
|
|
dueLines.push("");
|
|
} else if (due.maxAgeDays) {
|
|
dueLines.push(`${due.maxAgeDays}-DAY POLICY: nothing is older than ${due.maxAgeDays} days.`, "");
|
|
}
|
|
let report;
|
|
try {
|
|
report = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
} catch {
|
|
const err = fs.existsSync(errFile) ? fs.readFileSync(errFile, "utf8").slice(-1500) : "";
|
|
console.log(`logicsrc-rotate could not produce a report (exit ${status}).\n\n${err}`);
|
|
process.exit(0);
|
|
}
|
|
const R = report.results;
|
|
const by = (s) => R.filter((r) => r.status === s);
|
|
const out = [...dueLines];
|
|
const line = (s = "") => out.push(s);
|
|
const where = (r) => `${(r.vaults || []).length} vault(s)${r.sites && r.sites.length ? `, sites: ${[...new Set(r.sites.map((s) => s.site))].join(" ")}` : ""}`;
|
|
line(`Secret rotation ${live === "1" ? "LIVE run" : "DRY RUN (nothing changed)"} for team ${report.team}, ${report.at}`);
|
|
line(`Runbook: https://logicsrc.com/docs/rotation`);
|
|
line();
|
|
const rotated = by("rotated");
|
|
const would = by("would-rotate");
|
|
line(`Rotated: ${rotated.length} Would rotate: ${would.length} Rolled back: ${by("rolled-back").length} Failed: ${by("failed").length} Skipped: ${by("skipped").length} Manual: ${by("todo").length}`);
|
|
for (const [title, list] of [["ROTATED (verified healthy, old value revoked)", rotated], ["WOULD ROTATE (set LOGICSRC_ROTATE_LIVE=1 to do it)", would], ["ROLLED BACK (old value restored everywhere)", by("rolled-back")], ["FAILED", by("failed")]]) {
|
|
if (!list.length) continue;
|
|
line();
|
|
line(title);
|
|
// Dry runs list one line per key name; live results list every value.
|
|
if (title.startsWith("WOULD")) {
|
|
const g = new Map();
|
|
for (const r of list) {
|
|
const k = `${r.key} [${r.method}]`;
|
|
const e = g.get(k) || { n: 0, sites: new Set() };
|
|
e.n++;
|
|
for (const s of r.sites || []) e.sites.add(s.site);
|
|
g.set(k, e);
|
|
}
|
|
for (const [k, e] of g) line(` ${k} x${e.n} sites: ${[...e.sites].join(" ") || "-"}`);
|
|
continue;
|
|
}
|
|
for (const r of list) {
|
|
line(` ${r.key} [${r.method}] ${where(r)}`);
|
|
for (const s of r.sites || []) line(` ${s.site}: ${s.restart || "-"} / ${s.health || "-"}${s.backup ? ` (backup ${s.backup.split("/").pop()})` : ""}`);
|
|
if (r.note) line(` note: ${r.note}`);
|
|
if (r.error) line(` ${r.error}`);
|
|
}
|
|
}
|
|
const skipped = by("skipped");
|
|
if (skipped.length) {
|
|
line();
|
|
line("SKIPPED (left for a person, with the reason)");
|
|
const reasons = new Map();
|
|
for (const r of skipped) {
|
|
const why = (r.error || "").replace(/: .*/, "");
|
|
reasons.set(why, [...(reasons.get(why) || []), r]);
|
|
}
|
|
for (const [why, list] of reasons) {
|
|
const n = new Map();
|
|
for (const r of list) n.set(r.key, (n.get(r.key) || 0) + 1);
|
|
line(` ${why} (${list.length}): ${[...n].map(([k, c]) => (c > 1 ? `${k} x${c}` : k)).join(", ")}`);
|
|
}
|
|
}
|
|
const todo = by("todo");
|
|
if (todo.length) {
|
|
line();
|
|
line("MANUAL TO-DO (grouped by who does it, then family; steps once per family)");
|
|
const owners = [["anthony", "Anthony (money, signing, his logins: never automated)"], ["agent", "An agent with dashboard / CLI access"]];
|
|
for (const [owner, label] of owners) {
|
|
const mine = todo.filter((r) => (r.owner || "agent") === owner);
|
|
if (!mine.length) continue;
|
|
line();
|
|
line(`== ${label}: ${mine.length} value(s)`);
|
|
const fam = new Map();
|
|
for (const r of mine) fam.set(r.family || "other", [...(fam.get(r.family || "other") || []), r]);
|
|
for (const [f, list] of [...fam].sort((a, b) => b[1].length - a[1].length)) {
|
|
line();
|
|
line(` -- ${f} (${list.length})${list[0].url ? ` ${list[0].url}` : ""}`);
|
|
if (list[0].note) line(` ${list[0].note}`);
|
|
for (const s of list[0].steps || []) line(` - ${s}`);
|
|
const keys = new Map();
|
|
for (const r of list) keys.set(r.key, (keys.get(r.key) || 0) + (r.vaults || []).length);
|
|
line(` keys: ${[...keys].map(([k, n]) => `${k} (${n})`).join(", ")}`);
|
|
const why = [...new Set(list.map((r) => r.error).filter(Boolean))];
|
|
if (why.length) line(` blocked: ${why.join("; ")}`);
|
|
}
|
|
}
|
|
}
|
|
if (report.locked && report.locked.length) {
|
|
line();
|
|
line(`Vaults this identity cannot decrypt (not covered): ${report.locked.length}`);
|
|
}
|
|
console.log(out.join("\n"));
|
|
NODE
|
|
|
|
SUBJECT="logicsrc rotation $( [ "$LIVE" = 1 ] && echo live || echo dry-run ): $(node -e 'try{const r=require(process.argv[1]).results;const c=s=>r.filter(x=>x.status===s).length;console.log(`${c("rotated")} rotated, ${c("rolled-back")+c("failed")} failed, ${c("todo")} manual`)}catch{console.log("no report")}' "$REPORT")"
|
|
if [ "$MAIL" = 1 ]; then
|
|
# The shell's RESEND_API_KEY can be stale; mail's own resend account is the house path.
|
|
env -u RESEND_API_KEY mail send --via resend --to "$TO" --subject "$SUBJECT" --file "$BODY"
|
|
else
|
|
cat "$BODY"
|
|
fi
|
|
# Keep 12 weeks of reports.
|
|
find "$STATE" -maxdepth 1 -type f \( -name 'report-*.json' -o -name 'mail-*.txt' -o -name 'run-*.err' -o -name 'due-*.json' -o -name 'queue-*' \) -mtime +84 -delete
|
|
exit "$status"
|