Open History to see the last commit
1/**
2 * Reconcile production Worker secrets against a Doppler config.
3 *
4 * Runs entirely on a trusted machine using credentials that are already
5 * there — your Doppler CLI login and your wrangler login. Nothing new is
6 * stored anywhere, no token is added to CI, and the blast radius stays
7 * exactly what it is today: this laptop. That is the whole reason to prefer
8 * it over a CI job, which would need a long-lived Doppler service token AND a
9 * Cloudflare API token sitting in GitHub.
10 *
11 * Handling rules, because a careless version of this script is how secrets
12 * leak:
13 * - Values are held in memory only. Never written to disk, never passed as
14 * argv (visible in `ps`), never echoed. They reach wrangler over stdin.
15 * - Doppler is the source of truth. Everything in the config is pushed
16 * except the remove list (`DOPPLER_*` context vars, plus
17 * `BETTER_AUTH_URL` which is already a wrangler `vars` binding).
18 * `DOPPLER_*` are not application secrets; pushing them is how the
19 * deployed Worker ended up with three junk bindings.
20 * - Nothing is ever deleted. `wrangler secret bulk` can delete via null, but
21 * this script never emits one — removing a secret stays a deliberate
22 * manual act.
23 * - Dry run unless `--push` is passed.
24 *
25 * Secrets are written into a new Worker version rather than the live one;
26 * the next deploy inherits them. See the push call at the bottom for why.
27 *
28 * Usage:
29 * bun scripts/push-secrets.ts # reconcile + report
30 * bun scripts/push-secrets.ts --push # apply
31 * bun scripts/push-secrets.ts --config stg # non-prod target
32 */
33import {
34 PUSH_REMOVE_LIST,
35 dopplerSecrets,
36 isRemoved,
37 parseConfigFlag,
38 run,
39 withoutRemoved,
40} from './secrets.ts'
41
42const push = process.argv.includes('--push')
43const dopplerConfig = parseConfigFlag(process.argv.slice(2), 'prd')
44
45/** Secret names currently bound to the deployed Worker (names only). */
46function workerSecretNames(): Set<string> {
47 const raw = run('bunx', ['wrangler', 'secret', 'list'])
48 const parsed = JSON.parse(raw) as Array<{ name: string }>
49 return new Set(parsed.map((s) => s.name))
50}
51
52const rawDoppler = dopplerSecrets(dopplerConfig)
53const payload = withoutRemoved(rawDoppler, PUSH_REMOVE_LIST)
54const onWorker = workerSecretNames()
55
56const rows: Array<[string, string]> = []
57for (const name of Object.keys(payload).sort()) {
58 rows.push([name, onWorker.has(name) ? 'update' : 'CREATE'])
59}
60
61const width = rows.length > 0 ? Math.max(...rows.map(([n]) => n.length)) : 0
62console.log(
63 `\nDoppler config: ${dopplerConfig} Worker: modelschemas mode: ${
64 push ? 'PUSH' : 'dry run'
65 }\n`,
66)
67for (const [name, action] of rows) {
68 console.log(` ${name.padEnd(width)} ${action}`)
69}
70
71const dropped = Object.keys(rawDoppler)
72 .filter((name) => isRemoved(name, PUSH_REMOVE_LIST))
73 .sort()
74if (dropped.length > 0) {
75 console.log(`\n remove list, skipped: ${dropped.join(', ')}`)
76}
77
78const workerOnly = [...onWorker].filter((name) => !(name in payload)).sort()
79const workerRemoved = workerOnly.filter((name) =>
80 isRemoved(name, PUSH_REMOVE_LIST),
81)
82const workerMissing = workerOnly.filter(
83 (name) => !isRemoved(name, PUSH_REMOVE_LIST),
84)
85if (workerRemoved.length > 0) {
86 console.log(
87 ` on Worker, in the remove list (not deleted): ${workerRemoved.join(', ')}`,
88 )
89}
90if (workerMissing.length > 0) {
91 console.log(
92 `\n ${workerMissing.length} secret(s) live on the Worker but not in Doppler,` +
93 ` so they will be left untouched:\n ${workerMissing.join(', ')}` +
94 `\n Add them to the '${dopplerConfig}' config before relying on this script.`,
95 )
96}
97
98const count = Object.keys(payload).length
99if (!push) {
100 console.log(
101 `\nDry run — nothing sent. ${count} secret(s) would be written.` +
102 ` Re-run with --push to apply.\n`,
103 )
104 process.exit(0)
105}
106if (count === 0) {
107 console.log('\nNothing to push.\n')
108 process.exit(0)
109}
110
111/**
112 * One API call, over stdin: no temp file, nothing in argv, nothing in shell
113 * history. Deletions are impossible here because no key is ever set to null.
114 *
115 * `wrangler versions secret bulk` rather than the plain `wrangler secret
116 * bulk`, which edits the live Worker and refuses with Cloudflare error 10215
117 * whenever the newest uploaded version is not the deployed one — the normal
118 * state here, since Workers Builds uploads a preview version on every
119 * non-production branch push. The versions API has no such precondition, so
120 * this always works.
121 *
122 * It writes the secrets into a NEW version instead of the live one, and
123 * that is enough: a version inherits its bindings from the previous one, so
124 * the next deploy carries these secrets forward automatically (verified on
125 * this Worker — CI-uploaded versions list every secret despite CI never
126 * seeing a value). Only the bindings inherit, not the code, so staging from
127 * a branch cannot leak unmerged code into a later deploy.
128 */
129run('bunx', ['wrangler', 'versions', 'secret', 'bulk'], JSON.stringify(payload))
130console.log(
131 `\nWrote ${count} secret(s) into a new version.` +
132 `\nThey go live with the next deploy, which inherits them.\n`,
133)