Phase 9, part 4: secrets in the database, encrypted

The server and multi-tenant editions keep keys and tokens in a secrets
table (schema 30, the first migration for both databases), each sealed
with AES-256-GCM under a key derived from SUPERMARKET_SECRET_KEY and bound
to its name. A wrong key stops the start with a clear message;
SUPERMARKET_SECRET_KEY_OLD reseals everything under a new key. Values are
read when asked for, so several copies of the app agree on a refreshed
token. The local edition keeps secrets.json.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 07:31:41 -07:00
co-authored by Claude Opus 5.5
parent d827d507c1
commit c76a7791ee
9 changed files with 503 additions and 8 deletions
+21
View File
@@ -1926,6 +1926,27 @@ than planned: rather than helpers in every query, the SQL stays as it is
case-insensitivity and named indexes. Checked by feeding it drift on
purpose: a type, a missing index, a missing `nocase`.
**Part 4 built 2026-11-25** on `phase9`.
- `secrets.Store` has a second home: the `secrets` table (schema 30, the
first migration for both databases), used by the server and
multi-tenant editions. The local edition keeps `secrets.json`; the table
stays empty there. Callers don't change.
- Each value is sealed with AES-256-GCM under a key derived (HKDF) from
`SUPERMARKET_SECRET_KEY`, with a new nonce each time, and bound to its
name, so a value moved to another name doesn't open.
- Each row records the key's fingerprint, never the key. Starting with the
wrong key stops the app with a message saying so, rather than looking
as if every connection were lost. Every value is opened at start-up, so
a changed one is caught then too.
- Changing keys: give the old one in `SUPERMARKET_SECRET_KEY_OLD`, and the
new one in `SUPERMARKET_SECRET_KEY`. At start, everything is sealed again
in one transaction; then unset the old key.
- The database store reads each secret when it's asked for, not from
memory, so several copies of the app see a token one of them refreshed
(X's refresh tokens work once). If the database can't be read just then,
the value last read is used.
- On Postgres, the web tests run with this store.
Nothing changes for the local user; this makes the rest possible. A
commit after each part; the whole test suite runs against both SQLite and
Postgres from part 2 on.