Deploy applications that require pods with extended permissions.
Outcomes
Create service accounts and assign security context constraints (SCCs) to them.
Assign a service account to a deployment configuration.
Run applications that need root privileges.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that the cluster API is reachable and creates some HTPasswd users for the exercise.
[student@workstation ~]$ lab start appsec-scc
Instructions
Log in to the OpenShift cluster and create the appsec-scc project.
Log in to the cluster as the developer user with the developer password.
[student@workstation ~]$ oc login -u developer -p developer \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Create the appsec-scc project.
[student@workstation ~]$ oc new-project appsec-scc
Now using project "appsec-scc" on server ...
...output omitted...Deploy an application named gitlab by using the container image at registry.ocp4.example.com:8443/redhattraining/gitlab-ce:8.4.3-ce.0.
This image is a copy of the container image at docker.io/gitlab/gitlab-ce:8.4.3-ce.0.
Verify that the reason for the pod failure is because the container image needs root privileges.
Deploy the gitlab application.
[student@workstation ~]$ oc new-app --name gitlab \
--image registry.ocp4.example.com:8443/redhattraining/gitlab-ce:8.4.3-ce.0
...output omitted...
--> Creating resources ...
imagestream.image.openshift.io "gitlab" created
deployment.apps "gitlab" created
service "gitlab" created
--> Success
...output omitted...Determine whether the application is successfully deployed.
It should give an error, because this image needs root privileges to deploy.
[student@workstation ~]$oc get podsNAME READY STATUS RESTARTS AGE gitlab-d89cd88f8-jwqbp 0/1Error0 36s
It might take some time for the image to reach the Error state.
You might also see the CrashLoopBackOff status when you validate the health of the pod.
Review the application logs to confirm that insufficient privileges caused the failure.
[student@workstation ~]$oc logs pod/gitlab-...output omitted... ================================================================================ Recipe Compile Error in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/default.rb ================================================================================ Chef::Exceptions::InsufficientPermissions ----------------------------------------- directory[/etc/gitlab] (gitlab::default line 26) had an error: Chef::Exceptions::InsufficientPermissions:d89cd88f8-jwqbpCannot create directory[/etc/gitlab] at /etc/gitlab due to insufficient permissions...output omitted...
The application tries to write to the /etc directory.
To allow the application to write to the /etc directory, you can make the application run as the root user.
To run the application as the root user, you can grant the anyuid SCC to a service account.
Create a service account and assign the anyuid SCC to it.
Log in as the admin user with the redhatocp password.
[student@workstation ~]$ oc login -u admin -p redhatocp \
https://api.ocp4.example.com:6443
Login successful.
...output omitted...Verify the appropriate SCC to use with this deployment.
[student@workstation]$oc get deployNAME READY UP-TO-DATE AVAILABLE AGEgitlab0/1 1 0 109s [student@workstation]$oc get deploy/gitlab -o yaml | oc adm policy \ scc-subject-review -f -RESOURCE ALLOWED BY Deployment/gitlabanyuid
The output confirms that the anyuid SCC allows the gitlab deployment to create and update pods.
Create a service account named gitlab-sa.
[student@workstation ~]$ oc create sa gitlab-sa
serviceaccount/gitlab-sa createdAssign the anyuid SCC to the gitlab-sa service account.
[student@workstation ~]$ oc adm policy add-scc-to-user anyuid -z gitlab-sa
clusterrole.rbac.authorization.k8s.io/system:openshift:scc:anyuid added: "gitlab-sa"Modify the gitlab application to use the newly created service account.
Verify that the new deployment succeeds.
Log in as the developer user.
[student@workstation ~]$ oc login -u developer -p developer
Login successful.
...output omitted...Assign the gitlab-sa service account to the gitlab deployment.
[student@workstation ~]$ oc set serviceaccount deployment/gitlab gitlab-sa
deployment.apps/gitlab serviceaccount updatedVerify that the gitlab redeployment succeeds.
You might need to run the oc get pods command multiple times until you see a running application pod.
[student@workstation ~]$ oc get pods
NAME READY STATUS RESTARTS AGE
gitlab-86d6d65-zm2fd 1/1 Running 0 55sVerify that the gitlab application works.
Expose the gitlab application.
Because the gitlab service listens on ports 22, 80, and 443, you must use the --port option.
[student@workstation ~]$ oc expose service/gitlab --port 80 \
--hostname gitlab.apps.ocp4.example.com
route.route.openshift.io/gitlab exposedGet the exposed route.
[student@workstation ~]$ oc get routes
NAME HOST/PORT PATH SERVICES PORT ...
gitlab gitlab.apps.ocp4.example.com gitlab 80 ...Verify that the gitlab application is answering HTTP queries.
[student@workstation ~]$ curl -sL http://gitlab.apps.ocp4.example.com/ | \
grep '<title>'
<title>Sign in · GitLab</title>Delete the appsec-scc project.
[student@workstation ~]$ oc delete project appsec-scc
project.project.openshift.io "appsec-scc" deleted