Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

push-secrets.ts

141 lines5.4 KB

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, and
18 * `VITE_*` build-time variables). Use `bun run secrets:build` to build
19 * with public configuration from Doppler.
20 * `DOPPLER_*` are not application secrets; pushing them is how the
21 * deployed Worker ended up with three junk bindings.
22 * - Nothing is ever deleted. `wrangler secret bulk` can delete via null, but
23 * this script never emits one — removing a secret stays a deliberate
24 * manual act.
25 * - Dry run unless `--push` is passed.
26 *
27 * Secrets are written into a new Worker version rather than the live one;
28 * the next deploy inherits them. See the push call at the bottom for why.
29 *
30 * Usage:
31 * bun scripts/push-secrets.ts # reconcile + report
32 * bun scripts/push-secrets.ts --push # apply
33 * bun scripts/push-secrets.ts --config stg # non-prod target
34 */
35import {
36 PUSH_REMOVE_LIST,
37 dopplerSecrets,
38 isRemoved,
39 parseConfigFlag,
40 run,
41 withoutRemoved,
42} from './secrets.ts'
43​
44const push = process.argv.includes('--push')
45const dopplerConfig = parseConfigFlag(process.argv.slice(2), 'prd')
46​
47/** Secret names currently bound to the deployed Worker (names only). */
48function workerSecretNames(): Set<string> {
49 const raw = run('bunx', ['wrangler', 'secret', 'list'])
50 const parsed = JSON.parse(raw) as Array<{ name: string }>
51 return new Set(parsed.map((s) => s.name))
53​
54const rawDoppler = dopplerSecrets(dopplerConfig)
55const payload = withoutRemoved(rawDoppler, PUSH_REMOVE_LIST)
56const onWorker = workerSecretNames()
57​
58const rows: Array<[string, string]> = []
59for (const name of Object.keys(payload).sort()) {
60 rows.push([name, onWorker.has(name) ? 'update' : 'CREATE'])
62​
63const width = rows.length > 0 ? Math.max(...rows.map(([n]) => n.length)) : 0
64console.log(
65 `\nDoppler config: ${dopplerConfig} Worker: modelschemas mode: ${
66 push ? 'PUSH' : 'dry run'
67 }\n`,
69for (const [name, action] of rows) {
70 console.log(` ${name.padEnd(width)} ${action}`)
72​
73const dropped = Object.keys(rawDoppler)
74 .filter((name) => isRemoved(name, PUSH_REMOVE_LIST))
75 .sort()
76if (dropped.length > 0) {
77 console.log(`\n remove list, skipped: ${dropped.join(', ')}`)
79if (dropped.some((name) => name.startsWith('VITE_'))) {
80 console.log(
81 ' VITE_* values are build-time configuration, not Worker secrets.' +
82 '\n Build with bun run secrets:build, or configure Workers Builds variables.',
83 )
85​
86const workerOnly = [...onWorker].filter((name) => !(name in payload)).sort()
87const workerRemoved = workerOnly.filter((name) =>
88 isRemoved(name, PUSH_REMOVE_LIST),
90const workerMissing = workerOnly.filter(
91 (name) => !isRemoved(name, PUSH_REMOVE_LIST),
93if (workerRemoved.length > 0) {
94 console.log(
95 ` on Worker, in the remove list (not deleted): ${workerRemoved.join(', ')}`,
96 )
98if (workerMissing.length > 0) {
99 console.log(
100 `\n ${workerMissing.length} secret(s) live on the Worker but not in Doppler,` +
101 ` so they will be left untouched:\n ${workerMissing.join(', ')}` +
102 `\n Add them to the '${dopplerConfig}' config before relying on this script.`,
103 )
105​
106const count = Object.keys(payload).length
107if (!push) {
108 console.log(
109 `\nDry run — nothing sent. ${count} secret(s) would be written.` +
110 ` Re-run with --push to apply.\n`,
111 )
112 process.exit(0)
114if (count === 0) {
115 console.log('\nNothing to push.\n')
116 process.exit(0)
118​
119/**
120 * One API call, over stdin: no temp file, nothing in argv, nothing in shell
121 * history. Deletions are impossible here because no key is ever set to null.
122 *
123 * `wrangler versions secret bulk` rather than the plain `wrangler secret
124 * bulk`, which edits the live Worker and refuses with Cloudflare error 10215
125 * whenever the newest uploaded version is not the deployed one — the normal
126 * state here, since Workers Builds uploads a preview version on every
127 * non-production branch push. The versions API has no such precondition, so
128 * this always works.
129 *
130 * It writes the secrets into a NEW version instead of the live one, and
131 * that is enough: a version inherits its bindings from the previous one, so
132 * the next deploy carries these secrets forward automatically (verified on
133 * this Worker — CI-uploaded versions list every secret despite CI never
134 * seeing a value). Only the bindings inherit, not the code, so staging from
135 * a branch cannot leak unmerged code into a later deploy.
136 */
137run('bunx', ['wrangler', 'versions', 'secret', 'bulk'], JSON.stringify(payload))
138console.log(
139 `\nWrote ${count} secret(s) into a new version.` +
140 `\nThey go live with the next deploy, which inherits them.\n`,