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.

Admit attention lifecycle POSTs on the mutation scope

rcourtmanb161c42e311e
diff --git a/internal/api/attention_mutation_scope_test.go b/internal/api/attention_mutation_scope_test.go
new file mode 100644
index 0000000000..3530b39833
--- /dev/null
+++ b/internal/api/attention_mutation_scope_test.go
@@ -0,0 +1,59 @@
+package api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// The attention prefix serves reads, lifecycle mutations and action planning.
+// The pinned capability inventory says a lifecycle POST needs monitoring:write
+// and a read needs monitoring:read, so a token must be admitted by the scope
+// that matches what it is doing, not by the read scope for everything.
+func TestAttentionRoutesGateEachOperationOnItsOwnScope(t *testing.T) {
+ const readToken = "attention-scope-read.12345678"
+ const writeToken = "attention-scope-write.12345678"
+ cfg := newTestConfigWithTokens(t,
+ newTokenRecord(t, readToken, []string{config.ScopeMonitoringRead}, nil),
+ newTokenRecord(t, writeToken, []string{config.ScopeMonitoringWrite}, nil),
+ )
+ router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+
+ do := func(token, method, path string) *httptest.ResponseRecorder {
+ req := httptest.NewRequest(method, path, strings.NewReader(`{}`))
+ req.Header.Set("X-API-Token", token)
+ rec := httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ return rec
+ }
+ refused := func(rec *httptest.ResponseRecorder) bool {
+ return rec.Code == http.StatusForbidden || rec.Code == http.StatusUnauthorized
+ }
+
+ const mutation = "/api/ai/patrol/attention/item-1/acknowledge"
+ if rec := do(writeToken, http.MethodPost, mutation); refused(rec) {
+ t.Errorf("monitoring:write token POST %s refused with %d: %s", mutation, rec.Code, rec.Body.String())
+ }
+ if rec := do(readToken, http.MethodPost, mutation); rec.Code != http.StatusForbidden {
+ t.Errorf("monitoring:read token POST %s = %d, want 403", mutation, rec.Code)
+ }
+
+ const detail = "/api/ai/patrol/attention/item-1"
+ if rec := do(readToken, http.MethodGet, detail); refused(rec) {
+ t.Errorf("monitoring:read token GET %s refused with %d: %s", detail, rec.Code, rec.Body.String())
+ }
+ if rec := do(writeToken, http.MethodGet, detail); rec.Code != http.StatusForbidden {
+ t.Errorf("monitoring:write token GET %s = %d, want 403 (write does not imply read)", detail, rec.Code)
+ }
+
+ // Action planning shares the prefix but is not a lifecycle mutation: it
+ // keeps the read-side outer gate plus its own authorization, so write
+ // scope alone must not admit it.
+ const plan = "/api/ai/patrol/attention/item-1/actions/restart/plan"
+ if rec := do(writeToken, http.MethodPost, plan); rec.Code != http.StatusForbidden {
+ t.Errorf("monitoring:write token POST %s = %d, want 403", plan, rec.Code)
+ }
+}
diff --git a/internal/api/router_routes_ai_relay.go b/internal/api/router_routes_ai_relay.go
index a164a01c17..74f5fea1e4 100644
--- a/internal/api/router_routes_ai_relay.go
+++ b/internal/api/router_routes_ai_relay.go
@@ -120,7 +120,18 @@ func (r *Router) registerAIRelayRoutesGroup() {
r.mux.HandleFunc("/api/ai/patrol/objectives", RequireAuth(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandlePatrolObjectives)))
r.mux.HandleFunc("/api/ai/patrol/objectives/", RequireAuth(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandlePatrolObjective)))
r.mux.HandleFunc("/api/ai/patrol/attention", RequireAuth(r.config, requireRelayMobileRuntimeRoute(relayMobileRouteAttentionList, r.attentionHandlers.HandleAttention)))
- r.mux.HandleFunc("/api/ai/patrol/attention/", RequireAuth(r.config, requireRelayMobileRuntimeRoute(relayMobileRouteAttentionDetail, r.attentionHandlers.HandleAttention)))
+ // One prefix serves reads, lifecycle mutations and action planning. The
+ // outer gate follows the dispatch in HandleAttention: a lifecycle mutation
+ // is admitted by the mutation scope (monitoring:write), everything else by
+ // the read scope. Action planning keeps its own entitlement and
+ // plan/approve/execute authorization inside the handler.
+ r.mux.HandleFunc("/api/ai/patrol/attention/", RequireAuth(r.config, func(w http.ResponseWriter, req *http.Request) {
+ route := relayMobileRouteAttentionDetail
+ if req.Method == http.MethodPost && isAttentionMutationPath(strings.TrimPrefix(req.URL.Path, "/api/ai/patrol/attention")) {
+ route = relayMobileRouteAttentionMutation
+ }
+ requireRelayMobileRuntimeRoute(route, r.attentionHandlers.HandleAttention)(w, req)
+ }))
// SECURITY: AI Patrol read endpoints - require ai:execute scope
r.mux.HandleFunc("/api/ai/patrol/history", RequireAuth(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandleGetFindingsHistory)))
r.mux.HandleFunc("/api/ai/patrol/run", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandleForcePatrol)))