Add secretbox for AES-256-GCM secrets at rest
internal/secretbox decodes the standard-base64 key from MEALQUEST_SECRET_KEY_FILE, requires exactly 32 bytes, and seals secrets as nonce ‖ ciphertext with a fresh random 12-byte nonce per call. Errors are sentinels that never carry plaintext, ciphertext or key material. The server builds the box before opening the database, so a non-base64 or wrong-length key stops boot with an error naming the variable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/config"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/frontend"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/secretbox"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/server"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/internal/store"
|
||||
"code.cubecraftlabs.com/Maniacs_Mansion/Meal-Quest/migrations"
|
||||
@@ -41,6 +42,10 @@ func run(cfg config.Config, logger *slog.Logger) error {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
if _, err := openSecretBox(cfg.SecretKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := openStore(ctx, cfg.DataDir)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -86,6 +91,16 @@ func run(cfg config.Config, logger *slog.Logger) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// openSecretBox builds the secret box from the configured key, naming the
|
||||
// variable whose file holds a bad key. The key itself is never reported.
|
||||
func openSecretBox(key config.Secret) (*secretbox.Box, error) {
|
||||
box, err := secretbox.New(key.Reveal())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", config.EnvSecretKeyFile, err)
|
||||
}
|
||||
return box, nil
|
||||
}
|
||||
|
||||
// bootMigrations is the migration set applied at boot. Tests replace it to
|
||||
// prove a failing migration stops the boot.
|
||||
var bootMigrations fs.FS = migrations.FS
|
||||
|
||||
Reference in New Issue
Block a user