Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Require instance-wide authority to rotate credentials by token
rcourtman9a4f75b5b77b
diff --git a/internal/api/quick_setup_token_authority_test.go b/internal/api/quick_setup_token_authority_test.gonew file mode 100644index 0000000000..c1e086900b--- /dev/null+++ b/internal/api/quick_setup_token_authority_test.go@@ -0,0 +1,97 @@+package api++import (+ "net/http"+ "net/http/httptest"+ "os"+ "strings"+ "testing"++ "github.com/rcourtman/pulse-go-rewrite/internal/config"+ internalauth "github.com/rcourtman/pulse-go-rewrite/pkg/auth"+)++// Quick setup with force rotates the administrator username and password and+// replaces every API token with a full-access one. With authentication already+// configured, a token caller therefore needs instance-wide authority:+// settings:write alone must not be enough to take the instance over.+//+// The password in these requests is deliberately too weak. A caller that+// passes the authority check is then stopped by password validation with a+// 400, before any credentials are written or a session is created, so the+// tests prove which side of the authority check a caller lands on without+// touching shared session state.+func quickSetupWithTokenCaller(t *testing.T, callerScopes []string, callerOrg string) (*config.Config, *httptest.ResponseRecorder, string) {+ t.Helper()++ dir := t.TempDir()+ hash, err := internalauth.HashPassword("OriginalPass!1")+ if err != nil {+ t.Fatalf("hash password: %v", err)+ }+ const rawCaller = "quick-setup-caller-token.12345678"+ caller, err := config.NewAPITokenRecord(rawCaller, "caller", callerScopes)+ if err != nil {+ t.Fatalf("new caller record: %v", err)+ }+ if callerOrg != "" {+ caller.OrgID = callerOrg+ }+ cfg := &config.Config{+ DataPath: dir, ConfigPath: dir,+ AuthUser: "original-admin", AuthPass: hash,+ APITokens: []config.APITokenRecord{*caller},+ }+ cfg.SortAPITokens()+ router := &Router{config: cfg, persistence: config.NewConfigPersistence(dir)}++ body := `{"username":"new-admin","password":"weak","apiToken":"` + strings.Repeat("ab", 32) + `","force":true}`+ req := httptest.NewRequest(http.MethodPost, "/api/security/quick-setup", strings.NewReader(body))+ req.RemoteAddr = "127.0.0.1:54321"+ req.Header.Set("X-API-Token", rawCaller)+ rec := httptest.NewRecorder()+ authLimiter.Reset("127.0.0.1")+ handleQuickSecuritySetupFixed(router)(rec, req)+ return cfg, rec, caller.ID+}++func TestQuickSecuritySetupRefusesTokenCallersWithoutInstanceWideAuthority(t *testing.T) {+ for _, tc := range []struct {+ name string+ scopes []string+ org string+ }{+ {"settings:write only", []string{config.ScopeSettingsWrite}, ""},+ {"settings read and write", []string{config.ScopeSettingsRead, config.ScopeSettingsWrite}, ""},+ {"full access bound to another organization", []string{config.ScopeWildcard}, "org-a"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ cfg, rec, callerID := quickSetupWithTokenCaller(t, tc.scopes, tc.org)++ if rec.Code != http.StatusForbidden {+ t.Fatalf("status = %d (%s), want 403 from the authority check", rec.Code, rec.Body.String())+ }+ if cfg.AuthUser != "original-admin" {+ t.Fatalf("administrator username changed to %q", cfg.AuthUser)+ }+ if !internalauth.CheckPasswordHash("OriginalPass!1", cfg.AuthPass) {+ t.Fatal("administrator password was replaced")+ }+ if len(cfg.APITokens) != 1 || cfg.APITokens[0].ID != callerID {+ t.Fatalf("token inventory changed: %+v", cfg.APITokens)+ }+ if _, err := os.Stat(resolveAuthEnvPath(cfg.ConfigPath)); err == nil {+ t.Fatal("a refused setup still wrote the authentication file")+ }+ })+ }+}++func TestQuickSecuritySetupLetsAnInstanceWideTokenReachValidation(t *testing.T) {+ _, rec, _ := quickSetupWithTokenCaller(t, []string{config.ScopeWildcard}, "")++ // Past the authority check, the weak password is rejected as a bad request.+ if rec.Code != http.StatusBadRequest {+ t.Fatalf("status = %d (%s), want 400 from password validation", rec.Code, rec.Body.String())+ }+}diff --git a/internal/api/security_setup_fix.go b/internal/api/security_setup_fix.goindex e0517b1247..cfd91883cb 100644--- a/internal/api/security_setup_fix.go+++ b/internal/api/security_setup_fix.go@@ -441,6 +441,19 @@ func handleQuickSecuritySetupFixed(r *Router) http.HandlerFunc {return}+ // From here quick setup sets the administrator username and password+ // and replaces every API token with a full-access one. That is+ // instance-wide authority, so an API-token caller needs it too;+ // settings:write alone must not be enough to take the instance over.+ if callerToken := getAPITokenRecordFromRequest(req); callerToken != nil && !tokenHasInstanceWideAuthority(callerToken) {+ log.Warn().+ Str("token_id", callerToken.ID).+ Str("ip", clientIP).+ Msg("API token without instance-wide authority attempted quick security setup")+ http.Error(w, "Security setup replaces the administrator credentials and every API token, so it needs a full-access token that is not bound to any non-default organization.", http.StatusForbidden)+ return+ }+if setupRequest.Force {log.Info().Msg("Quick security setup invoked with force=true - rotating credentials")}diff --git a/internal/api/security_setup_fix_additional_test.go b/internal/api/security_setup_fix_additional_test.goindex d95d571d4a..308ea42dde 100644--- a/internal/api/security_setup_fix_additional_test.go+++ b/internal/api/security_setup_fix_additional_test.go@@ -564,7 +564,7 @@ func TestQuickSecuritySetupRestoresTokensWhenPersistenceFails(t *testing.T) {t.Fatalf("hash existing password: %v", err)}rawOldToken := strings.Repeat("ab", 32)- oldToken, err := config.NewAPITokenRecord(rawOldToken, "existing token", []string{config.ScopeSettingsWrite})+ oldToken, err := config.NewAPITokenRecord(rawOldToken, "existing token", []string{config.ScopeWildcard})if err != nil {t.Fatalf("new existing token: %v", err)}diff --git a/internal/api/security_setup_settings_preservation_test.go b/internal/api/security_setup_settings_preservation_test.goindex 00962d4b89..3ef07361db 100644--- a/internal/api/security_setup_settings_preservation_test.go+++ b/internal/api/security_setup_settings_preservation_test.go@@ -20,7 +20,7 @@ func TestQuickSecuritySetupForcePreservesSystemSettings(t *testing.T) {t.Fatal(err)}token := strings.Repeat("ab", 32)- record, err := config.NewAPITokenRecord(token, "synthetic", []string{config.ScopeSettingsWrite})+ record, err := config.NewAPITokenRecord(token, "synthetic", []string{config.ScopeWildcard})if err != nil {t.Fatal(err)}