feat(api-keys): store a SHA-256 hash and display prefix of each key secret (DW-1007)

Merged
TO
17d ago
+424-139

First step of storing API keys as hashes instead of plaintext (DW-1007, part 1 of 7 for DW-1005).

What changes

  • Adds nullable secret_hash BYTEA and secret_prefix TEXT to api_keys, with a unique index on secret_hash built concurrently (build, reindex and validate migrations).
  • Adds hash_api_key (SHA-256) and api_key_prefix (first 8 characters: sk- plus 5 random) in crypto.rs.
  • Every path that writes api_keys.secret now also writes the hash and prefix: create, the hidden-key upserts, child hidden keys, rotate_secret and the system-key seed.

What doesn't change

Lookups still use secret. There's no backfill and no NOT NULL check. Pods from the previous release keep inserting rows without a hash while this rolls out, so the backfill and the switch to hash lookups ship in the next release (DW-1008), once every serving pod writes hashes.

Also in this PR

  • migrations::tariff_indexes_tests now prebuilds only the tariff indexes. It replayed every later concurrent index build against a schema that predates the columns those indexes cover, so the new index broke it.
  • Only the .sqlx files for the changed queries were regenerated. A plain cargo sqlx prepare --workspace would also delete test-query files.

Testing

  • test_every_secret_write_stores_hash_and_prefix covers the five repository write paths; test_database_seeding_behavior checks the system key's hash and prefix.
  • just lint rust, cargo test -p dwctl --lib migrations:: and cargo nextest run -p dwctl pass locally.