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.

Preserve live SSO bindings across presentation-only edits

pulse-triage[bot]dbfb8605ade1
diff --git a/internal/api/identity_sso_handlers.go b/internal/api/identity_sso_handlers.go
index 3f6f572a5d..3ba2abcdab 100644
--- a/internal/api/identity_sso_handlers.go
+++ b/internal/api/identity_sso_handlers.go
@@ -580,14 +580,20 @@ func (r *Router) handleUpdateSSOProvider(w http.ResponseWriter, req *http.Reques
}
}
+ // Presentation-only saves must not discard the live service's pending
+ // login/logout bindings. Compare the effective config, after preservation.
+ presentationOnly := ssoPresentationOnlyUpdate(*existing, updated)
+
// Re-initialize SAML provider if applicable
if updated.Type == config.SSOProviderTypeSAML && updated.SAML != nil {
if updated.Enabled {
if err := r.syncSAMLPublicURL(); err != nil {
log.Warn().Err(err).Str("provider_id", updated.ID).Msg("Failed to synchronize SAML public URL before provider re-init")
}
- if err := r.samlManager.InitializeProvider(req.Context(), updated.ID, updated.SAML); err != nil {
- log.Warn().Err(err).Str("provider_id", updated.ID).Msg("Failed to re-initialize SAML provider")
+ if !presentationOnly || r.samlManager.GetService(updated.ID) == nil {
+ if err := r.samlManager.InitializeProvider(req.Context(), updated.ID, updated.SAML); err != nil {
+ log.Warn().Err(err).Str("provider_id", updated.ID).Msg("Failed to re-initialize SAML provider")
+ }
}
} else {
r.samlManager.RemoveProvider(updated.ID)
@@ -597,8 +603,10 @@ func (r *Router) handleUpdateSSOProvider(w http.ResponseWriter, req *http.Reques
// Re-initialize OIDC provider if applicable
if updated.Type == config.SSOProviderTypeOIDC {
if updated.Enabled && updated.OIDC != nil {
- if err := r.oidcManager.InitializeProvider(req.Context(), updated.ID, &updated, ""); err != nil {
- log.Warn().Err(err).Str("provider_id", updated.ID).Msg("Failed to re-initialize OIDC provider")
+ if !presentationOnly || r.oidcManager.GetService(updated.ID) == nil {
+ if err := r.oidcManager.InitializeProvider(req.Context(), updated.ID, &updated, ""); err != nil {
+ log.Warn().Err(err).Str("provider_id", updated.ID).Msg("Failed to re-initialize OIDC provider")
+ }
}
} else {
r.oidcManager.RemoveService(updated.ID)
diff --git a/internal/api/sso_presentation_update.go b/internal/api/sso_presentation_update.go
new file mode 100644
index 0000000000..c9aafc079a
--- /dev/null
+++ b/internal/api/sso_presentation_update.go
@@ -0,0 +1,57 @@
+package api
+
+import (
+ "reflect"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// ssoPresentationOnlyUpdate compares the entire effective provider except its
+// four presentation fields. New protocol or policy fields therefore require
+// initialization by default. Lifecycle changes never reuse an enabled service.
+func ssoPresentationOnlyUpdate(existing, updated config.SSOProvider) bool {
+ if existing.Type != updated.Type || !existing.Enabled || !updated.Enabled {
+ return false
+ }
+ switch existing.Type {
+ case config.SSOProviderTypeOIDC:
+ if existing.OIDC == nil || updated.OIDC == nil {
+ return false
+ }
+ case config.SSOProviderTypeSAML:
+ if existing.SAML == nil || updated.SAML == nil {
+ return false
+ }
+ default:
+ return false
+ }
+ normalize := func(p config.SSOProvider) config.SSOProvider {
+ p.Name, p.DisplayName, p.IconURL, p.Priority = "", "", "", 0
+ // Empty API collections and omitted persisted collections are the same
+ // policy. Work on copies so comparison cannot mutate either snapshot.
+ if len(p.AllowedGroups) == 0 {
+ p.AllowedGroups = nil
+ }
+ if len(p.AllowedDomains) == 0 {
+ p.AllowedDomains = nil
+ }
+ if len(p.AllowedEmails) == 0 {
+ p.AllowedEmails = nil
+ }
+ if len(p.GroupRoleMappings) == 0 {
+ p.GroupRoleMappings = nil
+ }
+ if p.OIDC != nil {
+ oidc := *p.OIDC
+ // This response marker is not a credential. The actual secret,
+ // merged before comparison, remains significant.
+ oidc.ClientSecretSet = oidc.ClientSecret != ""
+ if len(oidc.Scopes) == 0 {
+ oidc.Scopes = nil
+ }
+ p.OIDC = &oidc
+ }
+ return p
+ }
+ return reflect.DeepEqual(normalize(existing), normalize(updated))
+}
diff --git a/internal/api/sso_presentation_update_test.go b/internal/api/sso_presentation_update_test.go
new file mode 100644
index 0000000000..5be76f0bb2
--- /dev/null
+++ b/internal/api/sso_presentation_update_test.go
@@ -0,0 +1,329 @@
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/stretchr/testify/require"
+)
+
+func TestSSOPresentationComparison(t *testing.T) {
+ for _, kind := range []config.SSOProviderType{config.SSOProviderTypeOIDC, config.SSOProviderTypeSAML} {
+ t.Run(string(kind), func(t *testing.T) {
+ original := unrepresentedSSOFixture(kind)
+ original.Enabled = true
+ if kind == config.SSOProviderTypeOIDC {
+ original.SAML = nil
+ } else {
+ original.OIDC = nil
+ }
+ clone := func() config.SSOProvider {
+ return (&config.SSOConfig{Providers: []config.SSOProvider{original}}).Clone().Providers[0]
+ }
+ // Enumerate all fields, including future additions. Only the four
+ // presentation fields may avoid initialization; protocol fields,
+ // credentials, trust, claims and policies must stay significant.
+ var visit func(reflect.Value, string)
+ visit = func(v reflect.Value, prefix string) {
+ for i := 0; i < v.NumField(); i++ {
+ field := v.Type().Field(i)
+ name := prefix + field.Name
+ if v.Field(i).Kind() == reflect.Pointer && !v.Field(i).IsNil() {
+ visit(v.Field(i).Elem(), name+".")
+ continue
+ }
+ if name == "OIDC.ClientSecretSet" {
+ continue // presence marker, never a credential
+ }
+ t.Run(name, func(t *testing.T) {
+ updated := clone()
+ target := reflect.ValueOf(&updated).Elem()
+ for _, part := range strings.Split(name, ".") {
+ if target.Kind() == reflect.Pointer {
+ target = target.Elem()
+ }
+ target = target.FieldByName(part)
+ }
+ switch target.Kind() {
+ case reflect.String:
+ target.SetString(target.String() + "changed")
+ case reflect.Bool:
+ target.SetBool(!target.Bool())
+ case reflect.Int:
+ target.SetInt(target.Int() + 1)
+ case reflect.Slice:
+ target.Set(reflect.ValueOf([]string{"changed"}))
+ case reflect.Map:
+ if target.Type().Elem().Kind() == reflect.Bool {
+ target.Set(reflect.ValueOf(map[string]bool{"changed": true}))
+ } else {
+ target.Set(reflect.ValueOf(map[string]string{"changed": "viewer"}))
+ }
+ case reflect.Pointer:
+ target.Set(reflect.New(target.Type().Elem()))
+ default:
+ t.Fatalf("add a boundary control for new field %s", name)
+ }
+ want := name == "Name" || name == "DisplayName" || name == "IconURL" || name == "Priority"
+ require.Equal(t, want, ssoPresentationOnlyUpdate(original, updated))
+ })
+ }
+ }
+ visit(reflect.ValueOf(original), "")
+ before := clone()
+ updated := clone()
+ original.AllowedEmails, updated.AllowedEmails = nil, []string{}
+ original.GroupRoleMappings, updated.GroupRoleMappings = nil, map[string]string{}
+ if original.OIDC != nil {
+ original.OIDC.Scopes, updated.OIDC.Scopes = nil, []string{}
+ updated.OIDC.ClientSecretSet = false
+ }
+ require.True(t, ssoPresentationOnlyUpdate(original, updated), "API empty collections must not look like a policy change")
+ require.Empty(t, original.AllowedEmails)
+ require.Equal(t, before.SAML, original.SAML)
+ updated.Enabled = false
+ require.False(t, ssoPresentationOnlyUpdate(original, updated))
+ original.Enabled = false
+ require.False(t, ssoPresentationOnlyUpdate(original, updated))
+ })
+ }
+}
+
+type presentationFixture struct {
+ router *Router
+ provider config.SSOProvider
+ requests atomic.Int32
+ offline atomic.Bool
+}
+
+// Both protocols use real service initialization and a guest-local metadata
+// endpoint. All credentials, certificates and identities are synthetic.
+func newPresentationFixture(t *testing.T, kind config.SSOProviderType) *presentationFixture {
+ t.Helper()
+ r, _ := setupTestRouter(t)
+ r.config.PublicURL = "https://pulse.example.test"
+ require.NoError(t, r.syncSAMLPublicURL())
+ f := &presentationFixture{router: r}
+ server := newIPv4HTTPServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
+ f.requests.Add(1)
+ if f.offline.Load() {
+ http.Error(w, "synthetic metadata outage", http.StatusServiceUnavailable)
+ return
+ }
+ if kind == config.SSOProviderTypeSAML {
+ w.Header().Set("Content-Type", "application/xml")
+ fmt.Fprint(w, `<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="https://identity.example.test"><IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol"><SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://identity.example.test/sso"/><SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://identity.example.test/slo"/></IDPSSODescriptor></EntityDescriptor>`)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ base := "http://" + req.Host
+ require.NoError(t, json.NewEncoder(w).Encode(map[string]string{
+ "issuer": base, "authorization_endpoint": base + "/authorize",
+ "token_endpoint": base + "/token", "jwks_uri": base + "/jwks",
+ }))
+ }))
+ t.Cleanup(server.Close)
+ p := config.SSOProvider{ID: "presentation", Name: "Original " + strings.ToUpper(string(kind)), Type: kind, Enabled: true,
+ AllowedGroups: []string{"Operators"}, AllowedDomains: []string{"example.test"},
+ GroupsClaim: "groups", GroupRoleMappings: map[string]string{"Operators": "viewer"}}
+ if kind == config.SSOProviderTypeSAML {
+ cert, key, _ := generateTestCert(t)
+ p.SAML = &config.SAMLProviderConfig{IDPMetadataURL: server.URL + "/metadata", IDPSSOURL: "https://identity.example.test/sso", IDPEntityID: "https://identity.example.test",
+ SPEntityID: "pulse", SPCertificate: string(cert), SPPrivateKey: string(key), SignRequests: true,
+ UsernameAttr: "uid", EmailAttr: "mail", GroupsAttr: "groups", WantAssertionsSigned: true}
+ require.NoError(t, r.samlManager.InitializeProvider(t.Context(), p.ID, p.SAML))
+ } else {
+ p.OIDC = &config.OIDCProviderConfig{IssuerURL: server.URL, ClientID: "pulse", ClientSecret: "synthetic-presentation-secret", ClientSecretSet: true, Scopes: []string{"openid", "email"}}
+ require.NoError(t, r.oidcManager.InitializeProvider(t.Context(), p.ID, &p, ""))
+ t.Cleanup(func() { r.oidcManager.RemoveService(p.ID) })
+ }
+ require.NoError(t, r.ssoConfig.AddProvider(p))
+ r.ssoConfig.DefaultProviderID = p.ID
+ require.NoError(t, r.saveSSOConfig())
+ f.provider = p
+ return f
+}
+
+func presentationPut(t *testing.T, r *Router, payload interface{}) *httptest.ResponseRecorder {
+ t.Helper()
+ data, err := json.Marshal(payload)
+ require.NoError(t, err)
+ rec := httptest.NewRecorder()
+ r.handleSSOProvider(rec, httptest.NewRequest("PUT", "/api/security/sso/providers/presentation", bytes.NewReader(data)))
+ return rec
+}
+
+func TestSSOPresentationSavePreservesBindings(t *testing.T) {
+ for _, kind := range []config.SSOProviderType{config.SSOProviderTypeSAML, config.SSOProviderTypeOIDC} {
+ for _, offline := range []bool{false, true} {
+ for _, failSave := range []bool{false, true} {
+ t.Run(fmt.Sprintf("%s/offline=%t/failSave=%t", kind, offline, failSave), func(t *testing.T) {
+ f := newPresentationFixture(t, kind)
+ r, p := f.router, f.provider
+ priorSnapshot := r.ensureSSOConfig()
+ before := priorSnapshot.Clone()
+ file := filepath.Join(r.config.DataPath, "sso.enc")
+ diskBefore, err := os.ReadFile(file)
+ require.NoError(t, err)
+ var samlService *SAMLService
+ var oidcService *OIDCService
+ var loginID, logoutID, state string
+ if kind == config.SSOProviderTypeSAML {
+ samlService = r.samlManager.GetService(p.ID)
+ _, err := samlService.MakeAuthRequest("/history", "original-browser")
+ require.NoError(t, err)
+ ids, bound := samlService.authnRequests.outstanding("original-browser", time.Now())
+ require.True(t, bound)
+ require.Len(t, ids, 1)
+ loginID = ids[0]
+ _, err = samlService.MakeLogoutRequest("synthetic-subject", "synthetic-index", "original-session")
+ require.NoError(t, err)
+ ids, bound = samlService.logoutRequests.outstanding("original-session", time.Now())
+ require.True(t, bound)
+ require.Len(t, ids, 1)
+ logoutID = ids[0]
+ } else {
+ oidcService = r.oidcManager.GetService(p.ID)
+ state, _, err = oidcService.newStateEntry(p.ID, "/history")
+ require.NoError(t, err)
+ }
+ // Use the non-secret editor DTO: blank OIDC secrets and omitted
+ // advanced SAML settings must merge before runtime comparison.
+ detail := providerToDetailResponse(&p, r.config.PublicURL, httptest.NewRequest("GET", "/", nil))
+ payload := detail
+ payload.Name = "Renamed " + strings.ToUpper(string(kind))
+ f.offline.Store(offline)
+ if failSave {
+ // A regular-file obstruction forces a real persistence error
+ // without discarding the original encrypted file.
+ blocker := filepath.Join(t.TempDir(), "not-a-directory")
+ require.NoError(t, os.WriteFile(blocker, []byte("fixture"), 0600))
+ r.persistence = config.NewConfigPersistence(blocker)
+ }
+ rec := presentationPut(t, r, payload)
+ wantStatus := http.StatusOK
+ if failSave {
+ wantStatus = http.StatusInternalServerError
+ }
+ require.Equal(t, wantStatus, rec.Code, rec.Body.String())
+ require.Equal(t, int32(1), f.requests.Load(), "presentation save fetched IdP metadata")
+ require.Equal(t, before, priorSnapshot, "save mutated the published old snapshot")
+ if samlService != nil {
+ require.Same(t, samlService, r.samlManager.GetService(p.ID), "label edit discarded SAML request stores")
+ _, err := samlService.authnRequests.spend(loginID, "different-browser", time.Now())
+ require.ErrorIs(t, err, errSAMLResponseUnbound)
+ binding, err := samlService.authnRequests.spend(loginID, "original-browser", time.Now())
+ require.NoError(t, err)
+ require.Equal(t, "/history", binding.returnTo)
+ _, err = samlService.authnRequests.spend(loginID, "original-browser", time.Now())
+ require.ErrorIs(t, err, errSAMLResponseUnbound)
+ require.NoError(t, samlService.logoutRequests.consume(logoutID, "original-session", time.Now()))
+ require.ErrorIs(t, samlService.logoutRequests.consume(logoutID, "original-session", time.Now()), errSAMLLogoutResponseUnbound)
+ } else {
+ require.Same(t, oidcService, r.oidcManager.GetService(p.ID))
+ binding, ok := oidcService.consumeState(state)
+ require.True(t, ok, "label edit discarded OIDC nonce/PKCE state")
+ require.Equal(t, "/history", binding.ReturnTo)
+ require.NotEmpty(t, binding.Nonce)
+ require.NotEmpty(t, binding.CodeVerifier)
+ _, ok = oidcService.consumeState(state)
+ require.False(t, ok)
+ }
+ // Read the actual encrypted store through a fresh persistence
+ // reader, independent of the failed writer when applicable.
+ fresh := config.NewConfigPersistence(r.config.DataPath)
+ saved, err := fresh.LoadSSOConfig()
+ require.NoError(t, err)
+ expected := before.Clone()
+ if !failSave {
+ expected.Providers[0].Name = payload.Name
+ // Detail DTO renders an empty displayName as the name.
+ expected.Providers[0].DisplayName = payload.DisplayName
+ }
+ require.Equal(t, expected, saved)
+ require.Equal(t, expected, r.ensureSSOConfig())
+ diskAfter, err := os.ReadFile(file)
+ require.NoError(t, err)
+ require.False(t, json.Valid(diskAfter), "SSO config was not encrypted")
+ if failSave {
+ require.Equal(t, diskBefore, diskAfter)
+ }
+ for _, secret := range []string{"synthetic-presentation-secret", "PRIVATE KEY"} {
+ require.NotContains(t, rec.Body.String(), secret)
+ require.NotContains(t, string(diskAfter), secret)
+ }
+ })
+ }
+ }
+ }
+}
+
+func TestSSOPresentationRuntimeLifecycle(t *testing.T) {
+ for _, kind := range []config.SSOProviderType{config.SSOProviderTypeSAML, config.SSOProviderTypeOIDC} {
+ for _, change := range []string{"policy", "protocol", "missing service", "disable", "enable", "change type"} {
+ t.Run(string(kind)+"/"+change, func(t *testing.T) {
+ f := newPresentationFixture(t, kind)
+ r, p := f.router, f.provider
+ oldSAML, oldOIDC := r.samlManager.GetService(p.ID), r.oidcManager.GetService(p.ID)
+ switch change {
+ case "policy":
+ p.AllowedGroups = []string{"different-group"}
+ case "protocol":
+ if p.SAML != nil {
+ copy := *p.SAML
+ copy.NameIDFormat = "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress"
+ p.SAML = &copy
+ } else {
+ copy := *p.OIDC
+ copy.ClientSecret = "synthetic-replacement-secret"
+ p.OIDC = &copy
+ }
+ case "missing service":
+ r.samlManager.RemoveProvider(p.ID)
+ r.oidcManager.RemoveService(p.ID)
+ case "disable":
+ p.Enabled = false
+ case "enable":
+ _, err := r.commitSSOConfig(func(cfg *config.SSOConfig) error { cfg.GetProvider(p.ID).Enabled = false; return nil })
+ require.NoError(t, err)
+ case "change type":
+ other := newPresentationFixture(t, map[config.SSOProviderType]config.SSOProviderType{config.SSOProviderTypeSAML: config.SSOProviderTypeOIDC, config.SSOProviderTypeOIDC: config.SSOProviderTypeSAML}[kind])
+ p.Type, p.OIDC, p.SAML = other.provider.Type, other.provider.OIDC, other.provider.SAML
+ }
+ p.Name = "Renamed provider"
+ rec := presentationPut(t, r, p)
+ require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
+ if change == "disable" || change == "change type" {
+ if kind == config.SSOProviderTypeSAML {
+ require.Nil(t, r.samlManager.GetService(p.ID))
+ } else {
+ require.Nil(t, r.oidcManager.GetService(p.ID))
+ }
+ }
+ if p.Enabled {
+ if p.Type == config.SSOProviderTypeSAML {
+ require.NotNil(t, r.samlManager.GetService(p.ID))
+ require.NotSame(t, oldSAML, r.samlManager.GetService(p.ID))
+ } else {
+ require.NotNil(t, r.oidcManager.GetService(p.ID))
+ require.NotSame(t, oldOIDC, r.oidcManager.GetService(p.ID))
+ }
+ }
+ if change != "disable" && change != "change type" {
+ require.Equal(t, int32(2), f.requests.Load(), "changed runtime or missing service did not initialize")
+ }
+ })
+ }
+ }
+}