Bookmark this page

Guided Exercise: Control Application Permissions with Security Context Constraints

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

  1. Log in to the OpenShift cluster and create the appsec-scc project.

    1. 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...
    2. Create the appsec-scc project.

      [student@workstation ~]$ oc new-project appsec-scc
      Now using project "appsec-scc" on server ...
      ...output omitted...
  2. 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.

    1. 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...
    2. Determine whether the application is successfully deployed. It should give an error, because this image needs root privileges to deploy.

      [student@workstation ~]$ oc get pods
      NAME                     READY   STATUS              RESTARTS   AGE
      gitlab-d89cd88f8-jwqbp   0/1     Error               0          36s

      Note

      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.

    3. Review the application logs to confirm that insufficient privileges caused the failure.

      [student@workstation ~]$ oc logs pod/gitlab-d89cd88f8-jwqbp
      ...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: Cannot 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.

  3. Create a service account and assign the anyuid SCC to it.

    1. 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...
    2. Verify the appropriate SCC to use with this deployment.

      [student@workstation]$ oc get deploy
      NAME     READY   UP-TO-DATE   AVAILABLE   AGE
      gitlab   0/1     1            0           109s
      
      [student@workstation]$ oc get deploy/gitlab -o yaml | oc adm policy \
        scc-subject-review -f -
      RESOURCE            ALLOWED BY
      Deployment/gitlab   anyuid

      The output confirms that the anyuid SCC allows the gitlab deployment to create and update pods.

    3. Create a service account named gitlab-sa.

      [student@workstation ~]$ oc create sa gitlab-sa
      serviceaccount/gitlab-sa created
    4. Assign 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"
  4. Modify the gitlab application to use the newly created service account. Verify that the new deployment succeeds.

    1. Log in as the developer user.

      [student@workstation ~]$ oc login -u developer -p developer
      Login successful.
      ...output omitted...
    2. Assign the gitlab-sa service account to the gitlab deployment.

      [student@workstation ~]$ oc set serviceaccount deployment/gitlab gitlab-sa
      deployment.apps/gitlab serviceaccount updated
    3. Verify 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          55s
  5. Verify that the gitlab application works.

    1. 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 exposed
    2. Get the exposed route.

      [student@workstation ~]$ oc get routes
      NAME     HOST/PORT                      PATH   SERVICES   PORT   ...
      gitlab   gitlab.apps.ocp4.example.com          gitlab     80     ...
    3. 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>
  6. Delete the appsec-scc project.

    [student@workstation ~]$ oc delete project appsec-scc
    project.project.openshift.io "appsec-scc" deleted

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish appsec-scc

Revision: do280-4.14-08d11e1