diff --git a/packages/system/linstor/templates/blockstor-secrets-policy.yaml b/packages/system/linstor/templates/blockstor-secrets-policy.yaml new file mode 100644 index 000000000..d1726aa90 --- /dev/null +++ b/packages/system/linstor/templates/blockstor-secrets-policy.yaml @@ -0,0 +1,61 @@ +{{- if and (eq .Values.backend "blockstor") (.Capabilities.APIVersions.Has "admissionregistration.k8s.io/v1/ValidatingAdmissionPolicy") }} +{{- $sa := printf "system:serviceaccount:%s:" .Release.Namespace }} +--- +{{- /* + RBAC cannot limit create by name, and the blockstor controller and + apiserver need it for the keyring and the generated unlock Secret. A + compromised pod could otherwise create a Secret under a name its + name-limited get already covers but that does not exist yet, such as + linstor-passphrase on a cluster that never set one, typed + kubernetes.io/service-account-token for one of piraeus's ServiceAccounts + in this namespace, and read the token back. This admits a Secret create + by a blockstor ServiceAccount only for the two Secrets blockstor creates, + each with the type blockstor gives it; the passphrase Secret is input + that blockstor never writes. + + It matches nothing but CREATE of Secrets in this namespace by these + ServiceAccounts, so piraeus, cert-manager and everyone else are outside + it, as are updates, which RBAC already limits by name. The satellite + holds no create; it is listed so a later grant does not reopen the gap. */}} +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicy +metadata: + name: blockstor-secret-create +spec: + failurePolicy: Fail + matchConstraints: + namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: {{ .Release.Namespace }} + resourceRules: + - apiGroups: [""] + apiVersions: [v1] + operations: [CREATE] + resources: [secrets] + matchConditions: + - name: blockstor-service-account + expression: >- + request.userInfo.username in [ + '{{ $sa }}blockstor-apiserver', + '{{ $sa }}blockstor-controller', + '{{ $sa }}blockstor-satellite' + ] + validations: + - expression: >- + (object.metadata.name == 'blockstor-master-key' && + has(object.type) && object.type == 'blockstor.org/master-key') || + (object.metadata.name == 'blockstor-unlock' && + (!has(object.type) || object.type == 'Opaque')) + message: >- + blockstor may create only the Secrets blockstor-master-key (type + blockstor.org/master-key) and blockstor-unlock (type Opaque) + reason: Forbidden +--- +apiVersion: admissionregistration.k8s.io/v1 +kind: ValidatingAdmissionPolicyBinding +metadata: + name: blockstor-secret-create +spec: + policyName: blockstor-secret-create + validationActions: [Deny] +{{- end }} diff --git a/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml b/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml new file mode 100644 index 000000000..2b77ec989 --- /dev/null +++ b/packages/system/linstor/tests/blockstor_secrets_policy_test.yaml @@ -0,0 +1,90 @@ +# RBAC cannot limit a Secret create by name, so this policy is what keeps a +# compromised blockstor pod from minting a token Secret for one of piraeus's +# ServiceAccounts under a name its get already covers. It must reach nothing +# but blockstor's own creates in its own namespace. +suite: blockstor Secret create policy +templates: + - templates/blockstor-secrets-policy.yaml +release: + name: linstor + namespace: cozy-linstor +# helm unittest's default API versions do not include it, and the template +# renders only where the cluster serves it. +capabilities: + apiVersions: + - admissionregistration.k8s.io/v1/ValidatingAdmissionPolicy +set: + backend: blockstor +tests: + - it: renders nothing under the LINSTOR backend + set: + backend: linstor + asserts: + - hasDocuments: + count: 0 + + - it: matches only Secret creates in the release namespace + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - equal: + path: spec.matchConstraints + value: + namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: cozy-linstor + resourceRules: + - apiGroups: [""] + apiVersions: [v1] + operations: [CREATE] + resources: [secrets] + - equal: + path: spec.failurePolicy + value: Fail + + - it: applies only to the blockstor ServiceAccounts of the release namespace + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - lengthEqual: + path: spec.matchConditions + count: 1 + - equal: + path: spec.matchConditions[0].expression + value: >- + request.userInfo.username in [ + 'system:serviceaccount:cozy-linstor:blockstor-apiserver', + 'system:serviceaccount:cozy-linstor:blockstor-controller', + 'system:serviceaccount:cozy-linstor:blockstor-satellite' + ] + + - it: admits only the keyring and the unlock Secret, each with its type + documentSelector: + path: kind + value: ValidatingAdmissionPolicy + asserts: + - lengthEqual: + path: spec.validations + count: 1 + - matchRegex: + path: spec.validations[0].expression + pattern: "object.metadata.name == 'blockstor-master-key' &&\\s+has\\(object.type\\) && object.type == 'blockstor.org/master-key'" + - matchRegex: + path: spec.validations[0].expression + pattern: "object.metadata.name == 'blockstor-unlock' &&\\s+\\(!has\\(object.type\\) \\|\\| object.type == 'Opaque'\\)" + - notMatchRegex: + path: spec.validations[0].expression + pattern: linstor-passphrase + + - it: denies through a binding without its own selector + documentSelector: + path: kind + value: ValidatingAdmissionPolicyBinding + asserts: + - equal: + path: spec + value: + policyName: blockstor-secret-create + validationActions: [Deny]