Skip to content

Agent Mesh

decouple roles and groups

Antonio Ojeadf506d5ca354
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index 1254c5ac..688b73ea 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -411,32 +411,42 @@ func (h *Hub) parseAndVerifyJWT(ctx context.Context, jwtStr string, allowedAudie
}
func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remotePeer peer.ID) ([]byte, error) {
- var claimsGroups []string
+ var oidcRoles []string
if rolesAny, ok := claims["roles"].([]any); ok {
for _, r := range rolesAny {
- if str, ok := r.(string); ok {
- claimsGroups = append(claimsGroups, str)
+ if str, ok := r.(string); ok && str != "" {
+ oidcRoles = append(oidcRoles, str)
}
}
}
+
+ var oidcGroups []string
if groupsAny, ok := claims["groups"].([]any); ok {
for _, g := range groupsAny {
- if str, ok := g.(string); ok {
- claimsGroups = append(claimsGroups, str)
+ if str, ok := g.(string); ok && str != "" {
+ oidcGroups = append(oidcGroups, str)
}
}
}
- // Resolve roles based on configured group bindings (RBAC mapping)
+ // Resolve roles based on configured group bindings and explicit OIDC roles
resolvedRoles := make(map[string]bool)
if h.Policy != nil {
- for _, b := range h.Policy.Bindings {
- for _, cg := range claimsGroups {
+ // 1. Map OIDC groups to roles via configured bindings (RBAC mapping)
+ for _, cg := range oidcGroups {
+ for _, b := range h.Policy.Bindings {
if b.Group == cg {
resolvedRoles[b.Role] = true
}
}
}
+
+ // 2. Validate and accept pre-resolved OIDC roles directly if defined in policy (Zero-Trust check)
+ for _, r := range oidcRoles {
+ if _, exists := h.Policy.Roles[r]; exists {
+ resolvedRoles[r] = true
+ }
+ }
}
builder := biscuit.NewBuilder(h.KeyRing.GetCurrentKey())
@@ -464,8 +474,8 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
// Assert original OIDC groups in the token (semantic audit trail)
seenGroups := make(map[string]bool)
- for _, cg := range claimsGroups {
- if cg == "" || seenGroups[cg] {
+ for _, cg := range oidcGroups {
+ if seenGroups[cg] {
continue
}
seenGroups[cg] = true
diff --git a/cmd/sam-hub/main_test.go b/cmd/sam-hub/main_test.go
index 3e1e4d89..7c9217b7 100644
--- a/cmd/sam-hub/main_test.go
+++ b/cmd/sam-hub/main_test.go
@@ -22,6 +22,7 @@ import (
"testing"
"time"
+ "github.com/biscuit-auth/biscuit-go/v2"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/golang-jwt/jwt/v5"
"github.com/google/sam/api"
@@ -43,6 +44,12 @@ func TestMintBiscuitToken(t *testing.T) {
hub := &Hub{
KeyRing: kr,
Policy: &api.PolicyConfig{
+ Bindings: []api.Binding{
+ {
+ Group: "system:serviceaccounts:sam-canary",
+ Role: "canary-role",
+ },
+ },
Roles: map[string]api.RolePolicy{
"admin": {
MCP: api.MCPPolicy{
@@ -52,14 +59,15 @@ func TestMintBiscuitToken(t *testing.T) {
AllowedTargets: []string{"target1"},
},
},
+ "canary-role": {
+ MCP: api.MCPPolicy{
+ AllowedTools: []string{"/sam/mcp/1.0.0"},
+ },
+ },
},
},
}
- claims := jwt.MapClaims{
- "roles": []any{"admin"},
- }
-
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
if err != nil {
t.Fatal(err)
@@ -73,13 +81,101 @@ func TestMintBiscuitToken(t *testing.T) {
Expiry: time.Now().Add(1 * time.Hour),
}
- biscuitData, err := hub.mintBiscuitToken(claims, token, dummyPeer)
+ // Case 1: Direct OIDC role (valid)
+ claims1 := jwt.MapClaims{
+ "roles": []any{"admin"},
+ }
+ biscuitData1, err := hub.mintBiscuitToken(claims1, token, dummyPeer)
if err != nil {
t.Fatal(err)
}
+ if len(biscuitData1) == 0 {
+ t.Error("Expected non-empty biscuit data for direct role")
+ }
- if len(biscuitData) == 0 {
- t.Error("Expected non-empty biscuit data")
+ b1, err := biscuit.Unmarshal(biscuitData1)
+ if err != nil {
+ t.Fatal(err)
+ }
+ authorizer1, err := b1.Authorizer(kr.GetCurrentPublicKey())
+ if err != nil {
+ t.Fatal(err)
+ }
+ rule1 := biscuit.Policy{Queries: []biscuit.Rule{
+ {
+ Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},
+ Body: []biscuit.Predicate{
+ {Name: "role", IDs: []biscuit.Term{biscuit.String("admin")}},
+ },
+ },
+ }, Kind: biscuit.PolicyKindAllow}
+ authorizer1.AddPolicy(rule1)
+ if err := authorizer1.Authorize(); err != nil {
+ t.Errorf("Expected direct role 'admin' to be authorized: %v", err)
+ }
+
+ // Case 2: OIDC group claim mapped to role via bindings
+ claims2 := jwt.MapClaims{
+ "groups": []any{"system:serviceaccounts:sam-canary"},
+ }
+ biscuitData2, err := hub.mintBiscuitToken(claims2, token, dummyPeer)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(biscuitData2) == 0 {
+ t.Error("Expected non-empty biscuit data for mapped group")
+ }
+
+ b2, err := biscuit.Unmarshal(biscuitData2)
+ if err != nil {
+ t.Fatal(err)
+ }
+ authorizer2, err := b2.Authorizer(kr.GetCurrentPublicKey())
+ if err != nil {
+ t.Fatal(err)
+ }
+ rule2 := biscuit.Policy{Queries: []biscuit.Rule{
+ {
+ Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},
+ Body: []biscuit.Predicate{
+ {Name: "role", IDs: []biscuit.Term{biscuit.String("canary-role")}},
+ },
+ },
+ }, Kind: biscuit.PolicyKindAllow}
+ authorizer2.AddPolicy(rule2)
+ if err := authorizer2.Authorize(); err != nil {
+ t.Errorf("Expected mapped role 'canary-role' to be authorized: %v", err)
+ }
+
+ // Case 3: Unmapped OIDC group and undefined direct role
+ claims3 := jwt.MapClaims{
+ "groups": []any{"unknown-group"},
+ "roles": []any{"undefined-role"},
+ }
+ biscuitData3, err := hub.mintBiscuitToken(claims3, token, dummyPeer)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b3, err := biscuit.Unmarshal(biscuitData3)
+ if err != nil {
+ t.Fatal(err)
+ }
+ authorizer3, err := b3.Authorizer(kr.GetCurrentPublicKey())
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Verify no role matches
+ rule3 := biscuit.Policy{Queries: []biscuit.Rule{
+ {
+ Head: biscuit.Predicate{Name: "allow", IDs: []biscuit.Term{}},
+ Body: []biscuit.Predicate{
+ {Name: "role", IDs: []biscuit.Term{biscuit.Variable("any_role")}},
+ },
+ },
+ }, Kind: biscuit.PolicyKindAllow}
+ authorizer3.AddPolicy(rule3)
+ if err := authorizer3.Authorize(); err == nil {
+ t.Error("Expected unmapped role and group to fail authorization check")
}
}