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
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-05 21:45:04 +02:00
1 parent e221476ff5
commit 91e4ace9b8
5 files changed
+230 -11

No files matched your search

+30
View File
@@ -136,6 +136,9 @@ func (m MailConfig) effectiveFromDomains() []string {
func main() {
configPath := flag.String("config", "/etc/felhom-hub/hub.yaml", "Path to configuration file")
showVersion := flag.Bool("version", false, "Show version and exit")
// R-879 roll-back: open the sealed box secrets back to plaintext so a pre-R-879 hub (<= 0.137.0)
// can run on this database, then exit. Same -config and OFFSITE_SECRET_KEY as the server.
unsealBox := flag.Bool("unseal-box-secrets", false, "R-879 roll-back: unseal box API keys, passphrases and PBS tokens to plaintext, then exit")
flag.Parse()
if *showVersion {
@@ -188,6 +191,11 @@ func main() {
// that is NOT in the database (Secret/offsite-secret-key). Without it the hub cannot store, read or
// use any sub-account password — provisioning and the key registrar refuse (fail-closed), and the
// legacy plaintext rows stay as they are until the key arrives.
if *unsealBox {
code := runUnsealBoxSecrets(dataStore, logger)
dataStore.Close()
os.Exit(code)
}
offsiteKeyReady := false
if v := os.Getenv("OFFSITE_SECRET_KEY"); v == "" {
logger.Printf("[ERROR] OFFSITE_SECRET_KEY unset — off-site passwords cannot be sealed; provisioning and the key registrar are DISABLED")
@@ -1015,3 +1023,25 @@ func pruneAll(s *store.Store, maxDays int, logger *log.Logger) {
logger.Printf("[INFO] Pruned %d stale app issues", n)
}
}
// runUnsealBoxSecrets is `felhom-hub -unseal-box-secrets` (R-879 roll-back). It installs the sealing key
// exactly as the server does, opens every sealed box secret back to plaintext (all or nothing), logs
// counts only, and returns the process exit code. It never starts the server and never seals anything.
func runUnsealBoxSecrets(st *store.Store, logger *log.Logger) int {
key, err := store.ParseOffsiteSecretKey(os.Getenv("OFFSITE_SECRET_KEY"))
if err != nil {
logger.Printf("[ERROR] unseal-box-secrets: OFFSITE_SECRET_KEY: %v — nothing changed", err)
return 2
}
if err := st.SetOffsiteSecretKey(key); err != nil {
logger.Printf("[ERROR] unseal-box-secrets: %v — nothing changed", err)
return 2
}
n, err := st.UnsealBoxSecrets()
if err != nil {
logger.Printf("[ERROR] unseal-box-secrets: %v", err)
return 1
}
logger.Printf("[INFO] unseal-box-secrets: %d sealed value(s) opened back to plaintext; a hub <= 0.137.0 can run on this database now", n)
return 0
}
+58
View File
@@ -0,0 +1,58 @@
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())
}
}
+18
View File
@@ -4,6 +4,8 @@ import (
"go/ast"
"go/parser"
"go/token"
"os"
"strings"
"testing"
)
@@ -27,3 +29,19 @@ func TestR879_MainSealsLegacyBoxSecrets(t *testing.T) {
t.Fatal("cmd/hub/main.go never calls SealLegacyBoxSecrets — legacy box secrets stay in plaintext")
}
}
// R-879 roll-back wiring: the -unseal-box-secrets flag must reach UnsealBoxSecrets and must exit BEFORE
// the start-up sealing runs (otherwise the command would unseal and the same process re-seal).
func TestR879_UnsealFlagIsWiredBeforeSealing(t *testing.T) {
src, err := os.ReadFile("main.go")
if err != nil {
t.Fatal(err)
}
s := string(src)
flagAt := strings.Index(s, `"unseal-box-secrets"`)
runAt := strings.Index(s, "code := runUnsealBoxSecrets(dataStore, logger)")
sealAt := strings.Index(s, "dataStore.SealLegacyBoxSecrets()")
if flagAt < 0 || runAt < 0 || sealAt < 0 || runAt > sealAt || !strings.Contains(s, "st.UnsealBoxSecrets()") {
t.Fatalf("unseal wiring broken: flag@%d run@%d seal@%d", flagAt, runAt, sealAt)
}
}