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.

Read diagnostics token inventory under the config lock

rcourtman7996b4027240
diff --git a/internal/api/agent_fleet_doctor.go b/internal/api/agent_fleet_doctor.go
index 6ae4f93773..f24da24c03 100644
--- a/internal/api/agent_fleet_doctor.go
+++ b/internal/api/agent_fleet_doctor.go
@@ -38,7 +38,7 @@ func (r *Router) handleAgentFleetDiagnostics(w http.ResponseWriter, req *http.Re
if r.agentExecServer != nil {
connected = r.agentExecServer.GetConnectedAgentsForOrganization(organizationID)
}
- applyAgentFleetActionRunnerState(&diagnostics, actionRunnerCredentialSnapshot(r.config), connected, organizationID, now)
+ applyAgentFleetActionRunnerState(&diagnostics, snapshotAPITokenRecords(r.config), connected, organizationID, now)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(diagnostics); err != nil {
@@ -46,12 +46,12 @@ func (r *Router) handleAgentFleetDiagnostics(w http.ResponseWriter, req *http.Re
}
}
-// applyAgentFleetActionRunnerState joins the existing organization-scoped
-// command-session inventory to the existing Agent Doctor response. Admission
-// has already bound an action runner to one canonical host identity; the
-// projection still filters the closed runtime role and capability before
-// reporting it as connected.
-func actionRunnerCredentialSnapshot(cfg *config.Config) []config.APITokenRecord {
+// snapshotAPITokenRecords copies the token inventory under config.Mu. Token
+// writers edit cfg.APITokens in place under the write lock (scope edits,
+// delete-then-append, rotation, LastUsedAt on every token request), so a
+// reader must iterate this copy rather than the live slice. It takes the read
+// lock itself: callers must not already hold config.Mu.
+func snapshotAPITokenRecords(cfg *config.Config) []config.APITokenRecord {
if cfg == nil {
return nil
}
@@ -64,6 +64,11 @@ func actionRunnerCredentialSnapshot(cfg *config.Config) []config.APITokenRecord
return records
}
+// applyAgentFleetActionRunnerState joins the existing organization-scoped
+// command-session inventory to the existing Agent Doctor response. Admission
+// has already bound an action runner to one canonical host identity; the
+// projection still filters the closed runtime role and capability before
+// reporting it as connected.
func applyAgentFleetActionRunnerState(
diagnostics *monitoring.AgentFleetDiagnostics,
records []config.APITokenRecord,
diff --git a/internal/api/diagnostics.go b/internal/api/diagnostics.go
index 6220e99cea..a3aec466ab 100644
--- a/internal/api/diagnostics.go
+++ b/internal/api/diagnostics.go
@@ -1340,24 +1340,27 @@ func buildDiscoveryDiagnostic(cfg *config.Config, monitor *monitoring.Monitor) *
// buildAPITokenDiagnostic summarises the token inventory. A nil caller sees
// every token; an API-token caller sees only the tokens callerTokenMayManage
-// allows, and the counts, notes and usage are computed over that view.
+// allows, and the counts, notes and usage are computed over that view. It
+// reads a snapshot taken under config.Mu, so callers must not hold that lock.
func buildAPITokenDiagnostic(cfg *config.Config, monitor *monitoring.Monitor, caller *config.APITokenRecord) *APITokenDiagnostic {
if cfg == nil {
return nil
}
- records := make([]config.APITokenRecord, 0, len(cfg.APITokens))
- visibleIDs := make(map[string]struct{}, len(cfg.APITokens))
- for _, record := range cfg.APITokens {
- if !callerTokenMayManage(caller, &record) {
+ inventory := snapshotAPITokenRecords(cfg)
+ records := make([]config.APITokenRecord, 0, len(inventory))
+ visibleIDs := make(map[string]struct{}, len(inventory))
+ for i := range inventory {
+ record := &inventory[i]
+ if !callerTokenMayManage(caller, record) {
continue
}
- records = append(records, record)
+ records = append(records, *record)
visibleIDs[record.ID] = struct{}{}
}
diag := &APITokenDiagnostic{
- Enabled: cfg.HasAPITokens(),
+ Enabled: len(inventory) > 0,
TokenCount: len(records),
}
@@ -1368,7 +1371,7 @@ func buildAPITokenDiagnostic(cfg *config.Config, monitor *monitoring.Monitor, ca
diag.Notes = append(diag.Notes, note)
}
- diag.RecommendTokenSetup = !cfg.HasAPITokens()
+ diag.RecommendTokenSetup = len(inventory) == 0
if diag.RecommendTokenSetup {
appendNote("No API tokens are configured. Open Settings → Security to generate dedicated tokens for each automation or agent.")
diff --git a/internal/api/diagnostics_token_visibility_test.go b/internal/api/diagnostics_token_visibility_test.go
index 708ebf714c..9f4f905169 100644
--- a/internal/api/diagnostics_token_visibility_test.go
+++ b/internal/api/diagnostics_token_visibility_test.go
@@ -147,3 +147,72 @@ func TestDiagnosticsFiltersTokenSectionAfterTheSharedCache(t *testing.T) {
}
}
}
+
+// Token writers change config.APITokens in place under config.Mu: scope edits,
+// delete-then-append and rotation, and every token request records LastUsedAt.
+// Diagnostics reads the inventory on every request from a scoped token, and
+// when it rebuilds the cached instance view, so both paths must read it under
+// the lock. Run with -race: an unlocked read is reported as a data race.
+func TestDiagnosticsTokenSectionReadsTheInventoryUnderTheConfigLock(t *testing.T) {
+ f := newDiagnosticsTokenFixture(t)
+ router := &Router{config: f.cfg}
+
+ diagnosticsCacheMu.Lock()
+ prevCache := diagnosticsCache
+ diagnosticsCache = map[string]cachedDiagnosticsEntry{
+ diagnosticsScopeKey(context.Background()): {
+ diag: DiagnosticsInfo{Version: "cached", APITokens: &APITokenDiagnostic{}},
+ cachedAt: time.Now(),
+ },
+ }
+ diagnosticsCacheMu.Unlock()
+ t.Cleanup(func() {
+ diagnosticsCacheMu.Lock()
+ diagnosticsCache = prevCache
+ diagnosticsCacheMu.Unlock()
+ })
+
+ stop := make(chan struct{})
+ writerDone := make(chan struct{})
+ go func() {
+ defer close(writerDone)
+ scopeSets := [][]string{{config.ScopeWildcard}, {config.ScopeMonitoringRead, config.ScopeSettingsRead}}
+ for i := 0; ; i++ {
+ select {
+ case <-stop:
+ return
+ default:
+ }
+ config.Mu.Lock()
+ idx := i % len(f.cfg.APITokens)
+ edited := f.cfg.APITokens[idx]
+ edited.Scopes = append([]string(nil), scopeSets[i%len(scopeSets)]...)
+ f.cfg.APITokens = append(f.cfg.APITokens[:idx], f.cfg.APITokens[idx+1:]...)
+ f.cfg.APITokens = append(f.cfg.APITokens, edited)
+ used := time.Now().UTC()
+ f.cfg.APITokens[0].LastUsedAt = &used
+ config.Mu.Unlock()
+ }
+ }()
+
+ for i := 0; i < 200; i++ {
+ req := httptest.NewRequest(http.MethodGet, "/api/diagnostics", nil)
+ attachAPITokenRecord(req, &f.orgA)
+ rec := httptest.NewRecorder()
+ router.handleDiagnostics(rec, req)
+ var payload DiagnosticsInfo
+ if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
+ t.Fatalf("decode diagnostics: %v", err)
+ }
+ if ids := tokenIDs(payload.APITokens); len(ids) != 1 || !ids[f.orgA.ID] {
+ t.Fatalf("org-a caller saw tokens %v during concurrent token edits, want only its own", ids)
+ }
+
+ if all := buildAPITokenDiagnostic(f.cfg, nil, nil); all.TokenCount != 3 {
+ t.Fatalf("instance view counted %d tokens during concurrent token edits, want 3", all.TokenCount)
+ }
+ }
+
+ close(stop)
+ <-writerDone
+}