Skip to content

Commit 9472bb3

Browse files
scooby87claude
andcommitted
fix(etcd): clamp cleanup PVC resourceNames so replicas:0 cannot widen RBAC
Address CodeRabbit review on #3170: the PVC cleanup Role generates its resourceNames from .Values.replicas. values.schema.json sets no minimum, so an explicit replicas: 0 rendered an empty resourceNames list — and an empty resourceNames in an RBAC rule matches every PVC in the namespace (a wildcard), silently undoing the least-privilege scoping the previous commit added. Clamp the count to max(1, replicas) via a shared $replicaCount used by both the Role resourceNames range and the delete command, so at least data-etcd-0 is always listed (it exists for any running cluster and the delete uses --ignore-not-found). Add a regression test asserting replicas: 0 still yields a name-scoped Role and a single-name delete rather than a namespace-wide grant. Assisted-By: Claude <[email protected]> Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Signed-off-by: Алексей Артамонов <[email protected]>
1 parent 4a06df9 commit 9472bb3

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎packages/extra/etcd/templates/hooks/cleanup.yaml‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
{{- /*
2+
Clamp the replica count to at least 1 so an explicit replicas: 0 cannot
3+
render an empty resourceNames list below — an empty resourceNames in an RBAC
4+
rule matches ALL PVCs in the namespace (a wildcard), which would silently
5+
undo the least-privilege scoping. data-etcd-0 always exists for any running
6+
cluster, and the delete uses --ignore-not-found, so the clamp is safe.
7+
*/}}
8+
{{- $replicaCount := max 1 (int .Values.replicas) | int }}
19
---
210
apiVersion: v1
311
kind: ServiceAccount
@@ -28,7 +36,7 @@ rules:
2836
resources: ["persistentvolumeclaims"]
2937
verbs: ["get", "delete"]
3038
resourceNames:
31-
{{- range $i := until (int .Values.replicas) }}
39+
{{- range $i := until $replicaCount }}
3240
- data-etcd-{{ $i }}
3341
{{- end }}
3442
# cert-manager TLS secrets carry only the cluster-wide
@@ -128,7 +136,7 @@ spec:
128136
# deletion, so we delete them here by name (safe: no sibling
129137
# workload shares the data-etcd-* prefix in a tenant namespace).
130138
echo "Deleting orphaned etcd data PVCs for {{ .Release.Name }}..."
131-
kubectl delete pvc -n {{ .Release.Namespace }} {{ range $i := until (int .Values.replicas) }}data-etcd-{{ $i }} {{ end }}--ignore-not-found --wait=false \
139+
kubectl delete pvc -n {{ .Release.Namespace }} {{ range $i := until $replicaCount }}data-etcd-{{ $i }} {{ end }}--ignore-not-found --wait=false \
132140
|| echo "WARNING: etcd PVC cleanup failed for {{ .Release.Name }}; orphaned PVCs may remain" >&2
133141
# etcd-ca-tls / etcd-client-tls are pre-install "keep" secrets
134142
# this chart owns; etcd-peer-ca-tls / etcd-server-tls /

‎packages/extra/etcd/tests/cleanup_test.yaml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,29 @@ tests:
183183
- matchRegex:
184184
path: spec.template.spec.containers[0].command[2]
185185
pattern: 'kubectl delete pvc -n tenant-root data-etcd-0 data-etcd-1 data-etcd-2 data-etcd-3 data-etcd-4 --ignore-not-found --wait=false'
186+
187+
# An explicit replicas: 0 must NOT render an empty resourceNames list — an
188+
# empty resourceNames in an RBAC rule matches every PVC in the namespace
189+
# (a wildcard), which would silently undo the least-privilege scoping. The
190+
# $replicaCount clamp keeps at least data-etcd-0 in both the Role and command.
191+
- it: clamps to one PVC name when replicas is 0 so RBAC never becomes a wildcard
192+
set:
193+
replicas: 0
194+
asserts:
195+
- contains:
196+
path: rules
197+
content:
198+
apiGroups: [""]
199+
resources: ["persistentvolumeclaims"]
200+
verbs: ["get", "delete"]
201+
resourceNames:
202+
- data-etcd-0
203+
documentSelector:
204+
path: kind
205+
value: Role
206+
- matchRegex:
207+
path: spec.template.spec.containers[0].command[2]
208+
pattern: 'kubectl delete pvc -n tenant-root data-etcd-0 --ignore-not-found --wait=false'
209+
documentSelector:
210+
path: kind
211+
value: Job

0 commit comments

Comments
 (0)