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.

Pin cross-org and cross-resource isolation on the bundle's pending approvals

rcourtman404f87854ee2
diff --git a/internal/api/agent_resource_context.go b/internal/api/agent_resource_context.go
index 646115fb5d..60a5e28db8 100644
--- a/internal/api/agent_resource_context.go
+++ b/internal/api/agent_resource_context.go
@@ -6,6 +6,7 @@ import (
"strings"
"time"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/approval"
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)
@@ -517,6 +518,61 @@ func projectAgentResourceActions(
return out
}
+// pendingApprovalsForResourceFromStore is the named, testable form
+// of the AgentApprovalsProvider closure the router wires at
+// startup. Given an approval store, a canonical resource id, and
+// an org id, it returns every still-pending approval that
+// (a) belongs to the org via approval.BelongsToOrg, and
+// (b) targets exactly the requested resource via
+//
+// ApprovalRequest.CanonicalResourceID.
+//
+// Both filters apply: an approval that matches one but not the
+// other is excluded so cross-org and cross-resource leaks are
+// impossible at the substrate boundary. Returns nil for an empty
+// store, an empty resource id, or no matches; an empty non-nil
+// slice would imply "no matches" too but the bundle handler
+// normalizes nil to []AgentResourceApprovalSummary{} so the wire
+// shape is always an array.
+func pendingApprovalsForResourceFromStore(store approvalsPendingProvider, resourceID, orgID string) []AgentResourceApprovalSummary {
+ if store == nil || strings.TrimSpace(resourceID) == "" {
+ return nil
+ }
+ pending := store.GetPendingApprovals()
+ if len(pending) == 0 {
+ return nil
+ }
+ out := make([]AgentResourceApprovalSummary, 0, len(pending))
+ for _, req := range pending {
+ if req == nil {
+ continue
+ }
+ if !approval.BelongsToOrg(req, orgID) {
+ continue
+ }
+ if req.CanonicalResourceID() != resourceID {
+ continue
+ }
+ out = append(out, AgentResourceApprovalSummary{
+ ID: req.ID,
+ Command: req.Command,
+ RiskLevel: string(req.RiskLevel),
+ RequestedBy: req.RequestedBy,
+ RequestedAt: req.RequestedAt,
+ ExpiresAt: req.ExpiresAt,
+ })
+ }
+ return out
+}
+
+// approvalsPendingProvider is the minimal accessor surface the
+// pending-approvals bundle filter depends on. The production
+// implementation is *approval.Store; the interface keeps the
+// function unit-testable without bringing the whole store up.
+type approvalsPendingProvider interface {
+ GetPendingApprovals() []*approval.ApprovalRequest
+}
+
// projectAgentResourceVerification converts the canonical
// ActionVerificationResult into the agent-stable projection that
// both the resource-context bundle and the action.completed SSE
diff --git a/internal/api/agent_resource_context_test.go b/internal/api/agent_resource_context_test.go
index 2d46b1721c..90f4091cfd 100644
--- a/internal/api/agent_resource_context_test.go
+++ b/internal/api/agent_resource_context_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/approval"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
@@ -528,6 +529,120 @@ func TestHandleAgentFleetContext_RejectsNonGet(t *testing.T) {
}
}
+// pendingApprovalsForResourceFromStore is the named seam the
+// router-side AgentApprovalsProvider closure delegates to. The
+// next four tests pin the substrate's tenant- and
+// resource-isolation property: every approval the bundle returns
+// must match BOTH the requested org AND the requested resource.
+// Cross-org or cross-resource leaks at this seam would let an
+// agent with one org's token see approvals targeting another org's
+// infrastructure, which is the multi-tenant property the rest of
+// Pulse depends on.
+
+// newApprovalsTestStore builds an in-memory approval store seeded
+// with the given approvals. Used as the fixture for each isolation
+// test below.
+func newApprovalsTestStore(t *testing.T, approvals ...*approval.ApprovalRequest) *approval.Store {
+ t.Helper()
+ store, err := approval.NewStore(approval.StoreConfig{
+ DataDir: t.TempDir(),
+ DefaultTimeout: 5 * time.Minute,
+ DisablePersistence: true,
+ })
+ if err != nil {
+ t.Fatalf("approval.NewStore: %v", err)
+ }
+ for _, req := range approvals {
+ if err := store.CreateApproval(req); err != nil {
+ t.Fatalf("CreateApproval(%s): %v", req.ID, err)
+ }
+ }
+ return store
+}
+
+func TestPendingApprovalsForResource_FiltersByOrg(t *testing.T) {
+ // Two orgs each have a pending approval against the same
+ // canonical resource id. The filter must return only the org
+ // that matches.
+ orgA := &approval.ApprovalRequest{
+ ID: "appr-org-a", OrgID: "org-a",
+ Command: "systemctl restart nginx", TargetType: "agent", TargetID: "host-1", TargetName: "host-1",
+ }
+ orgB := &approval.ApprovalRequest{
+ ID: "appr-org-b", OrgID: "org-b",
+ Command: "systemctl restart nginx", TargetType: "agent", TargetID: "host-1", TargetName: "host-1",
+ }
+ store := newApprovalsTestStore(t, orgA, orgB)
+
+ gotA := pendingApprovalsForResourceFromStore(store, "agent:host-1", "org-a")
+ if len(gotA) != 1 || gotA[0].ID != "appr-org-a" {
+ t.Errorf("org-a query must return only org-a's approval; got %+v", gotA)
+ }
+ gotB := pendingApprovalsForResourceFromStore(store, "agent:host-1", "org-b")
+ if len(gotB) != 1 || gotB[0].ID != "appr-org-b" {
+ t.Errorf("org-b query must return only org-b's approval; got %+v", gotB)
+ }
+}
+
+func TestPendingApprovalsForResource_FiltersByResource(t *testing.T) {
+ // Same org, two approvals against different resources. The
+ // filter must return only the one matching the requested
+ // canonical resource id, never both.
+ a := &approval.ApprovalRequest{
+ ID: "appr-vm-101", OrgID: "org-a",
+ Command: "systemctl restart nginx", TargetType: "vm", TargetID: "101", TargetName: "vm-101",
+ }
+ b := &approval.ApprovalRequest{
+ ID: "appr-vm-202", OrgID: "org-a",
+ Command: "systemctl restart nginx", TargetType: "vm", TargetID: "202", TargetName: "vm-202",
+ }
+ store := newApprovalsTestStore(t, a, b)
+
+ got := pendingApprovalsForResourceFromStore(store, "vm:101", "org-a")
+ if len(got) != 1 || got[0].ID != "appr-vm-101" {
+ t.Errorf("vm:101 query must return only that resource's approval; got %+v", got)
+ }
+}
+
+func TestPendingApprovalsForResource_LegacyEmptyOrgIsDefaultOnly(t *testing.T) {
+ // Approvals without OrgID set are treated as default-org per
+ // approval.BelongsToOrg. Pin that legacy approvals do not leak
+ // into a non-default org's bundle, and that the default-org
+ // query sees them.
+ legacy := &approval.ApprovalRequest{
+ ID: "appr-legacy",
+ Command: "systemctl restart nginx", TargetType: "agent", TargetID: "host-1", TargetName: "host-1",
+ // OrgID intentionally empty.
+ }
+ store := newApprovalsTestStore(t, legacy)
+
+ gotDefault := pendingApprovalsForResourceFromStore(store, "agent:host-1", "default")
+ if len(gotDefault) != 1 {
+ t.Errorf("default org must see legacy approvals; got %d", len(gotDefault))
+ }
+ gotOther := pendingApprovalsForResourceFromStore(store, "agent:host-1", "org-other")
+ if len(gotOther) != 0 {
+ t.Errorf("non-default org must NOT see legacy approvals; got %+v", gotOther)
+ }
+}
+
+func TestPendingApprovalsForResource_EmptyInputsReturnNil(t *testing.T) {
+ // Nil store, empty resource id, and an empty store all return
+ // nil. The bundle handler normalizes nil to []
+ // AgentResourceApprovalSummary{} so the wire shape stays an
+ // array; this function only owns the "no data" case.
+ if got := pendingApprovalsForResourceFromStore(nil, "vm:101", "org-a"); got != nil {
+ t.Errorf("nil store must return nil; got %+v", got)
+ }
+ store := newApprovalsTestStore(t)
+ if got := pendingApprovalsForResourceFromStore(store, "", "org-a"); got != nil {
+ t.Errorf("empty resource id must return nil; got %+v", got)
+ }
+ if got := pendingApprovalsForResourceFromStore(store, "vm:none", "org-a"); got != nil {
+ t.Errorf("empty store must return nil; got %+v", got)
+ }
+}
+
func TestExtractAgentResourceContextID(t *testing.T) {
cases := []struct {
path string
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index b6d4de6edf..74cb943d2c 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -13550,23 +13550,37 @@ func TestContract_AgentResourceContextEndpointSurfacesStableShape(t *testing.T)
// shows up as an empty array even when there ARE pending approvals,
// silently breaking the substrate's "everything an agent needs in
// one read" contract for the governance dimension.
+//
+// The wire-up has two halves: router.go installs the provider with
+// a closure that delegates to a named function in
+// agent_resource_context.go, and that function applies the org and
+// resource filters. Both halves are pinned here so a refactor that
+// moves the filter logic still passes the test only if the
+// resulting code preserves the safety properties.
func TestContract_AgentResourceContextWiresApprovalsProvider(t *testing.T) {
- source, err := os.ReadFile("router.go")
+ router, err := os.ReadFile("router.go")
if err != nil {
t.Fatalf("read router.go: %v", err)
}
- src := string(source)
- if !strings.Contains(src, "r.agentContextHandler.SetApprovalsProvider(agentApprovalsProviderFunc(") {
+ if !strings.Contains(string(router), "r.agentContextHandler.SetApprovalsProvider(agentApprovalsProviderFunc(") {
t.Error("router.go must wire the approvals provider on the agent context handler so the bundle's pendingApprovals section is populated")
}
- if !strings.Contains(src, "store := approval.GetStore()") {
- t.Error("router.go must resolve the approval store at request time so multi-tenant store rebuilds stay honored")
+ if !strings.Contains(string(router), "pendingApprovalsForResourceFromStore(approval.GetStore(), resourceID, orgID)") {
+ t.Error("router.go must delegate to pendingApprovalsForResourceFromStore so the request-time store lookup and the filter logic stay together and stay testable")
+ }
+
+ bundle, err := os.ReadFile("agent_resource_context.go")
+ if err != nil {
+ t.Fatalf("read agent_resource_context.go: %v", err)
+ }
+ if !strings.Contains(string(bundle), "func pendingApprovalsForResourceFromStore(") {
+ t.Error("agent_resource_context.go must define pendingApprovalsForResourceFromStore as the named, testable filter — drift here breaks the cross-org / cross-resource isolation pins")
}
- if !strings.Contains(src, "if !approval.BelongsToOrg(req, orgID)") {
- t.Error("router.go must scope pending approvals via BelongsToOrg so cross-tenant requests don't leak into a resource-context bundle")
+ if !strings.Contains(string(bundle), "if !approval.BelongsToOrg(req, orgID)") {
+ t.Error("the named filter must scope pending approvals via BelongsToOrg so cross-tenant requests don't leak into a resource-context bundle")
}
- if !strings.Contains(src, "if req.CanonicalResourceID() != resourceID") {
- t.Error("router.go must filter pending approvals by CanonicalResourceID so the bundle only carries approvals targeting the requested resource")
+ if !strings.Contains(string(bundle), "if req.CanonicalResourceID() != resourceID") {
+ t.Error("the named filter must filter pending approvals by CanonicalResourceID so the bundle only carries approvals targeting the requested resource")
}
}
diff --git a/internal/api/router.go b/internal/api/router.go
index 5074eae7cd..12d0ac225f 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -420,35 +420,7 @@ func (r *Router) setupRoutes() {
// the wrong org.
r.agentContextHandler.SetApprovalsProvider(agentApprovalsProviderFunc(
func(resourceID, orgID string) []AgentResourceApprovalSummary {
- store := approval.GetStore()
- if store == nil || strings.TrimSpace(resourceID) == "" {
- return nil
- }
- pending := store.GetPendingApprovals()
- if len(pending) == 0 {
- return nil
- }
- out := make([]AgentResourceApprovalSummary, 0, len(pending))
- for _, req := range pending {
- if req == nil {
- continue
- }
- if !approval.BelongsToOrg(req, orgID) {
- continue
- }
- if req.CanonicalResourceID() != resourceID {
- continue
- }
- out = append(out, AgentResourceApprovalSummary{
- ID: req.ID,
- Command: req.Command,
- RiskLevel: string(req.RiskLevel),
- RequestedBy: req.RequestedBy,
- RequestedAt: req.RequestedAt,
- ExpiresAt: req.ExpiresAt,
- })
- }
- return out
+ return pendingApprovalsForResourceFromStore(approval.GetStore(), resourceID, orgID)
},
))
r.agentEventBroadcaster = NewAgentEventBroadcaster()