# Kubernetes Rolgebaseerde Toegangbeheer (RBAC) {{#include ../../banners/hacktricks-training.md}} ## Rolgebaseerde Toegangbeheer (RBAC) Kubernetes het 'n **autorisatiemodule genaamd Rolgebaseerde Toegangbeheer** ([**RBAC**](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)) wat help om gebruiksregte aan die API-bediener toe te ken. RBAC se toestemmingsmodel is gebou uit **drie individuele dele**: 1. **Rol\ClusterRol –** Die werklike toestemming. Dit bevat _**reëls**_ wat 'n stel toestemmings verteenwoordig. Elke reël bevat [hulpbronne](https://kubernetes.io/docs/reference/kubectl/overview/#resource-types) en [werkwoorde](https://kubernetes.io/docs/reference/access-authn-authz/authorization/#determine-the-request-verb). Die werkwoord is die aksie wat op die hulpbron toegepas sal word. 2. **Onderwerp (Gebruiker, Groep of Diensrekening) –** Die objek wat die toestemmings sal ontvang. 3. **Rolbinding\ClusterRolbinding –** Die verbinding tussen Rol\ClusterRol en die onderwerp. ![](https://www.cyberark.com/wp-content/uploads/2018/12/rolebiding_serviceaccount_and_role-1024x551.png) Die verskil tussen “**Rolle**” en “**ClusterRolle**” is net waar die rol toegepas sal word – 'n “**Rol**” sal toegang tot slegs **een** **spesifieke** **naamruimte** verleen, terwyl 'n “**ClusterRol**” in **alle naamruimtes** in die kluster gebruik kan word. Boonop kan **ClusterRolle** ook toegang verleen tot: - **kluster-geskepte** hulpbronne (soos knope). - **nie-hulpbron** eindpunte (soos /healthz). - naamruimte-hulpbronne (soos Pods), **oor alle naamruimtes**. Vanaf **Kubernetes** 1.6 en later, is **RBAC** beleide **standaard geaktiveer**. Maar om RBAC te aktiveer kan jy iets soos gebruik: ``` kube-apiserver --authorization-mode=Example,RBAC --other-options --more-options ``` ## Templates In die sjabloon van 'n **Rol** of 'n **ClusterRol** moet jy die **naam van die rol**, die **namespace** (in rolle) en dan die **apiGroups**, **hulpbronne** en **werkwoorde** van die rol aandui: - Die **apiGroups** is 'n array wat die verskillende **API namespaces** bevat waartoe hierdie reël van toepassing is. Byvoorbeeld, 'n Pod-definisie gebruik apiVersion: v1. _Dit kan waardes hê soos rbac.authorization.k8s.io of \[\*]_. - Die **hulpbronne** is 'n array wat definieer **watter hulpbronne hierdie reël van toepassing is**. Jy kan al die hulpbronne vind met: `kubectl api-resources --namespaced=true` - Die **werkwoorde** is 'n array wat die **toegelate werkwoorde** bevat. Die werkwoord in Kubernetes definieer die **soort aksie** wat jy op die hulpbron moet toepas. Byvoorbeeld, die lys werkwoord word teenoor versamelings gebruik terwyl "get" teenoor 'n enkele hulpbron gebruik word. ### Rules Verbs (_Hierdie inligting is geneem van_ [_**the docs**_](https://kubernetes.io/docs/reference/access-authn-authz/authorization/#determine-the-request-verb)) | HTTP werkwoord | versoek werkwoord | | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | POST | create | | GET, HEAD | get (vir individuele hulpbronne), list (vir versamelings, insluitend volledige objekinhoud), watch (vir die kyk na 'n individuele hulpbron of versameling hulpbronne) | | PUT | update | | PATCH | patch | | DELETE | delete (vir individuele hulpbronne), deletecollection (vir versamelings) | Kubernetes kontroleer soms magtiging vir addisionele toestemmings met behulp van gespesialiseerde werkwoorde. Byvoorbeeld: - [PodSecurityPolicy](https://kubernetes.io/docs/concepts/policy/pod-security-policy/) - `use` werkwoord op `podsecuritypolicies` hulpbronne in die `policy` API-groep. - [RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#privilege-escalation-prevention-and-bootstrapping) - `bind` en `escalate` werkwoorde op `roles` en `clusterroles` hulpbronne in die `rbac.authorization.k8s.io` API-groep. - [Authentication](https://kubernetes.io/docs/reference/access-authn-authz/authentication/) - `impersonate` werkwoord op `users`, `groups`, en `serviceaccounts` in die kern API-groep, en die `userextras` in die `authentication.k8s.io` API-groep. > [!WARNING] > Jy kan **alle werkwoorde wat elke hulpbron ondersteun** vind deur `kubectl api-resources --sort-by name -o wide` uit te voer. ### Examples ```yaml:Role apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: defaultGreen name: pod-and-pod-logs-reader rules: - apiGroups: [""] resources: ["pods", "pods/log"] verbs: ["get", "list", "watch"] ``` ```yaml:ClusterRole apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: # "namespace" omitted since ClusterRoles are not namespaced name: secret-reader rules: - apiGroups: [""] resources: ["secrets"] verbs: ["get", "watch", "list"] ``` Byvoorbeeld, jy kan 'n **ClusterRole** gebruik om 'n spesifieke gebruiker toe te laat om te loop: ``` kubectl get pods --all-namespaces ``` ### **Rolbinding en Klasrolbinding** [**Uit die dokumentasie:**](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#rolebinding-and-clusterrolebinding) 'n **Rolbinding gee die toestemmings wat in 'n rol gedefinieer is aan 'n gebruiker of stel gebruikers**. Dit hou 'n lys van onderwerpe (gebruikers, groepe, of diensrekeninge), en 'n verwysing na die rol wat toegeken word. 'n **Rolbinding** gee toestemmings binne 'n spesifieke **naamruimte** terwyl 'n **Klasrolbinding** daardie toegang **kluster-wyd** gee. ```yaml:RoleBinding piVersion: rbac.authorization.k8s.io/v1 # This role binding allows "jane" to read pods in the "default" namespace. # You need to already have a Role named "pod-reader" in that namespace. kind: RoleBinding metadata: name: read-pods namespace: default subjects: # You can specify more than one "subject" - kind: User name: jane # "name" is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: # "roleRef" specifies the binding to a Role / ClusterRole kind: Role #this must be Role or ClusterRole name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to apiGroup: rbac.authorization.k8s.io ``` ```yaml:ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 # This cluster role binding allows anyone in the "manager" group to read secrets in any namespace. kind: ClusterRoleBinding metadata: name: read-secrets-global subjects: - kind: Group name: manager # Name is case sensitive apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: secret-reader apiGroup: rbac.authorization.k8s.io ``` **Toestemmings is additief** so as jy 'n clusterRole het met “lys” en “verwyder” geheime kan jy dit byvoeg met 'n Rol met “kry”. Wees dus bewus en toets altyd jou rolle en toestemmings en **specifiseer wat TOEGESTAAN is, want alles is standaard VERWEERD.** ## **Opname van RBAC** ```bash # Get current privileges kubectl auth can-i --list # use `--as=system:serviceaccount::` to impersonate a service account # List Cluster Roles kubectl get clusterroles kubectl describe clusterroles # List Cluster Roles Bindings kubectl get clusterrolebindings kubectl describe clusterrolebindings # List Roles kubectl get roles kubectl describe roles # List Roles Bindings kubectl get rolebindings kubectl describe rolebindings ``` ### Misbruik Rol/ClusterRoles vir Privilege Escalation {{#ref}} abusing-roles-clusterroles-in-kubernetes/ {{#endref}} {{#include ../../banners/hacktricks-training.md}}