RBAC Cheat Sheet for CKS Exam
RBAC Key Concepts
- RBAC (Role-Based Access Control) controls access to resources based on the roles assigned to users or service accounts.
- Roles define what actions can be performed on what resources.
- RoleBindings and ClusterRoleBindings associate roles with subjects (users or service accounts).
RBAC Resources
- Roles
- Namespace-level resource.
- Defines access control within a specific namespace.
- Can be used with
RoleBindingto grant permissions.
Example:
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- ClusterRoles
- Cluster-wide resource.
- Defines access control across the entire cluster (e.g., access to nodes, cluster-level resources).
- Can be used with
ClusterRoleBindingto grant permissions.
Example:
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
# Name of the ClusterRole
name: cluster-admin
rules:
- apiGroups: [""]
resources: ["pods", "services"]
verbs: ["get", "list", "create", "delete"]
- RoleBindings
- Associates a Role with specific users or service accounts within a namespace.
- A RoleBinding grants the permissions defined in a Role to users, service accounts, or groups.
Example:
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: default
subjects:
- kind: User
name: "janedoe"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
- ClusterRoleBindings
- Associates a ClusterRole with specific users or service accounts across the entire cluster.
- A ClusterRoleBinding grants the permissions defined in a ClusterRole.
Example:
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: cluster-admin-binding
subjects:
- kind: ServiceAccount
name: default
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
RBAC Permissions (Verbs)
These verbs define what actions are allowed on resources:
- create: Create a resource.
- get: Retrieve a resource.
- list: List resources.
- update: Modify a resource.
- patch: Partially modify a resource.
- delete: Delete a resource.
- deletecollection: Delete a collection of resources.
- watch: Watch for changes to a resource.
- bind: Used for binding users to roles.
RBAC Best Practices
- Principle of Least Privilege (PoLP)
- Grant the minimum permissions required for a user or service account to perform their tasks.
-
Avoid assigning cluster-admin roles unless absolutely necessary.
-
Use Namespaced Roles When Possible
-
Prefer using Roles over ClusterRoles when limiting access to a specific namespace.
-
Use ServiceAccounts for Pods
-
Assign specific ServiceAccounts to pods to enforce RBAC policies.
-
Review and Audit RBAC Permissions Regularly
- Use tools like kubectl auth can-i to audit permissions.
Example to check if a user has a specific permission:
bash
kubectl auth can-i get pods --as janedoe
Common Commands for RBAC
-
List Roles and ClusterRoles:
bash kubectl get roles kubectl get clusterroles -
List RoleBindings and ClusterRoleBindings:
bash kubectl get rolebindings kubectl get clusterrolebindings -
Create a Role or ClusterRole:
bash kubectl apply -f role.yaml kubectl apply -f clusterrole.yaml -
Create a RoleBinding or ClusterRoleBinding:
bash kubectl apply -f rolebinding.yaml kubectl apply -f clusterrolebinding.yaml -
Check Permissions for a User or Service Account:
bash kubectl auth can-i <verb> <resource> --as <user>
Example:
bash
kubectl auth can-i get pods --as janedoe
- Describe a Role or RoleBinding:
bash kubectl describe role <role-name> -n <namespace> kubectl describe rolebinding <rolebinding-name> -n <namespace>
RBAC Troubleshooting Tips
- Check if a user has the correct permissions:
- Use
kubectl auth can-ito check what actions a user can perform on specific resources. -
Example: Check if a user can
getpods:bash kubectl auth can-i get pods --as <username> -
Review Resource Access Logs:
-
Ensure proper logging is enabled to track access to Kubernetes resources.
-
Debugging RBAC Issues:
- If a user or service account can't access resources, check for missing or incorrect RoleBindings/ClusterRoleBindings.
- Ensure the correct API group is used in the
roleRef.
RBAC Security Considerations
- Avoid Wildcards
- Avoid using wildcards (
*) in roles (e.g.,verbs: ["*"]), as it grants broad access. -
Instead, specify only the required resources and verbs.
-
Use Namespaces for Isolation
-
Limit permissions to specific namespaces to prevent cross-namespace access.
-
Review RBAC with Regular Audits
- Continuously monitor roles and bindings to ensure users have only the permissions they need.
RBAC Example: Pod Reader Role
Create a Role to allow a user to read pods in the default namespace.
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
Bind the Role to a specific user (janedoe):
yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: default
subjects:
- kind: User
name: "janedoe"
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io