Skip to content

Commit aed0162

Browse files
committed
fix(hooks): run the refreshed alpine/k8s kubectl hooks non-root
The vm-disk pvc-resize, cozystack-api, seaweedfs and vm-instance update hooks (whose alpine/k8s image this PR refreshes) ran as root with no securityContext. Add a pod-level runAsNonRoot + runAsUser 65534 + seccompProfile RuntimeDefault and a container-level allowPrivilegeEscalation: false + capabilities drop ALL, mirroring the dashboard adopt hook. kubectl needs no root, so this is behavior-neutral. Each hook is lookup-gated; pin the securityContext in a per-chart helm-unittest that mocks the gating object (DaemonSet / PVC / Service) via kubernetesProvider so the guard renders without a cluster. vm-disk and vm-instance had no Makefile test target, so add 'test: helm unittest .' to both so CI runs the new suites. Scope note: other kubectl hooks on clastix/kubectl (mariadb, keycloak, kubernetes, tenant) are untouched here — they are not part of this alpine/k8s refresh and belong to a separate hardening pass. Also fix the vm-disk resize hook's backoffLimit: it was nested under the pod spec (silently ignored, leaving the Job at the default 6 retries) instead of Job.spec. Move it to Job.spec and pin it in the test. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 4a9be4d commit aed0162

10 files changed

Lines changed: 234 additions & 1 deletion

File tree

‎packages/apps/vm-disk/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include ../../../hack/package.mk
22

3+
test:
4+
helm unittest .
5+
36
generate:
47
cozyvalues-gen -m 'vmdisk' -v values.yaml -s values.schema.json -r README.md -g ../../../api/apps/v1alpha1/vmdisk/types.go
58
../../../hack/update-crd.sh

‎packages/apps/vm-disk/templates/pvc-resize-hook.yaml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,26 @@ metadata:
2121
helm.sh/hook-weight: "0"
2222
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
2323
spec:
24+
backoffLimit: 1
2425
template:
2526
metadata:
2627
labels:
2728
policy.cozystack.io/allow-to-apiserver: "true"
2829
spec:
2930
serviceAccountName: {{ .Release.Name }}-volume-resize-hook
3031
restartPolicy: Never
31-
backoffLimit: 1
32+
securityContext:
33+
runAsNonRoot: true
34+
runAsUser: 65534
35+
seccompProfile:
36+
type: RuntimeDefault
3237
containers:
3338
- name: resize
3439
image: docker.io/alpine/k8s:1.36.2
40+
securityContext:
41+
allowPrivilegeEscalation: false
42+
capabilities:
43+
drop: ["ALL"]
3544
command: ["sh", "-xec"]
3645
args:
3746
- |
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
suite: vm-disk pvc-resize hook runs non-root
2+
3+
# The resize hook only renders when an existing PVC is being grown
4+
# (lookup-gated on the PVC + a larger .Values.storage), so mock a smaller PVC
5+
# and request a larger size to drive the render, then pin the non-root
6+
# securityContext. Scope to the hook template so the chart's tenant-identifier
7+
# lookups elsewhere don't need a live cluster.
8+
release:
9+
name: test-vm-disk
10+
namespace: tenant-test
11+
templates:
12+
- templates/pvc-resize-hook.yaml
13+
set:
14+
storage: 2Gi
15+
kubernetesProvider:
16+
scheme:
17+
"v1/PersistentVolumeClaim":
18+
gvr:
19+
group: ""
20+
version: "v1"
21+
resource: "persistentvolumeclaims"
22+
namespaced: true
23+
objects:
24+
- kind: PersistentVolumeClaim
25+
apiVersion: v1
26+
metadata:
27+
name: test-vm-disk
28+
namespace: tenant-test
29+
spec:
30+
resources:
31+
requests:
32+
storage: 1Gi
33+
tests:
34+
- it: pvc-resize hook Job runs under a restrictive non-root securityContext
35+
documentSelector:
36+
path: kind
37+
value: Job
38+
asserts:
39+
- equal:
40+
path: spec.template.spec.securityContext.runAsNonRoot
41+
value: true
42+
- equal:
43+
path: spec.template.spec.securityContext.runAsUser
44+
value: 65534
45+
- equal:
46+
path: spec.template.spec.securityContext.seccompProfile.type
47+
value: RuntimeDefault
48+
- equal:
49+
path: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
50+
value: false
51+
- contains:
52+
path: spec.template.spec.containers[0].securityContext.capabilities.drop
53+
content: ALL
54+
55+
- it: backoffLimit is set at Job.spec, not silently dropped under the pod spec
56+
documentSelector:
57+
path: kind
58+
value: Job
59+
asserts:
60+
- equal:
61+
path: spec.backoffLimit
62+
value: 1
63+
- notExists:
64+
path: spec.template.spec.backoffLimit

‎packages/apps/vm-instance/Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
include ../../../hack/package.mk
22

3+
test:
4+
helm unittest .
5+
36
generate:
47
cozyvalues-gen -m 'vminstance' -v values.yaml -s values.schema.json -r README.md -g ../../../api/apps/v1alpha1/vminstance/types.go
58
../../../hack/update-crd.sh

‎packages/apps/vm-instance/templates/vm-update-hook.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ spec:
5656
spec:
5757
serviceAccountName: {{ $.Release.Name }}-update-hook
5858
restartPolicy: Never
59+
securityContext:
60+
runAsNonRoot: true
61+
runAsUser: 65534
62+
seccompProfile:
63+
type: RuntimeDefault
5964
containers:
6065
- name: update-resources
6166
image: docker.io/alpine/k8s:1.36.2
67+
securityContext:
68+
allowPrivilegeEscalation: false
69+
capabilities:
70+
drop: ["ALL"]
6271
resources:
6372
requests:
6473
memory: "16Mi"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
suite: vm-instance update-hook runs non-root
2+
3+
# The update hook only renders when an existing VM/Service needs reconciling
4+
# (lookup-gated). Mock a Service whose type differs from the desired one
5+
# (existing LoadBalancer vs desired ClusterIP) to drive the render, then pin
6+
# the non-root securityContext. Scope to the hook template so the chart's
7+
# instanceType lookups elsewhere don't need a live cluster.
8+
release:
9+
name: test-vm
10+
namespace: tenant-test
11+
templates:
12+
- templates/vm-update-hook.yaml
13+
set:
14+
fullnameOverride: test-vm
15+
external: false
16+
kubernetesProvider:
17+
scheme:
18+
"v1/Service":
19+
gvr:
20+
group: ""
21+
version: "v1"
22+
resource: "services"
23+
namespaced: true
24+
objects:
25+
- kind: Service
26+
apiVersion: v1
27+
metadata:
28+
name: test-vm
29+
namespace: tenant-test
30+
spec:
31+
type: LoadBalancer
32+
tests:
33+
- it: update-hook Job runs under a restrictive non-root securityContext
34+
documentSelector:
35+
path: kind
36+
value: Job
37+
asserts:
38+
- equal:
39+
path: spec.template.spec.securityContext.runAsNonRoot
40+
value: true
41+
- equal:
42+
path: spec.template.spec.securityContext.runAsUser
43+
value: 65534
44+
- equal:
45+
path: spec.template.spec.securityContext.seccompProfile.type
46+
value: RuntimeDefault
47+
- equal:
48+
path: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
49+
value: false
50+
- contains:
51+
path: spec.template.spec.containers[0].securityContext.capabilities.drop
52+
content: ALL

‎packages/system/cozystack-api/templates/hook.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ spec:
1616
policy.cozystack.io/allow-to-apiserver: "true"
1717
spec:
1818
serviceAccountName: "cozystack-api-hook"
19+
securityContext:
20+
runAsNonRoot: true
21+
runAsUser: 65534
22+
seccompProfile:
23+
type: RuntimeDefault
1924
containers:
2025
- name: kubectl
2126
image: docker.io/alpine/k8s:1.36.2
27+
securityContext:
28+
allowPrivilegeEscalation: false
29+
capabilities:
30+
drop: ["ALL"]
2231
command:
2332
- sh
2433
args:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
suite: cozystack-api post-upgrade hook runs non-root
2+
3+
# The hook only renders when the legacy cozystack-api DaemonSet still exists
4+
# (lookup-gated), so mock that DaemonSet to drive the render, then pin the
5+
# non-root securityContext so it cannot regress to running kubectl as root.
6+
release:
7+
name: cozystack-api
8+
namespace: cozy-system
9+
templates:
10+
- templates/hook.yaml
11+
kubernetesProvider:
12+
scheme:
13+
"apps/v1/DaemonSet":
14+
gvr:
15+
group: "apps"
16+
version: "v1"
17+
resource: "daemonsets"
18+
namespaced: true
19+
objects:
20+
- kind: DaemonSet
21+
apiVersion: apps/v1
22+
metadata:
23+
name: cozystack-api
24+
namespace: cozy-system
25+
tests:
26+
- it: post-upgrade hook Job runs under a restrictive non-root securityContext
27+
documentSelector:
28+
path: kind
29+
value: Job
30+
asserts:
31+
- equal:
32+
path: spec.template.spec.securityContext.runAsNonRoot
33+
value: true
34+
- equal:
35+
path: spec.template.spec.securityContext.runAsUser
36+
value: 65534
37+
- equal:
38+
path: spec.template.spec.securityContext.seccompProfile.type
39+
value: RuntimeDefault
40+
- equal:
41+
path: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
42+
value: false
43+
- contains:
44+
path: spec.template.spec.containers[0].securityContext.capabilities.drop
45+
content: ALL

‎packages/system/seaweedfs/templates/hook.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ spec:
2424
policy.cozystack.io/allow-to-apiserver: "true"
2525
spec:
2626
serviceAccountName: seaweedfs-hook
27+
securityContext:
28+
runAsNonRoot: true
29+
runAsUser: 65534
30+
seccompProfile:
31+
type: RuntimeDefault
2732
containers:
2833
- name: kubectl
2934
image: docker.io/alpine/k8s:1.36.2
35+
securityContext:
36+
allowPrivilegeEscalation: false
37+
capabilities:
38+
drop: ["ALL"]
3039
command:
3140
- sh
3241
args:
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
suite: seaweedfs update-hook runs non-root
2+
3+
# The update-hook Job runs kubectl from docker.io/alpine/k8s. kubectl needs no
4+
# root, so pin a restrictive non-root securityContext (mirroring the dashboard
5+
# adopt hook, uid 65534) and guard against it regressing back to running as root.
6+
release:
7+
name: seaweedfs
8+
namespace: cozy-seaweedfs
9+
tests:
10+
- it: update-hook Job runs under a restrictive non-root securityContext
11+
template: templates/hook.yaml
12+
documentSelector:
13+
path: kind
14+
value: Job
15+
asserts:
16+
- equal:
17+
path: spec.template.spec.securityContext.runAsNonRoot
18+
value: true
19+
- equal:
20+
path: spec.template.spec.securityContext.runAsUser
21+
value: 65534
22+
- equal:
23+
path: spec.template.spec.securityContext.seccompProfile.type
24+
value: RuntimeDefault
25+
- equal:
26+
path: spec.template.spec.containers[0].securityContext.allowPrivilegeEscalation
27+
value: false
28+
- contains:
29+
path: spec.template.spec.containers[0].securityContext.capabilities.drop
30+
content: ALL

0 commit comments

Comments
 (0)