CodeBlanks Logo Icon CodeBlanks

Challenge - RBAC

Write a ServiceAccount and a RoleBinding.

Challenge: least privilege

A CI system should only be able to update the image of deployments in the magaza namespace and watch their status; it mustn't read Secrets or touch other namespaces. For this:

  1. The ci ServiceAccount
  2. A Role granting only get, list, watch, patch for deployments
  3. A RoleBinding binding the two
  4. Testing with auth can-i
kubectl auth can-i patch deployments -n magaza --as system:serviceaccount:magaza:ci   # yes
kubectl auth can-i get secrets -n magaza --as system:serviceaccount:magaza:ci          # no
kubectl auth can-i patch deployments -n prod --as system:serviceaccount:magaza:ci     # no

Practice The role

Write a Role YAML: name ci-dagitici, namespace magaza; a single rule: apiGroups: ["apps"], resources: ["deployments"], verbs: ["get", "list", "watch", "patch"].

Starter code

terminal
# buraya yaz

Sample solution

terminal
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: ci-dagitici
  namespace: magaza
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "patch"]

Expected output

role.rbac.authorization.k8s.io/ci-dagitici created

Practice Identity, binding and testing

Write four commands: create the ci ServiceAccount in magaza; create a RoleBinding ci-dagitici in magaza giving the ci-dagitici role to magaza:ci; query the permission to patch deployments in magaza as system:serviceaccount:magaza:ci; with the same identity, query the permission to get Secrets in magaza.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create serviceaccount ci -n magaza
kubectl create rolebinding ci-dagitici --role=ci-dagitici --serviceaccount=magaza:ci -n magaza
kubectl auth can-i patch deployments -n magaza --as system:serviceaccount:magaza:ci
kubectl auth can-i get secrets -n magaza --as system:serviceaccount:magaza:ci

Expected output

serviceaccount/ci created
rolebinding.rbac.authorization.k8s.io/ci-dagitici created
yes
no

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which verb is needed for kubectl set image?

  • patch (and get for reading) Correct answer
  • create
  • delete
  • escalate

Explanation: set image patches the deployment.

2 Write the deployments' API group.

Question
rules:
  - apiGroups: [____]
    resources: ["deployments"]
Answer
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]

Explanation: Deployment is in the apps group.

3 Match.

Term Meaning
ci ServiceAccount CI identity
Role Only magaza
patch Updating the image
secrets: no No access to secrets

All lessons in Advanced