Files
felhom.eu/hub/cmd/hub/r879_unseal_cmd_test.go
T
adminandClaude Opus 5.5 91e4ace9b8 R-879: roll-back command felhom-hub -unseal-box-secrets
Opens the four sealed box-secret columns back to plaintext (all or nothing; keeps api_key_hash;
idempotent; counts only in the log) and exits before any start-up sealing, so hub 0.137.0 can run on
the database again.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
2026-10-05 21:45:04 +02:00

59 lines
1.8 KiB
Go

package main
import (
"bytes"
"database/sql"
"encoding/hex"
"log"
"path/filepath"
"strings"
"testing"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
_ "modernc.org/sqlite"
)
// R-879 roll-back command, run the way `felhom-hub -unseal-box-secrets` runs it: key from the env, exit
// code, counts-only log. Afterwards the 0.137.0-shaped `WHERE api_key = ?` finds the box again.
func TestR879_UnsealCommand(t *testing.T) {
path := filepath.Join(t.TempDir(), "hub.db")
st, err := store.New(path, log.New(&bytes.Buffer{}, "", 0))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { st.Close() })
const hostKey = "cmd-host-key-0123456789abcdef"
if err := st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: hostKey}); err != nil {
t.Fatal(err)
}
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
defer db.Close()
oldLookup := func() string {
var id string
_ = db.QueryRow(`SELECT host_id FROM hosts WHERE api_key = ?`, hostKey).Scan(&id)
return id
}
if oldLookup() != "" {
t.Fatal("precondition: the key should be sealed")
}
var logBuf bytes.Buffer
t.Setenv("OFFSITE_SECRET_KEY", "")
if code := runUnsealBoxSecrets(st, log.New(&logBuf, "", 0)); code == 0 {
t.Fatal("unseal with no key exited 0")
}
t.Setenv("OFFSITE_SECRET_KEY", hex.EncodeToString([]byte("felhom-hub-test-only-seal-key-32")))
if code := runUnsealBoxSecrets(st, log.New(&logBuf, "", 0)); code != 0 {
t.Fatalf("unseal exit = %d, log: %s", code, logBuf.String())
}
if got := oldLookup(); got != "h1" {
t.Fatalf("0.137.0-shaped lookup after the command = %q, want h1", got)
}
if strings.Contains(logBuf.String(), hostKey) || !strings.Contains(logBuf.String(), "1 sealed value(s) opened") {
t.Fatalf("log must carry the count and never the value: %s", logBuf.String())
}
}