Skip to content

Agent Mesh

default node policies

Antonio Ojeab49f77daea50
diff --git a/cmd/sam-node/middleware.go b/cmd/sam-node/middleware.go
index 93818daa..0b270647 100644
--- a/cmd/sam-node/middleware.go
+++ b/cmd/sam-node/middleware.go
@@ -36,9 +36,18 @@ var (
baselineRule1 biscuit.Policy
baselineRule2 biscuit.Policy
baselineRule3 biscuit.Policy
+ baselineRule4 biscuit.Policy
baselineReplayCheck biscuit.Check
)
+// init compiles the baseline Datalog rules and checks for the node.
+// These rules are loaded at initialization to avoid runtime parsing overhead.
+// Baseline Rules:
+// 1. Exact Match: Allows the request if the token explicitly allows the specific service type and name.
+// 2. Global Wildcard: Allows the request if the token explicitly grants access to all services ("*", "*").
+// 3. Catalog Target: Allows access to the service discovery catalog ("system", "catalog").
+// 4. Type Wildcard: Allows the request if the token explicitly grants access to all names under a specific service type ($type, "*").
+// 5. Replay Check: Enforces that the peer identity bound in the token matches the physical connection peer ID.
func init() {
var err error
rule1Str := fmt.Sprintf(`allow if service($type, $name), %s($type, $name)`, api.FactAllowService)
@@ -59,6 +68,12 @@ func init() {
panic(fmt.Sprintf("failed to parse baseline rule 3: %v", err))
}
+ rule4Str := fmt.Sprintf(`allow if service($type, $name), %s($type, "*")`, api.FactAllowService)
+ baselineRule4, err = parser.FromStringPolicy(rule4Str)
+ if err != nil {
+ panic(fmt.Sprintf("failed to parse baseline rule 4: %v", err))
+ }
+
replayCheckStr := `check if client_peer_id($id), connection_peer_id($id)`
baselineReplayCheck, err = parser.FromStringCheck(replayCheckStr)
if err != nil {
@@ -280,6 +295,7 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.
authorizer.AddPolicy(baselineRule1)
authorizer.AddPolicy(baselineRule2)
authorizer.AddPolicy(baselineRule3)
+ authorizer.AddPolicy(baselineRule4)
}
err = authorizer.Authorize()
diff --git a/cmd/sam-node/middleware_test.go b/cmd/sam-node/middleware_test.go
index b83c83b8..db1e675f 100644
--- a/cmd/sam-node/middleware_test.go
+++ b/cmd/sam-node/middleware_test.go
@@ -19,6 +19,7 @@ import (
"crypto/ed25519"
"crypto/sha256"
"encoding/hex"
+ "fmt"
"io"
"os"
"path/filepath"
@@ -162,6 +163,135 @@ func TestAuthorize(t *testing.T) {
}
}
+func TestBaselineRules(t *testing.T) {
+ pub, priv, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ dummyPeer := peer.ID("dummy-peer")
+
+ tests := []struct {
+ name string
+ mintToken func(t *testing.T, builder biscuit.Builder)
+ protocol string
+ target string
+ expectSuccess bool
+ }{
+ {
+ name: "Baseline Rule 1: Exact Match",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ factStr := fmt.Sprintf(`%s("mcp", "test_tool")`, api.FactAllowService)
+ fact, _ := parser.FromStringFact(factStr)
+ _ = builder.AddAuthorityFact(fact)
+ },
+ protocol: "test_tool",
+ target: "mcp:test_tool",
+ expectSuccess: true,
+ },
+ {
+ name: "Baseline Rule 2: Global Wildcard",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ factStr := fmt.Sprintf(`%s("*", "*")`, api.FactAllowService)
+ fact, _ := parser.FromStringFact(factStr)
+ _ = builder.AddAuthorityFact(fact)
+ },
+ protocol: "anything",
+ target: "mcp:anything",
+ expectSuccess: true,
+ },
+ {
+ name: "Baseline Rule 3: Catalog Target",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ // No specific allowed_service facts needed
+ },
+ protocol: api.CatalogTarget, // "catalog"
+ target: "system:" + api.CatalogTarget,
+ expectSuccess: true,
+ },
+ {
+ name: "Baseline Rule 4: Type Wildcard",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ factStr := fmt.Sprintf(`%s("mcp", "*")`, api.FactAllowService)
+ fact, _ := parser.FromStringFact(factStr)
+ _ = builder.AddAuthorityFact(fact)
+ },
+ protocol: "test_tool",
+ target: "mcp:test_tool",
+ expectSuccess: true,
+ },
+ {
+ name: "Baseline Rule Rejection: Type Wildcard does not allow other types",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ factStr := fmt.Sprintf(`%s("mcp", "*")`, api.FactAllowService)
+ fact, _ := parser.FromStringFact(factStr)
+ _ = builder.AddAuthorityFact(fact)
+ },
+ protocol: "test_tool",
+ target: "system:test_tool",
+ expectSuccess: false,
+ },
+ {
+ name: "Baseline Replay Check Rejection: mismatched peer ID",
+ mintToken: func(t *testing.T, builder biscuit.Builder) {
+ factStr := fmt.Sprintf(`%s("mcp", "test_tool")`, api.FactAllowService)
+ fact, _ := parser.FromStringFact(factStr)
+ _ = builder.AddAuthorityFact(fact)
+ // deliberately add a different client_peer_id than the connection peer ID
+ err = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: "client_peer_id",
+ IDs: []biscuit.Term{biscuit.String("different-peer")},
+ }})
+ },
+ protocol: "test_tool",
+ target: "mcp:test_tool",
+ expectSuccess: false, // Should fail the connection_peer_id check
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ builder := biscuit.NewBuilder(priv)
+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: "node",
+ IDs: []biscuit.Term{biscuit.String(dummyPeer.String())},
+ }})
+
+ // For the happy paths, add the matching client_peer_id
+ if tt.name != "Baseline Replay Check Rejection: mismatched peer ID" {
+ _ = builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: "client_peer_id",
+ IDs: []biscuit.Term{biscuit.String(dummyPeer.String())},
+ }})
+ }
+
+ tt.mintToken(t, builder)
+
+ b, _ := builder.Build()
+ tokenBytes, _ := b.Serialize()
+
+ node := &SamNode{
+ trustedKeys: []TrustedKey{{Key: pub, ReceivedAt: time.Now()}},
+ TrustHubRBAC: true,
+ BiscuitTimeout: 500 * time.Millisecond,
+ }
+
+ req := RequestContext{
+ PeerID: dummyPeer,
+ Protocol: tt.protocol,
+ Target: tt.target,
+ }
+
+ err = node.Authorize(tokenBytes, req, pub)
+ if tt.expectSuccess && err != nil {
+ t.Errorf("expected success, got error: %v", err)
+ } else if !tt.expectSuccess && err == nil {
+ t.Error("expected failure, got success")
+ }
+ })
+ }
+}
+
func TestEnterprisePolicyEngine(t *testing.T) {
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {