Skip to content

Commit 05c3349

Browse files
authored
Merge pull request #79325 from mramendi/RHDEVDOCS-5719
RHDEVDOCS 5719 running Buildah as nonroot with user namespaces
2 parents ef9b914 + 4489e56 commit 05c3349

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// This module is included in the following assemblies:
2+
// * secure/unprivileged-building-of-container-images-using-buildah.adoc
3+
4+
:_mod-docs-content-type: PROCEDURE
5+
6+
[id="nonroot-buildah-user-namespaces_{context}"]
7+
= Running Buildah as a non-root user by configuring user namespaces
8+
9+
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.
10+
11+
.Prerequisites
12+
13+
* You have installed the `oc` command-line utility.
14+
15+
.Procedure
16+
17+
. To create a copy of the `buildah` task, which is provided in the `openshift-pipelines` namespace, and to change the name of the copy to `buildah-as-user`, enter the following command:
18+
+
19+
[source,terminal]
20+
----
21+
$ 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 -
22+
----
23+
24+
. Edit the copied `buildah` task by entering the following command:
25+
+
26+
[source,terminal]
27+
----
28+
$ oc edit task buildah-as-user
29+
----
30+
+
31+
In the new task, create `annotations` and `stepTemplate` sections, as shown in the following example:
32+
+
33+
.Example additions to the `buildah-as-user` task
34+
[source,yaml]
35+
----
36+
apiVersion: tekton.dev/v1
37+
kind: Task
38+
metadata:
39+
annotations:
40+
io.kubernetes.cri-o.userns-mode: 'auto:size=65536;map-to-root=true'
41+
io.openshift.builder: 'true'
42+
name: assemble-containerimage
43+
namespace: pipeline-namespace
44+
spec:
45+
description: This cluster task builds an image.
46+
# ...
47+
stepTemplate:
48+
env:
49+
- name: HOME
50+
value: /tekton/home
51+
image: $(params.builder-image)
52+
imagePullPolicy: IfNotPresent
53+
name: ''
54+
resources:
55+
limits:
56+
cpu: '1'
57+
memory: 4Gi
58+
requests:
59+
cpu: 100m
60+
memory: 2Gi
61+
securityContext:
62+
capabilities:
63+
add:
64+
- SETFCAP
65+
runAsNonRoot: true
66+
runAsUser: 1000 # <1>
67+
workingDir: $(workspaces.working-directory.path)
68+
# ...
69+
----
70+
<1> The `runAsUser:` setting is not strictly necessary, because `podTemplate` is used.
71+
72+
. Use the new `buildah-as-user` task to build the image in your pipeline.

secure/unprivileged-building-of-container-images-using-buildah.adoc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,27 @@ include::_attributes/common-attributes.adoc[]
66

77
toc::[]
88

9-
Running {pipelines-shortname} 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. To run builds of container images using Buildah as a non-root user, you can perform the following steps:
9+
Running {pipelines-shortname} 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.
10+
11+
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.
12+
13+
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`).
14+
15+
include::modules/op-nonroot-buildah-user-namespaces.adoc[leveloffset=+1]
16+
17+
[id="buildah-nonroot-sa-scc"]
18+
== Running Buildah as a non-root user by defining a custom SA and SCC
19+
20+
To run builds of container images using Buildah as a non-root user, you can perform the following steps:
1021

1122
* Define custom service account (SA) and security context constraint (SCC).
1223
* Configure Buildah to use the `build` user with id `1000`.
1324
* Start a task run with a custom config map, or integrate it with a pipeline run.
1425

15-
include::modules/op-configuring-custom-sa-and-scc.adoc[leveloffset=+1]
16-
include::modules/op-configuring-buildah-to-use-build-user.adoc[leveloffset=+1]
17-
include::modules/op-starting-a-task-run-pipeline-run-build-user.adoc[leveloffset=+1]
26+
include::modules/op-configuring-custom-sa-and-scc.adoc[leveloffset=+2]
27+
include::modules/op-configuring-buildah-to-use-build-user.adoc[leveloffset=+2]
28+
include::modules/op-starting-a-task-run-pipeline-run-build-user.adoc[leveloffset=+2]
29+
1830
include::modules/op-limitations-of-unprivileged-builds.adoc[leveloffset=+1]
1931

2032

0 commit comments

Comments
 (0)