Skip to content

Agent Mesh

add role bindings

Antonio Ojea7653f272f7ea
diff --git a/.github/k8s/sam-hub-template.yaml b/.github/k8s/sam-hub-template.yaml
index 093edc57..f8888574 100644
--- a/.github/k8s/sam-hub-template.yaml
+++ b/.github/k8s/sam-hub-template.yaml
@@ -97,6 +97,7 @@ spec:
- "--allowed-audiences=sam-mesh-audience,sam-hub-audience"
- "--tls-cert-file=/etc/sam/certs/tls.crt"
- "--tls-key-file=/etc/sam/certs/tls.key"
+ - "--policy-file=/etc/sam/policies/policies.yaml"
resources:
requests:
cpu: 100m
@@ -108,10 +109,31 @@ spec:
- name: tls-certs
mountPath: /etc/sam/certs
readOnly: true
+ - name: policies-volume
+ mountPath: /etc/sam/policies
+ readOnly: true
volumes:
- name: tls-certs
secret:
secretName: hub-sam-mesh-tls
+ - name: policies-volume
+ configMap:
+ name: sam-hub-policies-${ENV_NAME}
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+ name: sam-hub-policies-${ENV_NAME}
+ namespace: ${NAMESPACE}
+data:
+ policies.yaml: |
+ version: "v1alpha1"
+ roles:
+ system:serviceaccounts:sam-canary-${ENV_NAME}:
+ mcp:
+ allowed_tools:
+ - "/sam/mcp/1.0.0"
+
---
apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
diff --git a/api/policy.go b/api/policy.go
index 93aa8a9a..7629e769 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -2,8 +2,14 @@ package api
// PolicyConfig is the root authorization configuration for the SAM Hub.
type PolicyConfig struct {
- Version string `yaml:"version"`
- Roles map[string]RolePolicy `yaml:"roles"`
+ Version string `yaml:"version"`
+ Bindings []Binding `yaml:"bindings"`
+ Roles map[string]RolePolicy `yaml:"roles"`
+}
+
+type Binding struct {
+ Group string `yaml:"group"`
+ Role string `yaml:"role"`
}
type RolePolicy struct {
diff --git a/cmd/sam-hub/config.go b/cmd/sam-hub/config.go
index 829c5af5..651e0a66 100644
--- a/cmd/sam-hub/config.go
+++ b/cmd/sam-hub/config.go
@@ -15,8 +15,9 @@ import (
func LoadPolicyConfig(path string) (*api.PolicyConfig, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return &api.PolicyConfig{
- Version: "v1alpha1",
- Roles: make(map[string]api.RolePolicy),
+ Version: "v1alpha1",
+ Bindings: []api.Binding{},
+ Roles: make(map[string]api.RolePolicy),
}, nil
}
@@ -47,8 +48,20 @@ func LoadPolicyConfig(path string) (*api.PolicyConfig, error) {
return &config, nil
}
-// ValidatePolicyConfig ensures that no wildcards are used in policies.
+// 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.Role == "" {
+ return fmt.Errorf("binding role cannot be empty")
+ }
+ if _, exists := config.Roles[b.Role]; !exists {
+ return fmt.Errorf("binding role %q does not exist in defined roles", b.Role)
+ }
+ }
+
for role, rolePolicy := range config.Roles {
for _, target := range rolePolicy.Network.AllowedTargets {
if target == "*" {
diff --git a/cmd/sam-hub/config_test.go b/cmd/sam-hub/config_test.go
index d6ceb225..a3e33c63 100644
--- a/cmd/sam-hub/config_test.go
+++ b/cmd/sam-hub/config_test.go
@@ -112,4 +112,52 @@ roles:
if err == nil {
t.Error("expected error for wildcard target, got nil")
}
+
+ // 6. Test valid bindings
+ bindingsYAML := `
+version: "v1alpha1"
+bindings:
+ - group: "system:serviceaccounts:sam-canary-bananas"
+ role: "mesh-member"
+roles:
+ mesh-member:
+ mcp:
+ allowed_tools: ["/sam/mcp/1.0.0"]
+`
+ bindingsFile := filepath.Join(dir, "bindings.yaml")
+ if err := os.WriteFile(bindingsFile, []byte(bindingsYAML), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ config, err = LoadPolicyConfig(bindingsFile)
+ if err != nil {
+ t.Fatalf("expected no error for valid bindings, got %v", err)
+ }
+ if len(config.Bindings) != 1 {
+ t.Errorf("expected 1 binding, got %d", len(config.Bindings))
+ }
+ if config.Bindings[0].Group != "system:serviceaccounts:sam-canary-bananas" || config.Bindings[0].Role != "mesh-member" {
+ t.Errorf("unexpected binding values: %+v", config.Bindings[0])
+ }
+
+ // 7. Test invalid binding mapping (non-existent role)
+ invalidBindingYAML := `
+version: "v1alpha1"
+bindings:
+ - group: "system:serviceaccounts:sam-canary-bananas"
+ role: "non-existent-role"
+roles:
+ mesh-member:
+ mcp:
+ allowed_tools: ["/sam/mcp/1.0.0"]
+`
+ invalidBindingFile := filepath.Join(dir, "invalid_binding.yaml")
+ if err := os.WriteFile(invalidBindingFile, []byte(invalidBindingYAML), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ _, err = LoadPolicyConfig(invalidBindingFile)
+ if err == nil {
+ t.Error("expected error for invalid binding referencing a nonexistent role, got nil")
+ }
}
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index 0b591863..31b584b2 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -411,11 +411,30 @@ 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 roles []string
+ var claimsGroups []string
if rolesAny, ok := claims["roles"].([]any); ok {
for _, r := range rolesAny {
if str, ok := r.(string); ok {
- roles = append(roles, str)
+ claimsGroups = append(claimsGroups, str)
+ }
+ }
+ }
+ if groupsAny, ok := claims["groups"].([]any); ok {
+ for _, g := range groupsAny {
+ if str, ok := g.(string); ok {
+ claimsGroups = append(claimsGroups, str)
+ }
+ }
+ }
+
+ // Resolve roles based on configured group bindings (RBAC mapping)
+ resolvedRoles := make(map[string]bool)
+ if h.Policy != nil {
+ for _, b := range h.Policy.Bindings {
+ for _, cg := range claimsGroups {
+ if b.Group == cg {
+ resolvedRoles[b.Role] = true
+ }
}
}
}
@@ -443,7 +462,7 @@ func (h *Hub) mintBiscuitToken(claims jwt.MapClaims, token *oidc.IDToken, remote
return nil, fmt.Errorf("failed to add client_peer_id fact: %w", err)
}
- for _, role := range roles {
+ for role := range resolvedRoles {
if err := builder.AddAuthorityFact(biscuit.Fact{Predicate: biscuit.Predicate{
Name: api.FactGroup,
IDs: []biscuit.Term{biscuit.String(role)},
diff --git a/tests/e2e/policy.bats b/tests/e2e/policy.bats
index 7ebbba0e..d89b0b79 100644
--- a/tests/e2e/policy.bats
+++ b/tests/e2e/policy.bats
@@ -152,8 +152,11 @@ setup() {
# Write policies to volume
local hub_policy="version: \"v1alpha1\"
+bindings:
+ - group: \"data-scientist\"
+ role: \"mesh-member\"
roles:
- data-scientist:
+ mesh-member:
mcp:
allowed_tools:
- \"query_database\"