Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/newInstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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()
}

Expand Down
103 changes: 103 additions & 0 deletions src/utils/container_clean_install_test.go
Original file line number Diff line number Diff line change
@@ -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 <CONFIGFOLDER>/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)
}
}