feat(api-keys): store a SHA-256 hash and display prefix of each key secret (DW-1007)
Merged
TO
17d ago+424+424+012345678901234567890123456789-139-139-012345678901234567890123456789
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 BYTEAandsecret_prefix TEXTtoapi_keys, with a unique index onsecret_hashbuilt concurrently (build, reindex and validate migrations). - Adds
hash_api_key(SHA-256) andapi_key_prefix(first 8 characters:sk-plus 5 random) incrypto.rs. - Every path that writes
api_keys.secretnow also writes the hash and prefix:create, the hidden-key upserts, child hidden keys,rotate_secretand 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_testsnow 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
.sqlxfiles for the changed queries were regenerated. A plaincargo sqlx prepare --workspacewould also delete test-query files.
Testing
test_every_secret_write_stores_hash_and_prefixcovers the five repository write paths;test_database_seeding_behaviorchecks the system key's hash and prefix.just lint rust,cargo test -p dwctl --lib migrations::andcargo nextest run -p dwctlpass locally.