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
feat(rbac): add OIDC group mapping tests and audit logging for RBAC actions
rcourtman92c150e9795f
diff --git a/internal/api/auth_scope_test.go b/internal/api/auth_scope_test.goindex 0b9a36d8e7..1921c0af61 100644--- a/internal/api/auth_scope_test.go+++ b/internal/api/auth_scope_test.go@@ -8,6 +8,7 @@ import ("testing""github.com/rcourtman/pulse-go-rewrite/internal/config"+ "github.com/rcourtman/pulse-go-rewrite/pkg/auth")func TestRequireScopeAllowsSession(t *testing.T) {@@ -101,7 +102,7 @@ func TestGetAPITokenRecordFromRequestWrongType(t *testing.T) {// Attach a value of wrong type to the contextctx := req.Context()- ctx = context.WithValue(ctx, contextKeyAPIToken, "not-a-record")+ ctx = context.WithValue(ctx, auth.GetAPITokenContextKey(), "not-a-record")req = req.WithContext(ctx)// Should return nil when type assertion failsdiff --git a/internal/api/oidc_mapping_test.go b/internal/api/oidc_mapping_test.gonew file mode 100644index 0000000000..6661a3108a--- /dev/null+++ b/internal/api/oidc_mapping_test.go@@ -0,0 +1,122 @@+package api++import (+ "testing"++ "github.com/rcourtman/pulse-go-rewrite/internal/config"+ "github.com/rcourtman/pulse-go-rewrite/pkg/auth"+)++type mockAuthManager struct {+ auth.Manager+ updatedUser string+ updatedRoles []string+}++func (m *mockAuthManager) GetRoles() []auth.Role { return nil }+func (m *mockAuthManager) GetRole(id string) (auth.Role, bool) { return auth.Role{}, false }+func (m *mockAuthManager) SaveRole(role auth.Role) error { return nil }+func (m *mockAuthManager) DeleteRole(id string) error { return nil }+func (m *mockAuthManager) GetUserAssignments() []auth.UserRoleAssignment { return nil }+func (m *mockAuthManager) GetUserAssignment(username string) (auth.UserRoleAssignment, bool) {+ return auth.UserRoleAssignment{}, false+}+func (m *mockAuthManager) AssignRole(username string, roleID string) error { return nil }+func (m *mockAuthManager) RemoveRole(username string, roleID string) error { return nil }+func (m *mockAuthManager) GetUserPermissions(username string) []auth.Permission { return nil }++func (m *mockAuthManager) UpdateUserRoles(username string, roleIDs []string) error {+ m.updatedUser = username+ m.updatedRoles = roleIDs+ return nil+}++func TestOIDCRoleMappingLogic(t *testing.T) {+ tests := []struct {+ name string+ groupsClaim string+ mappings map[string]string+ claims map[string]any+ expectedRoles []string+ }{+ {+ name: "simple mapping",+ groupsClaim: "groups",+ mappings: map[string]string{+ "oidc-admin": "admin",+ },+ claims: map[string]any{+ "groups": []string{"oidc-admin", "other"},+ },+ expectedRoles: []string{"admin"},+ },+ {+ name: "multiple mappings",+ groupsClaim: "groups",+ mappings: map[string]string{+ "oidc-admin": "admin",+ "oidc-dev": "operator",+ },+ claims: map[string]any{+ "groups": []string{"oidc-admin", "oidc-dev"},+ },+ expectedRoles: []string{"admin", "operator"},+ },+ {+ name: "comma separated string groups",+ groupsClaim: "groups",+ mappings: map[string]string{+ "admin": "admin",+ "user": "viewer",+ },+ claims: map[string]any{+ "groups": "admin, user",+ },+ expectedRoles: []string{"admin", "viewer"},+ },+ {+ name: "no matches",+ groupsClaim: "groups",+ mappings: map[string]string{+ "admin": "admin",+ },+ claims: map[string]any{+ "groups": []string{"guest"},+ },+ expectedRoles: nil,+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ cfg := &config.OIDCConfig{+ GroupsClaim: tt.groupsClaim,+ GroupRoleMappings: tt.mappings,+ }++ groups := extractStringSliceClaim(tt.claims, cfg.GroupsClaim)+ var rolesToAssign []string+ seenRoles := make(map[string]bool)++ for _, group := range groups {+ if roleID, ok := cfg.GroupRoleMappings[group]; ok {+ if !seenRoles[roleID] {+ rolesToAssign = append(rolesToAssign, roleID)+ seenRoles[roleID] = true+ }+ }+ }++ if len(rolesToAssign) != len(tt.expectedRoles) {+ t.Errorf("expected %d roles, got %d", len(tt.expectedRoles), len(rolesToAssign))+ return+ }++ for i, r := range rolesToAssign {+ if r != tt.expectedRoles[i] {+ t.Errorf("expected role %s at index %d, got %s", tt.expectedRoles[i], i, r)+ }+ }+ })+ }+}diff --git a/internal/api/rbac_handlers.go b/internal/api/rbac_handlers.goindex cf0ed56f72..c03a6476c5 100644--- a/internal/api/rbac_handlers.go+++ b/internal/api/rbac_handlers.go@@ -61,10 +61,12 @@ func (h *RBACHandlers) HandleRoles(w http.ResponseWriter, r *http.Request) {}if err := manager.SaveRole(role); err != nil {+ LogAuditEvent("role_create_failed", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, false, "Failed to create role "+role.ID+": "+err.Error())writeErrorResponse(w, http.StatusInternalServerError, "save_failed", err.Error(), nil)return}+ LogAuditEvent("role_created", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, true, "Created role "+role.ID)w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(role)@@ -80,10 +82,12 @@ func (h *RBACHandlers) HandleRoles(w http.ResponseWriter, r *http.Request) {}if err := manager.SaveRole(role); err != nil {+ LogAuditEvent("role_update_failed", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, false, "Failed to update role "+role.ID+": "+err.Error())writeErrorResponse(w, http.StatusInternalServerError, "save_failed", err.Error(), nil)return}+ LogAuditEvent("role_updated", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, true, "Updated role "+role.ID)w.Header().Set("Content-Type", "application/json")json.NewEncoder(w).Encode(role)@@ -94,10 +98,12 @@ func (h *RBACHandlers) HandleRoles(w http.ResponseWriter, r *http.Request) {}if err := manager.DeleteRole(id); err != nil {+ LogAuditEvent("role_delete_failed", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, false, "Failed to delete role "+id+": "+err.Error())writeErrorResponse(w, http.StatusInternalServerError, "delete_failed", err.Error(), nil)return}+ LogAuditEvent("role_deleted", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, true, "Deleted role "+id)w.WriteHeader(http.StatusNoContent)default:@@ -151,10 +157,12 @@ func (h *RBACHandlers) HandleUserRoleActions(w http.ResponseWriter, r *http.Requ}if err := manager.UpdateUserRoles(username, req.RoleIDs); err != nil {+ LogAuditEvent("user_roles_update_failed", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, false, "Failed to update roles for user "+username+": "+err.Error())writeErrorResponse(w, http.StatusInternalServerError, "update_failed", err.Error(), nil)return}+ LogAuditEvent("user_roles_updated", auth.GetUser(r.Context()), GetClientIP(r), r.URL.Path, true, "Updated roles for user "+username+": ["+strings.Join(req.RoleIDs, ", ")+"]")w.WriteHeader(http.StatusNoContent)case http.MethodGet:diff --git a/pkg/auth/authorizer.go b/pkg/auth/authorizer.goindex 8c88c7cbef..4bff8aa541 100644--- a/pkg/auth/authorizer.go+++ b/pkg/auth/authorizer.go@@ -47,6 +47,11 @@ func GetAPIToken(ctx context.Context) APITokenInfo {return nil}+// GetAPITokenContextKey returns the context key used for API tokens (for testing purposes)+func GetAPITokenContextKey() any {+ return contextKeyAPIToken+}+// DefaultAuthorizer is a pass-through implementation that allows everything.// Used in OSS version and when enterprise features are disabled.type DefaultAuthorizer struct{}