Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
docs: clarify local permissive policy overrides and add integration tests
Antonio Ojeab6555cbe1a9c
diff --git a/site/content/docs/development/policy.md b/site/content/docs/development/policy.mdindex 0c8968e3..442d6d1e 100644--- a/site/content/docs/development/policy.md+++ b/site/content/docs/development/policy.md@@ -111,18 +111,16 @@ attenuation:checks:- 'check if time($time), $time < 2026-12-31T00:00:00Z;'policies:- - 'allow if service("mcp", "calculator");' # Technical override+ - 'deny if service("mcp", "restricted-tool"), group("externals");'```### 3.1 Understanding Authorization Limits (Local vs. Hub)-While the local configuration section is named `attenuation` (reflecting the architectural goal of further restricting access), policies defined under `attenuation.policies` are loaded directly into the execution authorizer.+The SAM network operates on a Zero Trust architecture where authorization is typically managed centrally by the Hub. However, local nodes have full sovereignty over their resources and can define their own authorization policies using the local `sam-node` configuration.-This means:-* **Service Permission Override (Possible)**: A local node owner can write `allow` policies in `sam-node-config.yaml` to bypass or override the Hub's service-level permissions. For example, they can expose their local `mcp://calculator` to all authenticated callers even if the Hub did not assign them access to that service.-* **Hard Constraints (Bypass Impossible)**: A local node owner **cannot** authorize unauthenticated or malicious requests. The baseline checks in the node middleware are hardcoded and will reject calls that:- 1. Fail signature verification against the trusted Hub keys.- 2. Fail the libp2p connection-level replay check (the caller must prove possession of the private key corresponding to the `client_peer_id` in the token).- 3. Fail target restrictions (if the Hub restricted the token to specific destination groups, the connection is dropped unless the node matches those target groups).+Local policies (defined under `attenuation.policies`) configure the **Verifier** on the local node.++* **Restrictive Policies (Deny)**: Local nodes can narrow down or restrict permissions granted by the Hub. For example, blocking users during off-hours, restricting specific contractors, or adding custom time-bound checks using `deny if` policies.+* **Permissive Policies (Allow)**: Local nodes can explicitly grant access to peers, overriding an **implicit deny** (e.g., when the Hub omits a `granted_service` fact). However, local allow policies **cannot bypass explicit constraints** (such as target restrictions or expirations) enforced via Hub-injected `check if` statements. In Biscuit, all `check if` conditions must evaluate to true; if the Hub seals a token with a target restriction that the destination node does not satisfy, the request is unconditionally rejected regardless of any local `allow if` policies.### 3.2 Evaluating `allowed_targets` at the Destination@@ -209,4 +207,111 @@ graph TD3. We add baseline rules (e.g. peer ID matching connection ID check, system catalog access policies) and local attenuation configurations (`sam-node-config.yaml`).4. We execute `Authorize()` to check all signatures, checks, and policies. If successful, the request is forwarded to the underlying service.+## 6. Comprehensive Mesh Policy Example++Here is a representative configuration for an engineering mesh deployment. It shows how to use roles, target groups, service namespaces, wildcards, and local attenuation checks to build a zero-trust development network.++### 6.1 Central Hub Policy (`policies.yaml`)++Deploy this file on `sam-hub` to define global roles and user/service-account mappings.++```yaml+version: "v1alpha1"++# Bindings map OIDC identities (sub/user, email, groups) to SAM Roles.+# Note: Kubernetes projected service account tokens do not carry 'groups' claims,+# so they must be bound explicitly using their 'user' claim format.+bindings:+ # 1. Global Admins (Infrastructure SAs, Lead Architects)+ - user: "system:serviceaccount:sam-mesh:admin-sa"+ role: "admin"+ - group: "infrastructure-leads"+ role: "admin"++ # 2. Software Developers+ - group: "software-engineering-team"+ role: "developer"++ # 3. Data Scientists & AI Engineers+ - group: "data-science-team"+ role: "data-scientist"++ # 4. Contractors / Read-Only Audits+ - email: "[email protected]"+ role: "auditor"++# Roles define the allowed destinations (allowed_targets) and tools (allowed_services)+roles:+ # Admins have full, unrestricted access to the entire mesh+ admin:+ allowed_targets:+ - "*"+ allowed_services:+ - "*"++ # Developers can call development tools on dev nodes+ developer:+ allowed_targets:+ - "group:dev-nodes" # Can only call nodes in the 'dev-nodes' target group+ allowed_services:+ - "mcp://code-reviewer" # Can call the code reviewer tool+ - "mcp://git-helper" # Can call git helper tools+ - "mcp://build-runner.*" # Wildcard: matches any build runner sub-service (e.g. build-runner.go)++ # Data Scientists can call database tools and all AI inference endpoints+ data-scientist:+ allowed_targets:+ - "group:data-nodes" # Can call nodes in the 'data-nodes' target group+ - "node:12D3KooWSpecialNode" # Can call a specific high-compute node directly+ allowed_services:+ - "mcp://db-reader" # Can query databases+ - "inference://*" # Wildcard: can access any LLM inference service++ # Auditors can only query metadata catalogs and cannot call operational tools+ auditor:+ allowed_targets:+ - "*"+ allowed_services:+ - "system://sam.catalog" # Strictly limited to tool discovery/metadata+```++### 6.2 Node-Level Configuration (`sam-node-config.yaml`)++Deploy this file on a specific database node in the `data-nodes` group to register services and enforce local restrictions.+```yaml+version: "v1alpha1"++# Static services hosted by this local node+services:+ - type: "mcp"+ name: "db-reader"+ description: "Read-only SQL execution tool"+ target_url: "http://127.0.0.1:5001/mcp"++ - type: "mcp"+ name: "db-writer"+ description: "Database modification tool"+ target_url: "http://127.0.0.1:5002/mcp"++# Local policies further restrict or override permissions on this specific node+attenuation:+ # local rules evaluate to block unauthorized actions+ rules:+ # 1. Block db-writer calls during off-hours (9 PM to 6 AM)+ - 'deny if service("mcp", "db-writer"), time($time), $time.hour() >= 21;'+ - 'deny if service("mcp", "db-writer"), time($time), $time.hour() < 6;'++ # local checks must be satisfied for any connection to succeed+ checks:+ # 2. Enforce strict TLS certificate expiry limit+ - 'check if time($time), $time < 2026-12-31T23:59:59Z;'++ # local policies can contain deny rules (to restrict Hub grants) or allow rules (to override implicit denies)+ policies:+ # 3. Restrict contractors from accessing db-writer even if Hub granted it+ - 'deny if service("mcp", "db-writer"), role("contractor");'++ # 4. Explicitly allow local admin bypass+ - 'allow if user("local-admin");'+```diff --git a/tests/integration/local_policy_test.go b/tests/integration/local_policy_test.gonew file mode 100644index 00000000..01bd4588--- /dev/null+++ b/tests/integration/local_policy_test.go@@ -0,0 +1,333 @@+package integration_test++import (+ "fmt"+ "os"+ "os/exec"+ "path/filepath"+ "strings"+ "testing"+)++func TestLocalPolicyCanGrantPermissions(t *testing.T) {+ hubBin := buildBinary(t, "./cmd/sam-hub")+ nodeBin := buildBinary(t, "./cmd/sam-node")+ tmpDir := t.TempDir()++ oidcURL, mintToken := startCustomMockOIDC(t)++ // Hub Policy that grants NOTHING.+ hubPolicyFile := filepath.Join(tmpDir, "policies.yaml")+ hubPolicyYAML := `version: "v1alpha1"+roles:+ none:+ allowed_services: []+ allowed_targets: []+bindings:+ - role: none+ user: "unprivileged-user"+ - role: none+ user: "nodeB-user"+`+ if err := os.WriteFile(hubPolicyFile, []byte(hubPolicyYAML), 0644); err != nil {+ t.Fatal(err)+ }++ httpPortHub := getFreePort(t)+ p2pPortHub := getFreePort(t)++ cmdHub := exec.Command(hubBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),+ "--policy-file", hubPolicyFile,+ "--keys-db", filepath.Join(tmpDir, "keys.db"),+ "--allow-loopback",+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ cmdHub.Stdout = os.Stdout+ cmdHub.Stderr = os.Stderr+ if err := cmdHub.Start(); err != nil {+ t.Fatalf("Failed to start Hub: %v", err)+ }+ defer func() { _ = cmdHub.Process.Kill(); _ = cmdHub.Wait() }()+ fetchPeerID(t, httpPortHub)++ // Node B Config with a permissive local policy+ nodeBPolicyFile := filepath.Join(tmpDir, "nodeB_config.yaml")+ nodeBPolicyYAML := `version: "v1alpha1"+services:+ - type: "mcp"+ name: "test-tool"+ command: ["echo", "test-tool"]+attenuation:+ policies:+ - 'allow if true;'+`+ if err := os.WriteFile(nodeBPolicyFile, []byte(nodeBPolicyYAML), 0644); err != nil {+ t.Fatal(err)+ }++ homeB := filepath.Join(tmpDir, "nodeB")+ apiTokenB := "tokenB"+ apiPortB := getFreePort(t)++ cmdB := exec.Command(nodeBin, "run",+ "--hub", fmt.Sprintf("http://127.0.0.1:%d", httpPortHub),+ "--data-dir", homeB,+ "--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortB),+ "--api-token", apiTokenB,+ "--jwt", mintToken(map[string]interface{}{+ "sub": "nodeB-user",+ }),+ "--listen", "/ip4/127.0.0.1/tcp/0",+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",+ "--allow-loopback",+ "--config", nodeBPolicyFile,+ )+ if err := os.MkdirAll(homeB, 0755); err != nil {+ t.Fatal(err)+ }+ logFileB, err := os.Create(filepath.Join(homeB, "node.log"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = logFileB.Close() }()+ cmdB.Stdout = logFileB+ cmdB.Stderr = logFileB+ if err := cmdB.Start(); err != nil {+ t.Fatalf("Failed to start Node B: %v", err)+ }+ defer func() { _ = cmdB.Process.Kill(); _ = cmdB.Wait() }()++ actualApiAddrB := waitForMCPAddr(t, filepath.Join(homeB, "node.log"))+ waitForAPI(t, actualApiAddrB)+ addrB := waitForPeerInfoInLog(t, filepath.Join(homeB, "node.log"))++ parts := strings.Split(addrB, "/p2p/")+ if len(parts) != 2 {+ t.Fatalf("unexpected addrB format: %s", addrB)+ }+ peerIDB := parts[1]++ // Node A Config (unprivileged user)+ homeA := filepath.Join(tmpDir, "nodeA")+ apiTokenA := "tokenA"+ apiPortA := getFreePort(t)++ cmdA := exec.Command(nodeBin, "run",+ "--hub", fmt.Sprintf("http://127.0.0.1:%d", httpPortHub),+ "--data-dir", homeA,+ "--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortA),+ "--api-token", apiTokenA,+ "--jwt", mintToken(map[string]interface{}{+ "sub": "unprivileged-user",+ }),+ "--listen", "/ip4/127.0.0.1/tcp/0",+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",+ "--allow-loopback",+ )+ if err := os.MkdirAll(homeA, 0755); err != nil {+ t.Fatal(err)+ }+ logFileA, err := os.Create(filepath.Join(homeA, "node.log"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = logFileA.Close() }()+ cmdA.Stdout = logFileA+ cmdA.Stderr = logFileA+ if err := cmdA.Start(); err != nil {+ t.Fatalf("Failed to start Node A: %v", err)+ }+ defer func() { _ = cmdA.Process.Kill(); _ = cmdA.Wait() }()++ actualApiAddrA := waitForMCPAddr(t, filepath.Join(homeA, "node.log"))+ waitForAPI(t, actualApiAddrA)++ // Make request from Node A to Node B+ // Even though Node A has no Hub permissions, Node B's local policy "allow if true;" should permit it.+ resp, callErr := callMCPAllowError(t, actualApiAddrA, apiTokenA, "call_remote_tool", map[string]any{+ "peer_id": peerIDB,+ "tool_name": "mcp://test-tool/test_tool",+ "arguments": map[string]any{},+ })++ failed := false+ if callErr != nil {+ if strings.Contains(callErr.Error(), "EOF") {+ failed = false+ } else {+ failed = true+ }+ } else if strings.Contains(resp, "Authorization failed") || strings.Contains(resp, "failed to connect") || strings.Contains(resp, "token lacks") || strings.Contains(resp, "denied") {+ failed = true+ }++ if failed {+ t.Errorf("expected success due to local permissive policy, got error: %v / %s", callErr, resp)+ }+}++func TestLocalPolicyCannotBypassHubTargetConstraint(t *testing.T) {+ hubBin := buildBinary(t, "./cmd/sam-hub")+ nodeBin := buildBinary(t, "./cmd/sam-node")+ tmpDir := t.TempDir()++ oidcURL, mintToken := startCustomMockOIDC(t)++ // Hub Policy that grants service access but RESTRICTS target to "group:admin-only".+ hubPolicyFile := filepath.Join(tmpDir, "policies.yaml")+ hubPolicyYAML := `version: "v1alpha1"+roles:+ restricted-role:+ allowed_services: ["*"]+ allowed_targets: ["group:admin-only"]+bindings:+ - role: restricted-role+ user: "client-user"+ - role: restricted-role+ user: "nodeB-user"+`+ if err := os.WriteFile(hubPolicyFile, []byte(hubPolicyYAML), 0644); err != nil {+ t.Fatal(err)+ }++ httpPortHub := getFreePort(t)+ p2pPortHub := getFreePort(t)++ cmdHub := exec.Command(hubBin,+ "--bind-address", fmt.Sprintf("127.0.0.1:%d", httpPortHub),+ "--listen", fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", p2pPortHub),+ "--policy-file", hubPolicyFile,+ "--keys-db", filepath.Join(tmpDir, "keys.db"),+ "--allow-loopback",+ "--issuer", oidcURL,+ "--insecure-skip-tls-verify",+ )+ cmdHub.Stdout = os.Stdout+ cmdHub.Stderr = os.Stderr+ if err := cmdHub.Start(); err != nil {+ t.Fatalf("Failed to start Hub: %v", err)+ }+ defer func() { _ = cmdHub.Process.Kill(); _ = cmdHub.Wait() }()+ fetchPeerID(t, httpPortHub)++ // Node B Config with a permissive local policy ("allow if true;")+ // Node B will NOT claim "group:admin-only", so it will fail the Hub's target check.+ nodeBPolicyFile := filepath.Join(tmpDir, "nodeB_config.yaml")+ nodeBPolicyYAML := `version: "v1alpha1"+services:+ - type: "mcp"+ name: "test-tool"+ command: ["echo", "test-tool"]+attenuation:+ policies:+ - 'allow if true;'+`+ if err := os.WriteFile(nodeBPolicyFile, []byte(nodeBPolicyYAML), 0644); err != nil {+ t.Fatal(err)+ }++ homeB := filepath.Join(tmpDir, "nodeB")+ apiTokenB := "tokenB"+ apiPortB := getFreePort(t)++ cmdB := exec.Command(nodeBin, "run",+ "--hub", fmt.Sprintf("http://127.0.0.1:%d", httpPortHub),+ "--data-dir", homeB,+ "--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortB),+ "--api-token", apiTokenB,+ "--jwt", mintToken(map[string]interface{}{+ "sub": "nodeB-user",+ }),+ "--listen", "/ip4/127.0.0.1/tcp/0",+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",+ "--allow-loopback",+ "--config", nodeBPolicyFile,+ )+ if err := os.MkdirAll(homeB, 0755); err != nil {+ t.Fatal(err)+ }+ logFileB, err := os.Create(filepath.Join(homeB, "node.log"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = logFileB.Close() }()+ cmdB.Stdout = logFileB+ cmdB.Stderr = logFileB+ if err := cmdB.Start(); err != nil {+ t.Fatalf("Failed to start Node B: %v", err)+ }+ defer func() { _ = cmdB.Process.Kill(); _ = cmdB.Wait() }()++ actualApiAddrB := waitForMCPAddr(t, filepath.Join(homeB, "node.log"))+ waitForAPI(t, actualApiAddrB)+ addrB := waitForPeerInfoInLog(t, filepath.Join(homeB, "node.log"))++ parts := strings.Split(addrB, "/p2p/")+ if len(parts) != 2 {+ t.Fatalf("unexpected addrB format: %s", addrB)+ }+ peerIDB := parts[1]++ // Node A Config (Client)+ homeA := filepath.Join(tmpDir, "nodeA")+ apiTokenA := "tokenA"+ apiPortA := getFreePort(t)++ cmdA := exec.Command(nodeBin, "run",+ "--hub", fmt.Sprintf("http://127.0.0.1:%d", httpPortHub),+ "--data-dir", homeA,+ "--bind-addr", fmt.Sprintf("127.0.0.1:%d", apiPortA),+ "--api-token", apiTokenA,+ "--jwt", mintToken(map[string]interface{}{+ "sub": "client-user",+ }),+ "--listen", "/ip4/127.0.0.1/tcp/0",+ "--listen", "/ip4/127.0.0.1/udp/0/quic-v1",+ "--allow-loopback",+ )+ if err := os.MkdirAll(homeA, 0755); err != nil {+ t.Fatal(err)+ }+ logFileA, err := os.Create(filepath.Join(homeA, "node.log"))+ if err != nil {+ t.Fatal(err)+ }+ defer func() { _ = logFileA.Close() }()+ cmdA.Stdout = logFileA+ cmdA.Stderr = logFileA+ if err := cmdA.Start(); err != nil {+ t.Fatalf("Failed to start Node A: %v", err)+ }+ defer func() { _ = cmdA.Process.Kill(); _ = cmdA.Wait() }()++ actualApiAddrA := waitForMCPAddr(t, filepath.Join(homeA, "node.log"))+ waitForAPI(t, actualApiAddrA)++ // Node A attempts to call Node B.+ // Node A's token allows calling "*" but restricts the target to "group:admin-only".+ // Node B doesn't have the "group:admin-only" identity, so the target check will fail.+ // Node B's local policy "allow if true;" cannot bypass this failed check.+ resp, callErr := callMCPAllowError(t, actualApiAddrA, apiTokenA, "call_remote_tool", map[string]any{+ "peer_id": peerIDB,+ "tool_name": "mcp://test-tool/test_tool",+ "arguments": map[string]any{},+ })++ failed := false+ if callErr != nil {+ if strings.Contains(callErr.Error(), "EOF") {+ failed = false+ } else {+ failed = true+ }+ } else if strings.Contains(resp, "Authorization failed") || strings.Contains(resp, "failed to connect") || strings.Contains(resp, "token lacks") || strings.Contains(resp, "denied") || strings.Contains(resp, "biscuit") {+ failed = true+ }++ if !failed {+ t.Errorf("expected failure due to Hub target constraint check, got success. Resp: %s", resp)+ }+}