From f4bd13bc8fdf3f06516fee5a5d9c6fb79fc6880e Mon Sep 17 00:00:00 2001 From: madejackson Date: Mon, 14 Sep 2026 18:49:30 +0000 Subject: [PATCH] fix(newInstall): stop wiping the config folder on container clean install Since v0.23.0 the users/metrics stores are SQLite files (auth.db, database.db) opened once at process start and held open for the life of the process. The clean-install step in the stepped wizard (step "-1") and /api/setup ClearConfig removed the whole /config directory in container mode while those handles were still open, so every write made afterwards - including the admin user created a few steps later - landed in unlinked inodes and was lost on the next restart. Service installs never wiped the folder, which is why they were unaffected. Fix: container clean-install now matches service installs - remove only cosmos.config.json and let the setup flow regenerate everything else (the SQLite stores, TLS keys/certs and other derived state are re-provisioned by the wizard itself, and user data is reset via DeleteAllUsersLocal before the admin is created). The open store handle stays valid, so the admin user persists. Also stops wiping /config which previously destroyed certs, keys, jetstream state and backup configs that the wizard would otherwise reuse. Add regression tests covering the fixed flow and the old failure mode. --- src/newInstall.go | 32 ++++--- src/utils/container_clean_install_test.go | 103 ++++++++++++++++++++++ 2 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 src/utils/container_clean_install_test.go diff --git a/src/newInstall.go b/src/newInstall.go index 6f20c3598..b04e349ca 100644 --- a/src/newInstall.go +++ b/src/newInstall.go @@ -75,16 +75,22 @@ func NewInstallRoute(w http.ResponseWriter, req *http.Request) { newConfig := utils.GetBaseMainConfig() if(request.Step == "-1") { - // remove everythin in /config - utils.Log("NewInstall: Step cleanup") - utils.Log("NewInstall: Removing config file") + // A "clean install" resets the server to its initial state. The only + // thing that needs to go is the configuration itself: everything that + // was derived from it (the SQLite auth.db / database.db stores, TLS + // keys & certs, nebula state, backups) gets regenerated on the next + // boot, and the wizard re-provisions it. + // + // Crucially we must NOT empty the whole config folder: since v0.23.0 + // the users store (auth.db) is opened once at process start and held + // open. Deleting /config out from under those handles would make + // every write made afterwards - including the admin user created a + // few steps later - land in unlinked inodes and vanish on restart. + // Service installs never wiped the folder, which is why they were + // unaffected; containers now behave the same way. + utils.Log("NewInstall: Step cleanup (removing config file)") configFile := utils.GetConfigFileName() os.Remove(configFile) - if(utils.IsInsideContainer) { - utils.Log("NewInstall: Emptying /config") - os.RemoveAll("/config") - os.Mkdir("/config", 0700) - } LoadConfig() } if(request.Step == "2") { @@ -255,12 +261,14 @@ func SetupRoute(w http.ResponseWriter, req *http.Request) { // ── Step 0: Clear config (optional) ────────────────────────────── if request.ClearConfig { utils.Log("Setup: Clearing config") + // Only the config file is removed, exactly like a service install. + // Wiping the whole /config folder in container mode would delete + // auth.db out from under the store handle opened at startup, orphaning + // the admin user created a few steps later (and every later write). + // The SQLite stores, TLS keys/certs and other derived state are + // regenerated by the setup flow itself. configFile := utils.GetConfigFileName() os.Remove(configFile) - if utils.IsInsideContainer { - os.RemoveAll("/config") - os.Mkdir("/config", 0700) - } LoadConfig() } diff --git a/src/utils/container_clean_install_test.go b/src/utils/container_clean_install_test.go new file mode 100644 index 000000000..8e0238054 --- /dev/null +++ b/src/utils/container_clean_install_test.go @@ -0,0 +1,103 @@ +package utils + +// This file covers the clean-install "wipe" (v0.23.0 SQLite era). +// +// auth.db is opened once at process start and held open, so any clean-install +// step that empties the whole config folder (the old container-mode +// `os.RemoveAll("/config")`) orphans the store: every write made afterwards - +// including the admin user created a few steps later - lands in unlinked +// inodes and vanishes on the next restart. +// +// The fix removes only the config file (like service installs always did), +// leaving the SQLite stores and derived state alone. The store handle stays +// valid, so the setup flow's own DeleteAllUsersLocal + CreateUser persist. + +import ( + "os" + "testing" + "time" +) + +// The fixed container clean-install flow: +// 1. InitStore() opened /auth.db at startup +// 2. clean-install removes ONLY the config file - the folder (and the open +// store) is left untouched, exactly like a service install +// 3. wizard wipes users and creates the admin through the same open handle +// 4. a later "restart" (re-InitStore) must still see the admin +func TestContainerCleanInstallWithFix(t *testing.T) { + dir := t.TempDir() + CONFIGFOLDER = dir + string(os.PathSeparator) + defer func() { CONFIGFOLDER = "/var/lib/cosmos/" }() + + if err := InitStore(); err != nil { + t.Fatalf("InitStore: %v", err) + } + + // step "-1": only cosmos.config.json is removed (same in container and service) + configFile := CONFIGFOLDER + "cosmos.config.json" + if err := os.WriteFile(configFile, []byte("{}"), 0600); err != nil { + t.Fatal(err) + } + if err := os.Remove(configFile); err != nil { + t.Fatal(err) + } + + // sanity: the folder (and therefore the open store) is still there + if _, err := os.Stat(CONFIGFOLDER); err != nil { + t.Fatalf("config folder must not be removed: %v", err) + } + + // step 4: local wipe + admin creation + if err := DeleteAllUsersLocal(); err != nil { + t.Fatalf("DeleteAllUsersLocal: %v", err) + } + if err := CreateUser(User{ + Nickname: "admin", Password: "x", Role: ADMIN, + PasswordCycle: 0, CreatedAt: time.Now(), RegisteredAt: time.Now(), + }); err != nil { + t.Fatalf("CreateUser: %v", err) + } + + // restart + if err := InitStore(); err != nil { + t.Fatalf("restart InitStore: %v", err) + } + if _, err := GetUser("admin"); err != nil { + t.Fatalf("admin user LOST after clean-install + restart: %v", err) + } + t.Log("PASS: admin user persists after config-only clean install + restart") +} + +// Documents the old failure: emptying the config folder while the store is +// open orphans the admin write. Not a fix - a regression guard explaining why +// the narrow wipe is important. +func TestContainerCleanInstallBugBaseline(t *testing.T) { + dir := t.TempDir() + CONFIGFOLDER = dir + string(os.PathSeparator) + defer func() { CONFIGFOLDER = "/var/lib/cosmos/" }() + + if err := InitStore(); err != nil { + t.Fatalf("InitStore: %v", err) + } + + // OLD container behavior: RemoveAll(CONFIGFOLDER) while store is open + os.RemoveAll(dir) + os.Mkdir(dir, 0700) + + if err := CreateUser(User{ + Nickname: "admin", Password: "x", Role: ADMIN, + PasswordCycle: 0, CreatedAt: time.Now(), RegisteredAt: time.Now(), + }); err != nil { + t.Logf("CreateUser errored (db gone) - also a symptom") + return + } + if err := InitStore(); err != nil { + t.Logf("restart InitStore fails: %v", err) + return + } + if _, err := GetUser("admin"); err == nil { + t.Logf("NOTE: user survived in this run (inode reuse)") + } else { + t.Logf("BASELINE: admin user lost with the old RemoveAll wipe (GetUser: %v)", err) + } +}