Skip to content

Commit b5d6cc3

Browse files
committed
Use Bash to assert on dropped caps in E2E tests
OpenShift appends to the list of dropped capabilities, and KUTTL is unable to assert a subset of that list. Do the assertion ourselves in a script rather than create a copy of the test specifically for OpenShift. Issue: [sc-15297] See: kudobuilder/kuttl#76
1 parent 6ac98d1 commit b5d6cc3

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

testing/kuttl/e2e/security-context/00-assert.yaml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ spec:
3232
- name: pgbackrest
3333
securityContext:
3434
allowPrivilegeEscalation: false
35-
capabilities: { drop: [ALL] }
3635
privileged: false
3736
readOnlyRootFilesystem: true
3837
runAsNonRoot: true
@@ -52,50 +51,43 @@ spec:
5251
- name: database
5352
securityContext:
5453
allowPrivilegeEscalation: false
55-
capabilities: { drop: [ALL] }
5654
privileged: false
5755
readOnlyRootFilesystem: true
5856
runAsNonRoot: true
5957
- name: replication-cert-copy
6058
securityContext:
6159
allowPrivilegeEscalation: false
62-
capabilities: { drop: [ALL] }
6360
privileged: false
6461
readOnlyRootFilesystem: true
6562
runAsNonRoot: true
6663
- name: pgbackrest
6764
securityContext:
6865
allowPrivilegeEscalation: false
69-
capabilities: { drop: [ALL] }
7066
privileged: false
7167
readOnlyRootFilesystem: true
7268
runAsNonRoot: true
7369
- name: pgbackrest-config
7470
securityContext:
7571
allowPrivilegeEscalation: false
76-
capabilities: { drop: [ALL] }
7772
privileged: false
7873
readOnlyRootFilesystem: true
7974
runAsNonRoot: true
8075
- name: exporter
8176
securityContext:
8277
allowPrivilegeEscalation: false
83-
capabilities: { drop: [ALL] }
8478
privileged: false
8579
readOnlyRootFilesystem: true
8680
runAsNonRoot: true
8781
initContainers:
8882
- name: postgres-startup
8983
securityContext:
9084
allowPrivilegeEscalation: false
91-
capabilities: { drop: [ALL] }
9285
privileged: false
9386
readOnlyRootFilesystem: true
9487
runAsNonRoot: true
9588
- name: nss-wrapper-init
9689
securityContext:
9790
allowPrivilegeEscalation: false
98-
capabilities: { drop: [ALL] }
9991
privileged: false
10092
readOnlyRootFilesystem: true
10193
runAsNonRoot: true
@@ -115,22 +107,19 @@ spec:
115107
- name: pgadmin
116108
securityContext:
117109
allowPrivilegeEscalation: false
118-
capabilities: { drop: [ALL] }
119110
privileged: false
120111
readOnlyRootFilesystem: true
121112
runAsNonRoot: true
122113
initContainers:
123114
- name: pgadmin-startup
124115
securityContext:
125116
allowPrivilegeEscalation: false
126-
capabilities: { drop: [ALL] }
127117
privileged: false
128118
readOnlyRootFilesystem: true
129119
runAsNonRoot: true
130120
- name: nss-wrapper-init
131121
securityContext:
132122
allowPrivilegeEscalation: false
133-
capabilities: { drop: [ALL] }
134123
privileged: false
135124
readOnlyRootFilesystem: true
136125
runAsNonRoot: true
@@ -147,14 +136,12 @@ spec:
147136
- name: pgbouncer
148137
securityContext:
149138
allowPrivilegeEscalation: false
150-
capabilities: { drop: [ALL] }
151139
privileged: false
152140
readOnlyRootFilesystem: true
153141
runAsNonRoot: true
154142
- name: pgbouncer-config
155143
securityContext:
156144
allowPrivilegeEscalation: false
157-
capabilities: { drop: [ALL] }
158145
privileged: false
159146
readOnlyRootFilesystem: true
160147
runAsNonRoot: true
@@ -175,29 +162,25 @@ spec:
175162
- name: pgbackrest
176163
securityContext:
177164
allowPrivilegeEscalation: false
178-
capabilities: { drop: [ALL] }
179165
privileged: false
180166
readOnlyRootFilesystem: true
181167
runAsNonRoot: true
182168
- name: pgbackrest-config
183169
securityContext:
184170
allowPrivilegeEscalation: false
185-
capabilities: { drop: [ALL] }
186171
privileged: false
187172
readOnlyRootFilesystem: true
188173
runAsNonRoot: true
189174
initContainers:
190175
- name: pgbackrest-log-dir
191176
securityContext:
192177
allowPrivilegeEscalation: false
193-
capabilities: { drop: [ALL] }
194178
privileged: false
195179
readOnlyRootFilesystem: true
196180
runAsNonRoot: true
197181
- name: nss-wrapper-init
198182
securityContext:
199183
allowPrivilegeEscalation: false
200-
capabilities: { drop: [ALL] }
201184
privileged: false
202185
readOnlyRootFilesystem: true
203186
runAsNonRoot: true
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
apiVersion: kuttl.dev/v1beta1
3+
kind: TestStep
4+
commands:
5+
- script: |
6+
# Check that every container has the correct capabilities.
7+
8+
# Capture every container name alongside its list of dropped capabilities.
9+
CONTAINERS_DROP_CAPS=$(
10+
kubectl --namespace "${NAMESPACE}" get pods --output "jsonpath={\
11+
range .items[*].spec.containers[*]\
12+
}{ @.name }{'\t\t'}{ @.securityContext.capabilities.drop }{'\n'}{\
13+
end\
14+
}"
15+
) || exit
16+
17+
WRONG=$( ! echo "${CONTAINERS_DROP_CAPS}" | grep -Fv '"ALL"' ) || {
18+
echo 'Not all containers have dropped "ALL" capabilities!'
19+
echo "${WRONG}"
20+
exit 1
21+
}
22+
23+
- script: |
24+
# Check that every Pod is assigned to the "restricted" SecurityContextConstraint
25+
# in OpenShift.
26+
27+
SCC=$(
28+
kubectl api-resources --cached |
29+
grep -F 'security.openshift.io/v1' |
30+
grep -F 'SecurityContextConstraint'
31+
)
32+
33+
# Skip this check when the API has no notion of SecurityContextConstraint.
34+
[ -z "${SCC}" ] && exit
35+
36+
PODS_SCC=$(
37+
kubectl --namespace "${NAMESPACE}" get pods --no-headers \
38+
--output "custom-columns=\
39+
NAME:.metadata.name,\
40+
SCC:.metadata.annotations['openshift\.io/scc']\
41+
"
42+
) || exit
43+
44+
WRONG=$( ! echo "${PODS_SCC}" | grep -Ev '\<restricted$' ) || {
45+
echo 'Found pods not assigned to the restricted security context constraint!'
46+
echo "${PODS_SCC}"
47+
exit 1
48+
}

0 commit comments

Comments
 (0)