Review fix 3: every install deletes the off-site end record — one point, SaveAppConfig (D72)

A `.fab` import installed the app and its end record survived; the restore
that installs from a unit had the same hole. Only DeployStack deleted it.

- stacks.SaveAppConfig, the only writer of an installed app's app.yaml, calls
  noteAppInstalled when the record says deployed (a hook per stacks directory,
  registered by SetMigrationDeps). A source pin lists every place that sets
  Deployed or writes an app.yaml; a new writer fails it.
- The living check is stacks.Manager.IsLivingApp (wired in main.go): the
  protected list, the manager's list (deployed, being deployed, protected) and
  the app's record on disk — an installed app the manager does not list is
  living. Tested by behaviour.

Tests seen red before the fix; 8 mutations seen red (felhom.eu
documentation/audits/build-release-4-2026-11-15/A/redproof-review-3.txt).

Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
admin committed 2026-11-15 14:42:07 +01:00
1 parent 3928c3db28
commit c7e4ec9b5e
7 files changed
+312 -14

No files matched your search

@@ -0,0 +1,228 @@
package stacks
import (
"io"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
)
// Review of 2026-11-15, finding 3: an IMPORT installed the app and its off-site end record survived — only
// DeployStack deleted it. The rule „an install of the same app deletes the record” (`07` §8.2f) now sits at ONE
// point every install passes: SaveAppConfig, the only writer of an installed app's app.yaml. A test per path, and a
// source pin that lists the writers so a new one cannot appear unseen.
// RED-PROOF: felhom.eu/documentation/audits/build-release-4-2026-11-15/A/redproof-review-3.txt.
func endRecordManager(t *testing.T) (*Manager, *settings.Settings, string) {
t.Helper()
m := gateManager(t, gatedYml)
sett, err := settings.Load(filepath.Join(t.TempDir(), "settings.json"), log.New(io.Discard, "", 0))
if err != nil {
t.Fatal(err)
}
m.SetMigrationDeps(sett, func() bool { return false })
if _, _, err := sett.StartOffsiteEnd("gapp", time.Now().Add(-29*24*time.Hour)); err != nil {
t.Fatal(err)
}
if _, _, err := sett.StartOffsiteEnd("otherapp", time.Now().Add(-29*24*time.Hour)); err != nil {
t.Fatal(err)
}
m.mu.Lock()
dir := filepath.Dir(m.stacks["gapp"].ComposePath)
m.mu.Unlock()
return m, sett, dir
}
func assertEndRecordGone(t *testing.T, sett *settings.Settings, how string) {
t.Helper()
if rec, ok := sett.GetOffsiteEnd("gapp"); ok {
t.Fatalf("%s: the app is installed again and its off-site end record survives (%+v) — the window would delete a living app's history", how, rec)
}
if _, ok := sett.GetOffsiteEnd("otherapp"); !ok {
t.Fatalf("%s: another app's record was deleted", how)
}
}
// PATH: a `.fab` import.
func TestD72_AnImportDeletesTheEndRecord(t *testing.T) {
m, sett, dir := endRecordManager(t)
if _, err := m.SaveImportedAppConfig(dir, map[string]string{}); err != nil {
t.Fatal(err)
}
assertEndRecordGone(t, sett, "import")
}
// PATH: a restore that installs the app again from its recovery unit („use my kept data”, the off-site reinstall).
func TestD72_ARestoreIntoAnInstallDeletesTheEndRecord(t *testing.T) {
m, sett, _ := endRecordManager(t)
if err := m.PersistUnitRedeployConfig("gapp", map[string]string{"DOMAIN": "example.hu", "SUBDOMAIN": "gapp"}); err != nil {
t.Fatal(err)
}
assertEndRecordGone(t, sett, "unit restore")
}
// PATH: the data move's record write (it sets Deployed on an app.yaml it loaded).
func TestD72_ADriveFlipRecordWriteDeletesTheEndRecord(t *testing.T) {
m, sett, dir := endRecordManager(t)
if err := os.WriteFile(filepath.Join(dir, "app.yaml"), []byte("deployed: false\nenv: {}\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := m.persistDriveFlip("gapp", "/mnt/felhom-drives/x"); err != nil {
t.Fatal(err)
}
assertEndRecordGone(t, sett, "drive flip")
}
// THE ONE POINT: any write of an app.yaml that says „deployed” — whoever makes it — deletes the record; a write that
// says „not deployed” (the install's transitional record, a removal's) does not.
func TestD72_SaveAppConfigIsTheOnePoint(t *testing.T) {
m, sett, dir := endRecordManager(t)
if err := SaveAppConfig(dir, &AppConfig{Deployed: false, Env: map[string]string{}}, m.encKey, nil); err != nil {
t.Fatal(err)
}
if _, ok := sett.GetOffsiteEnd("gapp"); !ok {
t.Fatal("a „not deployed” write deleted the end record")
}
if err := SaveAppConfig(dir, &AppConfig{Deployed: true, Env: map[string]string{}}, m.encKey, nil); err != nil {
t.Fatal(err)
}
assertEndRecordGone(t, sett, "SaveAppConfig(Deployed: true)")
// a second manager (another stacks directory) is not touched by this one's writes
m2, sett2, _ := endRecordManager(t)
if err := SaveAppConfig(dir, &AppConfig{Deployed: true, Env: map[string]string{}}, m.encKey, nil); err != nil {
t.Fatal(err)
}
if _, ok := sett2.GetOffsiteEnd("gapp"); !ok {
t.Fatal("a write in ANOTHER stacks directory deleted this box's record")
}
_ = m2
}
// THE SOURCE PIN: who can make an app „installed”. Every writer of an app's app.yaml and every place that sets
// Deployed is listed here. A new one fails this test: route it through SaveAppConfig (which deletes the off-site end
// record) or say here why it is not an install.
func TestD72_TheInstallWritersAreListed(t *testing.T) {
root := filepath.Join("..", "..")
find := func(re *regexp.Regexp) []string {
var out []string
for _, top := range []string{"internal", "cmd"} {
_ = filepath.Walk(filepath.Join(root, top), func(p string, fi os.FileInfo, err error) error {
if err != nil || fi.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
return nil
}
raw, rerr := os.ReadFile(p)
if rerr != nil {
t.Fatal(rerr)
}
if n := len(re.FindAll(raw, -1)); n > 0 {
rel, _ := filepath.Rel(root, p)
out = append(out, filepath.ToSlash(rel))
}
return nil
})
}
sort.Strings(out)
return out
}
for _, c := range []struct {
what string
re string
want []string
}{
{"a struct literal that says Deployed: true", `\bDeployed:\s*true\b`, []string{
"internal/backup/recovery_unit.go", // the stripped copy INSIDE a recovery unit — not the app's own app.yaml
"internal/stacks/deploy.go", // DeployStack, PersistUnitRedeployConfigGate → SaveAppConfig
"internal/stacks/import_record.go", // SaveImportedAppConfig → SaveAppConfig
"internal/stacks/install_interrupted.go", // a comment only: it reads the record, it writes none
}},
{"an assignment cfg.Deployed = true", `\bcfg\.Deployed = true\b`, []string{
"internal/stacks/migrate.go", // persistDriveFlip → SaveAppConfig
}},
{"a direct write of an app.yaml", `WriteFile\([^\n]*"app\.yaml"`, []string{
"internal/backup/offbox_reconstitute.go", // `install_id` only, into a scratch folder — never „deployed”
}},
{"the app.yaml writer itself", `filepath\.Join\(stackDir, "app\.yaml"\)\s*\n\s*header`, []string{
"internal/stacks/deploy.go", // SaveAppConfig
}},
} {
if got := find(regexp.MustCompile(c.re)); strings.Join(got, ",") != strings.Join(c.want, ",") {
t.Errorf("%s:\n found %v\n listed %v\nA new writer must go through SaveAppConfig, or be listed here with the reason it is not an install.", c.what, got, c.want)
}
}
// and SaveAppConfig really is the point
raw, err := os.ReadFile("deploy.go")
if err != nil {
t.Fatal(err)
}
if !regexp.MustCompile(`(?s)func SaveAppConfig\(.*?noteAppInstalled\(stackDir\)`).Match(raw) {
t.Error("SaveAppConfig does not call noteAppInstalled — no install path deletes the off-site end record")
}
}
// THE LIVING CHECK, by behaviour (main.go wires exactly this method into the clean-up window): an installed app, an
// app being installed, a protected stack and an installed app the manager does NOT list are all living; a catalog
// app that is not installed and an app that left the box are not.
func TestD72_IsLivingApp(t *testing.T) {
m := gateManager(t, gatedYml)
m.cfg.Stacks.Protected = []string{"traefik"}
set := func(fn func(s *Stack)) {
m.mu.Lock()
fn(m.stacks["gapp"])
m.mu.Unlock()
}
if m.IsLivingApp("gapp") {
t.Fatal("a catalog app that is NOT installed reads as living — its end could never be carried out")
}
if m.IsLivingApp("left-the-box") {
t.Fatal("an app with no template and no record reads as living")
}
for _, c := range []struct {
name string
prep func()
app string
}{
{"installed", func() { set(func(s *Stack) { s.Deployed = true }) }, "gapp"},
{"being installed (Deploying)", func() { set(func(s *Stack) { s.Deployed, s.Deploying = false, true }) }, "gapp"},
{"marked protected in the list", func() { set(func(s *Stack) { s.Deploying, s.Protected = false, true }) }, "gapp"},
{"a protected stack by the config, not in the list", func() { set(func(s *Stack) { s.Protected = false }) }, "traefik"},
{"always-protected samba", func() {}, "samba"},
{"the empty name", func() {}, ""},
{"a path, not a name", func() {}, "../gapp"},
} {
c.prep()
if !m.IsLivingApp(c.app) {
t.Errorf("%s: %q does not read as living", c.name, c.app)
}
}
set(func(s *Stack) { s.Deployed, s.Deploying, s.Protected = false, false, false })
if m.IsLivingApp("gapp") {
t.Fatal("fixture: gapp must be not-installed again")
}
// An installed app the manager does NOT list (GetStack finds nothing): its record on disk says it is installed.
orphan := filepath.Join(m.cfg.Paths.StacksDir, "orphanapp")
if err := os.MkdirAll(orphan, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(orphan, "app.yaml"), []byte("deployed: true\nenv: {}\n"), 0o600); err != nil {
t.Fatal(err)
}
if _, listed := m.GetStack("orphanapp"); listed {
t.Fatal("fixture: orphanapp must not be in the manager's list")
}
if !m.IsLivingApp("orphanapp") {
t.Fatal("an INSTALLED app the manager does not list reads as removed — the window could end its copies")
}
if err := os.WriteFile(filepath.Join(orphan, "app.yaml"), []byte("deployed: false\nenv: {}\n"), 0o600); err != nil {
t.Fatal(err)
}
if m.IsLivingApp("orphanapp") {
t.Fatal("a record that says not deployed reads as living")
}
}
+68 -5
View File
@@ -12,6 +12,7 @@ import (
"regexp"
"sort"
"strings"
"sync"
"time"
"gitea.dooplex.hu/admin/felhom-controller/internal/appbackup"
@@ -568,11 +569,8 @@ func (m *Manager) DeployStack(req DeployRequest) (string, error) {
// D72 (`07` §8.2f): an install of the same app ENDS its pending off-site end record — the household uses the
// app again, and its old copies are restore sources again. Here, at the first write of the install: a deploy
// that was refused above changed nothing. Pinned by TestD72_AnInstallDeletesTheEndRecord.
if m.settings != nil {
if _, cerr := m.settings.CancelOffsiteEnd(req.StackName, "the app is installed again"); cerr != nil {
m.logger.Printf("[WARN] [stacks] deploy %s: its off-site end record could not be deleted (%v) — the clean-up window skips an installed app anyway", req.StackName, cerr)
}
}
// (This first write says „not deployed yet”, so SaveAppConfig's own call does not fire; the install has begun.)
m.appInstalledAgain(req.StackName)
// Debug: log final env var keys (not values)
envKeys := make([]string, 0, len(env))
@@ -1225,9 +1223,74 @@ func SaveAppConfig(stackDir string, cfg *AppConfig, encKey []byte, sensitiveVars
return fmt.Errorf("renaming %s to %s: %w", tmpPath, path, err)
}
log.Printf("[INFO] [stacks] SaveAppConfig: saved config for %s", filepath.Base(stackDir))
// D72 (`07` §8.2f; review of 2026-11-15): THE ONE POINT „the app became installed”. Every install — a deploy, a
// `.fab` import, a restore that installs from a unit, the data move's record — writes the app's record HERE with
// Deployed set, so the rule „an install of the same app deletes its off-site end record” cannot be missed by a
// path that did not exist when it was written. Pinned by TestD72_TheInstallWritersAreListed.
if cfg.Deployed {
noteAppInstalled(stackDir)
}
return nil
}
// installedHooks: per stacks directory, what to do when an app's record is written as installed (SaveAppConfig).
// Keyed by the directory, not a single global, so two managers in one process (tests) do not reach each other's box.
var (
installedHooksMu sync.RWMutex
installedHooks = map[string]func(app string){}
)
func registerInstalledHook(stacksDir string, fn func(app string)) {
installedHooksMu.Lock()
installedHooks[filepath.Clean(stacksDir)] = fn
installedHooksMu.Unlock()
}
func noteAppInstalled(stackDir string) {
installedHooksMu.RLock()
fn := installedHooks[filepath.Clean(filepath.Dir(stackDir))]
installedHooksMu.RUnlock()
if fn != nil {
fn(filepath.Base(stackDir))
}
}
// appInstalledAgain deletes app's pending off-site end record (D72): the household uses the app again, and its old
// off-site copies are restore sources again.
func (m *Manager) appInstalledAgain(app string) {
if m.settings == nil {
return
}
if _, err := m.settings.CancelOffsiteEnd(app, "the app is installed again"); err != nil {
m.logger.Printf("[WARN] [stacks] %s is installed again, and its off-site end record could not be deleted (%v) — the clean-up window skips an installed app anyway", app, err)
}
}
// IsLivingApp answers the clean-up window's question (backup.SetOffsiteEndLiving): is this app installed on the box
// right now, being installed, or one of the box's own protected stacks? Such an app's off-site copies are never
// ended and it can get no end record. It reads THREE places, because each alone has a hole:
// - the protected list (config) — an infra stack has no app.yaml and reads as not deployed;
// - the manager's own list — Deployed, Deploying, Protected;
// - the app's record ON DISK — an installed app the manager does not list (its template left the catalog, or the
// list is mid-rescan) is still installed.
//
// Pinned by TestD72_IsLivingApp.
func (m *Manager) IsLivingApp(app string) bool {
if app == "" || m.cfg.IsProtectedStack(app) {
return true
}
if st, ok := m.GetStack(app); ok && (st.Deployed || st.Deploying || st.Protected) {
return true
}
if app != filepath.Base(app) || strings.ContainsAny(app, `/\`) {
return true // not a stack name: never „removed”
}
if c := LoadAppConfig(filepath.Join(m.cfg.Paths.StacksDir, app)); c != nil && c.Deployed {
return true
}
return false
}
// LoadAppConfigDecrypted loads app.yaml and decrypts any encrypted values.
func LoadAppConfigDecrypted(stackDir string, encKey []byte) *AppConfig {
cfg := LoadAppConfig(stackDir)
+2
View File
@@ -149,6 +149,8 @@ func (m *Manager) SetMigrationDeps(sett *settings.Settings, backupRunning func()
m.mu.Lock()
defer m.mu.Unlock()
m.settings = sett
// D72: every write of an app's record as installed deletes its off-site end record (SaveAppConfig).
registerInstalledHook(m.cfg.Paths.StacksDir, m.appInstalledAgain)
m.backupRunning = backupRunning
m.sysDataPath = m.cfg.Paths.SystemDataPath
}