From b9526ebfd26c164cf657a3ab9e87509ef694b9fe Mon Sep 17 00:00:00 2001 From: Andrei Kvapil Date: Thu, 12 Nov 2026 10:25:24 -0500 Subject: [PATCH] fix(linstor): grant blockstor its Secrets by name only blockstor reads the passphrase Secret past controller-runtime's cache, keeps the master key in its own keyring Secret and unlocks it from piraeus's linstor-passphrase. Its own RBAC grants the satellite, and the controller and apiserver, access only to those Secrets and the generated unlock Secret. cozy-linstor is shared with piraeus, so unnamed or cluster-wide grants would let any blockstor pod read piraeus's TLS keys; the satellite runs privileged on every node, which makes that one compromised node away. The apiserver reads and writes Secrets through a namespaced Role only. The controller creates the keyring itself, so it gets its own namespaced Role, and the namespace it looks the Secrets up in, which otherwise defaults to blockstor-system. create cannot be limited by name and is granted on its own. The passphrase Secret's name is a value so a cluster that names another Secret keeps the Roles in step. The named rules also allow list and watch: newer blockstor releases read these Secrets from one watch per Secret selected by metadata.name, and without the verbs fall back to a GET per read. A list or watch restricted by resourceNames is authorized only when its fieldSelector names one of them, so the rule still exposes nothing else in the namespace. Assisted-by: LLM Signed-off-by: Andrei Kvapil --- .../linstor/templates/_blockstor-secrets.tpl | 14 +++++ .../templates/blockstor-apiserver.yaml | 33 +++++----- .../templates/blockstor-controller.yaml | 46 ++++++++++++++ .../templates/blockstor-satellite.yaml | 13 ++-- .../linstor/tests/blockstor_rbac_test.yaml | 55 +++++++++++------ .../tests/blockstor_workloads_test.yaml | 61 ++++++++++++++++++- packages/system/linstor/values.yaml | 7 +++ 7 files changed, 191 insertions(+), 38 deletions(-) create mode 100644 packages/system/linstor/templates/_blockstor-secrets.tpl diff --git a/packages/system/linstor/templates/_blockstor-secrets.tpl b/packages/system/linstor/templates/_blockstor-secrets.tpl new file mode 100644 index 000000000..4cebc2b32 --- /dev/null +++ b/packages/system/linstor/templates/_blockstor-secrets.tpl @@ -0,0 +1,14 @@ +{{- /* +The only Secrets blockstor touches, as a YAML flow list: its keyring, the +unlock Secret it generates when no passphrase Secret is named, and the +passphrase Secret named by blockstorPassphraseSecret. Every Role that grants +blockstor Secret access names exactly these, because cozy-linstor is shared +with piraeus and its TLS keys must stay unreadable. +*/ -}} +{{- define "blockstor.secretNames" -}} +{{- $name := .Values.blockstorPassphraseSecret | toString }} +{{- if not (regexMatch "^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$" $name) }} +{{- fail (printf "linstor: blockstorPassphraseSecret must be a Secret name, got %q" $name) }} +{{- end }} +{{- list "blockstor-master-key" "blockstor-unlock" $name | uniq | toJson }} +{{- end }} diff --git a/packages/system/linstor/templates/blockstor-apiserver.yaml b/packages/system/linstor/templates/blockstor-apiserver.yaml index 781f0cd5b..ae873432f 100644 --- a/packages/system/linstor/templates/blockstor-apiserver.yaml +++ b/packages/system/linstor/templates/blockstor-apiserver.yaml @@ -38,16 +38,6 @@ rules: - storagepools - storagepools/status verbs: [get, list, watch, create, update, patch, delete] - {{- /* - Read-only, and cluster-wide only because the apiserver reads the - passphrase Secret through controller-runtime's default cached client, - whose informer lists and watches Secrets at cluster scope. Narrowing - this needs the binary to scope its Secret cache to its own namespace; - until then a namespaced Role here would leave that informer forbidden - and every passphrase read hanging. */}} - - apiGroups: [""] - resources: [secrets] - verbs: [get, list, watch] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding @@ -63,10 +53,19 @@ subjects: namespace: {{ .Release.Namespace }} --- {{- /* - Writes to Secrets stay in this namespace: the only Secret the apiserver - writes is the encryption passphrase, `encryption create-passphrase` and - `modify-passphrase`, and it always lives here. A cluster-wide write - grant would let this ServiceAccount rewrite any tenant's credentials. */}} + The encryption endpoints read and rewrite the keyring (add-, modify-, + remove-passphrase), create it and the generated unlock Secret when no + passphrase Secret is named, and read the passphrase Secret to unlock. + All of it is in this namespace and none of it goes through + controller-runtime's cache, which would watch every Secret: the verbs + read by name, and the encryption state of the resource view comes from + one watch per Secret, selected by metadata.name. get, list, watch and + update name the three Secrets, because this namespace is shared with + piraeus, whose TLS keys stay unreadable; a list or watch is allowed only + when it selects one of them by name. create cannot be limited by name, + so it is granted on its own; the ValidatingAdmissionPolicy in + blockstor-secrets-policy.yaml holds it to the two Secrets blockstor + creates. */}} apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: @@ -75,7 +74,11 @@ metadata: rules: - apiGroups: [""] resources: [secrets] - verbs: [create, update, patch] + resourceNames: {{ include "blockstor.secretNames" . }} + verbs: [get, list, watch, update] + - apiGroups: [""] + resources: [secrets] + verbs: [create] --- apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding diff --git a/packages/system/linstor/templates/blockstor-controller.yaml b/packages/system/linstor/templates/blockstor-controller.yaml index faca527ef..6c77ee912 100644 --- a/packages/system/linstor/templates/blockstor-controller.yaml +++ b/packages/system/linstor/templates/blockstor-controller.yaml @@ -68,6 +68,45 @@ subjects: name: blockstor-controller namespace: {{ .Release.Namespace }} --- +{{- /* + On its first pass the controller creates the keyring from the passphrase + Secret, and later drops superseded slots from it, so it reads both and + writes the keyring, by name or through one watch per Secret, never + through the manager's cache. get, list, watch and update name the three + Secrets blockstor uses, because this namespace is shared with piraeus, + whose TLS keys stay unreadable; a list or watch is allowed only when it + selects one of them by name. create cannot be limited by name, so it is + granted on its own; the ValidatingAdmissionPolicy in + blockstor-secrets-policy.yaml holds it to the two Secrets blockstor + creates. */}} +apiVersion: rbac.authorization.k8s.io/v1 +kind: Role +metadata: + name: blockstor-controller-secrets + namespace: {{ .Release.Namespace }} +rules: + - apiGroups: [""] + resources: [secrets] + resourceNames: {{ include "blockstor.secretNames" . }} + verbs: [get, list, watch, update] + - apiGroups: [""] + resources: [secrets] + verbs: [create] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: blockstor-controller-secrets + namespace: {{ .Release.Namespace }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: blockstor-controller-secrets +subjects: + - kind: ServiceAccount + name: blockstor-controller + namespace: {{ .Release.Namespace }} +--- apiVersion: apps/v1 kind: Deployment metadata: @@ -104,6 +143,13 @@ spec: - --health-probe-bind-address=:8081 - --metrics-bind-address=0 - --leader-elect=false + {{- /* + The keyring and the passphrase Secret are looked up in the + controller's namespace, which otherwise defaults to + blockstor-system. */}} + env: + - name: POD_NAMESPACE + valueFrom: {fieldRef: {fieldPath: metadata.namespace}} ports: - {name: health, containerPort: 8081} readinessProbe: diff --git a/packages/system/linstor/templates/blockstor-satellite.yaml b/packages/system/linstor/templates/blockstor-satellite.yaml index 8edd86ba6..de1a522e2 100644 --- a/packages/system/linstor/templates/blockstor-satellite.yaml +++ b/packages/system/linstor/templates/blockstor-satellite.yaml @@ -109,11 +109,13 @@ subjects: namespace: {{ .Release.Namespace }} --- {{- /* - The satellite reads one Secret, the LUKS passphrase, in its own - namespace, with a direct GET rather than through a cache, so a - namespaced get is all it needs. A privileged pod on every node holding - a token that reads every Secret in the cluster would hand any node - compromise every tenant's credentials. */}} + The satellite unlocks the master key itself, from the passphrase Secret + and the keyring, to unwrap the password of each LUKS volume it opens. It + reads them with a direct GET rather than through a cache, so a get by + name is all it needs. Named, because this namespace is shared with + piraeus: a privileged pod on every node holding a token that reads + piraeus's TLS keys, or every Secret in the cluster, would turn any node + compromise into far more than this node's volumes. */}} apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: @@ -122,6 +124,7 @@ metadata: rules: - apiGroups: [""] resources: [secrets] + resourceNames: {{ include "blockstor.secretNames" . }} verbs: [get] --- apiVersion: rbac.authorization.k8s.io/v1 diff --git a/packages/system/linstor/tests/blockstor_rbac_test.yaml b/packages/system/linstor/tests/blockstor_rbac_test.yaml index 52e2fbc0d..5af37586b 100644 --- a/packages/system/linstor/tests/blockstor_rbac_test.yaml +++ b/packages/system/linstor/tests/blockstor_rbac_test.yaml @@ -1,6 +1,7 @@ -# The apiserver writes exactly one Secret, the encryption passphrase, and -# always in its own namespace. A cluster-wide write grant would reach every -# tenant's admin kubeconfig and every managed-service credential. +# The apiserver reads and writes the keyring and the generated unlock Secret +# and reads the passphrase Secret, always in its own namespace, which it +# shares with piraeus. A cluster-wide grant would reach every tenant's admin +# kubeconfig; an unnamed one in the namespace would reach piraeus's TLS keys. suite: blockstor apiserver Secret access templates: - templates/blockstor-apiserver.yaml @@ -10,25 +11,23 @@ release: set: backend: blockstor tests: - - it: grants Secrets only read access cluster-wide + - it: grants no Secret access cluster-wide documentSelector: path: kind value: ClusterRole asserts: - - contains: - path: rules - content: - apiGroups: [""] - resources: [secrets] - verbs: [get, list, watch] - notContains: path: rules content: apiGroups: [""] resources: [secrets] - verbs: [get, list, watch, create, update, patch] + verbs: [get, list, watch] + any: true + - lengthEqual: + path: rules + count: 1 - - it: grants Secret writes in the release namespace only + - it: grants Secret reads and writes in the release namespace by name only documentSelector: path: kind value: Role @@ -36,12 +35,34 @@ tests: - equal: path: metadata.namespace value: cozy-linstor - - contains: + - equal: path: rules - content: - apiGroups: [""] - resources: [secrets] - verbs: [create, update, patch] + value: + - apiGroups: [""] + resources: [secrets] + resourceNames: [blockstor-master-key, blockstor-unlock, linstor-passphrase] + verbs: [get, list, watch, update] + - apiGroups: [""] + resources: [secrets] + verbs: [create] + + - it: names the passphrase Secret the chart is told to use + set: + blockstorPassphraseSecret: my-passphrase + documentSelector: + path: kind + value: Role + asserts: + - equal: + path: rules[0].resourceNames + value: [blockstor-master-key, blockstor-unlock, my-passphrase] + + - it: refuses a passphrase Secret name that is not a Secret name + set: + blockstorPassphraseSecret: "" + asserts: + - failedTemplate: + errorPattern: blockstorPassphraseSecret must be a Secret name - it: binds the namespaced Role to the apiserver ServiceAccount documentSelector: diff --git a/packages/system/linstor/tests/blockstor_workloads_test.yaml b/packages/system/linstor/tests/blockstor_workloads_test.yaml index 1818838a6..98ed86e40 100644 --- a/packages/system/linstor/tests/blockstor_workloads_test.yaml +++ b/packages/system/linstor/tests/blockstor_workloads_test.yaml @@ -77,7 +77,7 @@ tests: resources: [leases] verbs: [get, list, watch, create, update, patch, delete] - - it: lets the satellite read Secrets in its own namespace only + - it: lets the satellite read only blockstor's Secrets in its own namespace template: templates/blockstor-satellite.yaml documentSelector: path: kind @@ -91,8 +91,67 @@ tests: value: - apiGroups: [""] resources: [secrets] + resourceNames: [blockstor-master-key, blockstor-unlock, linstor-passphrase] verbs: [get] + - it: gives the controller no Secret access at cluster scope + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: ClusterRole + asserts: + - notContains: + path: rules + content: + apiGroups: [""] + resources: [secrets] + any: true + + - it: lets the controller read and write only blockstor's Secrets in its own namespace + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: Role + asserts: + - equal: + path: metadata.namespace + value: cozy-linstor + - equal: + path: rules + value: + - apiGroups: [""] + resources: [secrets] + resourceNames: [blockstor-master-key, blockstor-unlock, linstor-passphrase] + verbs: [get, list, watch, update] + - apiGroups: [""] + resources: [secrets] + verbs: [create] + + - it: binds the controller's Secret Role to its ServiceAccount + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: RoleBinding + asserts: + - equal: + path: roleRef.name + value: blockstor-controller-secrets + - equal: + path: subjects[0].name + value: blockstor-controller + + - it: tells the controller its namespace, where the keyring lives + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: Deployment + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: POD_NAMESPACE + valueFrom: {fieldRef: {fieldPath: metadata.namespace}} + - it: binds the satellite's Secret Role to its ServiceAccount template: templates/blockstor-satellite.yaml documentSelector: diff --git a/packages/system/linstor/values.yaml b/packages/system/linstor/values.yaml index d004abce5..9c3c424c6 100644 --- a/packages/system/linstor/values.yaml +++ b/packages/system/linstor/values.yaml @@ -36,6 +36,13 @@ blockstorSatellite: image: repository: ghcr.io/cozystack/blockstor-satellite tag: 0.3.0@sha256:abff381da5425fdf7a6abc0d52006727cf7fe13c438acc79aaea192fc8681cb4 +# The Secret, in this namespace, whose MASTER_PASSPHRASE key unlocks the +# blockstor master key that wraps every LUKS volume password. It is the +# Secret piraeus reads for LINSTOR, so a cluster migrated from LINSTOR keeps +# its passphrase. blockstor only reads it; the operator creates it. The +# chart renders it into ControllerConfig.spec.passphraseSecretRef and into +# the Secret names the blockstor Roles grant. +blockstorPassphraseSecret: linstor-passphrase # Talos-specific workarounds (disable for generic Linux like Ubuntu/Debian) talos: enabled: true