Skip to content

Agent Mesh

allow to set policies on OIDC sub and bind them to roles

Antonio Ojeace2e68786512
diff --git a/.github/k8s/sam-hub-template.yaml b/.github/k8s/sam-hub-template.yaml
index e65f8b6c..150beffe 100644
--- a/.github/k8s/sam-hub-template.yaml
+++ b/.github/k8s/sam-hub-template.yaml
@@ -129,7 +129,7 @@ data:
policies.yaml: |
version: "v1alpha1"
bindings:
- - group: "system:serviceaccounts:sam-canary-${ENV_NAME}"
+ - user: "system:serviceaccount:sam-canary-${ENV_NAME}:sam-node-sa"
role: "sam-canary"
roles:
sam-canary:
diff --git a/api/constants.go b/api/constants.go
index 28571e21..cfc47860 100644
--- a/api/constants.go
+++ b/api/constants.go
@@ -30,6 +30,7 @@ const (
FactClientPeerID = "client_peer_id"
FactGroup = "group"
FactRole = "role"
+ FactUser = "user"
FactMCPTool = "allow_mcp_tool"
FactNetworkTarget = "allow_network_target"
)
diff --git a/api/policy.go b/api/policy.go
index 7629e769..2c9b92ee 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -8,7 +8,8 @@ type PolicyConfig struct {
}
type Binding struct {
- Group string `yaml:"group"`
+ Group string `yaml:"group,omitempty"`
+ User string `yaml:"user,omitempty"`
Role string `yaml:"role"`
}
diff --git a/cmd/sam-hub/config.go b/cmd/sam-hub/config.go
index 651e0a66..fe488fc3 100644
--- a/cmd/sam-hub/config.go
+++ b/cmd/sam-hub/config.go
@@ -51,8 +51,11 @@ func LoadPolicyConfig(path string) (*api.PolicyConfig, error) {
// ValidatePolicyConfig ensures that no wildcards are used in policies, and that all referenced roles in bindings exist.
func ValidatePolicyConfig(config *api.PolicyConfig) error {
for _, b := range config.Bindings {
- if b.Group == "" {
- return fmt.Errorf("binding group cannot be empty")
+ if b.Group == "" && b.User == "" {
+ return fmt.Errorf("binding must specify either 'group' or 'user'")
+ }
+ if b.Group != "" && b.User != "" {
+ return fmt.Errorf("binding cannot specify both 'group' and 'user' concurrently")
}
if b.Role == "" {
return fmt.Errorf("binding role cannot be empty")
diff --git a/cmd/sam-hub/config_test.go b/cmd/sam-hub/config_test.go
index a3e33c63..8588a7fe 100644
--- a/cmd/sam-hub/config_test.go
+++ b/cmd/sam-hub/config_test.go
@@ -160,4 +160,73 @@ roles:
if err == nil {
t.Error("expected error for invalid binding referencing a nonexistent role, got nil")
}
+
+ // 8. Test valid user binding
+ userBindingYAML := `
+version: "v1alpha1"
+bindings:
+ - user: "system:serviceaccount:sam-canary:sam-node-sa"
+ role: "mesh-member"
+roles:
+ mesh-member:
+ mcp:
+ allowed_tools: ["/sam/mcp/1.0.0"]
+`
+ userBindingFile := filepath.Join(dir, "user_binding.yaml")
+ if err := os.WriteFile(userBindingFile, []byte(userBindingYAML), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ config, err = LoadPolicyConfig(userBindingFile)
+ if err != nil {
+ t.Fatalf("expected no error for valid user binding, got %v", err)
+ }
+ if len(config.Bindings) != 1 {
+ t.Errorf("expected 1 binding, got %d", len(config.Bindings))
+ }
+ if config.Bindings[0].User != "system:serviceaccount:sam-canary:sam-node-sa" || config.Bindings[0].Role != "mesh-member" {
+ t.Errorf("unexpected binding values: %+v", config.Bindings[0])
+ }
+
+ // 9. Test invalid binding with both group and user missing
+ missingBothYAML := `
+version: "v1alpha1"
+bindings:
+ - role: "mesh-member"
+roles:
+ mesh-member:
+ mcp:
+ allowed_tools: ["/sam/mcp/1.0.0"]
+`
+ missingBothFile := filepath.Join(dir, "missing_both.yaml")
+ if err := os.WriteFile(missingBothFile, []byte(missingBothYAML), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = LoadPolicyConfig(missingBothFile)
+ if err == nil {
+ t.Error("expected error for binding with both group and user missing, got nil")
+ }
+
+ // 10. Test invalid binding with both group and user populated
+ bothPopulatedYAML := `
+version: "v1alpha1"
+bindings:
+ - group: "some-group"
+ user: "some-user"
+ role: "mesh-member"
+roles:
+ mesh-member:
+ mcp:
+ allowed_tools: ["/sam/mcp/1.0.0"]
+`
+ bothPopulatedFile := filepath.Join(dir, "both_populated.yaml")
+ if err := os.WriteFile(bothPopulatedFile, []byte(bothPopulatedYAML), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = LoadPolicyConfig(bothPopulatedFile)
+ if err == nil {
+ t.Error("expected error for binding specifying both group and user, got nil")
+ }
}
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index 688b73ea..8eac8e4d 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -429,13 +429,22 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
}
}
- // Resolve roles based on configured group bindings and explicit OIDC roles
+ oidcSub, _ := claims["sub"].(string)
+
+ // Resolve roles based on configured bindings and explicit OIDC roles
resolvedRoles := make(map[string]bool)
if h.Policy != nil {
- // 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 {
+ // 1. Map OIDC groups and users to roles via configured bindings (RBAC mapping)
+ for _, b := range h.Policy.Bindings {
+ if b.Group != "" {
+ for _, cg := range oidcGroups {
+ if b.Group == cg {
+ resolvedRoles[b.Role] = true
+ }
+ }
+ }
+ if b.User != "" && oidcSub != "" {
+ if b.User == oidcSub {
resolvedRoles[b.Role] = true
}
}
@@ -472,6 +481,15 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
return nil, fmt.Errorf("failed to add client_peer_id fact: %w", err)
}
+ if oidcSub != "" {
+ if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
+ Name: api.FactUser,
+ IDs: []biscuit.Term{biscuit.String(oidcSub)},
+ }}); err != nil {
+ return nil, fmt.Errorf("failed to add user fact: %w", err)
+ }
+ }
+
// Assert original OIDC groups in the token (semantic audit trail)
seenGroups := make(map[string]bool)
for _, cg := range oidcGroups {
diff --git a/cmd/sam-hub/main_test.go b/cmd/sam-hub/main_test.go
index 7c9217b7..3d0daa3a 100644
--- a/cmd/sam-hub/main_test.go
+++ b/cmd/sam-hub/main_test.go
@@ -49,6 +49,10 @@ func TestMintBiscuitToken(t *testing.T) {
Group: "system:serviceaccounts:sam-canary",
Role: "canary-role",
},
+ {
+ User: "system:serviceaccount:sam-canary:sam-node-sa",
+ Role: "canary-role",
+ },
},
Roles: map[string]api.RolePolicy{
"admin": {
@@ -174,8 +178,34 @@ func TestMintBiscuitToken(t *testing.T) {
},
}, Kind: biscuit.PolicyKindAllow}
authorizer3.AddPolicy(rule3)
- if err := authorizer3.Authorize(); err == nil {
- t.Error("Expected unmapped role and group to fail authorization check")
+ // Case 4: GKE Workload Identity projected token (no groups claim, sub-based mapping)
+ claims4 := jwt.MapClaims{
+ "sub": "system:serviceaccount:sam-canary:sam-node-sa",
+ }
+ biscuitData4, err := hub.mintBiscuitToken(claims4, token, dummyPeer)
+ if err != nil {
+ t.Fatal(err)
+ }
+ b4, err := biscuit.Unmarshal(biscuitData4)
+ if err != nil {
+ t.Fatal(err)
+ }
+ authorizer4, err := b4.Authorizer(kr.GetCurrentPublicKey())
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Verify that it mapped the derived group "system:serviceaccounts:sam-canary" to "canary-role"
+ rule4 := 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}
+ authorizer4.AddPolicy(rule4)
+ if err := authorizer4.Authorize(); err != nil {
+ t.Errorf("Expected sub-derived role 'canary-role' to be authorized: %v", err)
}
}