Skip to content

Commit eae91cb

Browse files
committed
fix(platform): scope preflight pods/exec to the LINSTOR namespace
The hook bound a ClusterRole granting pods/exec cluster-wide (exec into any pod in any namespace). Keep only the read verbs (nodes/pods/ deployments get+list) cluster-wide so the LINSTOR check can always probe and cleanly detect a not-installed LINSTOR, and move the one privileged verb, pods/exec, into a namespaced Role in cozy-linstor bound by a RoleBinding. The Role/RoleBinding render only when the cozy-linstor namespace exists, so a cluster without LINSTOR neither receives the grant nor fails applying a Role into a missing namespace. Signed-off-by: IvanHunters <[email protected]>
1 parent 1b7747a commit eae91cb

2 files changed

Lines changed: 87 additions & 13 deletions

File tree

‎packages/core/platform/templates/preflight-hook.yaml‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ live `linstor` exec on every Flux HelmRelease reconcile. Being a pre-upgrade
1313
(not pre-install) hook, it also never runs on a fresh install, where there is
1414
no prior state to protect and no LINSTOR to query.
1515

16-
Unlike the migration hook this binds a ClusterRole limited to the verbs the
17-
checks need (read nodes/pods/deployments, create pods/exec) rather than
18-
cluster-admin: a read-only health probe has no reason to hold write access to
19-
the whole cluster. Note that pods/exec cannot be narrowed to a single pod in
20-
RBAC (pod names are generated), so the exec grant is necessarily cluster-wide;
21-
it is used only to exec into the linstor-controller.
16+
Unlike the migration hook (cluster-admin) this splits its access by least
17+
privilege: a ClusterRole with only read verbs (nodes/pods/deployments
18+
get+list), plus a namespaced Role in cozy-linstor for the single privileged
19+
verb pods/exec (running `linstor` in the controller). exec cannot be narrowed
20+
to one pod name, but scoping it to the one namespace the check targets keeps
21+
the blast radius contained; that Role renders only when cozy-linstor exists.
2222
*/ -}}
2323
{{- $pf := .Values.preflight | default dict }}
2424
{{- if dig "enabled" true $pf }}
@@ -94,18 +94,18 @@ metadata:
9494
helm.sh/hook-weight: "0"
9595
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
9696
rules:
97+
# Read-only, and deliberately cluster-wide. nodes are cluster-scoped anyway;
98+
# pods/deployments get+list stay here (not in the LINSTOR Role below) so the
99+
# LINSTOR check can always probe `get deploy -n cozy-linstor` and get a clean
100+
# NotFound -> N/A on a cluster that has no LINSTOR, without depending on the
101+
# cozy-linstor namespace existing. The only privileged verb (pods/exec) is
102+
# NOT here; it is scoped to the LINSTOR namespace below.
97103
- apiGroups: [""]
98104
resources: ["nodes"]
99105
verbs: ["get", "list"]
100106
- apiGroups: [""]
101107
resources: ["pods"]
102108
verbs: ["get", "list"]
103-
# Needed to run `linstor` inside the live controller pod. RBAC cannot scope
104-
# exec to a single pod (pod names are generated), so this is cluster-wide
105-
# create on pods/exec; the checks only ever exec into linstor-controller.
106-
- apiGroups: [""]
107-
resources: ["pods/exec"]
108-
verbs: ["create"]
109109
- apiGroups: ["apps"]
110110
resources: ["deployments"]
111111
verbs: ["get", "list"]
@@ -126,6 +126,47 @@ subjects:
126126
- kind: ServiceAccount
127127
name: cozystack-preflight-hook
128128
namespace: {{ .Release.Namespace | quote }}
129+
{{- if lookup "v1" "Namespace" "" "cozy-linstor" }}
130+
---
131+
# pods/exec (running `linstor` inside the controller) is the one privileged
132+
# grant, so it is a namespaced Role in cozy-linstor rather than cluster-wide:
133+
# exec cannot be scoped to a single pod name, but limiting it to the one
134+
# namespace the check ever targets keeps the blast radius contained. Rendered
135+
# only when the cozy-linstor namespace exists, so a cluster without LINSTOR
136+
# neither gets this grant nor fails applying a Role into a missing namespace
137+
# (there the check N/A-skips after the NotFound probe above).
138+
apiVersion: rbac.authorization.k8s.io/v1
139+
kind: Role
140+
metadata:
141+
name: cozystack-preflight-hook
142+
namespace: cozy-linstor
143+
annotations:
144+
helm.sh/hook: pre-upgrade
145+
helm.sh/hook-weight: "0"
146+
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
147+
rules:
148+
- apiGroups: [""]
149+
resources: ["pods/exec"]
150+
verbs: ["create"]
151+
---
152+
apiVersion: rbac.authorization.k8s.io/v1
153+
kind: RoleBinding
154+
metadata:
155+
name: cozystack-preflight-hook
156+
namespace: cozy-linstor
157+
annotations:
158+
helm.sh/hook: pre-upgrade
159+
helm.sh/hook-weight: "0"
160+
helm.sh/hook-delete-policy: hook-succeeded,before-hook-creation
161+
roleRef:
162+
apiGroup: rbac.authorization.k8s.io
163+
kind: Role
164+
name: cozystack-preflight-hook
165+
subjects:
166+
- kind: ServiceAccount
167+
name: cozystack-preflight-hook
168+
namespace: {{ .Release.Namespace | quote }}
169+
{{- end }}
129170
---
130171
apiVersion: v1
131172
kind: ServiceAccount

‎packages/core/platform/tests/preflight_hook_test.yaml‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ kubernetesProvider:
1515
version: "v1"
1616
resource: "configmaps"
1717
namespaced: true
18+
"v1/Namespace":
19+
gvr:
20+
version: "v1"
21+
resource: "namespaces"
22+
namespaced: false
1823
objects:
1924
- kind: ConfigMap
2025
apiVersion: v1
@@ -23,6 +28,12 @@ kubernetesProvider:
2328
namespace: cozy-system
2429
data:
2530
version: "52"
31+
# Present so the namespaced pods/exec Role (gated on this namespace's
32+
# existence) renders and can be asserted.
33+
- kind: Namespace
34+
apiVersion: v1
35+
metadata:
36+
name: cozy-linstor
2637
tests:
2738
- it: renders the preflight Job as a pre-upgrade hook at weight 0 (before migrations at weight 1)
2839
documentSelector:
@@ -130,12 +141,19 @@ tests:
130141
path: spec.template.spec.containers[0].env[1].value
131142
value: linstor,nodes-ready
132143

133-
- it: binds a scoped ClusterRole (nodes/pods read + pods/exec), not cluster-admin
144+
- it: keeps only read verbs in the ClusterRole (no pods/exec, no cluster-admin)
134145
documentSelector:
135146
path: kind
136147
value: ClusterRole
137148
asserts:
138149
- contains:
150+
path: rules
151+
content:
152+
apiGroups: [""]
153+
resources: ["nodes"]
154+
verbs: ["get", "list"]
155+
# The privileged verb must NOT be cluster-wide.
156+
- notContains:
139157
path: rules
140158
content:
141159
apiGroups: [""]
@@ -148,6 +166,21 @@ tests:
148166
resources: ["*"]
149167
verbs: ["*"]
150168

169+
- it: scopes pods/exec to a namespaced Role in cozy-linstor
170+
documentSelector:
171+
path: kind
172+
value: Role
173+
asserts:
174+
- equal:
175+
path: metadata.namespace
176+
value: cozy-linstor
177+
- contains:
178+
path: rules
179+
content:
180+
apiGroups: [""]
181+
resources: ["pods/exec"]
182+
verbs: ["create"]
183+
151184
- it: emits nothing when preflight is disabled
152185
set:
153186
preflight:

0 commit comments

Comments
 (0)