Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions infra/github-backup/tests/test_deploy_dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
combination, NO wrangler (and no bws)
invocation at all
--deploy-version alone -> exit 0, wrangler versions deploy
<id>@100% invoked (rollback path intact)
<id>@100% invoked (rollback path intact;
the stub answers versions view with a
configured version, per #3475's
verify-before-deploy contract)
--dry-run alone -> exit 0, wrangler deploy --dry-run
invoked, no versions deploy

Expand All @@ -34,7 +37,10 @@ def check(name, ok):
echo "$(basename "$0") $@" >> "$STUB_LOG"
case "$(basename "$0")" in
bws) echo '[{"key":"cloudflare.api-token-day-to-day","value":"stub-token"}]';;
wrangler) echo '[]';;
wrangler) case "$1 $2" in
"versions view") echo '{"resources":{"bindings":[{"name":"DRAIN_BEARER","type":"secret_text"},{"name":"GITHUB_WEBHOOK_SECRET","type":"secret_text"}]}}';;
*) echo '[]';;
esac;;
git) echo '0123456789abcdef0123456789abcdef01234567';;
esac
exit 0
Expand Down
154 changes: 154 additions & 0 deletions infra/github-backup/tests/test_deploy_rollback_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/usr/bin/env python3
"""test_deploy_rollback_version.py — regression tests for #3475.

deploy.mjs's normal flow deployed the uploaded version, then set the
Worker secrets — and `wrangler secret put` creates and deploys a NEW
version carrying the bindings, so the version the script advertised
("live: ... at version <uploaded id>") was the PRE-secrets version:
rolling back to it would restore a Worker without the runtime
configuration its authenticated endpoints need. The --deploy-version
(rollback) path never provisioned secrets at all.

The fix: after provisioning, re-read the live deployment and advertise
the final configured version. On the --deploy-version path the
configuration cannot be added after the fact (ordinary `secret put`
hard-errors when the latest version is not deployed — the rollback
state by definition — and `versions secret put` patches the latest
version, not the selected one), so the path verifies BEFORE deploying:
a target missing the required secret bindings is refused with the
roll-forward remedy named; a configured target deploys as-is.

Drives the real deploy.mjs (node) with stubbed bws/wrangler/git
executables on PATH. The wrangler stub is stateful: `versions deploy`
records the live version, each `secret put` replaces it with
"configured-final" (modelling the new version secret put creates),
and `deployments list --json` reports the recorded live version.
Mock secret values are stubs; a check asserts they never reach stdout
or stderr.

Run: python3 infra/github-backup/tests/test_deploy_rollback_version.py
"""
import os, subprocess, sys, tempfile

HERE = os.path.dirname(os.path.abspath(__file__))
DEPLOY = os.path.abspath(os.path.join(HERE, "..", "worker", "deploy.mjs"))

PASS = []
def check(name, ok):
PASS.append((name, ok))
print(("PASS " if ok else "FAIL ") + name)

BWS_STUB = """#!/bin/sh
echo "bws $@" >> "$STUB_LOG"
echo '[{"key":"cloudflare.api-token-day-to-day","value":"stub-token"},\
{"key":"github-app.backup-worker-bearer","value":"stub-bearer-value"},\
{"key":"github-app.backup-webhook-secret","value":"stub-webhook-value"}]'
exit 0
"""

WRANGLER_STUB = """#!/bin/sh
echo "wrangler $@" >> "$STUB_LOG"
case "$1 $2" in
"versions list")
echo '[{"id":"uploaded-v1","annotations":{"workers/tag":"git-0123456789ab"},"metadata":{"created_on":"2026-01-01T00:00:00Z"}}]';;
"versions deploy")
echo "$3" | cut -d@ -f1 > "$STUB_STATE/live";;
"versions upload")
echo uploaded;;
"deployments list")
live=$(cat "$STUB_STATE/live" 2>/dev/null)
if [ -n "$live" ]; then
echo "[{\\"created_on\\":\\"2026-01-02T00:00:00Z\\",\\"versions\\":[{\\"version_id\\":\\"$live\\",\\"percentage\\":100}]}]"
else
echo '[]'
fi;;
"secret put")
cat > /dev/null
echo "configured-final" > "$STUB_STATE/live";;
"versions view")
if [ "$3" = "old-v9" ]; then
echo '{"resources":{"bindings":[{"name":"DRAIN_BEARER","type":"secret_text"},{"name":"GITHUB_WEBHOOK_SECRET","type":"secret_text"}]}}'
else
echo '{"resources":{"bindings":[]}}'
fi;;
esac
exit 0
"""

GIT_STUB = """#!/bin/sh
echo "git $@" >> "$STUB_LOG"
echo '0123456789abcdef0123456789abcdef01234567'
exit 0
"""

def run_deploy(args):
root = tempfile.mkdtemp(prefix="ghb-deploy-rollback-test-")
stub_bin = os.path.join(root, "bin")
state = os.path.join(root, "state")
os.makedirs(stub_bin)
os.makedirs(state)
for name, body in (("bws", BWS_STUB), ("wrangler", WRANGLER_STUB), ("git", GIT_STUB)):
path = os.path.join(stub_bin, name)
with open(path, "w") as f:
f.write(body)
os.chmod(path, 0o755)
log = os.path.join(root, "stub.log")
env = {**os.environ,
"PATH": stub_bin + os.pathsep + os.environ["PATH"],
"CLOUDFLARE_ACCOUNT_ID": "stub-account",
"STUB_LOG": log,
"STUB_STATE": state}
proc = subprocess.run(["node", DEPLOY, *args], env=env,
capture_output=True, text=True, timeout=60)
calls = ""
if os.path.exists(log):
with open(log) as f:
calls = f.read()
return proc, calls

# 1. normal deploy advertises the final configured version
proc, calls = run_deploy([])
check("normal: exit 0", proc.returncode == 0)
check("normal: versions deploy of the upload happened",
"wrangler versions deploy uploaded-v1@100%" in calls)
check("normal: both Worker secrets provisioned",
"wrangler secret put DRAIN_BEARER" in calls and
"wrangler secret put GITHUB_WEBHOOK_SECRET" in calls)
check("normal: advertised live version is the configured version",
"at version configured-final" in proc.stdout)
live_lines = [ln for ln in proc.stdout.splitlines() if ln.startswith("live:")]
check("normal: pre-secrets upload is NOT advertised as live",
live_lines and all("uploaded-v1" not in ln for ln in live_lines))
check("normal: no mock secret value in output",
"stub-bearer-value" not in proc.stdout + proc.stderr and
"stub-webhook-value" not in proc.stdout + proc.stderr)

# 2. rollback of a CONFIGURED version deploys as-is, verified first
proc, calls = run_deploy(["--deploy-version", "old-v9"])
check("rollback: exit 0", proc.returncode == 0)
check("rollback: bindings verified before deploying",
"wrangler versions view old-v9" in calls and
calls.find("wrangler versions view old-v9") < calls.find("wrangler versions deploy"))
check("rollback: versions deploy at 100%",
"wrangler versions deploy old-v9@100%" in calls)
check("rollback: no secret put (a configured version needs none, and "
"secret put would hard-error in the rollback state)",
"wrangler secret put" not in calls)
check("rollback: advertised live version is the target, verified",
"at version old-v9" in proc.stdout and "verified" in proc.stdout)
check("rollback: no mock secret value in output",
"stub-bearer-value" not in proc.stdout + proc.stderr and
"stub-webhook-value" not in proc.stdout + proc.stderr)

# 3. rollback of an UNCONFIGURED version is refused before any deploy
proc, calls = run_deploy(["--deploy-version", "bare-v1"])
check("unconfigured: non-zero exit", proc.returncode != 0)
check("unconfigured: error names the missing bindings and the refusal",
"DRAIN_BEARER" in proc.stderr and "refusing to roll back" in proc.stderr)
check("unconfigured: no versions deploy happened",
"versions deploy" not in calls)
check("unconfigured: no secret put attempted",
"wrangler secret put" not in calls)

print(f"\n{sum(1 for _, ok in PASS if ok)}/{len(PASS)} checks passed")
sys.exit(0 if all(ok for _, ok in PASS) else 1)
73 changes: 66 additions & 7 deletions infra/github-backup/worker/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ import { fileURLToPath } from "node:url";

const here = dirname(fileURLToPath(import.meta.url));
const WORKER = "developing-today-backup";
// [Worker binding name, Bitwarden secret name] pairs the Worker needs
// to serve authenticated endpoints. A version without these bindings
// is not a sound rollback target (#3475).
const WORKER_SECRETS = [
["DRAIN_BEARER", "github-app.backup-worker-bearer"],
["GITHUB_WEBHOOK_SECRET", "github-app.backup-webhook-secret"],
];
const WORKER_URL = "https://developing-today-backup.developing-today.workers.dev";

const args = process.argv.slice(2);
Expand Down Expand Up @@ -66,8 +73,26 @@ if (!apiToken) {
const env = { ...process.env, CLOUDFLARE_API_TOKEN: apiToken };

if (deployVersion !== null) {
// #3475: deploying an existing version restores that version exactly,
// bindings included. A version uploaded by the normal flow predates
// the runtime-configuration step, so it carries no secret bindings —
// and the configuration cannot be added after the fact: ordinary
// `wrangler secret put` hard-errors when the latest version is not
// the deployed one (the rollback state by definition), and
// `wrangler versions secret put` patches the LATEST version, not the
// selected one. So verify BEFORE switching traffic: refuse a target
// missing the required secret bindings and name the remedy (roll
// forward through the normal flow, which uploads, deploys, and
// provisions, producing a configured version). Configured versions —
// including every version the normal flow now advertises — deploy
// as-is.
verifyVersionConfigured(deployVersion);
createDeployment(deployVersion);
reportLive();
console.log(
`live: ${WORKER_URL} at version ${deployVersion} ` +
`(rollback; required runtime configuration verified on the version)`
);
process.exit(0);
}

Expand Down Expand Up @@ -112,7 +137,20 @@ if (live.includes(version.id)) {
}
setWorkerSecrets();
reportLive();
console.log(`live: ${WORKER_URL} at version ${version.id} (${tag})`);
// #3475: `wrangler secret put` creates and deploys a NEW version that
// carries the runtime configuration; the uploaded version.id predates
// it and has no secret bindings. The version to advertise — and the
// only sound rollback target — is the final configured live version,
// re-read after provisioning, not the pre-secrets upload.
const configuredLive = liveVersionIds();
const liveId = configuredLive.join(", ") || version.id;
console.log(`live: ${WORKER_URL} at version ${liveId} (${tag})`);
if (liveId !== version.id) {
console.log(
`note: uploaded version ${version.id} was superseded by configured version ${liveId}; ` +
`use ${liveId} as the rollback target`
);
}

function workerExists() {
try {
Expand Down Expand Up @@ -147,20 +185,41 @@ function liveVersionIds() {
}
}

// Refuse to make live a version that lacks the required secret
// bindings. Reads the version's bindings via `wrangler versions view`;
// secret values are never fetched, only binding names.
function verifyVersionConfigured(versionId) {
const out = runWrangler(["versions", "view", versionId, "--name", WORKER, "--json"], { capture: true });
const parsed = JSON.parse(out);
const bindings = parsed?.resources?.bindings ?? parsed?.bindings ?? [];
const secretNames = new Set(
bindings.filter((b) => b?.type === "secret_text").map((b) => b.name)
);
const missing = WORKER_SECRETS.map(([envName]) => envName).filter((name) => !secretNames.has(name));
if (missing.length > 0) {
throw new Error(
`version ${versionId} lacks required runtime configuration (${missing.join(", ")} secret binding(s) missing); ` +
`refusing to roll back to an unconfigured version. Roll forward instead: check out the version's git tag and ` +
`run the normal deploy, which uploads, deploys, and provisions the secrets, producing a configured version.`
);
}
console.log(`version ${versionId} carries the required secret bindings (${[...secretNames].join(", ")})`);
}

function createDeployment(versionId) {
console.log(`creating deployment at version ${versionId} (100%)`);
runWrangler(["versions", "deploy", `${versionId}@100%`, "--name", WORKER, "--yes"], { stdio: "inherit" });
}

// (Re)set the Worker secrets from Bitwarden by name, so a renamed or
// fresh Worker is fully wired in the same run. Values pass through
// fresh Worker is fully wired in the same run. Each `secret put`
// creates a new Worker version carrying the binding, so after this
// runs the live version is a configured successor of the version that
// was deployed — callers must re-read the live version before
// advertising a rollback target (#3475). Values pass through
// wrangler's stdin only; nothing is printed.
function setWorkerSecrets() {
const wanted = [
["DRAIN_BEARER", "github-app.backup-worker-bearer"],
["GITHUB_WEBHOOK_SECRET", "github-app.backup-webhook-secret"],
];
for (const [envName, secretName] of wanted) {
for (const [envName, secretName] of WORKER_SECRETS) {
const value = byName(secretName);
if (!value) {
console.log(`warning: no Bitwarden secret named ${secretName}; skipping ${envName}`);
Expand Down