Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 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 Copier lienLien copié sur presse-papiers!
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
oc
command-line utility.
Procedure
To create a copy of the
buildah
task, which is provided in theopenshift-pipelines
namespace, 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 -
$ 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 -
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the copied
buildah
task by entering the following command:oc edit task buildah-as-user
$ oc edit task buildah-as-user
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the new task, create
annotations
andstepTemplate
sections, as shown in the following example:Example additions to the
buildah-as-user
taskCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- The
runAsUser:
setting is not strictly necessary, becausepodTemplate
is used.
-
Use the new
buildah-as-user
task to build the image in your pipeline.
6.2. Running Buildah as a non-root user by defining a custom SA and SCC Copier lienLien copié sur presse-papiers!
To run builds of container images using Buildah as a non-root user, you can perform the following steps:
- Define custom service account (SA) and security context constraint (SCC).
-
Configure Buildah to use the
build
user with id1000
. - Start a task run with a custom config map, or integrate it with a pipeline run.
6.2.1. Configuring custom service account and security context constraint Copier lienLien copié sur presse-papiers!
The default pipeline
SA allows using a user id outside of the namespace range. To reduce dependency on the default SA, you can define a custom SA and SCC with necessary cluster role and role bindings for the build
user with user id 1000
.
At this time, enabling the allowPrivilegeEscalation
setting is required for Buildah to run successfully in the container. With this setting, Buildah can leverage SETUID
and SETGID
capabilities when running as a non-root user.
Procedure
Create a custom SA and SCC with necessary cluster role and role bindings.
Example: Custom SA and SCC for used id
1000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- 1
- Define a custom SA.
- 2
- Define a custom SCC created based on restricted privileges, with modified
runAsUser
field. - 3
- At this time, enabling the
allowPrivilegeEscalation
setting is required for Buildah to run successfully in the container. With this setting, Buildah can leverageSETUID
andSETGID
capabilities when running as a non-root user. - 4
- Restrict any pod that gets attached with the custom SCC through the custom SA to run as user id
1000
. - 5
- Define a cluster role that uses the custom SCC.
- 6
- Bind the cluster role that uses the custom SCC to the custom SA.
6.2.2. Configuring Buildah to use build user Copier lienLien copié sur presse-papiers!
You can define a Buildah task to use the build
user with user id 1000
.
Procedure
Create a copy of the
buildah
task, which is provided in theopenshift-pipelines
namespace; change the name of the copy tobuildah-as-user
.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 -
$ 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 -
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the copied
buildah
task.oc edit task buildah-as-user
$ oc edit task buildah-as-user
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example: Modified Buildah task with
build
userCopy to Clipboard Copied! Toggle word wrap Toggle overflow
6.2.3. Starting a task run with custom config map, or a pipeline run Copier lienLien copié sur presse-papiers!
After defining the custom Buildah cluster task, you can create a TaskRun
object that builds an image as a build
user with user id 1000
. In addition, you can integrate the TaskRun
object as part of a PipelineRun
object.
Procedure
Create a
TaskRun
object with a customConfigMap
andDockerfile
objects.Example: A task run that runs Buildah as user id
1000
Copy to Clipboard Copied! Toggle word wrap Toggle overflow (Optional) Create a pipeline and a corresponding pipeline run.
Example: A pipeline and corresponding pipeline run
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Use the
git-clone
cluster task to fetch the source containing a Dockerfile and build it using the modified Buildah task. - 2
- Refer to the modified Buildah task.
- 3
- Use the service account that you created for the Buildah task.
- 4
- Share data between the
git-clone
task and the modified Buildah task using a persistent volume claim (PVC) created automatically by the controller.
- Start the task run or the pipeline run.
6.3. Limitations of unprivileged builds Copier lienLien copié sur presse-papiers!
The process for unprivileged builds works with most Dockerfile
objects. However, there are some known limitations might cause a build to fail:
-
Using the
--mount=type=cache
option might fail due to lack of necessay permissions issues. For more information, see this article. -
Using the
--mount=type=secret
option fails because mounting resources requires additionnal capabilities that are not provided by the custom SCC.
Additional resources