Exercise: a user who can read logs and nothing else
Host: controlplane · Time: 7 min · Domain: Cluster Hardening
Task
- Create user
jane(you will not issue a real certificate; use--as). - In namespace
prod,janemayget,list,watchPods and read Pod logs. janemust not be able to do anything else inprod, and nothing anywhere else.
Acceptance criteria
kubectl auth can-i list pods -n prod --as jane→yeskubectl auth can-i get pods/log -n prod --as jane→yeskubectl auth can-i delete pods -n prod --as jane→nokubectl auth can-i list pods -n default --as jane→no
Solution
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata: {name: pod-reader, namespace: prod}
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata: {name: jane-pod-reader, namespace: prod}
subjects:
- {kind: User, name: jane, apiGroup: rbac.authorization.k8s.io}
roleRef: {kind: Role, name: pod-reader, apiGroup: rbac.authorization.k8s.io}
kubectl auth can-i --list -n prod --as jane
Four checks, not one: allowed, allowed, denied, denied-elsewhere.