OpenShift Security Context Constraints (SCC)
Objective
On OpenShift, the SCC admission plugin checks every pod against the Security Context Constraints the creator is allowed to use. In this lab you will:
- Give a service account the rights to create pods with RBAC, and see that it is not enough
- Find which SCC admitted a pod, and which SCC a pod requires
- Grant an SCC to a service account
- Understand why a Deployment needs the SCC on the pod service account, not on the user who creates it
Complete this lab in 30-40 minutes.
Prerequisites
- An OpenShift cluster, logged in with
ocas cluster-admin - The slides
OPENSHIFT.pdf, section SecurityContextConstraints
Note
SCC are cluster-wide resources. On a shared cluster, each trainee works in their own namespace scc-<id> with their own service account, so the SCC grants below do not interfere.
Setup
Create the pod manifests used in this lab:
Tip
These manifests are also generated by ex3-prereq.sh.
Exercise 1: RBAC first
Allow the service account fake-user to create pods and deployments in $NS only.
Exercise 2: a pod without security requirements
Create the pod ubuntu-simple as fake-user.
Questions
- Can
fake-useruse the SCCrestricted-v2? Andanyuid? - Which SCC admitted the pod
ubuntu-simple? - Which user ID does the container run as? Where does it come from?
Exercise 3: a pod running as root
Try to create the pod ubuntu-root as fake-user. Then:
- Find which SCC this pod requires
- Check whether
fake-usercan use it - Grant it to
fake-user, check it, and create the pod
Tip
Useful commands:
oc adm policy scc-subject-review -f <podfile.yaml>: which SCC would admit this podoc adm policy scc-review -z <sa> -f <podfile.yaml>: which SCC would admit this pod for this service accountoc adm policy add-scc-to-user <scc-name> -z <sa>: grant an SCC to a service account
Exercise 4: a privileged pod
Do the same with the pod ubuntu-privileged.
Exercise 5: a privileged Deployment
Create the Deployment nginx-privileged as fake-user. Are the pods created? Why?
Going further
Recent OpenShift versions (seen on 4.21) add the SCC restricted-v3, which requires hostUsers: false in the pod spec: the pod runs in a Linux user namespace, so root in the container is not root on the node. It appears in the error messages above.
The safest way to run on OpenShift is to make images work under restricted-v2, instead of granting SCCs: see the lab Helm on OpenShift: Migrating to Security Context Constraints.
Clean up
Reference / full solution
ex3-scc.sh: run ./ex3-prereq.sh then ./ex3-scc.sh -s.