diff --git a/templates/_helpers.tpl b/templates/_helpers.tpl index 2e443441f..23baf8882 100644 --- a/templates/_helpers.tpl +++ b/templates/_helpers.tpl @@ -470,6 +470,31 @@ Sets extra injector service annotations {{- end }} {{- end -}} +{{/* +securityContext for the injector pod level. +*/}} +{{- define "injector.securityContext.pod" -}} + {{- if or (.Values.injector.uid) (.Values.injector.gid) }} + securityContext: + runAsNonRoot: true + runAsGroup: {{ .Values.injector.gid | default 1000 }} + runAsUser: {{ .Values.injector.uid | default 100 }} + {{- else if .Values.injector.securityContext.pod }} + securityContext: + {{- toYaml .Values.injector.securityContext.pod | nindent 8 }} + {{- end }} +{{- end -}} + +{{/* +securityContext for the injector container level. +*/}} +{{- define "injector.securityContext.container" -}} + {{- if .Values.injector.securityContext.container}} + securityContext: + {{- toYaml .Values.injector.securityContext.container | nindent 12 }} + {{- end }} +{{- end -}} + {{/* Sets extra injector service account annotations */}} diff --git a/templates/injector-deployment.yaml b/templates/injector-deployment.yaml index d65525b8b..ccc5b922e 100644 --- a/templates/injector-deployment.yaml +++ b/templates/injector-deployment.yaml @@ -40,10 +40,7 @@ spec: serviceAccountName: "{{ template "vault.fullname" . }}-agent-injector" {{- if not .Values.global.openshift }} hostNetwork: {{ .Values.injector.hostNetwork }} - securityContext: - runAsNonRoot: true - runAsGroup: {{ .Values.injector.gid | default 1000 }} - runAsUser: {{ .Values.injector.uid | default 100 }} + {{ template "injector.securityContext.pod" . -}} {{- end }} containers: - name: sidecar-injector @@ -51,8 +48,7 @@ spec: image: "{{ .Values.injector.image.repository }}:{{ .Values.injector.image.tag }}" imagePullPolicy: "{{ .Values.injector.image.pullPolicy }}" {{- if not .Values.global.openshift }} - securityContext: - allowPrivilegeEscalation: false + {{ template "injector.securityContext.container" . -}} {{- end }} env: - name: AGENT_INJECT_LISTEN diff --git a/test/unit/injector-deployment.bats b/test/unit/injector-deployment.bats index fc276eeab..477f78366 100755 --- a/test/unit/injector-deployment.bats +++ b/test/unit/injector-deployment.bats @@ -363,6 +363,122 @@ load _helpers [ "${value}" = "false" ] } +#-------------------------------------------------------------------- +# securityContext or pod and container + +# for backward compatibility +@test "injector/deployment: backward pod securityContext" { + cd `chart_dir` + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.uid=200' \ + --set 'injector.gid=4000' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext' | tee /dev/stderr) + + local value=$(echo $actual | yq -r .runAsUser | tee /dev/stderr) + [ "${value}" = "200" ] + + local value=$(echo $actual | yq -r .runAsGroup | tee /dev/stderr) + [ "${value}" = "4000" ] +} + +@test "injector/deployment: default pod securityContext" { + cd `chart_dir` + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext' | tee /dev/stderr) + [ "${actual}" != "null" ] + + local value=$(echo $actual | yq -r .fsGroup | tee /dev/stderr) + [ "${value}" = "1000" ] + + local value=$(echo $actual | yq -r .runAsGroup | tee /dev/stderr) + [ "${value}" = "1000" ] + + local value=$(echo $actual | yq -r .runAsNonRoot | tee /dev/stderr) + [ "${value}" = "true" ] + + local value=$(echo $actual | yq -r .runAsUser | tee /dev/stderr) + [ "${value}" = "100" ] +} + +@test "injector/deployment: custom pod securityContext" { + cd `chart_dir` + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.pod.runAsNonRoot=true' \ + --set 'injector.securityContext.pod.runAsGroup=1001' \ + --set 'injector.securityContext.pod.runAsUser=1001' \ + --set 'injector.securityContext.pod.fsGroup=1000' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext.runAsGroup' | tee /dev/stderr) + [ "${actual}" = "1001" ] + + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.pod.runAsNonRoot=false' \ + --set 'injector.securityContext.pod.runAsGroup=1000' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext.runAsNonRoot' | tee /dev/stderr) + [ "${actual}" = "false" ] + + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.pod.runAsUser=1001' \ + --set 'injector.securityContext.pod.fsGroup=1000' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext.runAsUser' | tee /dev/stderr) + [ "${actual}" = "1001" ] + + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.pod.runAsNonRoot=true' \ + --set 'injector.securityContext.pod.fsGroup=1001' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.securityContext.fsGroup' | tee /dev/stderr) + [ "${actual}" = "1001" ] +} + +@test "injector/deployment: default container securityContext sidecar-injector" { + cd `chart_dir` + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext' | tee /dev/stderr) + [ "${actual}" != "null" ] + + local value=$(echo $actual | yq -r .allowPrivilegeEscalation | tee /dev/stderr) + [ "${value}" = "false" ] + + local value=$(echo $actual | yq -r .capabilities.drop[0] | tee /dev/stderr) + [ "${value}" = "ALL" ] +} + +@test "injector/deployment: custom container securityContext sidecar-injector" { + cd `chart_dir` + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.container.privileged=true' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.privileged' | tee /dev/stderr) + [ "${actual}" = "true" ] + + local actual=$(helm template \ + --show-only templates/injector-deployment.yaml \ + --set 'injector.enabled=true' \ + --set 'injector.securityContext.container.readOnlyRootFilesystem=false' \ + . | tee /dev/stderr | + yq -r '.spec.template.spec.containers[0].securityContext.readOnlyRootFilesystem' | tee /dev/stderr) + [ "${actual}" = "false" ] +} + #-------------------------------------------------------------------- # extraEnvironmentVars diff --git a/values.yaml b/values.yaml index 468d64161..8cccdddb9 100644 --- a/values.yaml +++ b/values.yaml @@ -202,6 +202,19 @@ injector: certName: tls.crt keyName: tls.key + # Default pod and container security context for vault-injector + securityContext: + pod: + runAsNonRoot: true + runAsGroup: 1000 + runAsUser: 100 + fsGroup: 1000 + container: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + resources: {} # resources: # requests: