Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
identity: merge exact grants across all matched roles at mint time
Antonio Ojead7df56b6ec54
diff --git a/internal/controlplane/policy_scale_test.go b/internal/controlplane/policy_scale_test.goindex 197ce24d..c19a3913 100644--- a/internal/controlplane/policy_scale_test.go+++ b/internal/controlplane/policy_scale_test.go@@ -258,3 +258,80 @@ func TestValidatePolicyConfigFactBudget(t *testing.T) {}})}++// TestPolicyScaleManyRoles demonstrates that mintBiscuit now merges exact+// grants across ALL of an identity's matched roles (not just within a single+// role, see TestPolicyScaleSetEncoding) into one Set fact per service type /+// target fact name for the whole token, instead of one Set fact per role.+// That removes what used to be the dominant per-role cost (~2-3 facts/role),+// leaving only the one role(X) fact biscuit-go still has to add per matched+// role - a much cheaper, but still O(matched roles), residual cost. So the+// ceiling for "many roles matching one identity" grows roughly 3-4x (from+// ~300 to ~900 roles) rather than becoming flat; going meaningfully higher+// would require not embedding one role(X) fact per matched role at all.+//+// This deliberately bypasses validatePolicyConfig: that guard stays+// conservative (see maxIdentityFactBudget) because it must also bound+// internal/node/policy.go's mesh policy rules, which resolve roles+// dynamically from live claims at request time and so cannot pre-merge+// grants across roles the way mint time can. Probe further with e.g.:+//+// SAM_POLICY_SCALE_ROLES_MAX=900 go test ./internal/controlplane -run TestPolicyScaleManyRoles -v+func TestPolicyScaleManyRoles(t *testing.T) {+ roleTiers := []int{10, 100, 800}+ if extra := os.Getenv("SAM_POLICY_SCALE_ROLES_MAX"); extra != "" {+ n, err := strconv.Atoi(extra)+ if err != nil {+ t.Fatalf("invalid SAM_POLICY_SCALE_ROLES_MAX=%q: %v", extra, err)+ }+ roleTiers = append(roleTiers, n)+ }++ pub, priv, err := ed25519.GenerateKey(rand.Reader)+ if err != nil {+ t.Fatal(err)+ }++ nodeKey, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)+ if err != nil {+ t.Fatal(err)+ }+ nodePeer, err := peer.IDFromPrivateKey(nodeKey)+ if err != nil {+ t.Fatal(err)+ }++ for _, n := range roleTiers {+ roles := make([]*api.PolicyRole, 0, n)+ roleNames := make([]string, 0, n)+ for i := 0; i < n; i++ {+ name := fmt.Sprintf("role-%d", i)+ roleNames = append(roleNames, name)+ roles = append(roles, &api.PolicyRole{+ Name: name,+ AllowedServices: []string{+ fmt.Sprintf("mcp://svc-%d-a.example", i),+ fmt.Sprintf("mcp://svc-%d-b.example", i),+ },+ AllowedTargets: []string{fmt.Sprintf("group:team-%d", i)},+ })+ }++ claims := jwt.MapClaims{}+ start := time.Now()+ biscuitBytes, _, err := identity.MintBiscuitToken(priv, claims, nil, nodePeer, time.Now().Add(time.Hour), roleNames, roles)+ mintDur := time.Since(start)+ if err != nil {+ t.Fatalf("mint failed at n=%d matched roles: %v", n, err)+ }++ start = time.Now()+ _, authErr := identity.VerifyBiscuit(biscuitBytes, nodePeer, []ed25519.PublicKey{pub}, 5*time.Second)+ authorizeDur := time.Since(start)+ if authErr != nil {+ t.Fatalf("authorize failed at n=%d matched roles (cross-role Set merging should keep this well below the fact limit): %v", n, authErr)+ }++ t.Logf("matched_roles=%-6d mint=%-12s biscuit_size=%-10dB authorize=%-12s", n, mintDur, len(biscuitBytes), authorizeDur)+ }+}diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex 9ad8aee2..d7496c05 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -97,6 +97,11 @@ func mintBiscuit(signingKey ed25519.PrivateKey, remotePeer peer.ID, roles []strihasServices := falsesort.Strings(roles)var errs []error+ // Collected across every matched role and merged into Set facts once below: a token's+ // authorization is the union of its resolved roles' grants, so exact entries don't need to+ // stay siloed per role, and merging keeps fact counts flat regardless of how many roles match.+ var allServices []string+ var allTargets []stringfor _, role := range roles {if err := addFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: api.FactRole,@@ -127,22 +132,12 @@ func mintBiscuit(signingKey ed25519.PrivateKey, remotePeer peer.ID, roles []striif pr, ok := rolesMap[role]; ok {if len(pr.AllowedServices) > 0 {hasServices = true- // Exact-match entries are grouped into a single Set fact per service type so token/world- // fact counts stay flat regardless of how many exact services a role grants.- for _, fact := range api.BuildServiceDatalogFacts(pr.AllowedServices) {- if err := addFact(fact); err != nil {- errs = append(errs, fmt.Errorf("failed to add service fact for role %s: %w", role, err))- }- }+ allServices = append(allServices, pr.AllowedServices...)}if len(pr.AllowedTargets) > 0 {hasTargets = true- for _, fact := range api.BuildTargetDatalogFacts(pr.AllowedTargets) {- if err := addFact(fact); err != nil {- errs = append(errs, fmt.Errorf("failed to add target fact for role %s: %w", role, err))- }- }+ allTargets = append(allTargets, pr.AllowedTargets...)}for _, customFact := range pr.CustomDatalog {@@ -160,6 +155,17 @@ func mintBiscuit(signingKey ed25519.PrivateKey, remotePeer peer.ID, roles []stri}}+ for _, fact := range api.BuildServiceDatalogFacts(allServices) {+ if err := addFact(fact); err != nil {+ errs = append(errs, fmt.Errorf("failed to add service fact: %w", err))+ }+ }+ for _, fact := range api.BuildTargetDatalogFacts(allTargets) {+ if err := addFact(fact); err != nil {+ errs = append(errs, fmt.Errorf("failed to add target fact: %w", err))+ }+ }+if !hasServices && len(policyRoles) == 0 {if err := addFact(biscuit.Fact{Predicate: biscuit.Predicate{Name: api.FactGrantedServiceAllTypes,