Skip to content

Commit 3135e93

Browse files
committed
OCPBUGS-10458: corrections for unprivileged buildah config
1 parent 6742c59 commit 3135e93

4 files changed

+47
-38
lines changed

cicd/pipelines/unprivileged-building-of-container-images-using-buildah.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
:_content-type: ASSEMBLY
22
[id="unprivileged-building-of-container-images-using-buildah"]
3-
= Unprivileged building of container images using Buildah
3+
= Building of container images using Buildah as a non-root user
44
include::_attributes/common-attributes.adoc[]
55
:context: unprivileged-building-of-container-images-using-buildah
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. For secure unprivileged builds of container images using Buildah, 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. To run builds of container images using Buildah as a non-root user, you can perform the following steps:
1010

1111
* Define custom service account (SA) and security context constraint (SCC).
1212
* Configure Buildah to use the `build` user with id `1000`.
@@ -20,4 +20,4 @@ include::modules/op-limitations-of-unprivileged-builds.adoc[leveloffset=+1]
2020

2121
.Additional resources
2222

23-
* xref:../../authentication/managing-security-context-constraints.adoc#managing-pod-security-policies[Managing security context constraints (SCCs)]
23+
* xref:../../authentication/managing-security-context-constraints.adoc#managing-pod-security-policies[Managing security context constraints (SCCs)]

modules/op-configuring-buildah-to-use-build-user.adoc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
[id="configuring-builah-to-use-build-user_{context}"]
77
= Configuring Buildah to use `build` user
88

9-
You can define a Buildah task to use the `build` user with user id `1000`.
9+
You can define a Buildah task to use the `build` user with user id `1000`.
1010

1111
.Procedure
1212

1313
. Create a copy of the `buildah` cluster task as an ordinary task.
1414
+
1515
[source,terminal]
1616
----
17-
$ tkn task create --from=buildah
17+
$ oc get clustertask buildah -o yaml | yq '. |= (del .metadata |= with_entries(select(.key == "name" )))' | yq '.kind="Task"' | yq '.metadata.name="buildah-as-user"' | oc create -f -
1818
----
1919

2020
. Edit the copied `buildah` task.
2121
+
2222
[source,terminal]
2323
----
24-
$ oc edit task buildah
24+
$ oc edit task buildah-as-user
2525
----
2626
+
2727
.Example: Modified Buildah task with `build` user
@@ -98,13 +98,11 @@ spec:
9898
cat $(workspaces.source.path)/image-digest | tee /tekton/results/IMAGE_DIGEST
9999
volumeMounts:
100100
- name: varlibcontainers
101-
mountPath: /home/build/.local/share/containers
102-
volumeMounts:
103-
- name: varlibcontainers
104-
mountPath: /home/build/.local/share/containers
101+
mountPath: /home/build/.local/share/containers <3>
105102
volumes:
106103
- name: varlibcontainers
107104
emptyDir: {}
108105
----
109106
<1> Run the container explicitly as the user id `1000`, which corresponds to the `build` user in the Buildah image.
110-
<2> Display the user id to confirm that the process is running as user id `1000`.
107+
<2> Display the user id to confirm that the process is running as user id `1000`.
108+
<3> You can change the path for the volume mount as necessary.

modules/op-configuring-custom-sa-and-scc.adoc

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,33 @@
88

99
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`.
1010

11+
[IMPORTANT]
12+
====
13+
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.
14+
====
15+
1116
.Procedure
1217

1318
* Create a custom SA and SCC with necessary cluster role and role bindings.
1419
+
1520
.Example: Custom SA and SCC for used id `1000`
1621
[source,yaml]
1722
----
18-
apiVersion: v1
23+
apiVersion: v1
1924
kind: ServiceAccount
2025
metadata:
21-
name: pipelines-sa-userid-1000 <1>
26+
name: pipelines-sa-userid-1000 # <1>
2227
---
23-
kind: SecurityContextConstraints
28+
kind: SecurityContextConstraints
2429
metadata:
2530
annotations:
26-
name: pipelines-scc-userid-1000 <2>
31+
name: pipelines-scc-userid-1000 # <2>
2732
allowHostDirVolumePlugin: false
2833
allowHostIPC: false
2934
allowHostNetwork: false
3035
allowHostPID: false
3136
allowHostPorts: false
32-
allowPrivilegeEscalation: false
37+
allowPrivilegeEscalation: true # <3>
3338
allowPrivilegedContainer: false
3439
allowedCapabilities: null
3540
apiVersion: security.openshift.io/v1
@@ -42,7 +47,8 @@ priority: 10
4247
readOnlyRootFilesystem: false
4348
requiredDropCapabilities:
4449
- MKNOD
45-
runAsUser: <3>
50+
- KILL
51+
runAsUser: # <4>
4652
type: MustRunAs
4753
uid: 1000
4854
seLinuxContext:
@@ -58,10 +64,10 @@ volumes:
5864
- projected
5965
- secret
6066
---
61-
apiVersion: rbac.authorization.k8s.io/v1
67+
apiVersion: rbac.authorization.k8s.io/v1
6268
kind: ClusterRole
6369
metadata:
64-
name: pipelines-scc-userid-1000-clusterrole <4>
70+
name: pipelines-scc-userid-1000-clusterrole # <5>
6571
rules:
6672
- apiGroups:
6773
- security.openshift.io
@@ -72,10 +78,10 @@ rules:
7278
verbs:
7379
- use
7480
---
75-
apiVersion: rbac.authorization.k8s.io/v1
81+
apiVersion: rbac.authorization.k8s.io/v1
7682
kind: RoleBinding
7783
metadata:
78-
name: pipelines-scc-userid-1000-rolebinding <5>
84+
name: pipelines-scc-userid-1000-rolebinding # <6>
7985
roleRef:
8086
apiGroup: rbac.authorization.k8s.io
8187
kind: ClusterRole
@@ -89,8 +95,10 @@ subjects:
8995

9096
<2> Define a custom SCC created based on restricted privileges, with modified `runAsUser` field.
9197

92-
<3> Restrict any pod that gets attached with the custom SCC through the custom SA to run as user id `1000`.
98+
<3> 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.
99+
100+
<4> Restrict any pod that gets attached with the custom SCC through the custom SA to run as user id `1000`.
93101

94-
<4> Define a cluster role that uses the custom SCC.
102+
<5> Define a cluster role that uses the custom SCC.
95103

96-
<5> Bind the cluster role that uses the custom SCC to the custom SA.
104+
<6> Bind the cluster role that uses the custom SCC to the custom SA.

modules/op-starting-a-task-run-pipeline-run-build-user.adoc

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[id="starting-a-task-run-with-custom-config-map-or-a-pipeline-run_{context}"]
77
= Starting a task run with custom config map, or a pipeline run
88

9-
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.
9+
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.
1010

1111
.Procedure
1212

@@ -26,14 +26,14 @@ data:
2626
CMD git
2727
kind: ConfigMap
2828
metadata:
29-
name: dockerfile <1>
29+
name: dockerfile # <1>
3030
---
3131
apiVersion: tekton.dev/v1beta1
3232
kind: TaskRun
3333
metadata:
3434
name: buildah-as-user-1000
3535
spec:
36-
serviceAccountName: pipelines-sa-userid-1000
36+
serviceAccountName: pipelines-sa-userid-1000 # <2>
3737
params:
3838
- name: IMAGE
3939
value: image-registry.openshift-image-registry.svc:5000/test/buildahuser
@@ -42,11 +42,12 @@ spec:
4242
name: buildah-as-user
4343
workspaces:
4444
- configMap:
45-
name: dockerfile <2>
45+
name: dockerfile # <3>
4646
name: source
4747
----
48-
<1> Use a config map because the focus is on the task run, without any prior task that fetches some sources with a Dockerfile.
49-
<2> Mount a config map as the source workspace for the `buildah-as-user` task.
48+
<1> Use a config map because the focus is on the task run, without any prior task that fetches some sources with a Dockerfile.
49+
<2> The name of the service account that you created.
50+
<3> Mount a config map as the source workspace for the `buildah-as-user` task.
5051

5152
. (Optional) Create a pipeline and a corresponding pipeline run.
5253
+
@@ -66,7 +67,7 @@ spec:
6667
- name: sslcertdir
6768
optional: true
6869
tasks:
69-
- name: fetch-repository <1>
70+
- name: fetch-repository # <1>
7071
taskRef:
7172
name: git-clone
7273
kind: ClusterTask
@@ -82,7 +83,7 @@ spec:
8283
value: "true"
8384
- name: buildah
8485
taskRef:
85-
name: buildah-as-user <2>
86+
name: buildah-as-user # <2>
8687
runAfter:
8788
- fetch-repository
8889
workspaces:
@@ -99,17 +100,18 @@ kind: PipelineRun
99100
metadata:
100101
name: pipelinerun-buildah-as-user-1000
101102
spec:
102-
serviceAccountName: pipelines-sa-userid-1000
103+
taskRunSpecs:
104+
- pipelineTaskName: buildah
105+
taskServiceAccountName: pipelines-sa-userid-1000 # <3>
103106
params:
104107
- name: URL
105108
value: https://github.com/openshift/pipelines-vote-api
106109
- name: IMAGE
107110
value: image-registry.openshift-image-registry.svc:5000/test/buildahuser
108-
taskRef:
109-
kind: Pipeline
111+
pipelineRef:
110112
name: pipeline-buildah-as-user-1000
111113
workspaces:
112-
- name: shared-workspace <3>
114+
- name: shared-workspace # <4>
113115
volumeClaimTemplate:
114116
spec:
115117
accessModes:
@@ -120,6 +122,7 @@ spec:
120122
----
121123
<1> Use the `git-clone` cluster task to fetch the source containing a Dockerfile and build it using the modified Buildah task.
122124
<2> Refer to the modified Buildah task.
123-
<3> Share data between the `git-clone` task and the modified Buildah task using a persistent volume claim (PVC) created automatically by the controller.
125+
<3> Use the service account that you created for the Buildah task.
126+
<4> Share data between the `git-clone` task and the modified Buildah task using a persistent volume claim (PVC) created automatically by the controller.
124127

125-
. Start the task run or the pipeline run.
128+
. Start the task run or the pipeline run.

0 commit comments

Comments
 (0)