Bookmark this page

Chapter 8.  Application Security

Abstract

Goal

Run applications that require elevated or special privileges from the host operating system or Kubernetes.

Objectives
  • Create service accounts and apply permissions, and manage security context constraints.

  • Run an application that requires access to the Kubernetes API of the application's cluster.

  • Automate regular cluster and application management tasks by using Kubernetes cron jobs.

Sections
  • Control Application Permissions with Security Context Constraints (and Guided Exercise)

  • Allow Application Access to Kubernetes APIs (and Guided Exercise)

  • Cluster and Node Maintenance with Kubernetes Cron Jobs (and Guided Exercise)

Lab
  • Application Security

Control Application Permissions with Security Context Constraints

Objectives

  • Create service accounts and apply permissions, and manage security context constraints.

Security Context Constraints (SCCs)

Red Hat OpenShift provides security context constraints (SCCs), a security mechanism that limits the access from a running pod in OpenShift to the host environment. SCCs control the following host resources:

  • Running privileged containers

  • Requesting extra capabilities for a container

  • Using host directories as volumes

  • Changing the SELinux context of a container

  • Changing the user ID

Some community-developed containers might require relaxed security context constraints to access resources that are forbidden by default, such as file systems or sockets, or to access an SELinux context.

Cluster administrators can run the following command to list the SCCs that OpenShift defines:

[user@host ~]$ oc get scc

OpenShift provides the following default SCCs:

  • anyuid

  • hostaccess

  • hostmount-anyuid

  • hostnetwork

  • hostnetwork-v2

  • lvms-topolvm-node

  • lvms-vgmanager

  • machine-api-termination-handler

  • node-exporter

  • nonroot

  • nonroot-v2

  • privileged

  • restricted

  • restricted-v2

For additional information about an SCC, use the oc describe command:

[user@host ~]$ oc describe scc anyuid
Name:           anyuid
Priority:         10
Access:
  Users:          <none>
  Groups:         system:cluster-admins
Settings:
...output omitted...

Most pods that OpenShift creates use the restricted-v2 SCC, which provides limited access to resources that are external to OpenShift. Use the oc describe command to view the security context constraint that a pod uses.

[user@host ~]$ oc describe pod console-5df4fcbb47-67c52 \
    -n openshift-console | grep scc
                      openshift.io/scc: restricted-v2

Container images that are downloaded from public container registries, such as Docker Hub, might fail to run when using the restricted-v2 SCC. For example, a container image that requires running as a specific user ID can fail because the restricted-v2 SCC runs the container by using a random user ID. A container image that listens on port 80 or on port 443 can fail for a related reason. The random user ID that the restricted-v2 SCC uses cannot start a service that listens on a privileged network port (port numbers that are less than 1024). Use the scc-subject-review subcommand to list all the security context constraints that can overcome the limitations that hinder the container:

[user@host ~]$ oc get deployment deployment-name -o yaml | \
    oc adm policy scc-subject-review -f -

The anyuid SCC defines the run as user strategy to be RunAsAny, which means that the pod can run as any available user ID in the container. With this strategy, containers that require a specific user can run the commands by using a specific user ID.

To change the container to run with a different SCC, you must create a service account that is bound to a pod. Use the oc create serviceaccount command to create the service account, and use the -n option if the service account must be created in a different namespace from the current one:

[user@host ~]$ oc create serviceaccount service-account-name

To associate the service account with an SCC, use the oc adm policy command. Identify a service account by using the -z option, and use the -n option if the service account exists in a different namespace from the current one:

[user@host ~]$ oc adm policy add-scc-to-user SCC -z service-account

Important

Only cluster administrators can assign an SCC to a service account or remove an SCC from a service account. Allowing pods to run with a less restrictive SCC can make your cluster less secure. Use with caution.

Change an existing deployment or deployment configuration to use the service account by using the oc set serviceaccount command:

[user@host ~]$ oc set serviceaccount deployment/deployment-name \
    service-account-name

If the command succeeds, then the pods that are associated with the deployment or deployment configuration redeploy.

Privileged Containers

Some containers might need to access the runtime environment of the host. For example, the S2I builder class of privileged containers requires access beyond the limits of its own containers. These containers can pose security risks, because they can use any resources on an OpenShift node. Use SCCs to enable access for privileged containers by creating service accounts with privileged access.

References

For more information, refer to the Managing Security Context Constraints chapter in the Red Hat OpenShift Container Platform 4.14 Authentication and Authorization documentation at https://access.redhat.com/documentation/en-us/openshift_container_platform/4.14/html-single/authentication_and_authorization/index#managing-pod-security-policies

Revision: do280-4.14-08d11e1