Files
supermarket/internal/secrets/database_test.go
T
davidandClaude Opus 5.5 c76a7791ee 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>
2026-11-25 07:31:41 -07:00

107 lines
3.1 KiB
Go

package secrets
import (
"path/filepath"
"strings"
"testing"
"supermarket/internal/db"
)
var (
keyA = []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
keyB = []byte("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")
)
func openDB(t *testing.T) *db.DB {
t.Helper()
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { d.Close() })
return d
}
func TestDatabaseStore(t *testing.T) {
d := openDB(t)
s, err := OpenDB(d, keyA, nil)
if err != nil {
t.Fatal(err)
}
if err := s.Set(map[string]string{StripeAPIKey: "rk_live_123", GiteaToken: "gt-456"}); err != nil {
t.Fatal(err)
}
if s.Get(StripeAPIKey) != "rk_live_123" || !s.Has(GiteaToken) || s.Has(BingAPIKey) {
t.Fatal("not stored")
}
if got := s.Names(); strings.Join(got, ",") != GiteaToken+","+StripeAPIKey {
t.Errorf("names: %v", got)
}
// Encrypted: the database holds no value, only sealed ones, each
// different even for the same text.
var raw string
d.QueryRow(`SELECT group_concat(value, ' ') FROM secrets`).Scan(&raw)
if d.Dialect == db.Postgres {
d.QueryRow(`SELECT string_agg(value, ' ') FROM secrets`).Scan(&raw)
}
if strings.Contains(raw, "rk_live") || strings.Contains(raw, "gt-456") || !strings.HasPrefix(raw, "v1:") {
t.Fatalf("stored as %q", raw)
}
s.Set(map[string]string{BingAPIKey: "same", PlausibleAPIKey: "same"})
var a, b string
d.QueryRow(`SELECT value FROM secrets WHERE name = ?`, BingAPIKey).Scan(&a)
d.QueryRow(`SELECT value FROM secrets WHERE name = ?`, PlausibleAPIKey).Scan(&b)
if a == b {
t.Error("the same value sealed the same way twice")
}
// A value moved to another name doesn't open.
d.Exec(`UPDATE secrets SET value = ? WHERE name = ?`, a, PlausibleAPIKey)
if _, err := OpenDB(d, keyA, nil); err == nil || !strings.Contains(err.Error(), PlausibleAPIKey) {
t.Errorf("a swapped value was accepted: %v", err)
}
s.Set(map[string]string{PlausibleAPIKey: ""}) // removed
if s.Has(PlausibleAPIKey) {
t.Error("not removed")
}
// Another copy of the app sees a token this one refreshed.
other, err := OpenDB(d, keyA, nil)
if err != nil {
t.Fatal(err)
}
other.Get(GiteaToken)
s.Set(map[string]string{GiteaToken: "gt-789"})
if other.Get(GiteaToken) != "gt-789" {
t.Error("the other copy kept the old token")
}
}
func TestDatabaseStoreKeys(t *testing.T) {
d := openDB(t)
s, _ := OpenDB(d, keyA, nil)
s.Set(map[string]string{XAccessToken: "x-1", XRefreshToken: "x-2"})
// The wrong key: refused at the start, saying so.
if _, err := OpenDB(d, keyB, nil); err == nil || !strings.Contains(err.Error(), "another key") {
t.Fatalf("wrong key: %v", err)
}
// Changing keys: the old one given, every secret is sealed again.
s, err := OpenDB(d, keyB, keyA)
if err != nil {
t.Fatal(err)
}
if s.Get(XAccessToken) != "x-1" || s.Get(XRefreshToken) != "x-2" {
t.Fatal("lost in the change")
}
if _, err := OpenDB(d, keyB, nil); err != nil {
t.Errorf("after the change, the new key alone: %v", err)
}
if _, err := OpenDB(d, keyA, nil); err == nil {
t.Error("the old key still opens them")
}
if _, err := OpenDB(d, []byte("short"), nil); err == nil {
t.Error("a short key was accepted")
}
}