From a4ecc2cf1ec6d8f56a486d549bfc98b703866a5f Mon Sep 17 00:00:00 2001 From: ppenguin Date: Fri, 23 Oct 2026 14:13:23 +0200 Subject: [PATCH] fix(cli): `-ru` ends the process and needs no gateway MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - was: `casaos-user-service -ru -user ` reset the password in init(), then main() started a second service next to the running one - was: needed the gateway (management.url) → panicked on a box whose services are down, where the reset is most needed - now: `-ru` uses the account store only, exits 0 after the reset, 1 for a missing/unknown user Co-Authored-By: Claude Opus 5.5 --- main.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index e1719ad..5f40038 100644 --- a/main.go +++ b/main.go @@ -73,31 +73,41 @@ func init() { } sqliteDB := sqlite.GetDb(*dbFlag) - service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath) + // the commands below need the account store only, not the gateway: they + // run (and end) on a box whose services are down + accounts := func() service.UserService { + return service.NewUserService(sqliteDB, filepath.Join(config.AppInfo.DBPath, service.KeyFilename)) + } + + // -ru is a command: it ends the process, never goes on to start a second service if *resetUserFlag { + users := accounts() if userFlag == nil || len(*userFlag) == 0 { fmt.Println("user is empty") - return + os.Exit(1) } - userData := service.MyService.User().GetUserAllInfoByName(*userFlag) + userData := users.GetUserAllInfoByName(*userFlag) if userData.Id == 0 { fmt.Println("user not exist") - return + os.Exit(1) } password := random.RandomString(6, false) userData.Password = encryption.HashPassword(password) - service.MyService.User().UpdateUserPassword(userData) + users.UpdateUserPassword(userData) // The shell is the way back in when the authenticator and the recovery codes are both lost. userData.TotpSecret, userData.TotpEnabled, userData.TotpLastStep, userData.RecoveryCodes = "", false, 0, nil - service.MyService.User().UpdateUserTOTP(userData) + users.UpdateUserTOTP(userData) fmt.Println("User reset successful, two-factor authentication disabled") fmt.Println("UserName:" + userData.Username) fmt.Println("Password:" + password) + os.Exit(0) } + + service.MyService = service.NewService(sqliteDB, config.CommonInfo.RuntimePath) } func main() {