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.

Serialize tenant API token validation and usage metadata

pulse-triage[bot]6e156073de17
diff --git a/internal/api/auth.go b/internal/api/auth.go
index f7f3e9d967..904d960641 100644
--- a/internal/api/auth.go
+++ b/internal/api/auth.go
@@ -552,7 +552,10 @@ func explicitAPITokenFromRequest(r *http.Request) (string, bool) {
return "", false
}
-func validateGlobalAPITokenLocked(cfg *config.Config, token string) (*config.APITokenRecord, bool) {
+// validateAPITokenLocked requires config.Mu.RLock and returns with it still
+// held. Both inventories need exclusive ownership to normalize token metadata
+// and update LastUsedAt. Return a detached snapshot before dropping the writer.
+func validateAPITokenLocked(cfg *config.Config, token string) (*config.APITokenRecord, bool) {
if cfg == nil || token == "" || !cfg.IsValidAPIToken(token) {
return nil, false
}
@@ -560,6 +563,10 @@ func validateGlobalAPITokenLocked(cfg *config.Config, token string) (*config.API
config.Mu.RUnlock()
config.Mu.Lock()
record, ok := cfg.ValidateAPIToken(token)
+ if ok {
+ clone := record.Clone()
+ record = &clone
+ }
config.Mu.Unlock()
config.Mu.RLock()
@@ -575,12 +582,12 @@ func validateAPITokenAgainstConfigsLocked(globalCfg, targetCfg *config.Config, t
}
if targetCfg != nil && targetCfg != globalCfg {
- if record, ok := targetCfg.ValidateAPIToken(token); ok {
+ if record, ok := validateAPITokenLocked(targetCfg, token); ok {
return record, true
}
}
- return validateGlobalAPITokenLocked(globalCfg, token)
+ return validateAPITokenLocked(globalCfg, token)
}
// CheckProxyAuth validates proxy authentication headers
@@ -755,7 +762,15 @@ func checkAuth(cfg *config.Config, w http.ResponseWriter, r *http.Request, write
Msg("Checking authentication")
authenticateToken := func(token string) bool {
- if record, ok := validateAPITokenAgainstConfigsLocked(cfg, tenantTokenConfigForRequest(r), token); ok {
+ targetCfg := tenantTokenConfigForRequest(r)
+ if record, ok := validateAPITokenAgainstConfigsLocked(cfg, targetCfg, token); ok {
+ // Validation temporarily releases the read lock. A reload may have
+ // replaced the selected owner during that transition; do not attach
+ // authority from the old inventory. Resolve only cached owners, and
+ // keep monitor locks outside the config writer's critical section.
+ if targetCfg != nil && tenantTokenConfigForRequest(r) != targetCfg {
+ return false
+ }
attachAPITokenRecord(r, record)
if authenticatedUser := apiTokenAuthenticatedUser(record); authenticatedUser != "" {
w.Header().Set("X-Authenticated-User", authenticatedUser)
@@ -1394,7 +1409,7 @@ func extractAndStoreAuthContext(cfg *config.Config, mtm *monitoring.MultiTenantM
// an unauthorized request, that initialization can exceed an API client
// timeout and leave cleanup queued behind the same cold start.
if explicitTokenProvided {
- if record, ok := validateGlobalAPITokenLocked(cfg, providedToken); ok {
+ if record, ok := validateAPITokenLocked(cfg, providedToken); ok {
attachAPITokenRecord(r, record)
return attachUserContext(r, apiTokenAuthenticatedUser(record))
}
@@ -1423,6 +1438,12 @@ func extractAndStoreAuthContext(cfg *config.Config, mtm *monitoring.MultiTenantM
validateToken := func(token string) (*http.Request, bool) {
if record, ok := validateAPITokenAgainstConfigsLocked(cfg, targetConfig, token); ok {
+ if targetOrgID != "" && targetConfig != cfg {
+ owner, exists := mtm.PeekMonitor(targetOrgID)
+ if !exists || owner == nil || owner.GetConfig() != targetConfig {
+ return nil, false
+ }
+ }
attachAPITokenRecord(r, record)
if targetOrgID != "" && targetConfig != cfg {
*r = *r.WithContext(context.WithValue(r.Context(), tenantTokenSourceContextKey, tenantTokenSource{manager: mtm, orgID: targetOrgID}))
diff --git a/internal/api/auth_context_tenant_test.go b/internal/api/auth_context_tenant_test.go
index dc0e6b22a4..07dc8415f4 100644
--- a/internal/api/auth_context_tenant_test.go
+++ b/internal/api/auth_context_tenant_test.go
@@ -98,6 +98,93 @@ func TestCheckAuthTenantConcurrentValidation(t *testing.T) {
}
}
+func TestCheckAuthTenantSourceCannotChangeDuringValidation(t *testing.T) {
+ const orgID = "tenant-a"
+ const raw = "synthetic-replaced-during-validation.12345678"
+ record, err := config.NewAPITokenRecord(raw, "old-owner", []string{config.ScopeMonitoringRead})
+ if err != nil {
+ t.Fatal(err)
+ }
+ base := &config.Config{}
+ mtp := config.NewMultiTenantPersistence(t.TempDir())
+ if _, err := mtp.GetPersistence(orgID); err != nil {
+ t.Fatal(err)
+ }
+ makeManager := func(cfg *config.Config) *monitoring.MultiTenantMonitor {
+ t.Helper()
+ monitor := &monitoring.Monitor{}
+ setUnexportedField(t, monitor, "config", cfg)
+ manager := monitoring.NewMultiTenantMonitor(base, mtp, nil)
+ setUnexportedField(t, manager, "monitors", map[string]*monitoring.Monitor{orgID: monitor})
+ t.Cleanup(manager.Stop)
+ return manager
+ }
+ oldManager := makeManager(&config.Config{APITokens: []config.APITokenRecord{*record}})
+ newManager := makeManager(&config.Config{})
+ r := httptest.NewRequest(http.MethodGet, "/api/test", nil)
+ r.Header.Set("X-Pulse-Org-ID", orgID)
+ r.Header.Set("X-API-Token", raw)
+ r = extractAndStoreAuthContext(base, oldManager, r)
+ r = r.WithContext(context.WithValue(r.Context(), OrgIDContextKey, orgID))
+ source := r.Context().Value(tenantTokenSourceContextKey).(tenantTokenSource)
+ resolutions := 0
+ source.resolveManager = func() *monitoring.MultiTenantMonitor {
+ resolutions++
+ if resolutions == 1 {
+ return oldManager
+ }
+ return newManager
+ }
+ r = r.WithContext(context.WithValue(r.Context(), tenantTokenSourceContextKey, source))
+ if checkAuth(base, httptest.NewRecorder(), r, false) {
+ t.Fatal("route auth accepted the owner replaced across metadata validation")
+ }
+ if resolutions < 2 {
+ t.Fatal("route auth did not recheck its live owner after the lock transition")
+ }
+}
+
+func TestValidateAPITokenAgainstConfigsLockedReturnsDetachedSnapshot(t *testing.T) {
+ const raw = "synthetic-detached-token-snapshot.12345678"
+ for _, inventory := range []string{"tenant", "global"} {
+ t.Run(inventory, func(t *testing.T) {
+ record, err := config.NewAPITokenRecord(raw, "snapshot", []string{config.ScopeMonitoringRead})
+ if err != nil {
+ t.Fatal(err)
+ }
+ record.OrgIDs = []string{"tenant-a"}
+ record.Metadata = map[string]string{apiTokenMetadataOwnerUserID: "tenant-owner"}
+ base, tenant := &config.Config{}, &config.Config{}
+ live := tenant
+ if inventory == "global" {
+ live = base
+ }
+ live.APITokens = []config.APITokenRecord{*record}
+ config.Mu.RLock()
+ got, ok := validateAPITokenAgainstConfigsLocked(base, tenant, raw)
+ // The helper must restore the caller's read ownership.
+ if config.Mu.TryLock() {
+ config.Mu.Unlock()
+ t.Error("token validation lost the caller's config read lock")
+ }
+ config.Mu.RUnlock()
+ if !ok || got == nil || got.LastUsedAt == nil {
+ t.Fatal("valid credential returned no usage snapshot")
+ }
+ got.Scopes[0] = config.ScopeSettingsWrite
+ got.OrgIDs[0] = "tenant-b"
+ got.Metadata[apiTokenMetadataOwnerUserID] = "changed-owner"
+ *got.LastUsedAt = time.Time{}
+ config.Mu.RLock()
+ stored := live.APITokens[0].Clone()
+ config.Mu.RUnlock()
+ if !reflect.DeepEqual(stored.Scopes, []string{config.ScopeMonitoringRead}) || !reflect.DeepEqual(stored.OrgIDs, []string{"tenant-a"}) || stored.Metadata[apiTokenMetadataOwnerUserID] != "tenant-owner" || (stored.LastUsedAt == nil || stored.LastUsedAt.IsZero()) {
+ t.Error("returned auth snapshot aliases live authority or usage metadata")
+ }
+ })
+ }
+}
+
func TestExtractAndStoreAuthContext_UsesTenantConfigForToken(t *testing.T) {
const globalToken = "global-token-123.12345678"
const tenantToken = "tenant-token-123.12345678"