Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
controlplane, identity: drive claim resolution from the claim map
Antonio Ojeae0cd416a4135
diff --git a/internal/controlplane/resolve_roles_test.go b/internal/controlplane/resolve_roles_test.gonew file mode 100644index 00000000..3ad39db1--- /dev/null+++ b/internal/controlplane/resolve_roles_test.go@@ -0,0 +1,65 @@+// 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 controlplane++import (+ "testing"++ jwt "github.com/golang-jwt/jwt/v5"+ "github.com/google/sam/api"+)++// TestResolveRolesCoversEveryMappedClaim walks api.OIDCClaimToFact instead of a+// hardcoded list, so a claim added to the map is covered without anyone+// remembering to extend this test. resolveRoles used to read the four claims by+// name, so a new entry in the map reached the token but never resolved a+// binding, and the binding looked configured while granting nothing.+func TestResolveRolesCoversEveryMappedClaim(t *testing.T) {+ for claimKey, factName := range api.OIDCClaimToFact() {+ t.Run(claimKey, func(t *testing.T) {+ bindings := []*api.PolicyBinding{{+ Role: "resolved",+ Members: []string{factName + ":wanted"},+ }}++ got := resolveRoles("peer-id", jwt.MapClaims{claimKey: "wanted"}, bindings)+ if len(got) != 1 || got[0] != "resolved" {+ t.Errorf("claim %q with member %s:wanted resolved %v, want [resolved]", claimKey, factName, got)+ }++ // The same binding must not resolve for a different claim value.+ if got := resolveRoles("peer-id", jwt.MapClaims{claimKey: "other"}, bindings); len(got) != 0 {+ t.Errorf("claim %q with a non-matching value resolved %v, want none", claimKey, got)+ }+ })+ }+}++// TestResolveRolesMatchesNodeOnThePeerID pins that node: is matched against the+// connecting peer and not against a claim, since it is the one member prefix+// that does not come from the token.+func TestResolveRolesMatchesNodeOnThePeerID(t *testing.T) {+ bindings := []*api.PolicyBinding{{+ Role: "node-bound",+ Members: []string{api.FactNode + ":12D3KooWExamplePeer"},+ }}++ if got := resolveRoles("12D3KooWExamplePeer", jwt.MapClaims{}, bindings); len(got) != 1 || got[0] != "node-bound" {+ t.Errorf("matching peer resolved %v, want [node-bound]", got)+ }+ if got := resolveRoles("12D3KooWSomeoneElse", jwt.MapClaims{}, bindings); len(got) != 0 {+ t.Errorf("non-matching peer resolved %v, want none", got)+ }+}diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex 2fc3b28b..0abb2f8e 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -29,6 +29,7 @@ import ("io""net""net/http"+ "slices""strings""sync""time"@@ -1936,10 +1937,13 @@ func resolveRoles(peerID string, claims jwt.MapClaims, bindings []*api.PolicyBinif claims == nil {claims = make(jwt.MapClaims)}- oidcRoles := toStringSlice(claims["roles"])- oidcGroups := toStringSlice(claims["groups"])- oidcSub, _ := claims["sub"].(string)- oidcEmail, _ := claims["email"].(string)++ // Driven by the same claim->fact map that minting uses, so a claim added+ // there resolves bindings here too instead of being silently ignored.+ factValues := make(map[string][]string)+ for claim, fact := range api.OIDCClaimToFact() {+ factValues[fact] = append(factValues[fact], toStringSlice(claims[claim])...)+ }resolvedRoles := make(map[string]bool)for _, b := range bindings {@@ -1956,31 +1960,15 @@ func resolveRoles(peerID string, claims jwt.MapClaims, bindings []*api.PolicyBincontinue}prefix, value := parts[0], parts[1]- switch prefix {- case api.FactNode:+ // node is the peer presenting the request, not an OIDC claim.+ if prefix == api.FactNode {if peerID == value {resolvedRoles[b.Role] = true}- case api.FactGroup:- for _, g := range oidcGroups {- if g == value {- resolvedRoles[b.Role] = true- }- }- case api.FactUser:- if oidcSub == value {- resolvedRoles[b.Role] = true- }- case api.FactEmail:- if oidcEmail == value {- resolvedRoles[b.Role] = true- }- case api.FactRole:- for _, r := range oidcRoles {- if r == value {- resolvedRoles[b.Role] = true- }- }+ continue+ }+ if slices.Contains(factValues[prefix], value) {+ resolvedRoles[b.Role] = true}}}diff --git a/internal/identity/biscuit.go b/internal/identity/biscuit.goindex 934971df..02067c5e 100644--- a/internal/identity/biscuit.go+++ b/internal/identity/biscuit.go@@ -384,30 +384,20 @@ func translateClaimsToFacts(addFact func(biscuit.Fact) error, claims map[string]if !ok || val == nil {continue}- switch factName {- case api.FactUser, api.FactEmail:- if strVal, ok := val.(string); ok && strVal != "" {- if err := addFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: factName,- IDs: []biscuit.Term{biscuit.String(strVal)},- }}); err != nil {- return fmt.Errorf("failed to add %s fact: %w", factName, err)- }+ // Every claim is treated as a list; a scalar is a list of one. Special+ // casing which facts are multi-valued would mean a claim added to the+ // map but not to that list is silently dropped at mint time.+ seen := make(map[string]bool)+ for _, item := range toStringSlice(val) {+ if seen[item] {+ continue}- case api.FactGroup, api.FactRole:- items := toStringSlice(val)- seen := make(map[string]bool)- for _, item := range items {- if seen[item] {- continue- }- seen[item] = true- if err := addFact(biscuit.Fact{Predicate: biscuit.Predicate{- Name: factName,- IDs: []biscuit.Term{biscuit.String(item)},- }}); err != nil {- return fmt.Errorf("failed to add %s fact: %w", factName, err)- }+ seen[item] = true+ if err := addFact(biscuit.Fact{Predicate: biscuit.Predicate{+ Name: factName,+ IDs: []biscuit.Term{biscuit.String(item)},+ }}); err != nil {+ return fmt.Errorf("failed to add %s fact: %w", factName, err)}}}diff --git a/internal/identity/claims_test.go b/internal/identity/claims_test.gonew file mode 100644index 00000000..904a094b--- /dev/null+++ b/internal/identity/claims_test.go@@ -0,0 +1,60 @@+// 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 identity++import (+ "testing"++ "github.com/biscuit-auth/biscuit-go/v2"+ "github.com/google/sam/api"+)++// TestEveryMappedClaimBecomesAFact walks api.OIDCClaimToFact rather than a+// hardcoded list, so a claim added to the map is covered here without anyone+// remembering to extend this test. Minting used to special-case which facts+// were multi-valued, which meant a new entry in the map was accepted by the+// map's own consumers and then silently dropped at mint time.+func TestEveryMappedClaimBecomesAFact(t *testing.T) {+ for claimKey, factName := range api.OIDCClaimToFact() {+ t.Run(claimKey, func(t *testing.T) {+ got := map[string][]string{}+ collect := func(f biscuit.Fact) error {+ for _, id := range f.IDs {+ if s, ok := id.(biscuit.String); ok {+ got[f.Name] = append(got[f.Name], string(s))+ }+ }+ return nil+ }++ // A scalar claim, which is how sub and email arrive.+ if err := translateClaimsToFacts(collect, map[string]any{claimKey: "scalar-value"}); err != nil {+ t.Fatalf("translateClaimsToFacts: %v", err)+ }+ if vals := got[factName]; len(vals) != 1 || vals[0] != "scalar-value" {+ t.Errorf("claim %q produced %s = %v, want [scalar-value]", claimKey, factName, vals)+ }++ // A list claim, which is how groups and roles arrive.+ got = map[string][]string{}+ if err := translateClaimsToFacts(collect, map[string]any{claimKey: []any{"a", "b", "a"}}); err != nil {+ t.Fatalf("translateClaimsToFacts: %v", err)+ }+ if vals := got[factName]; len(vals) != 2 {+ t.Errorf("claim %q produced %s = %v, want two deduplicated values", claimKey, factName, vals)+ }+ })+ }+}