第6章 Building of container images using Buildah as a non-root user
Running OpenShift Pipelines as the root user on a container can expose the container processes and the host to other potentially malicious resources. You can reduce this type of exposure by running the workload as a specific non-root user in the container.
In most cases, you can run Buildah without root privileges by creating a custom task for building the image and configuring user namespaces in this task.
If your image does not build successfully using this configuration, you can use custom service account (SA) and security context constraint (SCC) definitions; however, if you use this option, you must enable the Buildah step to raise its privileges (allowPrivilegeEscalation: true).
6.1. Running Buildah as a non-root user by configuring user namespaces リンクのコピーリンクがクリップボードにコピーされました!
Configuring user namespaces is the simplest way to run Buildah in a task as a non-root user. However, some images might not build using this option.
Prerequisites
-
You have installed the
occommand-line utility.
Procedure
To create a copy of the
buildahtask, which is provided in theopenshift-pipelinesnamespace, and to change the name of the copy tobuildah-as-user, enter the following command:$ oc get task buildah -n openshift-pipelines -o yaml | yq '. |= (del .metadata |= with_entries(select(.key == "name" )))' | yq '.kind="Task"' | yq '.metadata.name="buildah-as-user"' | oc create -f -Edit the copied
buildahtask by entering the following command:$ oc edit task buildah-as-userIn the new task, create
annotationsandstepTemplatesections, as shown in the following example:Example additions to the
buildah-as-usertaskapiVersion: tekton.dev/v1 kind: Task metadata: annotations: io.kubernetes.cri-o.userns-mode: 'auto:size=65536;map-to-root=true' io.openshift.builder: 'true' name: assemble-containerimage namespace: pipeline-namespace spec: description: This cluster task builds an image. # ... stepTemplate: env: - name: HOME value: /tekton/home image: $(params.builder-image) imagePullPolicy: IfNotPresent name: '' resources: limits: cpu: '1' memory: 4Gi requests: cpu: 100m memory: 2Gi securityContext: capabilities: add: - SETFCAP runAsNonRoot: true runAsUser: 10001 workingDir: $(workspaces.working-directory.path) # ...- 1
- The
runAsUser:setting is not strictly necessary, becausepodTemplateis used.
-
Use the new
buildah-as-usertask to build the image in your pipeline.