|
| 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. |
0 commit comments