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
+20 -2
View File
@@ -91,6 +91,7 @@ type Config struct {
DatabaseURL string // postgres://…; empty on the local edition
SecretKey []byte // 32 bytes that encrypt secrets in the database; nil if not set
OldKey []byte // the key being replaced: secrets under it are encrypted again with SecretKey
Storage Storage
Mail *Mail // nil if not set
}
@@ -106,6 +107,7 @@ const (
EnvHostData = "SUPERMARKET_HOST_DATA"
EnvDatabaseURL = "SUPERMARKET_DATABASE_URL"
EnvSecretKey = "SUPERMARKET_SECRET_KEY"
EnvOldKey = "SUPERMARKET_SECRET_KEY_OLD"
EnvStorage = "SUPERMARKET_STORAGE"
EnvS3Endpoint = "SUPERMARKET_S3_ENDPOINT"
EnvS3Region = "SUPERMARKET_S3_REGION"
@@ -173,6 +175,18 @@ func Load(getenv func(string) string) (*Config, error) {
} else if shared {
bad("%s is needed on the %s edition: it encrypts the keys and tokens kept in the database. Run supermarket secret-key to make one, and keep a copy somewhere safe", EnvSecretKey, c.Edition.Name())
}
if k := get(EnvOldKey, ""); k != "" {
key, err := ParseKey(k)
switch {
case err != nil:
bad("%s: %v", EnvOldKey, err)
case c.SecretKey == nil:
bad("%s is set without %s: give the new key too", EnvOldKey, EnvSecretKey)
case string(key) == string(c.SecretKey):
bad("%s is the same key as %s", EnvOldKey, EnvSecretKey)
}
c.OldKey = key
}
// Files.
c.Storage = loadStorage(get, bad, c.DataDir)
@@ -326,10 +340,14 @@ func (c *Config) Summary() []Item {
}
items = append(items, files)
switch {
case c.SecretKey != nil:
items = append(items, Item{"Secrets", "Encrypted in the database", "With the key in " + EnvSecretKey + "."})
case c.Edition == Local:
items = append(items, Item{"Secrets", filepath.Join(c.HostData, "secrets.json"), "Readable only by you; never in backups unless you ask."})
case c.SecretKey != nil:
help := "With the key in " + EnvSecretKey + "."
if c.OldKey != nil {
help = "Moving from the key in " + EnvOldKey + " to the one in " + EnvSecretKey + ": unset " + EnvOldKey + " once Supermarket has started."
}
items = append(items, Item{"Secrets", "Encrypted in the database", help})
}
if c.Mail != nil {
items = append(items, Item{"Mail", c.Mail.From + " through " + c.Mail.Host, "The app's own mail: invitations and password resets (" + EnvMailURL + ")."})