Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(audit): abort the custodial key sweep when no decryption key is configured

nirholas3a80002678ba
diff --git a/scripts/audit-custodial-key-health.mjs b/scripts/audit-custodial-key-health.mjs
index 393d7bd95..1e2869ee0 100644
--- a/scripts/audit-custodial-key-health.mjs
+++ b/scripts/audit-custodial-key-health.mjs
@@ -28,6 +28,10 @@
//
// Env (read from .env / .env.local, or the process env):
// DATABASE_URL, WALLET_ENCRYPTION_KEY, JWT_SECRET, SOLANA_RPC_URL
+//
+// Exits 3 without touching the database when no decryption key is configured:
+// a keyless run can only report 100% undecryptable, which says nothing about
+// production. Exit 0 otherwise, stranded fleet or not.
import { readFileSync } from 'node:fs';
@@ -51,6 +55,16 @@ const PLATFORM_OWNER_EMAIL = '[email protected]';
const PLATFORM_EMAIL_SUFFIX = '@agents.three.ws';
const LAMPORTS_PER_SOL = 1_000_000_000;
+// Where an operator actually gets the key. The playbook order in CLAUDE.md:
+// local dotfiles first, then the running service, then Secret Manager.
+const KEY_FIX_HINT = [
+ ' Set WALLET_ENCRYPTION_KEY (>=32 chars) and re-run. Where to find it:',
+ ' .env / .env.local in this repo, or',
+ ' gcloud run services describe three-ws-api --region us-central1 \\',
+ ' --project aerial-vehicle-466722-p5 --format=yaml, or',
+ ' gcloud secrets versions access latest --secret=WALLET_ENCRYPTION_KEY',
+].join('\n');
+
for (const file of ['.env', '.env.local']) {
let text;
try {
@@ -65,10 +79,43 @@ for (const file of ['.env', '.env.local']) {
}
const { sql } = await import('../api/_lib/db.js');
-const { decryptSecret } = await import('../api/_lib/secret-box.js');
+const { decryptSecret, secretBoxKeyCandidates } = await import('../api/_lib/secret-box.js');
const { PublicKey } = await import('@solana/web3.js');
const { solanaConnection } = await import('../api/_lib/solana/connection.js');
+// A shell with NO decryption key configured cannot tell "sealed under a retired
+// key" from "this machine was never given the key". Without this check the
+// script decrypts nothing, reports 100% undecryptable, and prints the customer
+// escalation banner on any developer machine missing WALLET_ENCRYPTION_KEY: a
+// confident number that sends operators after the wrong problem, which is the
+// same class of failure the unread-balance handling below exists to prevent.
+// secret-box exports its candidate list precisely so this cannot drift from the
+// precedence decryptSecret() actually uses.
+const keyCandidates = secretBoxKeyCandidates();
+if (keyCandidates.length === 0) {
+ const blocked = {
+ checked_at: new Date().toISOString(),
+ error: 'no_decryption_key',
+ detail:
+ 'No custodial decryption key is configured, so every wallet would read as ' +
+ 'undecryptable here regardless of production state. This run proves nothing ' +
+ 'about stranded funds.',
+ fix: KEY_FIX_HINT,
+ };
+ if (AS_JSON) {
+ emitJson(JSON.stringify(blocked, null, 2));
+ process.exit(3);
+ }
+ console.error('custodial key health ABORTED: no decryption key configured');
+ console.error('');
+ console.error(' Every wallet would report as undecryptable here regardless of production');
+ console.error(' state, so this run cannot tell you anything about stranded funds.');
+ console.error('');
+ console.error(KEY_FIX_HINT);
+ process.exit(3);
+}
+
+
const rows = await sql`
SELECT a.id AS agent_id,
a.name AS name,
@@ -155,6 +202,9 @@ const report = {
checked_at: new Date().toISOString(),
rpc: process.env.SOLANA_RPC_URL || 'https://api.mainnet-beta.solana.com',
wallets: wallets.length,
+ // How many keys decryptSecret() had to try. 1 is the healthy steady state; a
+ // higher count means retired keys are still configured for the migration.
+ key_candidates: keyCandidates.length,
read_errors: readErrors.length,
decryptable: wallets.length - stranded.length,
undecryptable: stranded.length,
@@ -227,7 +277,18 @@ if (report.top_stranded.length) {
console.log(` ${String(w.sol).padStart(14)} SOL ${w.address} ${w.platform ? 'platform' : 'customer'} ${w.name || ''} (${w.reason})`);
}
}
-if (report.sol.stranded_customer > 0) {
+// A fleet-wide 100% failure is almost never 725 separately sealed wallets: it is
+// one wrong key. Saying so here keeps the escalation banner below from reading as
+// a mass customer incident when the real fix is a one-line env correction.
+if (report.wallets > 0 && report.undecryptable === report.wallets) {
+ console.log('');
+ console.log(` EVERY wallet failed to decrypt under the ${report.key_candidates} configured key(s).`);
+ console.log(' A fleet-wide 100% failure almost always means the key is wrong for this');
+ console.log(' deployment, not that every wallet is sealed. Confirm the key matches');
+ console.log(' production before treating this as a customer incident.');
+ console.log('');
+ console.log(KEY_FIX_HINT);
+} else if (report.sol.stranded_customer > 0) {
console.log('');
console.log(' Customer funds are stranded. This is a support obligation, not just an');
console.log(' ops number: those users cannot withdraw. Escalate before anything else.');
diff --git a/scripts/audit-mcp-safety.mjs b/scripts/audit-mcp-safety.mjs
index 355ed5ce9..243cf1003 100644
--- a/scripts/audit-mcp-safety.mjs
+++ b/scripts/audit-mcp-safety.mjs
@@ -49,12 +49,21 @@ import { checkTool, extractTools } from './lib/mcp-safety-check.mjs';
// Reviewed exemptions
// ---------------------------------------------------------------------------
// A tool listed here keeps `readOnlyHint: true` despite carrying write evidence,
-// because the write is a cache fill rather than the effect the caller asked for.
-// Keyed `tool:evidence`; every entry states why.
+// because the evidence is not the effect the caller receives. Keyed
+// `tool:evidence`; every entry states why. Two reviewed categories:
//
-// All four below are read-through cache fills: the caller asked for a read and
-// got a read, and the server warmed its own cache on the way. Each write is
-// wrapped non-fatally at the source, so a failed write still returns the read.
+// 1. Read-through cache fills: the caller asked for a read and got a read, and
+// the server warmed its own cache on the way. Each write is wrapped
+// non-fatally at the source, so a failed write still returns the read.
+// 2. Transaction BUILDERS that broadcast nothing: the handler assembles an
+// unsigned transaction and returns its bytes for a wallet to sign elsewhere.
+// Instruction constructors (umi's `transferSol`, Metaplex's `mintAsset`) read
+// as transfers and mints to the call-closure scan, but calling one appends an
+// instruction to a builder; it moves no funds and touches no chain. Auto-
+// approving such a call is genuinely safe: nothing changes until the owner
+// signs and some OTHER tool broadcasts. Only add an entry here after checking
+// the handler never reaches a send (`sendAndConfirm`, `hedgedSend`) and never
+// signs with a real keypair.
const EXEMPTIONS = new Map([
[
'oracle_coin:db-write',
@@ -72,6 +81,10 @@ const EXEMPTIONS = new Map([
'solana_agent_passport:db-write',
'ensureWarm() crawls attestations into the cache on a cold read; the caller receives the passport view.',
],
+ [
+ 'prepare_agent_mint:funds-transfer',
+ 'Builder, not sender (category 2). buildAgentMint() calls umi transferSol() to APPEND the mainnet deploy-fee instruction to the mint builder; the handler signs only with the new asset keypair, serializes to txs_base64 with the caller wallet as a noop fee payer, and returns. It never broadcasts. The sibling that does broadcast, mint_onchain_agent, is annotated readOnlyHint:false + destructiveHint:true.',
+ ],
]);
// ---------------------------------------------------------------------------
diff --git a/scripts/check-rules.mjs b/scripts/check-rules.mjs
index 5b567ca92..e590af762 100644
--- a/scripts/check-rules.mjs
+++ b/scripts/check-rules.mjs
@@ -101,6 +101,20 @@ const SKIP = [
// record the archive exists to preserve. The rule applies to prose we
// write, not to prose we transcribe.
/^data\/x-archive\//,
+ // The store-submission evidence captures, for the same reason. Each of these
+ // is a verbatim JSON-RPC wire payload pulled from the LIVE production server
+ // and kept so an OpenAI or MCP-directory reviewer can diff what we claim
+ // against what the endpoint actually answers. Normalizing their typography to
+ // satisfy a house rule falsifies the capture (it was done once, and the
+ // committed evidence silently stopped matching production). Prose rules
+ // belong on the tool descriptions in api/, which is where the wording that
+ // ends up in these files is authored.
+ /^prompts\/store-submissions\/_generated\/[a-z-]*tools-list\.json$/,
+ // Captured live API responses, stored verbatim as the evidence a gauntlet run
+ // produced. Same reasoning as the archive above: the third party's own
+ // wording is the record, and retyping its punctuation to satisfy a house
+ // rule would make the evidence a paraphrase of what the endpoint said.
+ /^prompts\/okx-ai\/e2e-evidence\//,
// The baked animation library. Every clip, the manifest, and the signature
// index are written by scripts/build-animations.mjs and
// scripts/compact-clips.mjs from FBX sources deliberately kept out of the
diff --git a/scripts/migrate-plaintext-secrets.mjs b/scripts/migrate-plaintext-secrets.mjs
new file mode 100755
index 000000000..f63db8f10
--- /dev/null
+++ b/scripts/migrate-plaintext-secrets.mjs
@@ -0,0 +1,437 @@
+#!/usr/bin/env node
+// Move plaintext secret-bearing env vars off a Cloud Run service and into
+// Secret Manager, then point the service at the secrets instead.
+//
+// WHY THIS EXISTS
+// A literal `value:` entry in a Cloud Run service config is readable by any
+// principal holding run.services.get on the project. A credential stored that
+// way (a wallet secret key, an API token, a database URL) is therefore only as
+// private as the broadest viewer role on the project. A `valueFrom.secretKeyRef`
+// is not: reading it needs secretmanager.versions.access on that one secret,
+// which this tool grants to the runtime service account and to nobody else.
+//
+// WHAT IT DOES
+// 1. Reads the live service config and splits every env var into three groups:
+// already a secretKeyRef, a plaintext literal that looks like a credential,
+// and a plaintext literal that is public config (an address, a mint, a key
+// id, anything containing PUBLIC).
+// 2. For each credential: reuses the Secret Manager secret that already holds
+// that exact value when one exists, otherwise creates the secret and adds
+// the live value as a version.
+// 3. Grants the service's own runtime service account
+// roles/secretmanager.secretAccessor on that one secret. Never project-wide.
+// 4. Flips every migrated var in ONE `gcloud run services update
+// --update-secrets` call, which both attaches the secret and drops the
+// plaintext literal.
+// 5. Re-reads the service and asserts the new revision holds no plaintext
+// literal for any migrated var and carries 100% of traffic.
+//
+// SAFETY
+// * Dry run by default. Nothing is written without --apply.
+// * Secret values are never printed, never passed as a CLI argument, and never
+// written to disk. Versions are added over stdin; comparisons are done on a
+// SHA-256 digest held in memory.
+// * An existing secret whose latest version differs from the live value is
+// REFUSED, not overwritten (another var may depend on it). Pass
+// --force-new-version to add the live value as a new version anyway.
+// * The classifier's "leave as config" decisions are printed with reasons on
+// every run, so a wrong call is visible before --apply, and --include
+// overrides it per var.
+//
+// USAGE
+// node scripts/migrate-plaintext-secrets.mjs # plan, writes nothing
+// node scripts/migrate-plaintext-secrets.mjs --only ECONOMY_MASTER_SECRET_BASE58 --apply
+// node scripts/migrate-plaintext-secrets.mjs --apply # migrate every credential found
+// node scripts/migrate-plaintext-secrets.mjs --verify # assert the end state only
+//
+// --only A,B restrict to these env vars
+// --include A,B also migrate these, overriding the "public config" call
+// --exclude A,B never migrate these
+// --map ENV=secret-name pin a var to a specific Secret Manager secret
+// --force-new-version add a version to an existing secret whose value differs
+// --service/--region/--project target something other than three-ws-api
+//
+// Verify the service still works after --apply: compare /api/healthz against the
+// reading you took before, and confirm a signing path that uses the migrated key
+// (for the economy master that is the treasury-topup cron, visible in
+// /api/ops/payment-outcomes and the healthz subsystem block).
+
+import { execFile, execFileSync } from 'node:child_process';
+import { createHash } from 'node:crypto';
+import { promisify } from 'node:util';
+import { pathToFileURL } from 'node:url';
+import './lib/gcloud-path.mjs';
+
+const execFileP = promisify(execFile);
+
+const argv = process.argv.slice(2);
+const APPLY = argv.includes('--apply');
+const VERIFY_ONLY = argv.includes('--verify');
+const FORCE_NEW_VERSION = argv.includes('--force-new-version');
+
+function optValue(flag, fallback) {
+ const i = argv.indexOf(flag);
+ return i === -1 ? fallback : argv[i + 1];
+}
+function optList(flag) {
+ const raw = optValue(flag, '');
+ return String(raw || '')
+ .split(',')
+ .map((s) => s.trim())
+ .filter(Boolean);
+}
+
+const SERVICE = optValue('--service', 'three-ws-api');
+const REGION = optValue('--region', 'us-central1');
+const PROJECT = optValue('--project', 'aerial-vehicle-466722-p5');
+const ONLY = optList('--only');
+const INCLUDE = optList('--include');
+const EXCLUDE = optList('--exclude');
+
+const NAME_MAP = new Map();
+for (let i = 0; i < argv.length; i++) {
+ if (argv[i] !== '--map') continue;
+ const [env, ...rest] = String(argv[i + 1] || '').split('=');
+ if (!env || !rest.length) fail(`--map expects ENV=secret-name, got "${argv[i + 1]}"`);
+ NAME_MAP.set(env, rest.join('='));
+}
+
+// A name segment from this set means the value is a credential.
+const SECRET_SEGMENTS = new Set([
+ 'SECRET',
+ 'SECRETS',
+ 'KEY',
+ 'KEYS',
+ 'KEYPAIR',
+ 'TOKEN',
+ 'TOKENS',
+ 'PRIVATE',
+ 'PASSWORD',
+ 'PASSWD',
+ 'MNEMONIC',
+ 'CREDENTIAL',
+ 'CREDENTIALS',
+ 'APIKEY',
+ 'DSN',
+]);
+
+// A name segment from this set means the value is a public identifier, even when
+// another segment matched above. In a crypto codebase TOKEN and KEY are heavily
+// overloaded: THREE_TOKEN_MINT is an address, not a credential.
+const PUBLIC_SEGMENTS = new Set(['PUBLIC', 'PUBKEY', 'ADDRESS', 'MINT', 'ISSUER', 'AUDIENCE']);
+
+// Suffixes that name a credential's handle rather than the credential itself.
+const PUBLIC_SUFFIXES = [
+ '_KEY_ID',
+ '_KEY_NAME',
+ '_KEY_RING',
+ '_KEY_PATH',
+ '_KEY_FILE',
+ '_KEYFILE',
+ '_SECRET_NAME',
+ '_SECRET_ID',
+ '_TOKEN_NAME',
+ '_TOKEN_SYMBOL',
+ '_TOKEN_DECIMALS',
+ '_TOKEN_URI',
+];
+
+// Credentials whose names carry no telltale segment.
+const EXTRA_SECRETS = new Set([
+ 'DATABASE_URL',
+ 'DATABASE_URL_UNPOOLED',
+ 'POSTGRES_URL',
+ 'POSTGRES_URL_NON_POOLING',
+ 'REDIS_URL',
+ 'A2A_PAYER_SOLANA_SECRET',
+]);
+
+function fail(msg) {
+ console.error(`\n FAILED: ${msg}\n`);
+ process.exit(1);
+}
+
+export function classify(name) {
+ if (EXCLUDE.includes(name)) return { secret: false, reason: 'excluded by --exclude' };
+ if (INCLUDE.includes(name)) return { secret: true, reason: 'forced by --include' };
+ if (EXTRA_SECRETS.has(name)) return { secret: true, reason: 'known credential' };
+ const suffix = PUBLIC_SUFFIXES.find((s) => name.endsWith(s));
+ if (suffix) return { secret: false, reason: `names a handle, not a value (${suffix})` };
+ const segments = name.split('_');
+ const publicSegment = segments.find((s) => PUBLIC_SEGMENTS.has(s));
+ if (publicSegment) return { secret: false, reason: `public identifier (${publicSegment})` };
+ const secretSegment = segments.find((s) => SECRET_SEGMENTS.has(s));
+ if (secretSegment) return { secret: true, reason: `credential-bearing name (${secretSegment})` };
+ return { secret: false, reason: 'no credential marker in the name' };
+}
+
+// x402-fee-payer-secret-base58 and upstash-redis-rest-token are the shape the
+// existing secrets on this project already use: the env var name, lowercased,
+// underscores to hyphens.
+export function defaultSecretName(env) {
+ return env.toLowerCase().replace(/_/g, '-');
+}
+
+function digest(value) {
+ return createHash('sha256').update(value, 'utf8').digest('hex');
+}
+
+async function gcloud(args, { input } = {}) {
+ return execFileP('gcloud', [...args, `--project=${PROJECT}`], {
+ maxBuffer: 16 * 1024 * 1024,
+ ...(input === undefined ? {} : { input }),
+ });
+}
+
+// execFile cannot feed stdin, so version writes go through execFileSync, which
+// keeps the value off the argument list and out of the process table.
+function gcloudWithStdin(args, input) {
+ return execFileSync('gcloud', [...args, `--project=${PROJECT}`], {
+ input,
+ encoding: 'utf8',
+ maxBuffer: 4 * 1024 * 1024,
+ stdio: ['pipe', 'pipe', 'pipe'],
+ });
+}
+
+async function describeService() {
+ let out;
+ try {
+ ({ stdout: out } = await gcloud([
+ 'run',
+ 'services',
+ 'describe',
+ SERVICE,
+ `--region=${REGION}`,
+ '--format=json',
+ ]));
+ } catch (e) {
+ const detail = (e?.stderr || e?.message || '').trim().split('\n').slice(0, 4).join(' ');
+ fail(`could not read ${SERVICE} in ${REGION}: ${detail}`);
+ }
+ const svc = JSON.parse(out);
+ const container = svc?.spec?.template?.spec?.containers?.[0] || {};
+ return {
+ raw: svc,
+ env: container.env || [],
+ serviceAccount: svc?.spec?.template?.spec?.serviceAccountName || null,
+ latestReady: svc?.status?.latestReadyRevisionName || null,
+ traffic: svc?.status?.traffic || [],
+ };
+}
+
+function partition(env) {
+ const alreadySecret = [];
+ const credentials = [];
+ const config = [];
+ for (const entry of env) {
+ const name = entry.name;
+ if (ONLY.length && !ONLY.includes(name)) continue;
+ if (entry.valueFrom?.secretKeyRef) {
+ alreadySecret.push({ name, secret: entry.valueFrom.secretKeyRef.name, key: entry.valueFrom.secretKeyRef.key });
+ continue;
+ }
+ const value = entry.value ?? '';
+ const verdict = classify(name);
+ if (!verdict.secret) {
+ config.push({ name, reason: verdict.reason });
+ continue;
+ }
+ if (!value) {
+ config.push({ name, reason: 'credential-bearing name but the value is empty' });
+ continue;
+ }
+ credentials.push({ name, value, reason: verdict.reason, secretName: NAME_MAP.get(name) || defaultSecretName(name) });
+ }
+ return { alreadySecret, credentials, config };
+}
+
+async function secretExists(name) {
+ try {
+ await gcloud(['secrets', 'describe', name, '--format=value(name)']);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+async function latestVersionDigest(name) {
+ try {
+ const { stdout } = await gcloud(['secrets', 'versions', 'access', 'latest', `--secret=${name}`]);
+ return digest(stdout);
+ } catch {
+ return null;
+ }
+}
+
+async function prepareSecret(item) {
+ const exists = await secretExists(item.secretName);
+ const liveDigest = digest(item.value);
+ if (!exists) {
+ if (!APPLY) return { action: 'create secret + add version 1' };
+ await gcloud(['secrets', 'create', item.secretName, '--replication-policy=automatic']);
+ gcloudWithStdin(['secrets', 'versions', 'add', item.secretName, '--data-file=-'], item.value);
+ return { action: 'created secret, added version 1' };
+ }
+ const existingDigest = await latestVersionDigest(item.secretName);
+ if (existingDigest === liveDigest) {
+ return { action: 'reused existing secret (latest version already holds this value)' };
+ }
+ if (!FORCE_NEW_VERSION) {
+ return {
+ action: null,
+ error:
+ existingDigest === null
+ ? `secret ${item.secretName} exists but its latest version is unreadable; resolve by hand or pass --force-new-version`
+ : `secret ${item.secretName} exists with a DIFFERENT value; another var may depend on it. Pass --force-new-version to add the live value as a new version, or --map ${item.name}=<other-name>`,
+ };
+ }
+ if (!APPLY) return { action: 'add a new version (value differs from latest)' };
+ gcloudWithStdin(['secrets', 'versions', 'add', item.secretName, '--data-file=-'], item.value);
+ return { action: 'added a new version (value differed from latest)' };
+}
+
+async function grantAccessor(secretName, serviceAccount) {
+ if (!APPLY) return 'would grant secretAccessor to the runtime service account';
+ await gcloud([
+ 'secrets',
+ 'add-iam-policy-binding',
+ secretName,
+ `--member=serviceAccount:${serviceAccount}`,
+ '--role=roles/secretmanager.secretAccessor',
+ '--condition=None',
+ ]);
+ return 'granted secretAccessor on this one secret';
+}
+
+function reportEndState(after, migratedNames) {
+ const byName = new Map(after.env.map((e) => [e.name, e]));
+ const problems = [];
+ for (const name of migratedNames) {
+ const entry = byName.get(name);
+ if (!entry) {
+ problems.push(`${name}: no longer present on the service`);
+ continue;
+ }
+ if (entry.value) problems.push(`${name}: STILL a plaintext literal`);
+ else if (!entry.valueFrom?.secretKeyRef) problems.push(`${name}: neither a literal nor a secretKeyRef`);
+ }
+ const leftoverLiterals = after.env
+ .filter((e) => e.value && classify(e.name).secret)
+ .map((e) => e.name);
+ const servingRevision = after.traffic.find((t) => Number(t.percent) === 100);
+ const trafficOk = Boolean(servingRevision) && (servingRevision.latestRevision || servingRevision.revisionName === after.latestReady);
+
+ console.log('\nEnd state');
+ console.log(` serving revision: ${after.latestReady || 'unknown'}`);
+ console.log(
+ ` traffic: ${
+ trafficOk
+ ? '100% on the newest ready revision'
+ : after.traffic.map((t) => `${t.percent}% ${t.revisionName || 'latest'}`).join(', ') || 'unknown'
+ }`,
+ );
+ for (const name of migratedNames) {
+ const entry = byName.get(name);
+ const ref = entry?.valueFrom?.secretKeyRef;
+ console.log(` ${name} -> ${ref ? `${ref.name}:${ref.key}` : 'NOT a secretKeyRef'}`);
+ }
+ if (leftoverLiterals.length) {
+ console.log(` plaintext credentials still on the service: ${leftoverLiterals.join(', ')}`);
+ } else {
+ console.log(' plaintext credentials still on the service: none');
+ }
+ if (problems.length) {
+ for (const p of problems) console.log(` PROBLEM: ${p}`);
+ process.exitCode = 1;
+ }
+ if (!trafficOk) {
+ console.log(' PROBLEM: the newest ready revision is not serving 100% of traffic');
+ process.exitCode = 1;
+ }
+ return problems.length === 0 && trafficOk;
+}
+
+async function main() {
+ const before = await describeService();
+ if (!before.serviceAccount) {
+ fail(`${SERVICE} has no explicit runtime service account; refusing to guess who to grant access to`);
+ }
+ const { alreadySecret, credentials, config } = partition(before.env);
+
+ console.log(`\nService: ${SERVICE} (${REGION}, ${PROJECT})`);
+ console.log(`Runtime service account: ${before.serviceAccount}`);
+ console.log(`Serving revision: ${before.latestReady || 'unknown'}`);
+ console.log(`Mode: ${VERIFY_ONLY ? 'VERIFY' : APPLY ? 'APPLY' : 'DRY RUN (nothing will change)'}`);
+
+ console.log(`\nAlready in Secret Manager (${alreadySecret.length}):`);
+ for (const e of alreadySecret) console.log(` ${e.name} -> ${e.secret}:${e.key}`);
+
+ console.log(`\nPlaintext, left as config (${config.length}):`);
+ for (const e of config) console.log(` ${e.name} (${e.reason})`);
+
+ console.log(`\nPlaintext credentials to migrate (${credentials.length}):`);
+ for (const e of credentials) console.log(` ${e.name} -> ${e.secretName} (${e.reason})`);
+
+ if (VERIFY_ONLY) {
+ const migrated = alreadySecret.map((e) => e.name);
+ const ok = reportEndState(before, migrated);
+ console.log(`\nVerify: ${ok && !credentials.length ? 'clean' : 'work remains'}`);
+ if (credentials.length) process.exitCode = 1;
+ return;
+ }
+
+ if (!credentials.length) {
+ console.log('\nNothing to migrate. Every credential-bearing env var is already a secretKeyRef.');
+ return;
+ }
+
+ const updates = [];
+ for (const item of credentials) {
+ const result = await prepareSecret(item);
+ if (result.error) {
+ console.log(`\n SKIPPED ${item.name}: ${result.error}`);
+ continue;
+ }
+ const grant = await grantAccessor(item.secretName, before.serviceAccount);
+ console.log(`\n ${item.name}`);
+ console.log(` secret: ${item.secretName}`);
+ console.log(` ${result.action}`);
+ console.log(` ${grant}`);
+ updates.push(item);
+ }
+
+ if (!updates.length) fail('no var could be prepared; nothing was flipped on the service');
+
+ const pairs = updates.map((u) => `${u.name}=${u.secretName}:latest`).join(',');
+ if (!APPLY) {
+ console.log(`\nDry run. To apply:\n node scripts/migrate-plaintext-secrets.mjs${ONLY.length ? ` --only ${ONLY.join(',')}` : ''} --apply`);
+ console.log(`\nThe single service update it would run:\n gcloud run services update ${SERVICE} --region ${REGION} --project ${PROJECT} \\\n --update-secrets ${pairs}`);
+ return;
+ }
+
+ console.log(`\nFlipping ${updates.length} var(s) on ${SERVICE} in one update...`);
+ await gcloud([
+ 'run',
+ 'services',
+ 'update',
+ SERVICE,
+ `--region=${REGION}`,
+ `--update-secrets=${pairs}`,
+ '--quiet',
+ ]);
+
+ const after = await describeService();
+ const ok = reportEndState(after, updates.map((u) => u.name));
+ console.log(
+ ok
+ ? '\nMigration complete. Now compare /api/healthz against the reading you took first, and confirm a signing path that uses a migrated key still works.'
+ : '\nMigration finished with problems above. Roll traffic back to the prior revision before diagnosing:\n gcloud run services update-traffic ' +
+ `${SERVICE} --region ${REGION} --project ${PROJECT} --to-revisions <prior>=100`,
+ );
+}
+
+const invokedDirectly = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
+if (invokedDirectly) {
+ main().catch((e) => fail(e?.stderr?.trim() || e?.message || String(e)));
+}