Skip to content

Agent Mesh

test(e2e): fix policy e2e tests due to schema and syntax errors

Antonio Ojea7361322c8389
diff --git a/api/policy.go b/api/policy.go
index cfff47f7..1f059ef8 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -1,3 +1,17 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
package api
// PolicyConfig is the root authorization configuration for the SAM Hub.
diff --git a/api/constants.go b/api/types.go
similarity index 80%
rename from api/constants.go
rename to api/types.go
index cb6e01c3..fea9f99f 100644
--- a/api/constants.go
+++ b/api/types.go
@@ -16,6 +16,7 @@ package api
import (
"maps"
+ "strings"
"github.com/libp2p/go-libp2p/core/protocol"
)
@@ -29,6 +30,9 @@ const AuthProtocolID protocol.ID = "/sam/auth/1.0.0"
// CatalogTarget is the special target service name used to retrieve tool catalogs from remote nodes.
const CatalogTarget = "/sam/catalog"
+// DefaultServiceType is the default type for services without a namespace.
+const DefaultServiceType = "system"
+
const DefaultAudience = "sam-mesh-audience"
// Biscuit fact names
@@ -70,3 +74,18 @@ var oidcClaimToFact = map[string]string{
func OIDCClaimToFact() map[string]string {
return maps.Clone(oidcClaimToFact)
}
+
+// ParseServiceTarget parses a service target string into its type and name components.
+// The target convention is "type:name" (e.g., "mcp:my_tool").
+// If no colon is present, the type defaults to the "system" namespace.
+// The global wildcard "*" is a special case that maps to type "*" and name "*".
+func ParseServiceTarget(target string) (svcType, svcName string) {
+ if target == "*" {
+ return "*", "*"
+ }
+ parts := strings.SplitN(target, ":", 2)
+ if len(parts) == 2 {
+ return parts[0], parts[1]
+ }
+ return DefaultServiceType, target
+}
diff --git a/api/types_test.go b/api/types_test.go
new file mode 100644
index 00000000..cdbf320c
--- /dev/null
+++ b/api/types_test.go
@@ -0,0 +1,100 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package api
+
+import (
+ "reflect"
+ "testing"
+)
+
+func TestParseServiceTarget(t *testing.T) {
+ tests := []struct {
+ name string
+ target string
+ wantType string
+ wantName string
+ }{
+ {
+ name: "Global wildcard",
+ target: "*",
+ wantType: "*",
+ wantName: "*",
+ },
+ {
+ name: "Explicit type and name",
+ target: "mcp:db-agent",
+ wantType: "mcp",
+ wantName: "db-agent",
+ },
+ {
+ name: "Type wildcard",
+ target: "mcp:*",
+ wantType: "mcp",
+ wantName: "*",
+ },
+ {
+ name: "System fallback (no colon)",
+ target: "catalog",
+ wantType: DefaultServiceType,
+ wantName: "catalog",
+ },
+ {
+ name: "Empty string",
+ target: "",
+ wantType: DefaultServiceType,
+ wantName: "",
+ },
+ {
+ name: "Multiple colons",
+ target: "mcp:tool:subtool",
+ wantType: "mcp",
+ wantName: "tool:subtool",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotType, gotName := ParseServiceTarget(tt.target)
+ if gotType != tt.wantType {
+ t.Errorf("ParseServiceTarget() gotType = %v, want %v", gotType, tt.wantType)
+ }
+ if gotName != tt.wantName {
+ t.Errorf("ParseServiceTarget() gotName = %v, want %v", gotName, tt.wantName)
+ }
+ })
+ }
+}
+
+func TestOIDCClaimToFact(t *testing.T) {
+ facts := OIDCClaimToFact()
+
+ want := map[string]string{
+ "sub": FactUser,
+ "email": FactEmail,
+ "groups": FactGroup,
+ }
+
+ if !reflect.DeepEqual(facts, want) {
+ t.Errorf("OIDCClaimToFact() = %v, want %v", facts, want)
+ }
+
+ // Verify that modifying the returned map does not mutate the internal map.
+ facts["new_claim"] = "new_fact"
+ facts2 := OIDCClaimToFact()
+
+ if _, ok := facts2["new_claim"]; ok {
+ t.Errorf("OIDCClaimToFact() returned map is not a clone, modifications affect internal state")
+ }
+}
diff --git a/cmd/sam-hub/biscuit.go b/cmd/sam-hub/biscuit.go
index 6b613088..b7baa81a 100644
--- a/cmd/sam-hub/biscuit.go
+++ b/cmd/sam-hub/biscuit.go
@@ -123,13 +123,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
if h.Policy != nil {
if rolePolicy, ok := h.Policy.Roles[role]; ok {
for _, svc := range rolePolicy.AllowedServices {
- parts := strings.SplitN(svc, ":", 2)
- svcType := "system"
- svcName := svc
- if len(parts) == 2 {
- svcType = parts[0]
- svcName = parts[1]
- }
+ svcType, svcName := api.ParseServiceTarget(svc)
if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactAllowService,
IDs: []biscuit.Term{biscuit.String(svcType), biscuit.String(svcName)},
diff --git a/cmd/sam-node/mcp_handlers.go b/cmd/sam-node/mcp_handlers.go
index d36f3c3b..2028877e 100644
--- a/cmd/sam-node/mcp_handlers.go
+++ b/cmd/sam-node/mcp_handlers.go
@@ -336,14 +336,19 @@ func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.
continue
}
+ targetService := svc.Name
+ if !strings.Contains(targetService, ":") {
+ targetService = "mcp:" + targetService
+ }
+
n.preparePeerAddrs(ctx, targetPeer)
- session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, svc.Name)
+ session, cleanup, err := n.ConnectMCPSession(ctx, targetPeer, targetService)
if err != nil {
- logger.Debugf("Failed to connect MCP session for service %s: %v", svc.Name, err)
- if serviceNameFilter == "" || svc.Name == serviceNameFilter || strings.HasPrefix(svc.Name, serviceNameFilter+".") {
+ logger.Debugf("Failed to connect MCP session for service %s: %v", targetService, err)
+ if serviceNameFilter == "" || targetService == serviceNameFilter || strings.HasPrefix(targetService, serviceNameFilter+".") {
rows = append(rows, remoteToolRow{
PeerID: targetPeer.String(),
- ToolName: svc.Name,
+ ToolName: targetService,
Error: fmt.Sprintf("failed to connect: %v", err),
})
}
@@ -356,7 +361,7 @@ func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.
if t == nil {
continue
}
- t.Name = svc.Name + "." + t.Name
+ t.Name = targetService + "." + t.Name
if serviceNameFilter != "" && !strings.HasPrefix(t.Name, serviceNameFilter+".") {
continue
}
@@ -367,10 +372,10 @@ func (n *SamNode) fetchRemoteToolCatalogue(ctx context.Context, targetPeer peer.
})
}
} else {
- if serviceNameFilter == "" || svc.Name == serviceNameFilter || strings.HasPrefix(svc.Name, serviceNameFilter+".") {
+ if serviceNameFilter == "" || targetService == serviceNameFilter || strings.HasPrefix(targetService, serviceNameFilter+".") {
rows = append(rows, remoteToolRow{
PeerID: targetPeer.String(),
- ToolName: svc.Name,
+ ToolName: targetService,
Error: fmt.Sprintf("failed to list tools: %v", err),
})
}
diff --git a/cmd/sam-node/middleware.go b/cmd/sam-node/middleware.go
index 0b270647..154ffc3a 100644
--- a/cmd/sam-node/middleware.go
+++ b/cmd/sam-node/middleware.go
@@ -20,8 +20,6 @@ import (
"encoding/hex"
"fmt"
- "strings"
-
"github.com/biscuit-auth/biscuit-go/v2"
"github.com/biscuit-auth/biscuit-go/v2/datalog"
"github.com/biscuit-auth/biscuit-go/v2/parser"
@@ -248,16 +246,12 @@ func (n *SamNode) Authorize(rawToken []byte, req RequestContext, pubKey ed25519.
}
// Inject the current action context (Standard Vocabulary)
- opType := "system"
- opName := req.Protocol
- if req.Target != "" {
- parts := strings.SplitN(req.Target, ":", 2)
- if len(parts) == 2 {
- opType = parts[0]
- opName = parts[1]
- } else {
- opName = req.Target
- }
+ var opType, opName string
+ if req.Target == "" {
+ opType = api.DefaultServiceType
+ opName = req.Protocol
+ } else {
+ opType, opName = api.ParseServiceTarget(req.Target)
}
authorizer.AddFact(biscuit.Fact{
Predicate: biscuit.Predicate{
diff --git a/development/kind/test-mesh-e2e.sh b/development/kind/test-mesh-e2e.sh
index 75f30097..0c572186 100755
--- a/development/kind/test-mesh-e2e.sh
+++ b/development/kind/test-mesh-e2e.sh
@@ -49,11 +49,11 @@ for i in $(seq 1 90); do
echo "discover attempt $i: $tools"
peer="$(printf '%s' "$tools" \
- | jq -r '.[]? | select(.tool_name=="calculator.add") | .peer_id' 2>/dev/null \
+ | jq -r '.[]? | select(.tool_name=="mcp:calculator.add") | .peer_id' 2>/dev/null \
| head -n1 || true)"
if [ -n "$peer" ]; then
- echo "calculator.add discovered on peer: $peer (attempt $i)"
+ echo "mcp:calculator.add discovered on peer: $peer (attempt $i)"
break
fi
@@ -67,7 +67,7 @@ for i in $(seq 1 90); do
done
[ -n "$peer" ] || {
- echo "calculator.add not discovered after 90s"
+ echo "mcp:calculator.add not discovered after 90s"
echo "final find_remote_tools:"
mcp -tool find_remote_tools -args '{}' || true
echo "final mesh info:"
@@ -75,9 +75,9 @@ done
exit 1
}
-echo "== call calculator.add(2,3) =="
+echo "== call mcp:calculator.add(2,3) =="
result=$(mcp -tool call_remote_tool \
- -args "{\"peer_id\":\"$peer\",\"tool_name\":\"calculator.add\",\"arguments\":{\"a\":2,\"b\":3}}")
+ -args "{\"peer_id\":\"$peer\",\"tool_name\":\"mcp:calculator.add\",\"arguments\":{\"a\":2,\"b\":3}}")
echo "result: $result"
if [[ "$result" != *"5"* ]]; then
echo "calculator.add did not return 5"
diff --git a/site/content/docs/development/policy.md b/site/content/docs/development/policy.md
index 413777fa..73ca5792 100644
--- a/site/content/docs/development/policy.md
+++ b/site/content/docs/development/policy.md
@@ -68,10 +68,12 @@ allow if group("engineering");
Admins define central permissions by mapping OIDC roles to specific capabilities.
* **`allowed_targets`**: Defines which logical groups or specific peers a user can route messages to, analogous to Active Directory security groups. Target definitions must be formatted as resolved facts (e.g., `group:<name>`, `user:<sub-id>`, `email:<email>`, `role:<role-name>`, or `node:<peer-id>`). *Note: These are evaluated exclusively by the destination node (see Section 3.1).*
-* **`allowed_services`**: Defines the application-level tools or endpoints a user can access, prefixed by their service type.
+* **`allowed_services`**: Defines the application-level tools or endpoints a user can access. Services use a strict `type:name` convention (e.g., `mcp:db-agent` or `inference:openrouter`).
+ * **Default Namespace (`system`)**: If a service string does not contain a `:` separator (e.g., `my-tool`), it automatically defaults to the `system` type (parsed as `system:my-tool`).
+ * **Wildcards**: Because `allowed_services` translates to the two-argument Biscuit fact `service($type, $name)`, SAM natively supports wildcards. You can grant access to an entire type via `type:*` (e.g., `mcp:*`), or grant global access to everything via `*`.
> [!NOTE]
-> Because `allowed_services` translates to the two-argument Biscuit fact `service($type, $name)`, policies natively support Datalog wildcards (e.g. `allow if service("mcp", $any);`). You can configure wildcard permissions in your policy files, or explicit granular permissions like `mcp:db-agent` and `inference:openrouter`.
+> The `*` global wildcard is a special case. It maps exactly to type `*` and name `*`. It does *not* default to the `system` type.
```yaml
version: "v1alpha1"
diff --git a/tests/e2e/policy.bats b/tests/e2e/policy.bats
index 8af5f0c1..7e4164e4 100644
--- a/tests/e2e/policy.bats
+++ b/tests/e2e/policy.bats
@@ -149,15 +149,14 @@ bindings:
role: \"mesh-member\"
roles:
mesh-member:
- mcp:
- allowed_servers:
- - \"query_database\"
- - \"delete_tables\""
+ allowed_services:
+ - \"mcp:query_database\"
+ - \"mcp:delete_tables\""
local node_policy="version: \"v1alpha1\"
attenuation:
policies:
- - 'deny if service("system", \"delete_tables\");'"
+ - 'deny if service(\"mcp\", \"delete_tables\");'"
docker run --rm -v "${POLICY_VOL}:/policies" busybox sh -c "cat <<'EOF' > /policies/policies.yaml
${hub_policy}
diff --git a/tests/integration/federation_test.go b/tests/integration/federation_test.go
index 8ef36ff8..9eef8cfa 100644
--- a/tests/integration/federation_test.go
+++ b/tests/integration/federation_test.go
@@ -34,12 +34,8 @@ bindings:
role: admin
roles:
admin:
- mcp:
- allowed_servers:
- - "/sam/mcp/1.0.0"
- - "list_local_services"
- - "discover_remote_services"
- - "federated-tool"
+ allowed_services:
+ - "mcp:*"
`
if err := os.WriteFile(policyFile, []byte(policyContent), 0644); err != nil {
t.Fatal(err)