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.

fix(api/ai): resolve critical auth, agent download, and lifecycle issues

rcourtman935326ebb764
diff --git a/internal/ai/service.go b/internal/ai/service.go
index 919891206f..b7869cd53c 100644
--- a/internal/ai/service.go
+++ b/internal/ai/service.go
@@ -158,6 +158,7 @@ type Service struct {
// Infrastructure discovery service - detects apps running on hosts
infraDiscoveryService *infradiscovery.Service
+ infraDiscoveryCancel context.CancelFunc
// AI-powered deep discovery store - detailed service analysis with commands
discoveryStore *servicediscovery.Store
@@ -323,8 +324,16 @@ func (s *Service) SetStateProvider(sp StateProvider) {
)
// Wire the AI service as the analyzer (implements infradiscovery.AIAnalyzer)
s.infraDiscoveryService.SetAIAnalyzer(s)
- s.infraDiscoveryService.Start(context.Background())
- log.Info().Msg("AI-powered infrastructure discovery service started")
+
+ // Only start if AI is enabled
+ if s.cfg != nil && s.cfg.Enabled {
+ ctx, cancel := context.WithCancel(context.Background())
+ s.infraDiscoveryCancel = cancel
+ s.infraDiscoveryService.Start(ctx)
+ log.Info().Msg("AI-powered infrastructure discovery service started")
+ } else {
+ log.Info().Msg("AI-powered infrastructure discovery initialized (stopped - AI disabled)")
+ }
}
// Initialize AI-powered deep discovery service if not already done
@@ -749,6 +758,22 @@ func (s *Service) StopPatrol() {
}
}
+// Stop stops all background services
+func (s *Service) Stop() {
+ s.StopPatrol()
+
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ if s.infraDiscoveryCancel != nil {
+ s.infraDiscoveryCancel()
+ s.infraDiscoveryCancel = nil
+ }
+ if s.discoveryService != nil {
+ s.discoveryService.Stop()
+ }
+}
+
// ReconfigurePatrol updates the patrol configuration without restarting
// Call this after changing patrol settings to apply them immediately
func (s *Service) ReconfigurePatrol() {
diff --git a/internal/api/auth.go b/internal/api/auth.go
index 8c36190a3f..d9b1078f79 100644
--- a/internal/api/auth.go
+++ b/internal/api/auth.go
@@ -248,12 +248,34 @@ func CheckAuth(cfg *config.Config, w http.ResponseWriter, r *http.Request) bool
// API-only mode: when only API token is configured (no password auth)
if cfg.AuthUser == "" && cfg.AuthPass == "" && cfg.HasAPITokens() {
- // Check if an API token was provided
- providedToken := r.Header.Get("X-API-Token")
+ // Check if an API token was provided (via header, bearer, or query)
+ var providedToken string
+ if t := r.Header.Get("X-API-Token"); t != "" {
+ providedToken = t
+ } else if authHeader := r.Header.Get("Authorization"); strings.HasPrefix(strings.ToLower(authHeader), "bearer ") {
+ providedToken = strings.TrimSpace(authHeader[7:])
+ } else if t := r.URL.Query().Get("token"); t != "" {
+ providedToken = t
+ }
// If a token was provided, validate it
if providedToken != "" {
- if record, ok := cfg.ValidateAPIToken(providedToken); ok {
+ // Optimistically check validity with RLock to avoid write lock overhead for invalid tokens
+ if !cfg.IsValidAPIToken(providedToken) {
+ if w != nil {
+ http.Error(w, "Invalid API token", http.StatusUnauthorized)
+ }
+ return false
+ }
+
+ // Token appears valid, upgrade to Write lock to update stats
+ config.Mu.RUnlock()
+ config.Mu.Lock()
+ record, ok := cfg.ValidateAPIToken(providedToken)
+ config.Mu.Unlock()
+ config.Mu.RLock() // Restore Read lock for the defer at end of function
+
+ if ok {
attachAPITokenRecord(r, record)
tokenID := record.ID
if tokenID == "" && len(record.Hash) >= 8 {
@@ -263,7 +285,7 @@ func CheckAuth(cfg *config.Config, w http.ResponseWriter, r *http.Request) bool
w.Header().Set("X-Auth-Method", "api_token")
return true
}
- // Invalid token provided
+ // Should not happen if IsValidAPIToken returned true, unless race/expiration occurred
if w != nil {
http.Error(w, "Invalid API token", http.StatusUnauthorized)
}
@@ -289,7 +311,19 @@ func CheckAuth(cfg *config.Config, w http.ResponseWriter, r *http.Request) bool
if token == "" {
return false
}
- if record, ok := cfg.ValidateAPIToken(token); ok {
+ // Optimistically check validity with RLock
+ if !cfg.IsValidAPIToken(token) {
+ return false
+ }
+
+ // Upgrade to Write lock for stats update
+ config.Mu.RUnlock()
+ config.Mu.Lock()
+ record, ok := cfg.ValidateAPIToken(token)
+ config.Mu.Unlock()
+ config.Mu.RLock() // Restore Read lock
+
+ if ok {
attachAPITokenRecord(r, record)
tokenID := record.ID
if tokenID == "" && len(record.Hash) >= 8 {
@@ -772,9 +806,9 @@ func AuthContextMiddleware(cfg *config.Config, mtm *monitoring.MultiTenantMonito
// extractAndStoreAuthContext extracts user/token info from the request and stores in context.
// Returns the request with updated context. Does not enforce auth.
func extractAndStoreAuthContext(cfg *config.Config, mtm *monitoring.MultiTenantMonitor, r *http.Request) *http.Request {
- // Use Lock (Write) instead of RLock because ValidateAPIToken mutates LastUsedAt
- config.Mu.Lock()
- defer config.Mu.Unlock()
+ // Use RLock for common case, upgrade to Lock only if we need to update token stats
+ config.Mu.RLock()
+ defer config.Mu.RUnlock()
// Dev mode bypass
if adminBypassEnabled() {
@@ -831,7 +865,34 @@ func extractAndStoreAuthContext(cfg *config.Config, mtm *monitoring.MultiTenantM
if token == "" {
return nil, false
}
- // Use targetConfig instead of global cfg
+
+ // If using global config, we need to handle locking carefully
+ if targetConfig == cfg {
+ // Optimistic check with RLock
+ if !cfg.IsValidAPIToken(token) {
+ return nil, false
+ }
+
+ // Upgrade to Write lock for stats update
+ config.Mu.RUnlock()
+ config.Mu.Lock()
+ record, ok := cfg.ValidateAPIToken(token)
+ config.Mu.Unlock()
+ config.Mu.RLock() // Restore Read lock
+
+ if ok {
+ attachAPITokenRecord(r, record)
+ tokenID := record.ID
+ if tokenID == "" && len(record.Hash) >= 8 {
+ tokenID = "legacy-" + record.Hash[:8]
+ }
+ return attachUserContext(r, fmt.Sprintf("token:%s", tokenID)), true
+ }
+ return nil, false
+ }
+
+ // For tenant configs or other non-global configs, assume they handle their own locking
+ // or don't use the global config.Mu
if record, ok := targetConfig.ValidateAPIToken(token); ok {
attachAPITokenRecord(r, record)
tokenID := record.ID
diff --git a/internal/api/unified_agent.go b/internal/api/unified_agent.go
index fc57e105bc..47f1d8c209 100644
--- a/internal/api/unified_agent.go
+++ b/internal/api/unified_agent.go
@@ -117,6 +117,13 @@ func (r *Router) handleDownloadUnifiedAgent(w http.ResponseWriter, req *http.Req
w.Header().Set("Expires", "0")
archParam := strings.TrimSpace(req.URL.Query().Get("arch"))
+
+ // Validate architecture if provided
+ if archParam != "" && normalizeUnifiedAgentArch(archParam) == "" {
+ http.Error(w, "Invalid architecture specified", http.StatusBadRequest)
+ return
+ }
+
searchPaths := make([]string, 0, 6)
// If a specific architecture is requested, only look for that architecture