Implementing RBAC with Service Mesh
1. Understanding Mesh RBAC Model
| Component | Description | Usage |
|---|---|---|
| Role | Defines a set of permissions | Assigned to users or groups |
| RoleBinding | Binds a role to a user or group | Enables access control |
| ClusterRole | Defines permissions across the cluster | Used for cluster-wide access |
| ClusterRoleBinding | Binds a ClusterRole to users or groups | Applies permissions across the cluster |
Example: Basic Role Definition
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
Example: ClusterRole for Mesh Access
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: mesh-admin
rules:
- apiGroups: ["networking.istio.io"]
resources: ["*"]
verbs: ["get", "list", "watch", "create", "update", "delete"]
Note: RBAC in service meshes like Istio is often managed via Kubernetes RBAC or custom policies. Ensure that the service mesh components (e.g., Istiod) have appropriate permissions to enforce these rules.
2. Configuring Role-Based Rules
| Rule Type | Description | Example | Notes |
|---|---|---|---|
| Role | Defines a set of permissions | |
Roles are assigned to users or groups |
| RoleBinding | Binds a role to a user or group | |
RoleBindings are namespace-scoped |
| ClusterRole | Defines permissions across the entire cluster | |
Used for cluster-wide access control |
| ClusterRoleBinding | Binds a ClusterRole to a user or group | |
Applies to all namespaces in the cluster |
Example: Defining a Role for Service Access
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "Role",
"metadata": {
"namespace": "production"
},
"rules": [
{
"apiGroups": [""],
"resources": ["services"],
"verbs": ["get", "list"]
}
]
}
Example: Binding Role to User
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "RoleBinding",
"metadata": {
"name": "prod-read-binding",
"namespace": "production"
},
"subjects": [
{
"kind": "User",
"name": "developer"
}
],
"roleRef": {
"kind": "Role",
"name": "service-reader"
}
}
Note: RBAC rules in service meshes like Istio are often managed through Kubernetes RBAC or custom policies, depending on the implementation.
3. Implementing Service Account Authorization
| Property | Description | Example |
|---|---|---|
| serviceAccount.name | Name of the Kubernetes service account | backend-sa |
| serviceAccount.namespace | Namespace where the service account resides | production |
| serviceAccount.token | Token used for authentication (e.g., in Istio) | eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... |
Example: Configuring Service Account in Istio
apiVersion: v1
kind: ServiceAccount
metadata:
name: backend-sa
namespace: production
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: backend-policy
namespace: production
spec:
selector:
matchLabels:
app: backend
rules:
- from:
- source:
principals: ["cluster.local/ns/production/sa/backend-sa"]
Note: The service account must be created in the same namespace as the workload it's used for.
4. Setting Namespace-Based Access
| Property | Description | Example |
|---|---|---|
| namespace | The Kubernetes namespace to which access is restricted | production |
| role | The role assigned to users or services within the namespace | admin |
| permissions | Permissions granted to the role in the namespace | read, write |
Example: Namespace-Based RBAC Configuration
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: service-admin
rules:
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "list", "watch", "create", "update", "delete"]
Note: Ensure that the service mesh's control plane has sufficient permissions to manage RBAC policies across namespaces.
5. Configuring Path-Based Authorization
| Field | Description | Example |
|---|---|---|
| match | Defines the path and method to match | { "path": "/api/users", "method": "GET" } |
| principal | Specifies the user or service allowed to access the path | "user:alice" |
Example: Path-Based Authorization Rule
apiVersion: rbac.istio.io/v1alpha1
kind: RBACPolicy
metadata:
name: user-access-policy
spec:
mode: PERMISSIVE
rules:
- name: access-users-api
match:
path: "/api/users"
method: "GET"
principals:
- user:alice
Note: Path-based rules are evaluated in order, and the first matching rule is applied.
6. Implementing Method-Based Authorization
| Method | Description | Example |
|---|---|---|
| GET | Retrieves data from the service | curl -X GET http://service.example.com/api/users |
| POST | Creates a new resource or triggers an action | curl -X POST http://service.example.com/api/users -d '{"name":"John"}' |
| PUT | Updates an existing resource | curl -X PUT http://service.example.com/api/users/123 -d '{"name":"Jane"}' |
| DELETE | Removes a resource | curl -X DELETE http://service.example.com/api/users/123 |
Example: Configuring Method-Based Authorization in Istio
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: method-authorization
namespace: default
spec:
selector:
matchLabels:
app: backend
rules:
- from:
- source:
principals: ["user1@example.com"]
to:
- operation:
methods: ["GET", "POST"]
- from:
- source:
principals: ["admin@example.com"]
to:
- operation:
methods: ["GET", "POST", "PUT", "DELETE"]
Note: Method-based authorization allows fine-grained control over which HTTP methods are accessible by specific users or roles.
7. Managing RBAC Policy Hierarchy
| Policy Type | Description | Scope | Notes |
|---|---|---|---|
| Role | Defines a set of permissions for a specific resource or operation | Namespace-level or cluster-level | Can be assigned to users, groups, or service accounts |
| RoleBinding | Binds a Role to a set of subjects (users, groups, or service accounts) | Namespace-level | Links roles to identities within a namespace |
| ClusterRole | Defines permissions at the cluster level, not bound to a namespace | Cluster-level | Can be referenced by ClusterRoleBinding |
| ClusterRoleBinding | Binds a ClusterRole to subjects across the entire cluster | Cluster-level | Allows global access to permissions defined in ClusterRole |
Example: Defining a Role and Binding
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: production
subjects:
- kind: User
name: alice
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Note: The policy hierarchy follows a strict order: ClusterRole > Role > ClusterRoleBinding > RoleBinding. Permissions are additive, and higher-level policies can override lower ones.
8. Configuring Default Allow/Deny
| Setting | Description | Values | Notes |
|---|---|---|---|
| defaultAllow | Default policy for traffic not explicitly allowed | true / false |
When false, all traffic is denied by default unless explicitly allowed |
| defaultDeny | Explicitly deny all traffic not matching rules | true / false |
When true, traffic is denied unless explicitly permitted |
Example: Default Deny Policy
apiVersion: rbac.istio.io/v1alpha1
kind: RBACConfig
metadata:
name: default
spec:
mode: <span class="highlight">enforcing</span>
defaultDeny: <span class="highlight">true</span>
Note: Setting
defaultDeny to true ensures that only explicitly allowed traffic is permitted, enhancing security by default.
9. Implementing Fine-Grained Access Control
| Resource | Permission | Notes |
|---|---|---|
| service | read |
Allows reading service metadata and configuration |
| service | write |
Allows modifying service configuration and routing rules |
| route | read |
Allows reading route definitions and traffic policies |
| route | write |
Allows creating and updating route configurations |
| policy | read |
Allows reading access control policies |
| policy | write |
Allows creating and modifying access control policies |
Example: Defining a Fine-Grained Policy
apiVersion: rbac.istio.io/v1alpha1
kind: RBAC
metadata:
name: fine-grained-access
spec:
mode: ENFORCED
rules:
- name: "allow-read-service"
resources:
- service
operations:
- read
- name: "allow-write-route"
resources:
- route
operations:
- write
Note: Fine-grained access control requires proper identity management and integration with the service mesh's authentication system.
10. Auditing RBAC Policies
Example: RBAC Policy Audit Log Structure
{
"timestamp": "2026-01-15T14:30:00Z",
"user": "alice@example.com",
"action": "access",
"resource": "/api/v1/users/123",
"policy": "read-users",
"status": "allowed",
"details": {
"requestId": "req-abc123",
"sourceIp": "192.168.1.100"
}
}
| Field | Description | Notes |
|---|---|---|
| timestamp | ISO 8601 formatted time of the event | UTC timezone |
| user | Identity of the user or service making the request | Can be email, service account name, or principal |
| action | The operation attempted (e.g., access, modify, delete) | Typically mapped from HTTP methods or gRPC calls |
| resource | Resource path or identifier being accessed | May include query parameters or versioning info |
| policy | Name or ID of the RBAC policy evaluated | Used for correlation and analysis |
| status | Outcome of the RBAC check (allowed, denied) | Denials should be logged for security review |
| details | Additional metadata about the request | Includes source IP, request ID, session info |
Note: Audit logs must be stored securely and retained for compliance purposes. Consider using a centralized logging system like ELK or Loki to aggregate logs from multiple service mesh instances.
Best Practices for RBAC Auditing
- Log all access attempts, including denied requests
- Include contextual information such as source IP and user agent
- Use structured logging for easy parsing and querying
- Implement log retention policies based on compliance requirements
- Secure audit logs to prevent tampering or unauthorized access
11. Implementing RBAC Testing Strategies
Example: RBAC Policy Test with Istio
apiVersion: rbac.istio.io/v1alpha1
kind: RBACPolicy
metadata:
name: test-policy
spec:
mode: enforced
rules:
- services:
- "api-service.default.svc.cluster.local"
principals:
- "cluster.local/ns/default/sa/test-user"
| Test Type | Description | Tools | Status |
|---|---|---|---|
| Unit Testing | Testing individual RBAC policies in isolation | istioctl, kubectl |
NEW |
| Integration Testing | Testing RBAC policies in a simulated cluster environment | istioctl proxy-config, istioctl authn |
BETA |
| End-to-End Testing | Testing RBAC policies in a real service mesh environment | kubectl exec, curl |
DEPRECATED |
Testing Strategy Overview
- Start with unit tests for each policy rule
- Use integration tests to simulate real-world usage
- Validate end-to-end behavior in staging environments
RBAC Testing Workflow
- Create a test policy with limited permissions
- Deploy to a staging environment
- Validate access control using
istioctl authn - Run integration tests with simulated traffic
- Verify policy enforcement in production-like conditions
Note: Ensure all test policies are removed or disabled after testing to avoid unintended access.
12. Managing RBAC Policy Versioning
| Version | Policy Format | Notes |
|---|---|---|
| v1 | rbac.v1 |
Initial policy format, supports basic role and permission definitions |
| v2 | rbac.v2 |
NEW Enhanced policy structure with nested roles and inheritance support |
| v3 | rbac.v3 |
NEW Includes dynamic policy evaluation and audit logging capabilities |