Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

Let SSO sessions carry admin privileges through the settings gate

rcourtmand4d12f328974
diff --git a/internal/api/ensure_admin_session_sso_test.go b/internal/api/ensure_admin_session_sso_test.go
new file mode 100644
index 0000000000..e551c3f62c
--- /dev/null
+++ b/internal/api/ensure_admin_session_sso_test.go
@@ -0,0 +1,107 @@
+package api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ internalauth "github.com/rcourtman/pulse-go-rewrite/pkg/auth"
+)
+
+// The privileged-session gate only recognised cfg.AuthUser, so SSO users —
+// keyed by their provider-scoped principal — could never reach settings-scoped
+// routes, even with an admin group role mapping (#1533, #1535).
+func TestEnsureAdminSessionSSOUsers(t *testing.T) {
+ dir := t.TempDir()
+ InitSessionStore(dir)
+
+ manager, err := internalauth.NewFileManager(t.TempDir())
+ if err != nil {
+ t.Fatalf("new file manager: %v", err)
+ }
+ origManager := internalauth.GetManager()
+ internalauth.SetManager(manager)
+ t.Cleanup(func() { internalauth.SetManager(origManager) })
+
+ newSessionRequest := func(t *testing.T, username string) *http.Request {
+ t.Helper()
+ token := generateSessionToken()
+ GetSessionStore().CreateSession(token, time.Hour, "agent", "127.0.0.1", username)
+ req := httptest.NewRequest(http.MethodGet, "/api/security/sso/providers", nil)
+ req.AddCookie(&http.Cookie{Name: "pulse_session", Value: token})
+ return req
+ }
+
+ const ssoAdmin = "sso:oidc:corp:abc123admin"
+ const ssoPlain = "sso:oidc:corp:def456plain"
+
+ if err := manager.UpdateUserRoles(ssoAdmin, []string{internalauth.RoleAdmin}); err != nil {
+ t.Fatalf("assign admin role: %v", err)
+ }
+ if err := manager.UpdateUserRoles(ssoPlain, nil); err != nil {
+ t.Fatalf("assign empty roles: %v", err)
+ }
+
+ cfgWithAdmin := &config.Config{AuthUser: "richard"}
+ cfgNoAdmin := &config.Config{}
+
+ tests := []struct {
+ name string
+ cfg *config.Config
+ user string
+ want bool
+ }{
+ {name: "configured local admin passes", cfg: cfgWithAdmin, user: "richard", want: true},
+ {name: "sso user with admin role passes", cfg: cfgWithAdmin, user: ssoAdmin, want: true},
+ {name: "sso user without roles is rejected when local admin exists", cfg: cfgWithAdmin, user: ssoPlain, want: false},
+ {name: "local non-admin user is rejected", cfg: cfgWithAdmin, user: "mallory", want: false},
+ {name: "sso user passes on an instance with no local admin", cfg: cfgNoAdmin, user: ssoPlain, want: true},
+ {name: "non-sso session is rejected when no local admin configured", cfg: cfgNoAdmin, user: "mallory", want: false},
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ req := newSessionRequest(t, tc.user)
+ rec := httptest.NewRecorder()
+ got := ensureAdminSession(tc.cfg, rec, req)
+ if got != tc.want {
+ t.Fatalf("ensureAdminSession(%q) = %v, want %v (status %d)", tc.user, got, tc.want, rec.Code)
+ }
+ if !tc.want && rec.Code != http.StatusForbidden {
+ t.Fatalf("expected 403 for rejected session, got %d", rec.Code)
+ }
+ })
+ }
+}
+
+func TestSessionUserHasRBACAdminGrant(t *testing.T) {
+ manager, err := internalauth.NewFileManager(t.TempDir())
+ if err != nil {
+ t.Fatalf("new file manager: %v", err)
+ }
+ origManager := internalauth.GetManager()
+ internalauth.SetManager(manager)
+ t.Cleanup(func() { internalauth.SetManager(origManager) })
+
+ if err := manager.UpdateUserRoles("adm", []string{internalauth.RoleAdmin}); err != nil {
+ t.Fatalf("assign admin: %v", err)
+ }
+ if err := manager.UpdateUserRoles("viewer", []string{internalauth.RoleViewer}); err != nil {
+ t.Fatalf("assign viewer: %v", err)
+ }
+
+ if !sessionUserHasRBACAdminGrant("adm") {
+ t.Fatal("expected admin grant for admin-role user")
+ }
+ if sessionUserHasRBACAdminGrant("viewer") {
+ t.Fatal("viewer role must not carry the admin grant")
+ }
+ if sessionUserHasRBACAdminGrant("") {
+ t.Fatal("empty username must not carry the admin grant")
+ }
+ if sessionUserHasRBACAdminGrant("nobody") {
+ t.Fatal("unassigned user must not carry the admin grant")
+ }
+}
diff --git a/internal/api/security_setup_fix.go b/internal/api/security_setup_fix.go
index 4321eed279..0a46c3405e 100644
--- a/internal/api/security_setup_fix.go
+++ b/internal/api/security_setup_fix.go
@@ -149,13 +149,21 @@ func ensureAdminSession(cfg *config.Config, w http.ResponseWriter, req *http.Req
// Org-scoped tenant sessions preserve canonical org management
// privileges for settings-bound routes.
+ orgScoped := false
if org := GetOrganization(req.Context()); org != nil {
orgID := strings.TrimSpace(org.ID)
- if orgID != "" && orgID != "default" && org.CanUserIDManage(sessionUser) {
- return true
+ if orgID != "" && orgID != "default" {
+ orgScoped = true
+ if org.CanUserIDManage(sessionUser) {
+ return true
+ }
}
}
+ if !orgScoped && sessionUserCarriesAdminPrivileges(cfg, sessionUser) {
+ return true
+ }
+
if configuredAdmin == "" || !strings.EqualFold(sessionUser, configuredAdmin) {
log.Warn().
Str("path", req.URL.Path).
@@ -168,6 +176,54 @@ func ensureAdminSession(cfg *config.Config, w http.ResponseWriter, req *http.Req
return true
}
+// sessionUserCarriesAdminPrivileges reports whether a non-org-scoped session
+// username carries instance admin privileges: the configured local admin
+// identity, an RBAC assignment granting the admin action (how SSO group role
+// mappings make an SSO user an admin, #1533/#1535), or any SSO principal when
+// the instance has no local admin configured at all — the v5 OIDC-only
+// pattern, where SSO sessions are the only administrators the instance has.
+func sessionUserCarriesAdminPrivileges(cfg *config.Config, sessionUser string) bool {
+ sessionUser = strings.TrimSpace(sessionUser)
+ if sessionUser == "" {
+ return false
+ }
+ configuredAdmin := ""
+ if cfg != nil {
+ configuredAdmin = strings.TrimSpace(cfg.AuthUser)
+ }
+ if configuredAdmin != "" && strings.EqualFold(sessionUser, configuredAdmin) {
+ return true
+ }
+ if sessionUserHasRBACAdminGrant(sessionUser) {
+ return true
+ }
+ return configuredAdmin == "" && strings.HasPrefix(sessionUser, "sso:")
+}
+
+// sessionUserHasRBACAdminGrant reports whether the user's effective RBAC
+// permissions include an allow of the admin action on all resources — the
+// shape of the built-in Administrator role that SSO group role mappings
+// assign. A missing manager or empty assignment simply reports false.
+func sessionUserHasRBACAdminGrant(username string) bool {
+ username = strings.TrimSpace(username)
+ if username == "" {
+ return false
+ }
+ manager := internalauth.GetManager()
+ if manager == nil {
+ return false
+ }
+ for _, perm := range manager.GetUserPermissions(username) {
+ if strings.EqualFold(perm.Effect, internalauth.EffectDeny) {
+ continue
+ }
+ if perm.Action == "admin" && perm.Resource == "*" {
+ return true
+ }
+ }
+ return false
+}
+
func ensureSettingsScope(cfg *config.Config, w http.ResponseWriter, req *http.Request, scope string) bool {
record := getAPITokenRecordFromRequest(req)
if record != nil {
diff --git a/internal/api/security_status_capabilities.go b/internal/api/security_status_capabilities.go
index dc48cee8ab..41a88112da 100644
--- a/internal/api/security_status_capabilities.go
+++ b/internal/api/security_status_capabilities.go
@@ -152,16 +152,25 @@ func (r *Router) buildSecurityStatusAuthSnapshot(req *http.Request) securityStat
if cookie, err := readSessionCookie(req); err == nil && cookie.Value != "" && ValidateSession(cookie.Value) {
username := strings.TrimSpace(GetSessionUsername(cookie.Value))
snapshotReq := attachUserContext(req, username)
- configuredAdmin := ""
- if r.config != nil {
- configuredAdmin = strings.TrimSpace(r.config.AuthUser)
+ // Same privilege rule as ensureAdminSession: the configured admin
+ // identity, an RBAC admin grant (SSO group role mappings), or an SSO
+ // session on an instance with no local admin. Org-scoped sessions keep
+ // their own management rules.
+ sessionIsAdmin := false
+ orgScoped := false
+ if org := GetOrganization(req.Context()); org != nil {
+ orgID := strings.TrimSpace(org.ID)
+ orgScoped = orgID != "" && orgID != "default"
+ }
+ if !orgScoped {
+ sessionIsAdmin = sessionUserCarriesAdminPrivileges(r.config, username)
}
return securityStatusAuthSnapshot{
request: snapshotReq,
authenticated: true,
authMethod: "session",
username: username,
- sessionIsAdmin: configuredAdmin != "" && strings.EqualFold(username, configuredAdmin),
+ sessionIsAdmin: sessionIsAdmin,
}
}