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

push-secrets.ts

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