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
+11 -1
View File
@@ -179,7 +179,17 @@ func run(log *slog.Logger) error {
log.Error("writing the CRM state file", "err", err)
}
sec, err := secrets.Open(filepath.Join(dataDir, "secrets.json"))
// The local edition keeps its keys and tokens in a file beside the
// database; the others, in the database, encrypted.
var sec *secrets.Store
if cfg.Edition == config.Local {
sec, err = secrets.Open(filepath.Join(dataDir, "secrets.json"))
} else {
sec, err = secrets.OpenDB(database, cfg.SecretKey, cfg.OldKey)
if err == nil && cfg.OldKey != nil {
log.Warn("secrets: encrypted with the new key; unset " + config.EnvOldKey)
}
}
if err != nil {
return err
}