|
| 1 | +# Role-Based Access Control (RBAC) |
| 2 | + |
| 3 | +Role-Based Access Control (RBAC) is a method of regulating access to resources based on the roles of individual users. RBAC uses the `rbac.authorization.k8s.io` API group to drive authorization decisions, allowing you to dynamically configure policies through the Kubernetes API. |
| 4 | + |
| 5 | +## RBAC Components |
| 6 | + |
| 7 | +RBAC authorization uses four kinds of Kubernetes objects: |
| 8 | + |
| 9 | +| Object | Scope | Description | |
| 10 | +|--------|-------|-------------| |
| 11 | +| **Role** | Namespace | Grants permissions within a specific namespace | |
| 12 | +| **ClusterRole** | Cluster-wide | Grants permissions cluster-wide or to cluster-scoped resources | |
| 13 | +| **RoleBinding** | Namespace | Binds a Role or ClusterRole to users within a namespace | |
| 14 | +| **ClusterRoleBinding** | Cluster-wide | Binds a ClusterRole to users across the entire cluster | |
| 15 | + |
| 16 | +## How RBAC Works |
| 17 | + |
| 18 | +1. **Roles/ClusterRoles** define *what* actions can be performed on *which* resources |
| 19 | +2. **RoleBindings/ClusterRoleBindings** define *who* can perform those actions |
| 20 | +3. Permissions are purely additive (there are no "deny" rules) |
| 21 | + |
| 22 | +## Resources |
| 23 | + |
| 24 | +=== "OpenShift" |
| 25 | + |
| 26 | + [RBAC Overview :fontawesome-solid-shield-halved:](https://docs.openshift.com/container-platform/4.17/authentication/using-rbac.html){ .md-button target="_blank"} |
| 27 | + |
| 28 | + [Default Cluster Roles :fontawesome-solid-shield-halved:](https://docs.openshift.com/container-platform/4.17/authentication/using-rbac.html#default-roles_using-rbac){ .md-button target="_blank"} |
| 29 | + |
| 30 | +=== "Kubernetes" |
| 31 | + |
| 32 | + [RBAC Authorization :fontawesome-solid-shield-halved:](https://kubernetes.io/docs/reference/access-authn-authz/rbac/){ .md-button target="_blank"} |
| 33 | + |
| 34 | + [Using RBAC :fontawesome-solid-shield-halved:](https://kubernetes.io/docs/admin/authorization/rbac/){ .md-button target="_blank"} |
| 35 | + |
| 36 | +## References |
| 37 | + |
| 38 | +_Role - Grants read access to pods in a namespace_ |
| 39 | + |
| 40 | +```yaml |
| 41 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 42 | +kind: Role |
| 43 | +metadata: |
| 44 | + namespace: default |
| 45 | + name: pod-reader |
| 46 | +rules: |
| 47 | + - apiGroups: [""] |
| 48 | + resources: ["pods"] |
| 49 | + verbs: ["get", "watch", "list"] |
| 50 | +``` |
| 51 | +
|
| 52 | +_RoleBinding - Binds the Role to a user_ |
| 53 | +
|
| 54 | +```yaml |
| 55 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 56 | +kind: RoleBinding |
| 57 | +metadata: |
| 58 | + name: read-pods |
| 59 | + namespace: default |
| 60 | +subjects: |
| 61 | + - kind: User |
| 62 | + name: jane |
| 63 | + apiGroup: rbac.authorization.k8s.io |
| 64 | +roleRef: |
| 65 | + kind: Role |
| 66 | + name: pod-reader |
| 67 | + apiGroup: rbac.authorization.k8s.io |
| 68 | +``` |
| 69 | +
|
| 70 | +_ClusterRole - Grants read access to secrets cluster-wide_ |
| 71 | +
|
| 72 | +```yaml |
| 73 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 74 | +kind: ClusterRole |
| 75 | +metadata: |
| 76 | + name: secret-reader |
| 77 | +rules: |
| 78 | + - apiGroups: [""] |
| 79 | + resources: ["secrets"] |
| 80 | + verbs: ["get", "watch", "list"] |
| 81 | +``` |
| 82 | +
|
| 83 | +_ClusterRoleBinding - Binds ClusterRole to a group_ |
| 84 | +
|
| 85 | +```yaml |
| 86 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 87 | +kind: ClusterRoleBinding |
| 88 | +metadata: |
| 89 | + name: read-secrets-global |
| 90 | +subjects: |
| 91 | + - kind: Group |
| 92 | + name: developers |
| 93 | + apiGroup: rbac.authorization.k8s.io |
| 94 | +roleRef: |
| 95 | + kind: ClusterRole |
| 96 | + name: secret-reader |
| 97 | + apiGroup: rbac.authorization.k8s.io |
| 98 | +``` |
| 99 | +
|
| 100 | +_Role for a ServiceAccount with deployment permissions_ |
| 101 | +
|
| 102 | +```yaml |
| 103 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 104 | +kind: Role |
| 105 | +metadata: |
| 106 | + namespace: production |
| 107 | + name: deployment-manager |
| 108 | +rules: |
| 109 | + - apiGroups: ["apps"] |
| 110 | + resources: ["deployments"] |
| 111 | + verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] |
| 112 | + - apiGroups: [""] |
| 113 | + resources: ["pods", "pods/log"] |
| 114 | + verbs: ["get", "list", "watch"] |
| 115 | +--- |
| 116 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 117 | +kind: RoleBinding |
| 118 | +metadata: |
| 119 | + name: deployment-manager-binding |
| 120 | + namespace: production |
| 121 | +subjects: |
| 122 | + - kind: ServiceAccount |
| 123 | + name: deploy-bot |
| 124 | + namespace: production |
| 125 | +roleRef: |
| 126 | + kind: Role |
| 127 | + name: deployment-manager |
| 128 | + apiGroup: rbac.authorization.k8s.io |
| 129 | +``` |
| 130 | +
|
| 131 | +## Common Verbs |
| 132 | +
|
| 133 | +| Verb | Description | |
| 134 | +|------|-------------| |
| 135 | +| `get` | Read a specific resource | |
| 136 | +| `list` | List resources of a type | |
| 137 | +| `watch` | Watch for changes to resources | |
| 138 | +| `create` | Create new resources | |
| 139 | +| `update` | Update existing resources | |
| 140 | +| `patch` | Partially update resources | |
| 141 | +| `delete` | Delete resources | |
| 142 | +| `deletecollection` | Delete multiple resources | |
| 143 | + |
| 144 | +## Default ClusterRoles |
| 145 | + |
| 146 | +| Role | Description | |
| 147 | +|------|-------------| |
| 148 | +| `cluster-admin` | Full access to all resources | |
| 149 | +| `admin` | Full access within a namespace | |
| 150 | +| `edit` | Read/write access to most resources in a namespace | |
| 151 | +| `view` | Read-only access to most resources in a namespace | |
| 152 | + |
| 153 | +=== "OpenShift" |
| 154 | + |
| 155 | + ``` Bash title="Get Roles in Namespace" |
| 156 | + oc get roles |
| 157 | + ``` |
| 158 | + |
| 159 | + ``` Bash title="Get ClusterRoles" |
| 160 | + oc get clusterroles |
| 161 | + ``` |
| 162 | + |
| 163 | + ``` Bash title="Get RoleBindings" |
| 164 | + oc get rolebindings |
| 165 | + ``` |
| 166 | + |
| 167 | + ``` Bash title="Describe a Role" |
| 168 | + oc describe role pod-reader |
| 169 | + ``` |
| 170 | + |
| 171 | + ``` Bash title="Check if User Can Perform Action" |
| 172 | + oc auth can-i create pods --as=jane |
| 173 | + ``` |
| 174 | + |
| 175 | + ``` Bash title="Add Role to User (OpenShift)" |
| 176 | + oc adm policy add-role-to-user edit jane -n myproject |
| 177 | + ``` |
| 178 | + |
| 179 | + ``` Bash title="Add Cluster Role to User" |
| 180 | + oc adm policy add-cluster-role-to-user cluster-admin admin-user |
| 181 | + ``` |
| 182 | + |
| 183 | +=== "Kubernetes" |
| 184 | + |
| 185 | + ``` Bash title="Get Roles in Namespace" |
| 186 | + kubectl get roles |
| 187 | + ``` |
| 188 | + |
| 189 | + ``` Bash title="Get ClusterRoles" |
| 190 | + kubectl get clusterroles |
| 191 | + ``` |
| 192 | + |
| 193 | + ``` Bash title="Get RoleBindings" |
| 194 | + kubectl get rolebindings |
| 195 | + ``` |
| 196 | + |
| 197 | + ``` Bash title="Describe a Role" |
| 198 | + kubectl describe role pod-reader |
| 199 | + ``` |
| 200 | + |
| 201 | + ``` Bash title="Check if User Can Perform Action" |
| 202 | + kubectl auth can-i create pods --as=jane |
| 203 | + ``` |
| 204 | + |
| 205 | + ``` Bash title="Check All Permissions for User" |
| 206 | + kubectl auth can-i --list --as=jane |
| 207 | + ``` |
| 208 | + |
| 209 | + ``` Bash title="Create Role Imperatively" |
| 210 | + kubectl create role pod-reader --verb=get,list,watch --resource=pods |
| 211 | + ``` |
| 212 | + |
| 213 | + ``` Bash title="Create RoleBinding Imperatively" |
| 214 | + kubectl create rolebinding read-pods --role=pod-reader --user=jane |
| 215 | + ``` |
0 commit comments