From 3b31772ebc91cfa94edee96c4a6dcd6dd1086149 Mon Sep 17 00:00:00 2001 From: Todor Tsankov Date: Thu, 19 Nov 2026 12:56:33 +0200 Subject: [PATCH] Fix minion PKI preparation and security defaults Prepare raw PEM Secret keys in a writable PKI volume as the minion user. Honor explicit key-generation settings, retain configurable workload images, and give keygen independent restricted security contexts. Consolidate behavioral Helm tests and document the supported key sources. Validation: 28 Helm unit tests, Helm lint, and diff checks passed. --- helm/salt-minion/README.md | 38 +++- helm/salt-minion/templates/_helpers.tpl | 63 ++++++ helm/salt-minion/templates/deployment.yaml | 47 ++--- helm/salt-minion/templates/keygen-job.yaml | 9 +- .../templates/minion-keygen-rb.yaml | 2 +- .../templates/minion-keygen-role.yaml | 2 +- helm/salt-minion/templates/minion-secret.yaml | 9 +- helm/salt-minion/tests/deployment_test.yaml | 97 +++++++++ helm/salt-minion/tests/images_test.yaml | 39 ++-- helm/salt-minion/tests/keygen_test.yaml | 189 ++++++++++++++++++ .../salt-minion/tests/minion_secret_test.yaml | 33 +++ helm/salt-minion/values.yaml | 70 +++---- src/minion/vcf/Dockerfile | 12 +- 13 files changed, 499 insertions(+), 111 deletions(-) create mode 100644 helm/salt-minion/tests/deployment_test.yaml create mode 100644 helm/salt-minion/tests/keygen_test.yaml create mode 100644 helm/salt-minion/tests/minion_secret_test.yaml diff --git a/helm/salt-minion/README.md b/helm/salt-minion/README.md index 0771e8f..e3e0924 100644 --- a/helm/salt-minion/README.md +++ b/helm/salt-minion/README.md @@ -42,6 +42,7 @@ Selected via `agent.authMode`: - Kubernetes 1.24+ - Helm 3+ - Salt Master reachable from minion (required for in_cluster mode) +- For in-cluster mode, provide `agent.minion.keySecretName` or both `agent.minion.privateKeyB64` and `agent.minion.publicKeyB64`, or explicitly enable `agent.minion.allowSelfGeneratedKey`. - Persistent storage (optional, for PKI persistence) ## Quick Start @@ -51,6 +52,7 @@ Selected via `agent.authMode`: ```bash # Install with basic configuration helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --namespace salt \ --create-namespace \ --set agent.saltMasterHost=salt-master.example.com @@ -73,6 +75,7 @@ kubectl create token salt-minion -n salt ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --namespace salt \ --create-namespace \ --set agent.saltMasterHost=salt-master.example.com @@ -84,6 +87,7 @@ For production, enable PKI persistence to maintain minion identity across restar ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --namespace salt \ --create-namespace \ --set agent.saltMasterHost=salt-master.example.com \ @@ -97,6 +101,7 @@ Enable Vault for secure secret sourcing: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --namespace salt \ --create-namespace \ --set agent.saltMasterHost=salt-master.example.com \ @@ -110,6 +115,7 @@ Connect to multiple masters simultaneously: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --namespace salt \ --create-namespace \ --set 'agent.saltMasterHost={master-0.example.com,master-1.example.com,master-2.example.com}' @@ -184,10 +190,18 @@ See [values.yaml](values.yaml) for the complete list of configuration options. | --- | --- | --- | | `namespace` | Kubernetes namespace for deployment | `salt` | | `agent.authMode` | `in_cluster` or `external` | `in_cluster` | +| `agent.minion.keySecretName` | Key Secret name. Uses an existing Secret unless `privateKeyB64` and `publicKeyB64` are supplied to create it. Empty uses `-keys` | `""` | +| `agent.minion.privateKeyB64` | Base64-encoded private PEM key; requires `publicKeyB64` | `""` | +| `agent.minion.publicKeyB64` | Base64-encoded public PEM key; requires `privateKeyB64` | `""` | +| `agent.minion.allowSelfGeneratedKey` | Allow the keygen Job in in-cluster mode when `keySecretName`, `privateKeyB64`, and `publicKeyB64` are empty | `false` | +| `agent.podSecurityContext` | Pod security context shared by the minion and key-preparation init container | See [values.yaml](values.yaml) | +| `agent.containerSecurityContext` | Container security context applied to both the minion and key-preparation init container | See [values.yaml](values.yaml) | | `agent.image.repository` | Minion image repository | `ghcr.io/saltstack/salt-kubernetes/salt-minion` | | `agent.image.tag` | Image tag/version | `0.1.0` | | `agent.image.pullPolicy` | Image pull policy | `IfNotPresent` | | `agent.image.imagePullSecrets` | Minion pull Secrets | `[]` | +| `keygen.podSecurityContext` | Pod security context for the key-generation Job | See [values.yaml](values.yaml) | +| `keygen.containerSecurityContext` | Container security context for key generation | See [values.yaml](values.yaml) | | `keygen.image.repository` | Keygen image repository | `bitnami/kubectl` | | `keygen.image.tag` | Keygen image tag | `latest` | | `keygen.image.pullPolicy` | Keygen image pull policy | `Always` | @@ -214,6 +228,7 @@ Requires a StorageClass: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --set agent.persistence.enabled=true \ --set agent.persistence.type=pvc \ --set agent.persistence.size=1Gi @@ -223,6 +238,7 @@ Or use an existing PVC: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --set agent.persistence.enabled=true \ --set agent.persistence.pvc.existingClaim=my-pvc ``` @@ -233,6 +249,7 @@ For clusters without dynamic provisioning: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --set agent.persistence.enabled=true \ --set agent.persistence.type=hostPath \ --set agent.persistence.hostPath.path=/var/lib/salt-minion/pki \ @@ -261,19 +278,23 @@ credentials: ### Key Management +Provide the minion keypair using one of these options: + +- Set `agent.minion.keySecretName` to an existing Secret containing `minion.pem` (private key) and `minion.pub` (public key), both as PEM text. +- Set `agent.minion.privateKeyB64` and `agent.minion.publicKeyB64` together to have the chart create a Secret from base64-encoded PEM values. +- Set `agent.minion.allowSelfGeneratedKey=true` to generate a keypair when none is supplied. + +The Salt master must trust the minion's public key. After changing the keypair in the Secret, recreate the minion Pod to apply it. + #### Pre-Seeded Keys For production with pre-generated keys: ```bash -# Encode keys -PRIVATE_KEY=$(base64 -w0 < /path/to/minion.pem) -PUBLIC_KEY=$(base64 -w0 < /path/to/minion.pub) - # Create secret kubectl create secret generic my-minion-keys \ - --from-literal=minion.pem="$PRIVATE_KEY" \ - --from-literal=minion.pub="$PUBLIC_KEY" \ + --from-file=minion.pem=/path/to/minion.pem \ + --from-file=minion.pub=/path/to/minion.pub \ -n salt # Use in Helm @@ -287,7 +308,7 @@ The chart's pre-install Job automatically generates keys: ```bash helm install salt-minion ./helm/salt-minion \ - --set agent.minion.keySecretName="" # Let job create it + --set agent.minion.allowSelfGeneratedKey=true ``` ### Health Checks @@ -312,6 +333,7 @@ For Salt's native multi-master setup: ```bash helm install salt-minion ./helm/salt-minion \ + --set agent.minion.allowSelfGeneratedKey=true \ --set 'agent.saltMasterHost=master-0.example.com,master-1.example.com,master-2.example.com' ``` @@ -340,7 +362,7 @@ The comma-separated list is converted to a YAML list in minion configuration. The chart applies comprehensive security controls: -- Runs as non-root user (UID 10000) +- Runs as a non-root user by default - Disables privilege escalation - Drops all Linux capabilities - Private keys stored with mode 0400 diff --git a/helm/salt-minion/templates/_helpers.tpl b/helm/salt-minion/templates/_helpers.tpl index f73ad70..ad6afd5 100644 --- a/helm/salt-minion/templates/_helpers.tpl +++ b/helm/salt-minion/templates/_helpers.tpl @@ -1,3 +1,23 @@ +{{/* Validation */}} +{{- define "salt-minion.validateKeyPair" -}} +{{- $minion := .Values.agent.minion -}} +{{- if ne (empty $minion.privateKeyB64) (empty $minion.publicKeyB64) -}} +{{- fail "agent.minion.privateKeyB64 and agent.minion.publicKeyB64 must be set together" -}} +{{- end -}} +{{- end -}} + +{{- define "salt-minion.validateKeySource" -}} +{{- $minion := .Values.agent.minion -}} +{{- if not (or $minion.keySecretName $minion.privateKeyB64 $minion.allowSelfGeneratedKey) -}} +{{- fail "set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey" -}} +{{- end -}} +{{- end -}} + +{{/* Values */}} +{{- define "salt-minion.keySecretName" -}} +{{- .Values.agent.minion.keySecretName | default (printf "%s-keys" .Release.Name) -}} +{{- end -}} + {{/* Format an image repository and tag. */}} {{- define "salt-minion.image" -}} {{- printf "%s:%s" .repository (toString .tag) -}} @@ -12,3 +32,46 @@ {{- define "salt-minion.keygenImage" -}} {{- include "salt-minion.image" .Values.keygen.image -}} {{- end -}} + +{{- define "salt-minion.masterHost" -}} +{{- if kindIs "slice" .Values.agent.saltMasterHost -}} +{{- join "," .Values.agent.saltMasterHost -}} +{{- else -}} +{{- .Values.agent.saltMasterHost -}} +{{- end -}} +{{- end -}} + +{{/* Flags */}} +{{- define "salt-minion.keySecretEnabled" -}} +{{- if .Values.agent.minion.privateKeyB64 -}} +true +{{- end -}} +{{- end -}} + +{{- define "salt-minion.keygenEnabled" -}} +{{- $minion := .Values.agent.minion -}} +{{- if and (eq .Values.agent.authMode "in_cluster") $minion.allowSelfGeneratedKey (not $minion.keySecretName) (not $minion.privateKeyB64) -}} +true +{{- end -}} +{{- end -}} + +{{- define "salt-minion.keygenJobEnabled" -}} +{{- if include "salt-minion.keygenEnabled" . -}} +{{- $secretName := include "salt-minion.keySecretName" . -}} +{{- if not (lookup "v1" "Secret" .Values.namespace $secretName) -}} +true +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "salt-minion.keygenRbacEnabled" -}} +{{- if and .Values.rbac.create (include "salt-minion.keygenEnabled" .) -}} +true +{{- end -}} +{{- end -}} + +{{- define "salt-minion.deploymentEnabled" -}} +{{- if eq .Values.agent.authMode "in_cluster" -}} +true +{{- end -}} +{{- end -}} diff --git a/helm/salt-minion/templates/deployment.yaml b/helm/salt-minion/templates/deployment.yaml index 63e2e00..2b9240e 100644 --- a/helm/salt-minion/templates/deployment.yaml +++ b/helm/salt-minion/templates/deployment.yaml @@ -1,5 +1,6 @@ -{{- if eq .Values.agent.authMode "in_cluster" }} -{{- $keySecret := .Values.agent.minion.keySecretName | default (printf "%s-keys" .Release.Name) }} +{{- if include "salt-minion.deploymentEnabled" . }} +{{- include "salt-minion.validateKeySource" . -}} +{{- $keySecret := include "salt-minion.keySecretName" . }} apiVersion: apps/v1 kind: Deployment metadata: @@ -45,34 +46,30 @@ spec: - | set -eu - cp /var/run/salt-minion-keys/minion.pem \ - /etc/salt/pki/minion/minion.pem + umask 077 + test -s /var/run/salt-minion-keys/minion.pem + test -s /var/run/salt-minion-keys/minion.pub + mkdir -p /pki/minion + chmod 0700 /pki/minion + rm -f /pki/minion/minion.pem /pki/minion/minion.pub + cp /var/run/salt-minion-keys/minion.pem \ + /pki/minion/minion.pem cp /var/run/salt-minion-keys/minion.pub \ - /etc/salt/pki/minion/minion.pub + /pki/minion/minion.pub - chown -R 10001:10001 /etc/salt/pki/minion - chmod 0700 /etc/salt/pki/minion - chmod 0400 /etc/salt/pki/minion/minion.pem - chmod 0644 /etc/salt/pki/minion/minion.pub + test -s /pki/minion/minion.pem + test -s /pki/minion/minion.pub + chmod 0400 /pki/minion/minion.pem + chmod 0644 /pki/minion/minion.pub volumeMounts: - name: minion-keys-source mountPath: /var/run/salt-minion-keys readOnly: true - name: minion-pki - mountPath: /etc/salt/pki/minion + mountPath: /pki securityContext: - runAsUser: 0 - runAsGroup: 0 - runAsNonRoot: false - allowPrivilegeEscalation: false - capabilities: - drop: - - ALL - add: - - CHOWN - - FOWNER - - DAC_OVERRIDE + {{- toYaml .Values.agent.containerSecurityContext | nindent 12 }} containers: - name: salt-minion @@ -80,11 +77,7 @@ spec: imagePullPolicy: {{ .Values.agent.image.pullPolicy }} env: - name: SALT_MASTER - {{- if kindIs "slice" .Values.agent.saltMasterHost }} - value: {{ join "," .Values.agent.saltMasterHost | quote }} - {{- else }} - value: {{ .Values.agent.saltMasterHost | quote }} - {{- end }} + value: {{ include "salt-minion.masterHost" . | quote }} - name: SALT_MASTER_PORT value: {{ .Values.agent.saltMasterPort | quote }} @@ -147,6 +140,7 @@ spec: volumeMounts: - name: minion-pki mountPath: /etc/salt/pki/minion + subPath: minion livenessProbe: exec: @@ -177,6 +171,7 @@ spec: - name: minion-keys-source secret: secretName: {{ $keySecret }} + defaultMode: 0444 - name: minion-pki emptyDir: {} diff --git a/helm/salt-minion/templates/keygen-job.yaml b/helm/salt-minion/templates/keygen-job.yaml index 277924e..01a77c1 100644 --- a/helm/salt-minion/templates/keygen-job.yaml +++ b/helm/salt-minion/templates/keygen-job.yaml @@ -1,6 +1,5 @@ -{{- $secretName := .Values.agent.minion.keySecretName | default (printf "%s-keys" .Release.Name) -}} -{{- $existingSecret := lookup "v1" "Secret" .Values.namespace $secretName -}} -{{- if and (not .Values.agent.minion.keySecretName) (not $existingSecret) }} +{{- if include "salt-minion.keygenJobEnabled" . }} +{{- $secretName := include "salt-minion.keySecretName" . }} apiVersion: batch/v1 kind: Job metadata: @@ -11,6 +10,8 @@ spec: template: spec: serviceAccountName: {{ .Values.serviceAccount.name }} + securityContext: + {{- toYaml .Values.keygen.podSecurityContext | nindent 8 }} {{- with .Values.keygen.image.imagePullSecrets }} imagePullSecrets: {{- toYaml . | nindent 8 }} @@ -21,6 +22,8 @@ spec: - name: keygen image: {{ include "salt-minion.keygenImage" . | quote }} imagePullPolicy: {{ .Values.keygen.image.pullPolicy }} + securityContext: + {{- toYaml .Values.keygen.containerSecurityContext | nindent 12 }} command: ["/bin/sh", "-c"] args: - | diff --git a/helm/salt-minion/templates/minion-keygen-rb.yaml b/helm/salt-minion/templates/minion-keygen-rb.yaml index 71027fa..74480c5 100644 --- a/helm/salt-minion/templates/minion-keygen-rb.yaml +++ b/helm/salt-minion/templates/minion-keygen-rb.yaml @@ -1,4 +1,4 @@ -{{- if and .Values.rbac.create (not .Values.agent.minion.keySecretName) }} +{{- if include "salt-minion.keygenRbacEnabled" . }} apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: diff --git a/helm/salt-minion/templates/minion-keygen-role.yaml b/helm/salt-minion/templates/minion-keygen-role.yaml index e7be094..ae0a6b5 100644 --- a/helm/salt-minion/templates/minion-keygen-role.yaml +++ b/helm/salt-minion/templates/minion-keygen-role.yaml @@ -1,4 +1,4 @@ -{{- if and .Values.rbac.create (not .Values.agent.minion.keySecretName) }} +{{- if include "salt-minion.keygenRbacEnabled" . }} apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: diff --git a/helm/salt-minion/templates/minion-secret.yaml b/helm/salt-minion/templates/minion-secret.yaml index b11a18a..b5cc525 100644 --- a/helm/salt-minion/templates/minion-secret.yaml +++ b/helm/salt-minion/templates/minion-secret.yaml @@ -1,5 +1,6 @@ -{{- $secretName := .Values.agent.minion.keySecretName | default (printf "%s-keys" .Release.Name) -}} -{{- if and .Values.agent.minion.privateKeyB64 .Values.agent.minion.publicKeyB64 }} +{{- include "salt-minion.validateKeyPair" . -}} +{{- if include "salt-minion.keySecretEnabled" . }} +{{- $secretName := include "salt-minion.keySecretName" . }} apiVersion: v1 kind: Secret metadata: @@ -11,6 +12,6 @@ metadata: helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version }} type: Opaque data: - private-key-b64: {{ .Values.agent.minion.privateKeyB64 | b64enc }} - public-key-b64: {{ .Values.agent.minion.publicKeyB64 | b64enc }} + minion.pem: {{ .Values.agent.minion.privateKeyB64 | quote }} + minion.pub: {{ .Values.agent.minion.publicKeyB64 | quote }} {{- end }} \ No newline at end of file diff --git a/helm/salt-minion/tests/deployment_test.yaml b/helm/salt-minion/tests/deployment_test.yaml new file mode 100644 index 0000000..ae61885 --- /dev/null +++ b/helm/salt-minion/tests/deployment_test.yaml @@ -0,0 +1,97 @@ +suite: minion deployment +templates: + - templates/deployment.yaml +tests: + - it: runs the pod as non-root by default + set: + agent.minion.keySecretName: existing-keys + asserts: + - equal: + path: spec.template.spec.securityContext.runAsNonRoot + value: true + - equal: + path: spec.template.spec.securityContext.seccompProfile.type + value: RuntimeDefault + + - it: mounts the generated key Secret read-only for preparation + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName + value: RELEASE-NAME-keys + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.defaultMode + value: 292 # 0444: readable by the preparation user + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-keys-source")].mountPath + value: /var/run/salt-minion-keys + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-keys-source")].readOnly + value: true + + - it: keeps the source Secret out of the minion container + set: + agent.minion.keySecretName: existing-keys + asserts: + - notExists: + path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-keys-source")] + + - it: provides writable PKI storage for key preparation + set: + agent.minion.keySecretName: existing-keys + asserts: + - exists: + path: spec.template.spec.volumes[?(@.name == "minion-pki")] + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-pki")].mountPath + value: /pki + - notEqual: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].volumeMounts[?(@.name == "minion-pki")].readOnly + value: true + + - it: mounts the prepared PKI directory writable in the minion + set: + agent.minion.keySecretName: existing-keys + asserts: + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-pki")].mountPath + value: /etc/salt/pki/minion + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-pki")].subPath + value: minion + - notEqual: + path: spec.template.spec.containers[?(@.name == "salt-minion")].volumeMounts[?(@.name == "minion-pki")].readOnly + value: true + + - it: uses the configured UID for both key preparation and the minion + set: + agent.minion.keySecretName: existing-keys + agent.containerSecurityContext.runAsUser: 4000 + asserts: + - equal: + path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].securityContext.runAsUser + value: 4000 + - equal: + path: spec.template.spec.containers[?(@.name == "salt-minion")].securityContext.runAsUser + value: 4000 + + - it: uses the requested existing Secret + set: + agent.minion.keySecretName: existing-keys + asserts: + - equal: + path: spec.template.spec.volumes[?(@.name == "minion-keys-source")].secret.secretName + value: existing-keys + + - it: renders external mode without requiring keys + set: + agent.authMode: external + asserts: + - hasDocuments: + count: 0 + + - it: requires a key source by default + asserts: + - failedTemplate: + errorMessage: set agent.minion.keySecretName or both agent.minion.privateKeyB64 and agent.minion.publicKeyB64, or enable agent.minion.allowSelfGeneratedKey diff --git a/helm/salt-minion/tests/images_test.yaml b/helm/salt-minion/tests/images_test.yaml index b9a34c1..dec575f 100644 --- a/helm/salt-minion/tests/images_test.yaml +++ b/helm/salt-minion/tests/images_test.yaml @@ -1,32 +1,25 @@ -suite: workload images +suite: container images templates: - templates/deployment.yaml - templates/keygen-job.yaml +set: + agent.minion.allowSelfGeneratedKey: true + agent.image.repository: test.example/minion + keygen.image.repository: test.example/kubectl tests: - - it: renders the default images - asserts: - - template: templates/deployment.yaml - equal: - path: spec.template.spec.containers[?(@.name == "salt-minion")].image - value: ghcr.io/saltstack/salt-kubernetes/salt-minion:0.1.0 - - template: templates/keygen-job.yaml - equal: - path: spec.template.spec.containers[?(@.name == "keygen")].image - value: bitnami/kubectl:latest - - it: preserves quoted tags with trailing zeros set: - agent.image.tag: '1.10' - keygen.image.tag: '1.10' + agent.image.tag: "1.10" + keygen.image.tag: "1.10" asserts: - template: templates/deployment.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image - value: ghcr.io/saltstack/salt-kubernetes/salt-minion:1.10 + value: test.example/minion:1.10 - template: templates/keygen-job.yaml equal: path: spec.template.spec.containers[?(@.name == "keygen")].image - value: bitnami/kubectl:1.10 + value: test.example/kubectl:1.10 - it: renders numeric tags set: @@ -36,29 +29,31 @@ tests: - template: templates/deployment.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image - value: ghcr.io/saltstack/salt-kubernetes/salt-minion:123 + value: test.example/minion:123 - template: templates/keygen-job.yaml equal: path: spec.template.spec.containers[?(@.name == "keygen")].image - value: bitnami/kubectl:123 + value: test.example/kubectl:123 - - it: uses each configured repository + - it: uses each configured repository and tag set: agent.image.repository: mirror.example:5000/team/minion + agent.image.tag: agent-test keygen.image.repository: other.example/team/kubectl + keygen.image.tag: keygen-test asserts: - template: templates/deployment.yaml equal: path: spec.template.spec.containers[?(@.name == "salt-minion")].image - value: mirror.example:5000/team/minion:0.1.0 + value: mirror.example:5000/team/minion:agent-test - template: templates/deployment.yaml equal: path: spec.template.spec.initContainers[?(@.name == "prepare-minion-keys")].image - value: mirror.example:5000/team/minion:0.1.0 + value: mirror.example:5000/team/minion:agent-test - template: templates/keygen-job.yaml equal: path: spec.template.spec.containers[?(@.name == "keygen")].image - value: other.example/team/kubectl:latest + value: other.example/team/kubectl:keygen-test - it: uses separate pull secrets for each workload set: diff --git a/helm/salt-minion/tests/keygen_test.yaml b/helm/salt-minion/tests/keygen_test.yaml new file mode 100644 index 0000000..bfb8f2a --- /dev/null +++ b/helm/salt-minion/tests/keygen_test.yaml @@ -0,0 +1,189 @@ +suite: key generation +templates: + - templates/keygen-job.yaml + - templates/minion-keygen-role.yaml + - templates/minion-keygen-rb.yaml +release: + name: custom-release + namespace: custom-namespace +set: + namespace: custom-namespace + serviceAccount.name: custom-minion +tests: + - it: creates the Job when key generation is enabled + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - hasDocuments: + count: 1 + template: templates/keygen-job.yaml + - isKind: + of: Job + template: templates/keygen-job.yaml + - equal: + path: metadata.namespace + value: custom-namespace + template: templates/keygen-job.yaml + - equal: + path: spec.template.spec.serviceAccountName + value: custom-minion + template: templates/keygen-job.yaml + + - it: runs key generation with restricted security defaults + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - template: templates/keygen-job.yaml + equal: + path: spec.template.spec.securityContext.runAsNonRoot + value: true + - template: templates/keygen-job.yaml + equal: + path: spec.template.spec.securityContext.seccompProfile.type + value: RuntimeDefault + - template: templates/keygen-job.yaml + equal: + path: spec.template.spec.containers[?(@.name == "keygen")].securityContext.allowPrivilegeEscalation + value: false + - template: templates/keygen-job.yaml + equal: + path: spec.template.spec.containers[?(@.name == "keygen")].securityContext.capabilities.drop + value: [ALL] + + - it: creates the Role when key generation is enabled + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - hasDocuments: + count: 1 + template: templates/minion-keygen-role.yaml + - isKind: + of: Role + template: templates/minion-keygen-role.yaml + - equal: + path: metadata.namespace + value: custom-namespace + template: templates/minion-keygen-role.yaml + - equal: + path: metadata.name + value: custom-release-minion-keygen-role + template: templates/minion-keygen-role.yaml + - equal: + path: rules + value: + - apiGroups: + - "" + resources: + - secrets + verbs: + - create + - get + - list + template: templates/minion-keygen-role.yaml + + - it: creates the RoleBinding when key generation is enabled + set: + agent.minion.allowSelfGeneratedKey: true + asserts: + - hasDocuments: + count: 1 + template: templates/minion-keygen-rb.yaml + - isKind: + of: RoleBinding + template: templates/minion-keygen-rb.yaml + - equal: + path: metadata.namespace + value: custom-namespace + template: templates/minion-keygen-rb.yaml + - equal: + path: roleRef + value: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: custom-release-minion-keygen-role + template: templates/minion-keygen-rb.yaml + - equal: + path: subjects + value: + - kind: ServiceAccount + name: custom-minion + namespace: custom-namespace + template: templates/minion-keygen-rb.yaml + + - it: skips key generation resources when an existing Secret is supplied + set: + agent.minion.keySecretName: existing-keys + agent.minion.allowSelfGeneratedKey: true + asserts: + - template: templates/keygen-job.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-role.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-rb.yaml + hasDocuments: + count: 0 + + - it: skips key generation resources for keys supplied through Helm values + set: + agent.minion.privateKeyB64: cHJpdmF0ZQ== + agent.minion.publicKeyB64: cHVibGlj + agent.minion.allowSelfGeneratedKey: true + asserts: + - template: templates/keygen-job.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-role.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-rb.yaml + hasDocuments: + count: 0 + + - it: skips key generation resources in external mode + set: + agent.authMode: external + agent.minion.allowSelfGeneratedKey: true + asserts: + - template: templates/keygen-job.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-role.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-rb.yaml + hasDocuments: + count: 0 + + - it: skips key generation resources when generation is disabled + set: + agent.minion.allowSelfGeneratedKey: false + asserts: + - template: templates/keygen-job.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-role.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-rb.yaml + hasDocuments: + count: 0 + + - it: creates only the Job when RBAC is managed externally + set: + agent.minion.allowSelfGeneratedKey: true + rbac.create: false + asserts: + - template: templates/keygen-job.yaml + hasDocuments: + count: 1 + - template: templates/keygen-job.yaml + isKind: + of: Job + - template: templates/minion-keygen-role.yaml + hasDocuments: + count: 0 + - template: templates/minion-keygen-rb.yaml + hasDocuments: + count: 0 diff --git a/helm/salt-minion/tests/minion_secret_test.yaml b/helm/salt-minion/tests/minion_secret_test.yaml new file mode 100644 index 0000000..f127ef5 --- /dev/null +++ b/helm/salt-minion/tests/minion_secret_test.yaml @@ -0,0 +1,33 @@ +suite: minion secret +templates: + - templates/minion-secret.yaml +tests: + - it: stores base64 key values under the mounted PEM filenames + set: + agent.minion.privateKeyB64: cHJpdmF0ZQ== + agent.minion.publicKeyB64: cHVibGlj + asserts: + - equal: + path: data + value: + minion.pem: cHJpdmF0ZQ== + minion.pub: cHVibGlj + + - it: does not create a Secret without keys supplied through Helm values + asserts: + - hasDocuments: + count: 0 + + - it: rejects a private key without a public key + set: + agent.minion.privateKeyB64: cHJpdmF0ZQ== + asserts: + - failedTemplate: + errorMessage: agent.minion.privateKeyB64 and agent.minion.publicKeyB64 must be set together + + - it: rejects a public key without a private key + set: + agent.minion.publicKeyB64: cHVibGlj + asserts: + - failedTemplate: + errorMessage: agent.minion.privateKeyB64 and agent.minion.publicKeyB64 must be set together diff --git a/helm/salt-minion/values.yaml b/helm/salt-minion/values.yaml index 65b5863..b1ff933 100644 --- a/helm/salt-minion/values.yaml +++ b/helm/salt-minion/values.yaml @@ -8,39 +8,23 @@ agent: # external — create RBAC only; the minion runs outside the cluster authMode: in_cluster - # No numeric runAsUser/runAsGroup/fsGroup here on purpose - the image runs - # as the 'salt' system user salt-common's own postinst creates, whose - # numeric uid isn't a stable constant to pin. runAsNonRoot alone still - # gets enforced (Kubernetes checks whatever user the image resolves to). + # Identity inherited by the minion and key preparation container. + # PKI access uses UID ownership; no fsGroup is required. podSecurityContext: + runAsUser: 10001 + runAsGroup: 10001 runAsNonRoot: true + seccompProfile: + type: RuntimeDefault - # No added capabilities: when agent.persistence.enabled mounts a - # PVC/hostPath at /etc/salt/pki, it comes back root-owned regardless of - # fsGroup, and capabilities.add can't fix that either - not every - # container runtime populates the *ambient* capability set for a - # non-root container (confirmed on this project's own test cluster: - # CapBnd includes CHOWN/FOWNER, CapEff/CapAmb are still empty, so the - # capability is granted but unusable), and even setcap-based file - # capabilities need allowPrivilegeEscalation: true to take effect on - # exec. initContainers.fixPkiPerms below (runs as root, once, briefly) - # is the tradeoff instead: this long-running container never runs as - # root, ever. + # No added capabilities. The minion and key preparation init container + # use this context and run without privilege escalation. containerSecurityContext: allowPrivilegeEscalation: false - runAsNonRoot: true capabilities: drop: - ALL - # Only relevant when persistence.enabled mounts a volume at /etc/salt/pki. - # Reuses image.repository/tag itself (not e.g. busybox) so it can resolve - # 'salt' by name via that image's own /etc/passwd, rather than - # duplicating its numeric uid here. - initContainers: - fixPkiPerms: - enabled: true - # ── in_cluster mode settings ────────────────────────────────────────────── image: imagePullSecrets: [] @@ -108,25 +92,23 @@ agent: publicKeyB64: "" # Name of an EXISTING Kubernetes Secret (created out-of-band, never via - # Helm values) with keys 'private-key-b64' and 'public-key-b64' - a - # base64-encoded PEM RSA keypair for the minion itself. REQUIRED (the - # chart fails fast otherwise - see allowSelfGeneratedKey below for the - # escape hatch): the minion's public key must already be registered as + # Helm values) with raw 'minion.pem' and 'minion.pub' entries. When + # empty, the chart creates a Secret from privateKeyB64 and publicKeyB64 + # or, when explicitly enabled below, a keygen Job. + # For pre-registered trust, the minion's public key must already be # trusted (e.g. via salt-key-operator's SaltMinionKey - see # ../../salt-key-operator) *before* the minion starts, so it must be # handed that exact keypair rather than generating its own, which the - # trust store would never have been told about. With - # agent.persistence.enabled=false (the default), a self-generated key - # also wouldn't even survive a pod restart. + # trust store would never have been told about. keySecretName: "" - # Explicit opt-out of the keySecretName requirement above - falls back - # to the minion generating its own keypair on first start. Only makes - # sense combined with agent.persistence.enabled=true (otherwise a fresh - # keypair, and a fresh unaccepted minion, appears on every pod - # restart). Named explicitly rather than just leaving keySecretName - # empty, so choosing this legacy behavior is a deliberate, visible - # decision in your values file. + # Explicit opt-out of the keySecretName requirement above when + # privateKeyB64 and publicKeyB64 are not supplied - allows the keygen + # Job to create a keypair. + # The generated public key must be accepted by the Salt master. + # Named explicitly rather than just leaving keySecretName empty, so + # choosing this behavior is a deliberate, visible decision in your + # values file. allowSelfGeneratedKey: false # Optional Vault integration (saltext.vault, baked into this chart's @@ -173,6 +155,18 @@ agent: nodeSelector: {} keygen: + # Use the keygen image's default user, with restricted pod security settings. + podSecurityContext: + runAsNonRoot: true + seccompProfile: + type: RuntimeDefault + + containerSecurityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + image: imagePullSecrets: [] repository: bitnami/kubectl diff --git a/src/minion/vcf/Dockerfile b/src/minion/vcf/Dockerfile index b83acff..d0f9f68 100644 --- a/src/minion/vcf/Dockerfile +++ b/src/minion/vcf/Dockerfile @@ -69,7 +69,7 @@ RUN set -eu; \ rm -f /tmp/bootstrap-salt.sh /tmp/bootstrap-salt.sh.sha256 /tmp/bootstrap-salt.sh.sha256.check # Ensure non‑root salt user exists -RUN id salt >/dev/null 2>&1 || useradd --system --uid 999 --no-create-home --shell /usr/sbin/nologin salt +RUN id salt >/dev/null 2>&1 || useradd --system --uid 10001 --no-create-home --shell /usr/sbin/nologin salt # Copy the requirements file into the image COPY salt-extensions.txt /tmp/salt-extensions.txt @@ -137,13 +137,9 @@ ENV SALT_MASTER="" \ VAULT_TOKEN_FILE="" \ VAULT_SDB_PROFILE="vault_sdb" -# Numeric, not the name: Kubernetes' runAsNonRoot check can't resolve a -# named USER without executing code inside the image, and refuses to start -# the container at all ("cannot verify user is non-root") if it can't. 999 -# is what salt-common's own postinst allocates the 'salt' user/group in -# this build (verify with `id salt` if the base image or install order -# ever changes). -USER 999:999 +# Numeric, not the name: Kubernetes' runAsNonRoot check cannot resolve +# a named USER without executing code inside the image. +USER 10001:10001 VOLUME ["/etc/salt/pki/minion"]