Skip to content

Agent Mesh

policy: a publish grant reaches its verifiers through the policy text

Antonio Ojea5dc66536ab91
diff --git a/api/egress.go b/api/egress.go
index 95ecbfd7..bac8b2ec 100644
--- a/api/egress.go
+++ b/api/egress.go
@@ -267,17 +267,23 @@ func WithEgressPublish(policyRoles []*PolicyRole, egress []*EgressDestination, r
}
// BuildEgressServingRules grants each destination to the nodes that serve it:
-// granted_service_exact("egress", name) <- role(r) or <- label(k, v) for every
-// served_by entry. The serving node evaluates its own credential when a local
-// client asks for the destination, so the grant has to reach it; the rules
-// travel with the mesh policy like every other grant.
+// granted_service_exact("egress", name) and granted_publish_exact("egress", name)
+// <- role(r) or <- label(k, v) for every served_by entry. The serving node
+// evaluates its own credential when a local client asks for the destination,
+// and the router and the callers evaluate it when the node publishes, so
+// both grants have to reach them; the rules travel with the mesh policy like
+// every other grant.
func BuildEgressServingRules(egress []*EgressDestination) []PolicyRule {
var rules []PolicyRule
for _, d := range egress {
if d == nil || ValidateEgressName(d.GetName()) != nil {
continue
}
- head := biscuit.Predicate{Name: FactGrantedServiceExact, IDs: []biscuit.Term{biscuit.String(ServiceTypeStringEgress), biscuit.String(d.GetName())}}
+ destination := []biscuit.Term{biscuit.String(ServiceTypeStringEgress), biscuit.String(d.GetName())}
+ heads := []biscuit.Predicate{
+ {Name: FactGrantedServiceExact, IDs: destination},
+ {Name: FactGrantedPublishExact, IDs: destination},
+ }
for _, sel := range d.GetServedBy() {
var body biscuit.Predicate
if key, value, isLabel := strings.Cut(sel, "="); isLabel {
@@ -291,8 +297,10 @@ func BuildEgressServingRules(egress []*EgressDestination) []PolicyRule {
}
body = biscuit.Predicate{Name: FactRole, IDs: []biscuit.Term{biscuit.String(sel)}}
}
- r := biscuit.Rule{Head: head, Body: []biscuit.Predicate{body}}
- rules = append(rules, PolicyRule{Rule: r, Text: renderRule(r)})
+ for _, head := range heads {
+ r := biscuit.Rule{Head: head, Body: []biscuit.Predicate{body}}
+ rules = append(rules, PolicyRule{Rule: r, Text: renderRule(r)})
+ }
}
}
return rules
diff --git a/api/egress_test.go b/api/egress_test.go
index de1a98b7..3d4e4ac2 100644
--- a/api/egress_test.go
+++ b/api/egress_test.go
@@ -142,7 +142,9 @@ func TestBuildEgressServingRules(t *testing.T) {
texts := PolicyRuleTexts(rules)
want := []string{
`granted_service_exact("egress", "api.github.com") <- role("pep")`,
+ `granted_publish_exact("egress", "api.github.com") <- role("pep")`,
`granted_service_exact("egress", "api.github.com") <- label("site", "eu")`,
+ `granted_publish_exact("egress", "api.github.com") <- label("site", "eu")`,
}
if !slices.Equal(texts, want) {
t.Errorf("rules = %v, want %v", texts, want)
diff --git a/api/policy_rules.go b/api/policy_rules.go
index 15a4a08b..44069948 100644
--- a/api/policy_rules.go
+++ b/api/policy_rules.go
@@ -225,6 +225,9 @@ func BuildPolicyRules(roles []*PolicyRole, bindings []*PolicyBinding) (rules []P
add(fact.Predicate, fromRole)
}
}
+ for _, fact := range BuildPublishDatalogFacts(role.AllowedPublish) {
+ add(fact.Predicate, fromRole)
+ }
hasUnrestricted := false
var nonWildcardTargets []string
diff --git a/api/policy_rules_test.go b/api/policy_rules_test.go
index 942235d5..11094e69 100644
--- a/api/policy_rules_test.go
+++ b/api/policy_rules_test.go
@@ -41,6 +41,7 @@ func TestBuildPolicyRules(t *testing.T) {
Name: "test-role",
AllowedTargets: []string{"*", "node:" + nodeCID, "node:not-a-peer-id", "custom-fact:custom-val", "legacy-peer"},
AllowedServices: []string{"*:*", "mcp:*", "mcp:*.suffix", "mcp:prefix.*", "mcp:exact"},
+ AllowedPublish: []string{"mcp://*", "a2a://echo"},
CustomDatalog: []string{
"custom_rule($x) <- fact($x), $x > 3;",
"custom_fact(\"hello\")",
@@ -66,6 +67,8 @@ func TestBuildPolicyRules(t *testing.T) {
"granted_service_suffix(\"mcp\", \".suffix\") <- role(\"test-role\")": false,
"granted_service_prefix(\"mcp\", \"prefix.\") <- role(\"test-role\")": false,
"granted_service_set(\"mcp\", [\"exact\"]) <- role(\"test-role\")": false,
+ "granted_publish_all(\"mcp\") <- role(\"test-role\")": false,
+ "granted_publish_set(\"a2a\", [\"echo\"]) <- role(\"test-role\")": false,
"target_unrestricted(true) <- role(\"test-role\")": false,
"target_restricted(true) <- role(\"test-role\")": false,
"granted_target_set(\"node\", [\"" + nodeID + "\", \"legacy-peer\"]) <- role(\"test-role\")": false,
diff --git a/internal/controlplane/resolve_roles_test.go b/internal/controlplane/resolve_roles_test.go
index 298b8540..f4fefe02 100644
--- a/internal/controlplane/resolve_roles_test.go
+++ b/internal/controlplane/resolve_roles_test.go
@@ -15,6 +15,7 @@
package controlplane
import (
+ "strings"
"testing"
jwt "github.com/golang-jwt/jwt/v5"
@@ -123,3 +124,25 @@ func TestResolveRolesWildcardsAndValidation(t *testing.T) {
}
}
}
+
+func TestValidatePolicyConfigAllowedPublish(t *testing.T) {
+ role := func(publish ...string) *api.PolicyConfig {
+ return &api.PolicyConfig{
+ Roles: []*api.PolicyRole{{Name: "r1", AllowedPublish: publish}},
+ Bindings: []*api.PolicyBinding{{Role: "r1", Members: []string{"user:alice"}}},
+ }
+ }
+ for _, ok := range [][]string{nil, {"*"}, {"mcp://*"}, {"mcp://calc", "a2a://echo"}, {"egress://api.github.com"}} {
+ if err := ValidatePolicyConfig(role(ok...)); err != nil {
+ t.Errorf("ValidatePolicyConfig rejected allowed_publish %q: %v", ok, err)
+ }
+ }
+ for _, bad := range []string{"calc", "mcp:calc", "mcp://calc?x=1", "mcp://calc#f", ""} {
+ err := ValidatePolicyConfig(role(bad))
+ if err == nil {
+ t.Errorf("ValidatePolicyConfig accepted allowed_publish %q", bad)
+ } else if !strings.Contains(err.Error(), "allowed_publish") {
+ t.Errorf("rejecting allowed_publish %q, the error does not name the field: %v", bad, err)
+ }
+ }
+}
diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.go
index 9a5de97d..62cf426a 100644
--- a/internal/identity/biscuit.go
+++ b/internal/identity/biscuit.go
@@ -818,14 +818,20 @@ func RequireRole(b *biscuit.Biscuit, key ed25519.PublicKey, expectedRole string,
// RequirePublish reports whether the credential grants serving the service
// svcType://svcName: api.PublishPolicies over the token's granted_publish_*
-// facts and a publish() fact. b must have passed verification and key must
-// be the key that verified it; what is decided here is the grant alone.
-func RequirePublish(b *biscuit.Biscuit, key ed25519.PublicKey, svcType, svcName string, timeout time.Duration) error {
+// facts, those the mesh policy rules derive from its role() and label()
+// facts, and a publish() fact. The rules are what a policy change reaches a
+// verifier through before the holder's credential is minted again. b must
+// have passed verification and key must be the key that verified it; what
+// is decided here is the grant alone.
+func RequirePublish(b *biscuit.Biscuit, key ed25519.PublicKey, svcType, svcName string, policyRules []biscuit.Rule, timeout time.Duration) error {
authorizer, err := b.Authorizer(key, AuthorizerOptions(timeout)...)
if err != nil {
return fmt.Errorf("failed to create authorizer: %w", err)
}
authorizer.AddFact(api.PublishFact(svcType, svcName))
+ for _, r := range policyRules {
+ authorizer.AddRule(r)
+ }
for _, p := range api.PublishPolicies {
authorizer.AddPolicy(p)
}
diff --git a/internal/identity/biscuit_test.go b/internal/identity/biscuit_test.go
index 5285eff4..9d0e0d38 100644
--- a/internal/identity/biscuit_test.go
+++ b/internal/identity/biscuit_test.go
@@ -798,7 +798,7 @@ func TestMintBiscuitToken_PublishFacts(t *testing.T) {
{"mcp", "tools", false},
{"egress", "api.github.com", false},
} {
- err := RequirePublish(b, pub, tc.svcType, tc.svcName, testTimeout)
+ err := RequirePublish(b, pub, tc.svcType, tc.svcName, nil, testTimeout)
if (err == nil) != tc.granted {
t.Errorf("RequirePublish(%s://%s) = %v, want granted %v", tc.svcType, tc.svcName, err, tc.granted)
}
@@ -809,16 +809,34 @@ func TestMintBiscuitToken_PublishFacts(t *testing.T) {
t.Fatal(err)
}
nb, _ := biscuit.Unmarshal(none)
- if err := RequirePublish(nb, pub, "mcp", "calc", testTimeout); err == nil {
+ if err := RequirePublish(nb, pub, "mcp", "calc", nil, testTimeout); err == nil {
t.Error("a credential minted under no policy role grants publishing nothing")
}
+ // The mesh policy rules reach a verifier before the holder's credential
+ // is minted again: a credential carrying only role("publisher") is
+ // granted what the current policy gives that role, and only that.
+ rules, warnings := api.BuildPolicyRules(roles, nil)
+ if len(warnings) != 0 {
+ t.Fatalf("BuildPolicyRules warnings: %v", warnings)
+ }
+ parsed, err := api.ParseDatalogRules(api.PolicyRuleTexts(rules))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := RequirePublish(nb, pub, "mcp", "calc", parsed, testTimeout); err != nil {
+ t.Errorf("the policy rules grant role publisher mcp://calc, RequirePublish = %v", err)
+ }
+ if err := RequirePublish(nb, pub, "mcp", "clock", parsed, testTimeout); err == nil {
+ t.Error("the policy rules do not grant role publisher mcp://clock, RequirePublish = nil")
+ }
+
delegated, err := MintDelegatedBiscuitToken(priv, jwt.MapClaims{"sub": "user"}, p, time.Now().Add(time.Hour), []string{"publisher"}, roles, nil, false)
if err != nil {
t.Fatal(err)
}
db, _ := biscuit.Unmarshal(delegated)
- if err := RequirePublish(db, pub, "mcp", "calc", testTimeout); err == nil {
+ if err := RequirePublish(db, pub, "mcp", "calc", nil, testTimeout); err == nil {
t.Error("a delegated credential grants publishing nothing")
}
}
diff --git a/internal/node/labels_gate.go b/internal/node/labels_gate.go
index 80ecee36..6349ecee 100644
--- a/internal/node/labels_gate.go
+++ b/internal/node/labels_gate.go
@@ -313,7 +313,10 @@ func (n *AgentMeshNode) checkProvider(providerBiscuit []byte, peerID peer.ID, re
if svcType == "" || svcName == "" {
return fmt.Errorf("service %q must be <type>://<name>", service)
}
- if err := identity.RequirePublish(b, key, svcType, svcName, n.BiscuitTimeout); err != nil {
+ n.MeshPolicyMu.RLock()
+ meshRules := n.MeshPolicyRules
+ n.MeshPolicyMu.RUnlock()
+ if err := identity.RequirePublish(b, key, svcType, svcName, meshRules, n.BiscuitTimeout); err != nil {
return fmt.Errorf("%w: %w", ErrProviderNotGranted, err)
}
}
diff --git a/internal/node/labels_gate_test.go b/internal/node/labels_gate_test.go
index e3694619..16880099 100644
--- a/internal/node/labels_gate_test.go
+++ b/internal/node/labels_gate_test.go
@@ -159,6 +159,35 @@ func TestCheckPeerLabels(t *testing.T) {
}
})
}
+
+ // The synced mesh policy grants too, from the provider's role, so a
+ // policy change reaches the caller at its next sync and not at the
+ // provider's next credential.
+ t.Run("granted by the mesh policy", func(t *testing.T) {
+ ungranted := mintPublish()
+ if err := node.checkProvider(ungranted, providerPeer, nil, "mcp://calc"); !errors.Is(err, ErrProviderNotGranted) {
+ t.Fatalf("checkProvider before the policy = %v, want ErrProviderNotGranted", err)
+ }
+ rules, _ := api.BuildPolicyRules([]*api.PolicyRole{{Name: api.RoleNode, AllowedPublish: []string{"mcp://calc"}}}, nil)
+ parsed, err := api.ParseDatalogRules(api.PolicyRuleTexts(rules))
+ if err != nil {
+ t.Fatal(err)
+ }
+ node.MeshPolicyMu.Lock()
+ node.MeshPolicyRules = parsed
+ node.MeshPolicyMu.Unlock()
+ t.Cleanup(func() {
+ node.MeshPolicyMu.Lock()
+ node.MeshPolicyRules = nil
+ node.MeshPolicyMu.Unlock()
+ })
+ if err := node.checkProvider(ungranted, providerPeer, nil, "mcp://calc"); err != nil {
+ t.Fatalf("checkProvider with the policy granting the node role mcp://calc = %v", err)
+ }
+ if err := node.checkProvider(ungranted, providerPeer, nil, "mcp://weather"); !errors.Is(err, ErrProviderNotGranted) {
+ t.Fatalf("checkProvider(mcp://weather) = %v, want ErrProviderNotGranted: the policy grants mcp://calc only", err)
+ }
+ })
})
}
diff --git a/internal/node/node.go b/internal/node/node.go
index 6a1210a5..34128de9 100644
--- a/internal/node/node.go
+++ b/internal/node/node.go
@@ -182,6 +182,7 @@ type AgentMeshNode struct {
trustedKeys []TrustedKey
keysMu sync.RWMutex
MeshPolicyRules []biscuit.Rule
+ meshPolicyTexts []string
MeshPolicyMu sync.RWMutex
// pendingEgress holds assignments that arrived before Start created the
// service registry (SyncControlPlane runs first); Start applies them.
@@ -2699,10 +2700,18 @@ func (n *AgentMeshNode) syncMeshPolicy(ctx context.Context) error {
}
n.MeshPolicyMu.Lock()
+ changed := n.meshPolicyTexts != nil && !slices.Equal(n.meshPolicyTexts, policyResp.DatalogRules)
n.MeshPolicyRules = rules
+ n.meshPolicyTexts = policyResp.DatalogRules
n.MeshPolicyMu.Unlock()
logger.Infof("Successfully synchronized mesh policy (%d rules)", len(rules))
+ if changed && n.reprovideTrigger != nil {
+ // The policy decides what this node may publish; a record the
+ // directory refused under the previous one is announced again now.
+ logger.Info("Mesh policy changed; announcing this node's records again")
+ n.triggerReprovide()
+ }
return nil
}
diff --git a/internal/router/config.go b/internal/router/config.go
index ecd67fc9..915c72f3 100644
--- a/internal/router/config.go
+++ b/internal/router/config.go
@@ -41,6 +41,7 @@ type Options struct {
ListenAddrs []string
ExternalAddrs []string
KeysSyncInterval time.Duration
+ PolicySyncInterval time.Duration
LeaseRenewInterval time.Duration
OIDCProvider string // For OIDC enrollment
OIDCToken string // OIDC JWT token for enrollment
@@ -121,6 +122,9 @@ func (o *Options) Default() {
if o.KeysSyncInterval <= 0 {
o.KeysSyncInterval = 5 * time.Minute
}
+ if o.PolicySyncInterval <= 0 {
+ o.PolicySyncInterval = 15 * time.Minute
+ }
if o.LeaseRenewInterval <= 0 {
o.LeaseRenewInterval = 30 * time.Second
}
diff --git a/internal/router/directory.go b/internal/router/directory.go
index e6866bf4..a4f01c3f 100644
--- a/internal/router/directory.go
+++ b/internal/router/directory.go
@@ -244,7 +244,10 @@ func (e *directoryDeniedError) Error() string { return e.err.Error() }
func (e *directoryDeniedError) Unwrap() error { return e.err }
// requirePublish checks the admitted member's credential grants serving
-// the service a record names. A record under the member's own peer key
+// the service a record names: its own granted_publish_* facts, or those
+// the mesh policy rules derive from its role() and label() facts, so that
+// a policy change reaches the directory at the next sync and not at the
+// member's next credential. A record under the member's own peer key
// names no service and needs no grant: a member may always say where it
// is reached.
func (r *Router) requirePublish(provider peer.ID, svcType api.ServiceType, svcName string) error {
@@ -260,7 +263,7 @@ func (r *Router) requirePublish(provider peer.ID, svcType api.ServiceType, svcNa
return &directoryDeniedError{service: typeStr + "://" + svcName, err: errors.New("no credential held for the member")}
}
cred := v.(memberCredential)
- if err := identity.RequirePublish(cred.biscuit, cred.key, typeStr, svcName, r.config.BiscuitTimeout); err != nil {
+ if err := identity.RequirePublish(cred.biscuit, cred.key, typeStr, svcName, r.meshPolicyRules(), r.config.BiscuitTimeout); err != nil {
return &directoryDeniedError{service: typeStr + "://" + svcName, err: err}
}
return nil
diff --git a/internal/router/directory_test.go b/internal/router/directory_test.go
index aefc1433..8e3ec8df 100644
--- a/internal/router/directory_test.go
+++ b/internal/router/directory_test.go
@@ -466,4 +466,26 @@ func TestDirectoryDeniesWhatTheCredentialDoesNotGrant(t *testing.T) {
if resp := ask(t, member, a.Host.ID(), provide(calc, "mcp://calculator")); resp.GetError() == "" {
t.Fatal("stored with no credential held; want a refusal")
}
+
+ // The mesh policy the router synced grants too: a credential carrying
+ // role("member") and no publish fact is granted what the policy gives
+ // that role now, so a policy change does not wait for the member's next
+ // credential.
+ member = attach(t, a)
+ if resp := ask(t, member, a.Host.ID(), provide(weather, "mcp://weather")); resp.GetError() == "" {
+ t.Fatal("stored under a policy granting nothing; want a refusal")
+ }
+ rules, _ := api.BuildPolicyRules([]*api.PolicyRole{{Name: "member", AllowedPublish: []string{"mcp://weather"}}}, nil)
+ texts := api.PolicyRuleTexts(rules)
+ parsed, err := api.ParseDatalogRules(texts)
+ if err != nil {
+ t.Fatal(err)
+ }
+ a.policy.Store(&meshPolicy{rules: parsed, texts: texts})
+ if resp := ask(t, member, a.Host.ID(), provide(weather, "mcp://weather")); resp.GetError() != "" {
+ t.Fatalf("refused under a policy granting the role mcp://weather: %s", resp.GetError())
+ }
+ if resp := ask(t, member, a.Host.ID(), provide(calc, "mcp://calculator")); resp.GetError() == "" {
+ t.Fatal("stored mcp://calculator under a policy granting the role mcp://weather only; want a refusal")
+ }
}
diff --git a/internal/router/router.go b/internal/router/router.go
index f1a957b5..20c39f18 100644
--- a/internal/router/router.go
+++ b/internal/router/router.go
@@ -27,6 +27,7 @@ import (
"os"
"path/filepath"
"runtime/debug"
+ "slices"
"strings"
"sync"
"sync/atomic"
@@ -342,6 +343,10 @@ type Router struct {
// credentials is the credential each admitted member presented, as
// memberCredential, for what the directory checks against it.
credentials sync.Map
+ // policy is the mesh policy as the control plane renders it
+ // (PolicyConfigGetResponse.datalog_rules), what a member's credential is
+ // evaluated with besides its own facts; nil until the first sync.
+ policy atomic.Pointer[meshPolicy]
bannedPeers sync.Map
// directory is how the mesh keeps its directory, read from /info at
// start (syncDirectory); records is the records this router holds.
@@ -374,7 +379,8 @@ type Router struct {
// only the last biscuit it issued.
refreshMu sync.Mutex
- keysSyncTrigger chan struct{}
+ keysSyncTrigger chan struct{}
+ policySyncTrigger chan struct{}
// Control contexts
ctx context.Context
@@ -409,11 +415,12 @@ func NewRouter(ctx context.Context, config Options) (*Router, error) {
}
return &Router{
- config: config,
- ctx: ctx,
- cancel: cancel,
- handshakeLimiter: handshakeLimiter,
- keysSyncTrigger: make(chan struct{}, 1),
+ config: config,
+ ctx: ctx,
+ cancel: cancel,
+ handshakeLimiter: handshakeLimiter,
+ keysSyncTrigger: make(chan struct{}, 1),
+ policySyncTrigger: make(chan struct{}, 1),
}, nil
}
@@ -592,9 +599,10 @@ func (r *Router) Start() (retErr error) {
})
// 5. Start background routines
- r.wg.Add(8)
+ r.wg.Add(9)
go r.runLeaseRenewalLoop()
go r.runKeysSyncLoop()
+ go r.runPolicySyncLoop()
go r.runFederationLoop()
go r.runBiscuitRenewalLoop()
go r.runShedLoop()
@@ -1043,6 +1051,9 @@ func (r *Router) listenForControlPlaneEvents(ctx context.Context) {
case api.MeshEvent_KEY_ROTATION:
logger.Infof("[Router Event] Received KEY_ROTATION event, triggering key sync")
r.triggerKeysSync()
+ case api.MeshEvent_POLICY_UPDATE:
+ logger.Infof("[Router Event] Received POLICY_UPDATE event, triggering policy sync")
+ r.triggerPolicySync()
}
}
}
@@ -1072,6 +1083,80 @@ func (r *Router) runKeysSyncLoop() {
}
}
+func (r *Router) triggerPolicySync() {
+ select {
+ case r.policySyncTrigger <- struct{}{}:
+ default:
+ }
+}
+
+// runPolicySyncLoop pulls the mesh policy at start, on each POLICY_UPDATE
+// event and every PolicySyncInterval: the event is best effort, the pull is
+// what bounds how long a policy change takes to reach this router.
+func (r *Router) runPolicySyncLoop() {
+ defer r.wg.Done()
+ ticker := time.NewTicker(r.config.PolicySyncInterval)
+ defer ticker.Stop()
+
+ for {
+ if err := r.syncPolicy(); err != nil {
+ logger.Errorf("Failed policy sync: %v", err)
+ }
+ select {
+ case <-ticker.C:
+ case <-r.policySyncTrigger:
+ case <-r.ctx.Done():
+ return
+ }
+ }
+}
+
+// syncPolicy replaces the mesh policy rules with what the control plane
+// renders now. The text is the contract: the router adds it to the
+// authorizer and derives nothing from roles and bindings itself.
+func (r *Router) syncPolicy() error {
+ r.keysMu.RLock()
+ biscuit := r.credential.biscuit
+ r.keysMu.RUnlock()
+ if len(biscuit) == 0 {
+ return errors.New("router has no credential yet")
+ }
+ resp, err := r.controlPlane(10*time.Second).FetchPolicy(r.ctx, biscuit)
+ if err != nil {
+ return err
+ }
+ if len(resp.ProtoReflect().GetUnknown()) > 0 {
+ return fmt.Errorf("control plane %s predates datalog_rules in its policy response; upgrade the control plane", r.config.ControlPlaneURL)
+ }
+ rules, err := api.ParseDatalogRules(resp.DatalogRules)
+ if err != nil {
+ return fmt.Errorf("mesh policy carries an unparseable rule: %w", err)
+ }
+ before := r.policy.Swap(&meshPolicy{rules: rules, texts: resp.DatalogRules})
+ if before == nil || !slices.Equal(before.texts, resp.DatalogRules) {
+ logger.Infof("Synced mesh policy (%d rules)", len(rules))
+ } else {
+ logger.Debugf("Synced mesh policy (%d rules, unchanged)", len(rules))
+ }
+ return nil
+}
+
+// meshPolicy is one synced rendering of the mesh policy: the rules the
+// authorizer runs and the text they were parsed from, to tell a change.
+type meshPolicy struct {
+ rules []biscuit.Rule
+ texts []string
+}
+
+// meshPolicyRules is the mesh policy as last synced; empty before the first
+// sync, when only a credential's own facts decide.
+func (r *Router) meshPolicyRules() []biscuit.Rule {
+ if p := r.policy.Load(); p != nil {
+ return p.rules
+ }
+ return nil
+}
+
func (r *Router) runLeaseRenewalLoop() {
defer r.wg.Done()
ticker := time.NewTicker(r.config.LeaseRenewInterval)
diff --git a/internal/storage/mesh_policy_test.go b/internal/storage/mesh_policy_test.go
index 0e17aacc..e3c9bea1 100644
--- a/internal/storage/mesh_policy_test.go
+++ b/internal/storage/mesh_policy_test.go
@@ -46,8 +46,10 @@ func TestMeshPolicyRoundTripsEveryRoleField(t *testing.T) {
AllowedServices: []string{"mcp://tool"},
CustomDatalog: []string{`region("emea")`},
AllowedLabels: []string{"region=*"},
- AllowedPublish: []string{"mcp://tool", "inference://*"},
- Http: []*api.HTTPGrant{{Service: "mcp://tool", Methods: []string{"GET"}, Paths: []string{"/v1/*"}}},
+ // Sorted as the store reads them back: the order of a list has no
+ // meaning in the policy, and a stable one is what tells a change.
+ AllowedPublish: []string{"inference://*", "mcp://tool"},
+ Http: []*api.HTTPGrant{{Service: "mcp://tool", Methods: []string{"GET"}, Paths: []string{"/v1/*"}}},
}
if err := store.SaveMeshPolicy(ctx, []*api.PolicyRole{want}, nil); err != nil {
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index 71676d81..f8fe901a 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -1272,9 +1272,11 @@ func (s *SQLStore) saveMeshPolicyTx(ctx context.Context, tx *sql.Tx, roles []*ap
return nil
}
-// GetMeshPolicy retrieves the entire mesh policy as structured data.
+// GetMeshPolicy retrieves the entire mesh policy as structured data, in an
+// order that does not change between reads: the policy is rendered to text
+// a member compares with the last text it fetched to tell a change.
func (s *SQLStore) GetMeshPolicy(ctx context.Context) ([]*api.PolicyRole, []*api.PolicyBinding, error) {
- rolesRows, err := s.db.QueryContext(ctx, s.rebind("SELECT name FROM roles"))
+ rolesRows, err := s.db.QueryContext(ctx, s.rebind("SELECT name FROM roles ORDER BY name"))
if err != nil {
return nil, nil, err
}
@@ -1296,7 +1298,7 @@ func (s *SQLStore) GetMeshPolicy(ctx context.Context) ([]*api.PolicyRole, []*api
return nil, nil, err
}
- permsRows, err := s.db.QueryContext(ctx, s.rebind("SELECT role_name, resource_type, resource_value FROM role_permissions"))
+ permsRows, err := s.db.QueryContext(ctx, s.rebind("SELECT role_name, resource_type, resource_value FROM role_permissions ORDER BY role_name, resource_type, resource_value"))
if err != nil {
return nil, nil, err
}
@@ -1332,34 +1334,28 @@ func (s *SQLStore) GetMeshPolicy(ctx context.Context) ([]*api.PolicyRole, []*api
return nil, nil, err
}
- bindingsRows, err := s.db.QueryContext(ctx, s.rebind("SELECT role_name, member FROM role_bindings"))
+ bindingsRows, err := s.db.QueryContext(ctx, s.rebind("SELECT role_name, member FROM role_bindings ORDER BY role_name, member"))
if err != nil {
return nil, nil, err
}
defer func() { _ = bindingsRows.Close() }()
- bindingsMap := make(map[string]*api.PolicyBinding)
+ var bindings []*api.PolicyBinding
for bindingsRows.Next() {
var roleName, member string
if err := bindingsRows.Scan(&roleName, &member); err != nil {
return nil, nil, err
}
- b, ok := bindingsMap[roleName]
- if !ok {
- b = &api.PolicyBinding{Role: roleName}
- bindingsMap[roleName] = b
+ if n := len(bindings); n > 0 && bindings[n-1].Role == roleName {
+ bindings[n-1].Members = append(bindings[n-1].Members, member)
+ continue
}
- b.Members = append(b.Members, member)
+ bindings = append(bindings, &api.PolicyBinding{Role: roleName, Members: []string{member}})
}
if err := bindingsRows.Err(); err != nil {
return nil, nil, err
}
- var bindings []*api.PolicyBinding
- for _, b := range bindingsMap {
- bindings = append(bindings, b)
- }
-
return roles, bindings, nil
}
diff --git a/sdk/js/src/biscuit.ts b/sdk/js/src/biscuit.ts
index 793a0a94..fc1234f9 100644
--- a/sdk/js/src/biscuit.ts
+++ b/sdk/js/src/biscuit.ts
@@ -281,7 +281,7 @@ export function requireRole(verified: VerifiedBiscuit, role: string): void {
* who may serve it. The provider's own catalog names no service and needs
* no grant; so does an empty target.
*/
-export async function requirePublish(verified: VerifiedBiscuit, targetService: string): Promise<void> {
+export async function requirePublish(verified: VerifiedBiscuit, targetService: string, policyRules: readonly string[] = []): Promise<void> {
if (targetService === "" || targetService === `system://${BASELINE_DATALOG.catalog_target}`) {
return;
}
@@ -293,6 +293,12 @@ export async function requirePublish(verified: VerifiedBiscuit, targetService: s
const token = wasm.Biscuit.fromBytes(verified.token, wasm.PublicKey.fromBytes(verified.verifyingKey, wasm.SignatureAlgorithm.Ed25519));
const builder = new wasm.AuthorizerBuilder();
builder.addFact(wasm.Fact.fromString(`${BASELINE_DATALOG.fact_publish}(${JSON.stringify(m[1])}, ${JSON.stringify(m[2])})`));
+ // The mesh policy derives grants from the credential's role() and label()
+ // facts, so a policy change reaches this check before the provider's
+ // credential is minted again.
+ for (const r of policyRules) {
+ builder.addRule(wasm.Rule.fromString(r));
+ }
for (const policy of BASELINE_DATALOG.publish_policies) {
builder.addPolicy(wasm.Policy.fromString(policy));
}
diff --git a/sdk/js/src/mcp.test.ts b/sdk/js/src/mcp.test.ts
index 1bc821b7..4300875a 100644
--- a/sdk/js/src/mcp.test.ts
+++ b/sdk/js/src/mcp.test.ts
@@ -278,6 +278,16 @@ test("only a node granted the service is its provider, as agentmesh-node's check
await (await openMCPSession(conn, frame(""), [cpKey])).close();
}
}
+
+ // The mesh policy the session synced grants too, from the provider's
+ // role: a policy change reaches the caller at its next sync, not at
+ // the provider's next credential.
+ providerBiscuit = mint(provider.peerId.toString(), ROLE_NODE, { region: "eu" }, []);
+ const rules = [`granted_publish_set("mcp", ["calc"]) <- role(${JSON.stringify(ROLE_NODE)})`];
+ let conn = await caller.dial(provider.getMultiaddrs()[0] as Parameters<typeof caller.dial>[0]);
+ await (await openMCPSession(conn, frame("mcp://calc"), [cpKey], { policyRules: rules })).close();
+ conn = await caller.dial(provider.getMultiaddrs()[0] as Parameters<typeof caller.dial>[0]);
+ await assert.rejects(openMCPSession(conn, frame("mcp://weather"), [cpKey], { policyRules: rules }), /is not granted serving mcp:\/\/weather/);
} finally {
providerBiscuit = nodeBiscuit;
}
diff --git a/sdk/js/src/mcp.ts b/sdk/js/src/mcp.ts
index 6f597d0c..92cede73 100644
--- a/sdk/js/src/mcp.ts
+++ b/sdk/js/src/mcp.ts
@@ -91,6 +91,8 @@ export interface MCPSessionOptions {
/** Labels the provider's credential must all carry, e.g. { region: "eu", compliance: "gdpr" }. */
requiredLabels?: Record<string, string>;
signal?: AbortSignal;
+ /** The mesh policy as the session synced it (Session.policyRules); set by the session, not the caller. */
+ policyRules?: readonly string[];
}
/** An open MCP session with a provider on the mesh. */
@@ -174,7 +176,7 @@ export async function openMCPSession(
// Only nodes host services; a router's or an admin's credential is a member, not a provider.
requireRole(provider, ROLE_NODE);
// And only a node the policy lets serve the named service is one.
- await requirePublish(provider, decodeAuthFrame(frame).targetService);
+ await requirePublish(provider, decodeAuthFrame(frame).targetService, options.policyRules);
requireLabels(provider, options.requiredLabels);
requireEgressLabels(provider, egressRequireLabels);
} catch (err) {
diff --git a/sdk/js/src/session.ts b/sdk/js/src/session.ts
index 17179095..3e3fe5d1 100644
--- a/sdk/js/src/session.ts
+++ b/sdk/js/src/session.ts
@@ -907,7 +907,7 @@ export class MeshSession {
*/
async openMCP(peer: Peer, targetService: string, options: MCPSessionOptions = {}): Promise<MCPSession> {
const conn = await this.connect(peer, options.signal);
- return openMCPSession(conn, encodeAuthFrame(this.biscuit, targetService), this.mesh.credential.controlPlaneKeys, options, this.#egressRequireLabels);
+ return openMCPSession(conn, encodeAuthFrame(this.biscuit, targetService), this.mesh.credential.controlPlaneKeys, { ...options, policyRules: this.policyRules }, this.#egressRequireLabels);
}
/** Lists the tools a provider serves for a service. */
@@ -1006,12 +1006,13 @@ export class MeshSession {
if (!this.#closed) {
void this.keepRelay().catch(() => {});
}
- if (this.endpoint !== undefined) {
- try {
- await this.syncPolicy();
- } catch (err) {
- result.errors.push(`policy: ${err instanceof Error ? err.message : String(err)}`);
- }
+ // Callers evaluate the policy too: it is what grants a provider the
+ // service it serves (requirePublish), not only what an A2A endpoint
+ // authorizes its callers with.
+ try {
+ await this.syncPolicy();
+ } catch (err) {
+ result.errors.push(`policy: ${err instanceof Error ? err.message : String(err)}`);
}
return result;
}
@@ -1095,7 +1096,7 @@ export class MeshSession {
/**
* The mesh policy rules this member evaluates for callers, as the control
* plane rendered them (PolicyConfigGetResponse.datalog_rules). Empty until
- * acceptA2A() or syncPolicy().
+ * the first control plane sync, acceptA2A() or syncPolicy().
*/
get policyRules(): string[] {
return this.#policyRules ?? [];
@@ -1130,7 +1131,7 @@ export class MeshSession {
const provider = await authenticateWithPeer(conn, this.mesh.authFrame(), this.mesh.credential.controlPlaneKeys);
// Only nodes host services; a router's or an admin's credential is a member, not a provider.
requireRole(provider, ROLE_NODE);
- await requirePublish(provider, targetService);
+ await requirePublish(provider, targetService, this.policyRules);
requireEgressLabels(provider, this.#egressRequireLabels);
this.#egressVerdicts.set(verdict, new Date(Date.now() + EGRESS_VERDICT_TTL_MS));
return conn;
diff --git a/sdk/js/src/sync.test.ts b/sdk/js/src/sync.test.ts
index d9f24081..467fae44 100644
--- a/sdk/js/src/sync.test.ts
+++ b/sdk/js/src/sync.test.ts
@@ -44,6 +44,7 @@ import {
KeysResponseSchema,
MeshEvent_Type,
MeshEventSchema,
+ PolicyConfigGetResponseSchema,
TokenRefreshResponseSchema,
} from "./gen/agentmesh_pb.ts";
import { Identity } from "./identity.ts";
@@ -125,6 +126,9 @@ class FakeControlPlane {
),
);
}
+ if (req.method === "GET" && path === "/policies") {
+ return proto(toBinary(PolicyConfigGetResponseSchema, create(PolicyConfigGetResponseSchema, { datalogRules: ['granted_publish_all_types(true) <- role("mesh:role:node")'] })));
+ }
if (req.method === "POST" && path === "/refresh") {
this.refreshes++;
// The peer is whoever the presented biscuit is bound to; every valid key is tried.
diff --git a/sdk/python/src/agent_mesh/biscuit.py b/sdk/python/src/agent_mesh/biscuit.py
index 279a818e..8c7c3cd6 100644
--- a/sdk/python/src/agent_mesh/biscuit.py
+++ b/sdk/python/src/agent_mesh/biscuit.py
@@ -301,12 +301,15 @@ def require_role(verified: VerifiedBiscuit, role: str) -> None:
raise BiscuitVerificationError(f"biscuit lacks expected role {role!r}")
-def require_publish(verified: VerifiedBiscuit, target_service: str) -> None:
+def require_publish(verified: VerifiedBiscuit, target_service: str, policy_rules: Sequence[str] = ()) -> None:
"""Requires that an already verified provider credential grants serving
target_service ("<type>://<name>"), as identity.RequirePublish: the
directory names whoever announced the service, only the credential says
- who may serve it. The provider's own catalog names no service and needs
- no grant; so does an empty target."""
+ who may serve it. policy_rules is the mesh policy as the session synced
+ it; it derives grants from the credential's role() and label() facts, so
+ a policy change reaches this check before the provider's credential is
+ minted again. The provider's own catalog names no service and needs no
+ grant; so does an empty target."""
if target_service == "" or target_service == f"system://{_DATALOG['catalog_target']}":
return
scheme, sep, name = target_service.partition("://")
@@ -316,6 +319,8 @@ def require_publish(verified: VerifiedBiscuit, target_service: str) -> None:
builder = ba.AuthorizerBuilder()
builder.set_limits(_limits())
builder.add_fact(ba.Fact(_DATALOG["fact_publish"] + "({t}, {n})", {"t": scheme, "n": name}))
+ for rule in policy_rules:
+ builder.add_rule(ba.Rule(rule))
for policy in _DATALOG["publish_policies"]:
builder.add_policy(ba.Policy(policy))
try:
diff --git a/sdk/python/src/agent_mesh/mcp_client.py b/sdk/python/src/agent_mesh/mcp_client.py
index 3080efc2..5569e8f2 100644
--- a/sdk/python/src/agent_mesh/mcp_client.py
+++ b/sdk/python/src/agent_mesh/mcp_client.py
@@ -112,11 +112,13 @@ async def open_mcp_session(
*,
required_labels: Optional[Mapping[str, str]] = None,
egress_require_labels: Optional[Mapping[str, str]] = None,
+ policy_rules: Sequence[str] = (),
) -> AsyncIterator[tuple[ClientSession, VerifiedBiscuit]]:
"""Opens /mesh/mcp/1.0.0 to a connected provider with `frame`, this member's
AuthFrame naming the service, verifies the provider and yields an
initialized MCP ClientSession with the provider's credential; egress_require_labels
- is the session's, not the caller's (require_egress_labels)."""
+ and policy_rules are the session's, not the caller's (require_egress_labels,
+ Session.policy_rules)."""
stream = await open_stream(host, peer_id, MCP_PROTOCOL, AUTH_HANDSHAKE_TIMEOUT)
try:
with trio.fail_after(AUTH_HANDSHAKE_TIMEOUT):
@@ -133,7 +135,7 @@ async def open_mcp_session(
# Only nodes host services; a router's or an admin's credential is a member, not a provider.
require_role(provider, ROLE_NODE)
# And only a node the policy lets serve the named service is one.
- require_publish(provider, pb.AuthFrame.FromString(frame).target_service)
+ require_publish(provider, pb.AuthFrame.FromString(frame).target_service, policy_rules)
except BiscuitVerificationError as err:
raise AuthRejectedError(str(peer_id), f"provider credential rejected: {err}") from err
require_labels(provider, required_labels)
diff --git a/sdk/python/src/agent_mesh/session.py b/sdk/python/src/agent_mesh/session.py
index 9d2a74aa..5345482f 100644
--- a/sdk/python/src/agent_mesh/session.py
+++ b/sdk/python/src/agent_mesh/session.py
@@ -732,7 +732,13 @@ class MeshSession:
peer_id = await self.connect(peer)
frame = encode_auth_frame(self.biscuit, target_service)
async with open_mcp_session(
- self.host, peer_id, frame, self.mesh.credential.control_plane_keys, required_labels=required_labels, egress_require_labels=self.egress_require_labels
+ self.host,
+ peer_id,
+ frame,
+ self.mesh.credential.control_plane_keys,
+ required_labels=required_labels,
+ egress_require_labels=self.egress_require_labels,
+ policy_rules=self.policy_rules,
) as opened_session:
yield opened_session
@@ -757,7 +763,7 @@ class MeshSession:
def policy_rules(self) -> list[str]:
"""The mesh policy rules this member evaluates for callers, as the
control plane rendered them (PolicyConfigGetResponse.datalog_rules).
- Empty until accept_a2a() or sync_policy()."""
+ Empty until the first control plane sync, accept_a2a() or sync_policy()."""
return list(self._policy_rules or [])
async def sync_policy(self) -> None:
@@ -781,11 +787,13 @@ class MeshSession:
if len(self._held_router_ids) < self._want_routers():
await self.top_up_routers()
self._relay_trigger.set()
- if self.endpoint is not None:
- try:
- await self.sync_policy()
- except Exception as err: # noqa: BLE001 - the last good policy stays in force
- result.errors.append(f"policy: {err}")
+ # Callers evaluate the policy too: it is what grants a provider the
+ # service it serves (require_publish), not only what an A2A
+ # endpoint authorizes its callers with.
+ try:
+ await self.sync_policy()
+ except Exception as err: # noqa: BLE001 - the last good policy stays in force
+ result.errors.append(f"policy: {err}")
return result
def trigger_sync(self) -> None:
@@ -1014,7 +1022,7 @@ class MeshSession:
provider = await authenticate_with_peer(self.host, peer_id, self.mesh.auth_frame(), self.mesh.credential.control_plane_keys)
# Only nodes host services; a router's or an admin's credential is a member, not a provider.
require_role(provider, ROLE_NODE)
- require_publish(provider, target_service)
+ require_publish(provider, target_service, self.policy_rules)
require_egress_labels(provider, self.egress_require_labels)
self._egress_verdicts[verdict] = time.monotonic() + EGRESS_VERDICT_TTL
return peer_id
diff --git a/sdk/python/tests/test_mcp.py b/sdk/python/tests/test_mcp.py
index 9325feae..0523eb95 100644
--- a/sdk/python/tests/test_mcp.py
+++ b/sdk/python/tests/test_mcp.py
@@ -266,6 +266,18 @@ def test_tools_over_the_mesh_stream():
pass
async with open_mcp_session(caller, other.get_id(), frame(caller_biscuit, ""), [CP_KEY]):
pass
+
+ # The mesh policy the session synced grants too, from the
+ # provider's role: a policy change reaches the caller at its
+ # next sync, not at the provider's next credential.
+ ungranted, ungranted_addr, _ = await start_provider(nursery, publish=())
+ await caller.connect(info_from_p2p_addr(ungranted_addr))
+ rules = [f'granted_publish_set("mcp", ["calc"]) <- role("{ROLE_NODE}")']
+ async with open_mcp_session(caller, ungranted.get_id(), frame(caller_biscuit, "mcp://calc"), [CP_KEY], policy_rules=rules):
+ pass
+ with pytest.raises(AuthRejectedError, match="is not granted serving mcp://weather"):
+ async with open_mcp_session(caller, ungranted.get_id(), frame(caller_biscuit, "mcp://weather"), [CP_KEY], policy_rules=rules):
+ pass
nursery.cancel_scope.cancel()
async def with_timeout():
diff --git a/sdk/python/tests/test_sync.py b/sdk/python/tests/test_sync.py
index 709f43df..f5745875 100644
--- a/sdk/python/tests/test_sync.py
+++ b/sdk/python/tests/test_sync.py
@@ -101,6 +101,8 @@ class FakeControlPlane:
return 200, pb.KeysResponse(public_keys=[k.pub for k in self.keys], sign_time=unsigned.sign_time, signatures=[k.identity.sign(payload) for k in self.keys]).SerializeToString()
if (method, path) == ("GET", "/info"):
return 200, pb.ControlPlaneInfoResponse(router_addresses=[self.router_addr], banned_peer_ids=self.banned).SerializeToString()
+ if (method, path) == ("GET", "/policies"):
+ return 200, pb.PolicyConfigGetResponse(datalog_rules=['granted_publish_all_types(true) <- role("mesh:role:node")']).SerializeToString()
if (method, path) == ("POST", "/refresh"):
self.refreshes += 1
presented = base64.b64decode(headers["Authorization"].removeprefix("Bearer "))
diff --git a/site/content/docs/reference/policy.md b/site/content/docs/reference/policy.md
index bdabc4c8..ea14a783 100644
--- a/site/content/docs/reference/policy.md
+++ b/site/content/docs/reference/policy.md
@@ -121,6 +121,17 @@ An egress destination needs no entry: `served_by` selects the nodes that
serve it, and the control plane mints `granted_publish_exact("egress", "<name>")`
into their credentials with the destination.
+A change to `allowed_publish` takes effect without a new credential. The
+grant is also rendered into the policy text as
+`granted_publish_*(...) <- role("<role>")` (and `<- label(...)` for
+`served_by`), and every verifier adds that text to its authorizer: the
+router, a node and an SDK member at their next sync, all of them at once on
+the `POLICY_UPDATE` event
+the control plane publishes. A node whose policy text changed announces its
+services again, so a record the router refused under the previous policy is
+in the directory within the sync interval; until then the router answers
+`credential does not grant serving <service>`, which the node logs.
+
### Target patterns
`fact:value`, where `fact` is one of the identity facts that a node's
@@ -343,8 +354,9 @@ At enrollment, refresh, and token exchange, the control plane resolves the
identity's roles from the bindings and mints into the credential one `role()`
fact per role and the compiled `granted_*` facts of every role. The control
plane also renders the policy as Datalog rules served as `datalog_rules` at
-`GET /policies`. Nodes fetch this text (`--control-plane-sync-interval`, 15
-minutes) and add it to their authorizer as it arrives.
+`GET /policies`. Nodes, routers and SDK members fetch this text
+(`--control-plane-sync-interval`, 15 minutes) and add it to their authorizer
+as it arrives.
At the destination node, in this order:
diff --git a/site/content/docs/reference/router.md b/site/content/docs/reference/router.md
index 32eada20..390bb4f1 100644
--- a/site/content/docs/reference/router.md
+++ b/site/content/docs/reference/router.md
@@ -77,7 +77,9 @@ replica count.
closest to the key, as many as the mesh's replication (`/info`). A
record is kept only for a service the member's credential grants serving
(the role's `allowed_publish`), checked against the credential the
- handshake presented; a record for anything else is refused. A record
+ handshake presented and the mesh policy the router synced, so a grant
+ added to a role reaches the directory at the next pass; a record for
+ anything else is refused. A record
lives for the mesh's record TTL after its last announcement,
and an announcement replaces the addresses of the one before; a member
that stops a service or leaves withdraws its records, and the router
diff --git a/tests/integration/publish_grant_test.go b/tests/integration/publish_grant_test.go
new file mode 100644
index 00000000..852cb4a3
--- /dev/null
+++ b/tests/integration/publish_grant_test.go
@@ -0,0 +1,151 @@
+// 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 integration_test
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/google/agentmesh/api"
+)
+
+// TestUngrantedServiceIsNeitherPublishedNorServed pins the publish grant end
+// to end, from the policy to both verifiers. The node role grants
+// mcp://granted and nothing else; a node declares that service and a second
+// one, mcp://ungranted, both running. The router the node holds keeps the
+// granted record and refuses the other, so no caller finds the second by
+// name, and a caller that names the node by peer ID anyway is refused by its
+// own node before the request leaves it. Then the policy grants the second
+// service too, and nothing is restarted: the POLICY_UPDATE event brings the
+// change to the router, the provider and the caller, the provider announces
+// again, and the service is found and called.
+func TestUngrantedServiceIsNeitherPublishedNorServed(t *testing.T) {
+ m := startAttachmentMesh(t)
+
+ grant := func(publish string) {
+ t.Helper()
+ policyFile := filepath.Join(m.tmpDir, "publish-policy.yaml")
+ policy := fmt.Sprintf(`roles:
+ - name: %s
+ allowed_services: []
+ allowed_targets: ["*"]
+ - name: %s
+ allowed_services: ["mcp://*"]
+ allowed_publish: [%q]
+ allowed_targets: ["*"]
+bindings:
+ - role: %s
+ members: ["group:routers"]
+ - role: %s
+ members: ["user:mock-user"]
+`, api.RoleRouter, api.RoleNode, publish, api.RoleRouter, api.RoleNode)
+ if err := os.WriteFile(policyFile, []byte(policy), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ injectPolicyYAML(t, m.cpPort, testAdminToken, policyFile)
+ }
+ grant("mcp://granted")
+
+ router := m.startRouter(t, "router")
+
+ mcpServer := httptest.NewServer(newBoundaryMCPHandler(t))
+ t.Cleanup(mcpServer.Close)
+
+ // A node acts on a POLICY_UPDATE event after a random delay of up to a
+ // tenth of its sync interval, so a fleet does not pull together; the
+ // default 15 minutes would make that up to 90 seconds here.
+ common := append(m.nodeArgs(), "--allow-loopback", "--control-plane-sync-interval", "5s")
+ provider := launchNode(t, m.nodeBin, m.nodeEnv("provider"), filepath.Join(m.tmpDir, "provider"), append(slices.Clone(common),
+ "--api-token-path", tokenPath(t, "token-provider"),
+ "--config", writeNodeConfig(t, m.tmpDir, nil,
+ svcDecl{Type: "mcp", Name: "granted", TargetURL: mcpServer.URL},
+ svcDecl{Type: "mcp", Name: "ungranted", TargetURL: mcpServer.URL}),
+ )...)
+ caller := launchNode(t, m.nodeBin, m.nodeEnv("caller"), filepath.Join(m.tmpDir, "caller"), append(slices.Clone(common),
+ "--api-token-path", tokenPath(t, "token-caller"),
+ )...)
+ provider.waitForAPI(t)
+ caller.waitForAPI(t)
+
+ // The granted service is found by name and answers.
+ callAcrossMesh(t, caller, "token-caller", provider, "granted")
+
+ // The router refused the other service's records, by name and by type,
+ // and said so; the node logged the refusal it got back.
+ denied := `agentmesh_router_directory_requests_total{op="provide",result="denied"}`
+ deadline := time.Now().Add(10 * time.Second)
+ for router.gauge(t, denied) < 2 {
+ if time.Now().After(deadline) {
+ t.Fatalf("router counted %v denied records, want 2 (mcp://ungranted by name and by type)\n--- provider ---\n%s", router.gauge(t, denied), provider.log())
+ }
+ time.Sleep(200 * time.Millisecond)
+ }
+ if log := provider.log(); !strings.Contains(log, "credential does not grant serving mcp://ungranted") {
+ t.Fatalf("the provider did not log the refusal\n--- provider ---\n%s", log)
+ }
+
+ client := &http.Client{Timeout: 5 * time.Second}
+ discover, _ := http.NewRequest(http.MethodGet, "http://"+caller.apiAddr+"/mesh/service/discover?type=mcp&name=ungranted", nil)
+ discover.Header.Set(api.HeaderMeshAuthentication, "Bearer token-caller")
+ resp, err := client.Do(discover)
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if strings.Contains(string(body), provider.peerID.String()) {
+ t.Fatalf("mcp://ungranted is in the directory: %s", body)
+ }
+
+ // Named by peer ID, the caller's own node checks the provider's
+ // credential and refuses before anything crosses the mesh.
+ call, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("http://%s/mesh/%s/mcp/ungranted", caller.apiAddr, provider.peerID),
+ bytes.NewBufferString(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`))
+ call.Header.Set("Content-Type", "application/json")
+ call.Header.Set("Accept", "application/json, text/event-stream")
+ call.Header.Set(api.HeaderMeshAuthentication, "Bearer token-caller")
+ resp, err = client.Do(call)
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, _ = io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusForbidden || !strings.Contains(string(body), "provider is not granted mcp://ungranted") {
+ t.Fatalf("call to mcp://ungranted by peer ID: %d %s, want 403 naming the missing grant\n--- caller ---\n%s", resp.StatusCode, body, caller.log())
+ }
+
+ // The policy now grants every mcp service. No credential is minted
+ // again: the grant travels in the policy text, the event brings it to
+ // the router and both nodes, the provider announces again, and the
+ // service is found and called.
+ grant("mcp://*")
+ deadline = time.Now().Add(10 * time.Second)
+ for !strings.Contains(provider.log(), "Mesh policy changed; announcing this node's records again") {
+ if time.Now().After(deadline) {
+ t.Fatalf("the provider did not learn of the policy change\n--- provider ---\n%s", provider.log())
+ }
+ time.Sleep(200 * time.Millisecond)
+ }
+ callAcrossMesh(t, caller, "token-caller", provider, "ungranted")
+}
diff --git a/tests/integration/router_attachment_test.go b/tests/integration/router_attachment_test.go
index 3a438e17..6440e066 100644
--- a/tests/integration/router_attachment_test.go
+++ b/tests/integration/router_attachment_test.go
@@ -53,6 +53,10 @@ type routerFixture struct {
// test sets it before starting its routers and clears it after.
var extraRouterArgs []string
+// extraControlPlaneArgs is appended to the control plane startAttachmentMesh
+// starts; a test sets it before starting the mesh and clears it after.
+var extraControlPlaneArgs []string
+
// gauge reads one metric from the router's /metrics, by its full name with
// labels, and fails when it is not there.
func (r *routerFixture) gauge(t *testing.T, name string) float64 {
@@ -374,14 +378,14 @@ bindings:
}
m.cpPort = getFreePort(t)
- cpCmd := exec.Command(cpBin,
+ cpCmd := exec.Command(cpBin, append([]string{
"--bind-address", fmt.Sprintf("127.0.0.1:%d", m.cpPort),
"--admin-token-path", tokenPath(t, testAdminToken),
- "--db-dsn", filepath.Join(m.tmpDir, "cp.db")+"?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)",
+ "--db-dsn", filepath.Join(m.tmpDir, "cp.db") + "?_pragma=journal_mode(DELETE)&_pragma=busy_timeout(5000)",
"--issuer", oidcURL,
"--insecure-skip-tls-verify",
"--mesh-reconnect-interval", "500ms",
- )
+ }, extraControlPlaneArgs...)...)
cpLog := filepath.Join(m.tmpDir, "control-plane.log")
cpLogFile, err := os.Create(cpLog)
if err != nil {