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:
@@ -1,5 +1,7 @@
|
||||
// Package secrets keeps API keys and tokens in one file, readable only by the
|
||||
// user, outside the database so a copy of the database holds no credentials.
|
||||
// Package secrets keeps API keys and tokens. The local edition keeps them
|
||||
// in one file, readable only by the user, outside the database so a copy
|
||||
// of the database holds no credentials. The server and multi-tenant
|
||||
// editions keep them in the database, encrypted (database.go).
|
||||
package secrets
|
||||
|
||||
import (
|
||||
@@ -10,6 +12,7 @@ import (
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/fileutil"
|
||||
)
|
||||
|
||||
@@ -62,11 +65,19 @@ const (
|
||||
RedditUsername = "reddit.username"
|
||||
)
|
||||
|
||||
// Store is the secrets file. It is safe for concurrent use.
|
||||
// Store is the secrets: a file, or the database. It is safe for
|
||||
// concurrent use.
|
||||
type Store struct {
|
||||
mu sync.Mutex
|
||||
|
||||
// The file (local edition)
|
||||
path string
|
||||
mu sync.Mutex
|
||||
vals map[string]string
|
||||
|
||||
// The database (server and multi-tenant editions)
|
||||
d *db.DB
|
||||
key *sealer
|
||||
cache map[string]string // the values last read, for when the database can't be
|
||||
}
|
||||
|
||||
// Open loads the secrets file at path, or starts empty if there is none. An
|
||||
@@ -91,6 +102,9 @@ func Open(path string) (*Store, error) {
|
||||
|
||||
// Get returns the secret name, or "" if it isn't set.
|
||||
func (s *Store) Get(name string) string {
|
||||
if s.d != nil {
|
||||
return s.dbGet(name)
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.vals[name]
|
||||
@@ -104,6 +118,9 @@ func (s *Store) Has(name string) bool {
|
||||
// Set stores the given secrets (an empty value removes one) and saves the
|
||||
// file. On a failed save nothing changes in memory either.
|
||||
func (s *Store) Set(updates map[string]string) error {
|
||||
if s.d != nil {
|
||||
return s.dbSet(updates)
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
next := make(map[string]string, len(s.vals)+len(updates))
|
||||
@@ -131,6 +148,9 @@ func (s *Store) Set(updates map[string]string) error {
|
||||
// Names lists the secrets that are set, for status pages. It never returns
|
||||
// values.
|
||||
func (s *Store) Names() []string {
|
||||
if s.d != nil {
|
||||
return s.dbNames()
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
names := make([]string, 0, len(s.vals))
|
||||
|
||||
Reference in New Issue
Block a user