feat(api-keys): backfill secret_hash and look API keys up by hash (DW-1008)

Merged
TO
16d ago
+537-224

Second step of storing API keys as hashes (DW-1008, part 2 of 7 for DW-1005). #2138 is merged and in production.

Must ship in the release after #2138 is fully rolled out. The backfill relies on every serving pod already writing secret_hash.

What changes

Migrations

  • 20261029130000: backfills secret_hash and secret_prefix with SQL sha256. -- no-transaction; walks the primary key 5000 rows at a time, committing each batch with a 5s lock timeout, so locks stay short at any table size and a failed run resumes where it stopped.
  • 20261029130010/130020: CHECK (secret_hash IS NOT NULL) NOT VALID, hash any rows inserted between the backfill and the check, then VALIDATE.
  • 20261029130030: the api_keys NOTIFY trigger fires on secret_hash instead of secret.

Lookups by hash — repository lookups (get_user_id_by_secret, get_user_info_by_secret), admin API key auth, error enrichment, images, ai_models, the inference handler, store and middleware, and the key_policy cache. Authentication lookups run on the primary pool, so a just-created or just-deleted key isn't judged from a lagging replica. The onwards routing cache moves to hashes in the follow-up PR stacked on this one; until then it still builds from secret, which is fine because plaintext is still stored.

In-process memos — the prompt-cache principal resolver and the continuation purpose resolver are keyed by token hash instead of the token.

Plaintext secret is still written and kept; it's removed in DW-1012 and DW-1013.

Rollout

  • Pods from #2138 and this PR work side by side: every row has both columns and both releases write both.
  • After this migration, rolling back to a release from before #2138 breaks key creation (that code writes no hash). Rolling back to #2138 is safe.

Testing

  • The SQL hash equals hash_api_key byte for byte (12k rows, the system key, a non-ASCII secret); rows with only secret authenticate after the backfill; the check rejects hashless writes; the trigger fires on the hash only.
  • cargo nextest run -p dwctl -p onwards (3644 passed), cargo test -p dwctl --lib migrations::, and just lint rust pass locally.