Skip to content

d1 backup sweep: authenticate with the real secrets key - #152

Merged
ryuzdev merged 1 commit into
mainfrom
fix/d1-backup-real-key
Nov 19, 2026
Merged

ryuzdev merged 1 commit into
mainfrom
fix/d1-backup-real-key

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

fix(d1): sweep backs up with the server's real key

Problem

The scheduled hourly D1 backup sweep never succeeded after #115
(2026-11-06). d1_backup::tick built its D1Ctx with
SecretsKey::test_key() (crates/outer/src/d1_backup.rs, old line ~802),
so the CELLO_OPERATOR_TOKEN it derived for every cello d1 invocation
never matched the token the fleet holds — the fleet's token is derived
from the real key (SecretsKey::derive(&config.outer_secret) in serve,
crates/outer/src/main.rs, passed into FleetManager via
ManagerSettings::secrets_key). Every scheduled backup 401d and the sweep
logged a warning and moved on.

On-demand backup_database was unaffected: it builds D1Ctx::from_state,
which clones the real mcp.secrets_key.

Verified by reading the paths, not by guessing:

  • Fleet side: FleetManager::new stores secrets_key from config
    (fleet/manager.rs), and d1_exec/d1_export send
    operator_token_env(&ctx.secrets_key, slug) as CELLO_OPERATOR_TOKEN
    (http/console_data/d1.rs:848,926). The guarded fleet listener
    requires that token.
  • Sweep side: tick hardcoded SecretsKey::test_key() into the same
    ctx.secrets_key slot, so the two tokens differed by HKDF input.

Change

  • d1_backup::tick takes secrets_key: &SecretsKey and uses it for the
    sweep's D1Ctx; spawn_backup_task takes the owned key and passes it
    through each loop.
  • serve clones the config-derived key into sweep_secrets and hands it
    to spawn_backup_task (crates/outer/src/main.rs).
  • Guard against recurrence: SecretsKey::test_key() and
    AccessKey::test_key() are now #[cfg(test)], so production code
    cannot reference them (a future use fails to compile). Audited every
    test_key() caller: the sweep was the only production one — all others
    sit inside #[cfg(test)] modules (mcp/tools/data.rs already used the
    real key from state).

Test

New d1_backup::tests::tick_backs_up_with_the_servers_real_key: seeds a
due app, fronts the fake cello with a wrapper that 401s unless
CELLO_OPERATOR_TOKEN equals operator_token(&real_key, "acme")
(real_key from derive, never test_key), stubs the fleet health gate
so ensure_awake short-circuits, runs tick with the real key, and
asserts one dump row plus non-empty bytes in the store.

Fail-before proof: with the sweep's key replaced by a mismatched derived
key (reproducing what test_key() did — a token the fleet rejects), the
new test fails (no dump stored); with the fix it passes.

Gates

  • cargo fmt --all --check — clean
  • cargo clippy --locked --all-targets -- -D warnings — clean
  • cargo test --locked (full) — 1280 passed, 0 failed
  • scripts/check-pins.sh — clean

Catch-up after deploy

Prod is being wiped anyway, so no existing app needs a manual backup or
catch-up pass after this deploys — there is nothing worth restoring yet.
The next hourly sweep after deploy will take the first real scheduled
backups. Worth a glance at the d1 backup stored log lines post-deploy
to confirm the 401s are gone.

Summary by CodeRabbit

  • Bug Fixes
    • D1 backups now use the server’s configured secrets key when creating backups, allowing valid backups to be stored.
    • Backup sweeps preserve access to the configured key while other server services use it.

- `d1_backup::tick` built its `D1Ctx` with `SecretsKey::test_key()`,
  so the derived operator token never matched the fleet's and every
  scheduled backup 401d. The sweep now takes the server key (derived
  from config, threaded from `serve` through `spawn_backup_task`).
- Guard: `SecretsKey::test_key` and `AccessKey::test_key` are now
  `#[cfg(test)]`, unreachable from production builds.
- Test: `tick_backs_up_with_the_servers_real_key` runs the sweep
  against a token-enforcing fake cello keyed with a real non-test
  key and asserts a dump is stored (fails with a mismatched key).
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The D1 backup task now passes the server’s SecretsKey to each sweep. The sweep uses that key in D1Ctx instead of a test key. A regression test checks that a sweep using the fleet’s matching key stores a nonempty backup.

Changes: D1 backup authentication

Layer / File(s) Summary
Pass the server key through the backup sweep
crates/outer/src/access.rs, crates/outer/src/secrets.rs, crates/outer/src/d1_backup.rs, crates/outer/src/main.rs
The backup task and tick accept the server’s SecretsKey, and tick uses it in D1Ctx. main.rs passes cloned keys to the task and other consumers. The test-key documentation now describes unit-test use, and a regression test checks that the matching key stores a nonempty backup.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

Merge Risk: Minimal · up to f5b2b

This change makes scheduled D1 backups authenticate with the server's real secrets key, which should fix the 401 failures. It adds a regression test, and the risk of merging looks low. After deploy, check the d1 backup stored logs to confirm the first hourly sweep succeeds.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "d1 backup sweep: authenticate with the real secrets key" matches the main change. The sweep in d1_backup.rs now receives the server's SecretsKey through spawn_backup_task and tick, replacin…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "d1 backup sweep: authenticate with the real secrets key" matches the main change. The sweep in d1_backup.rs now receives the server's SecretsKey through spawn_backup_task and tick, replacing SecretsKey::test_key(), which caused 401 errors. The test-only key restrictions in secrets.rs and access.rs support the same fix. The title is specific and concise.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9083fba2-2d1d-436c-a42c-991433d88760
Commits

Reviewing files that changed from the base of the PR and between 1e015c4 and f5b2bed.

Files selected for processing: 4

  • crates/outer/src/access.rs
  • crates/outer/src/d1_backup.rs
  • crates/outer/src/main.rs
  • crates/outer/src/secrets.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@ryuzdev

ryuzdev commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

Security review record (head f5b2bed). Independent reviewer SecOuterKey: pass, no findings.

  • No key or token leak: Debug is redacted, and the token travels only in the env after env_clear.
  • Dumps stay scoped per app under outer/backups/{slug}/.
  • test_key() is #[cfg(test)], so production can't link it.
  • The test fake genuinely enforces the operator token.

@ryuzdev
ryuzdev merged commit f26b331 into main Nov 19, 2026
5 checks passed
@ryuzdev
ryuzdev deleted the fix/d1-backup-real-key branch November 19, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant