Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/adding container security context #750

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0da4947
feat: adding container security context not available in podSecurityC…
ChrisFraun Jun 22, 2022
7936a0f
refactor: collecting securityContexts as parameter in vaules
ChrisFraun Jun 22, 2022
512e37a
refactor: change values location
ChrisFraun Jun 22, 2022
eb1922d
Revert "refactor: change values location"
ChrisFraun Jun 24, 2022
377e933
Revert "refactor: collecting securityContexts as parameter in vaules"
ChrisFraun Jun 24, 2022
ea18bdb
Revert "feat: adding container security context not available in podS…
ChrisFraun Jun 24, 2022
e5991a0
revert: change values for backwards compatibility
ChrisFraun Jun 24, 2022
4a31076
refactor: use helper to add securityContext pod and container level
ChrisFraun Jul 25, 2022
6cc97b3
fix: typo of nindent
ChrisFraun Jul 25, 2022
30d7d0e
Merge branch 'main' into feat/adding-container-security-context
ChrisFraun Jul 25, 2022
7bac1ee
Update values.yaml
ChrisFraun Jul 26, 2022
3897b3d
Update values.yaml
ChrisFraun Jul 26, 2022
c1e4db2
Update templates/injector-deployment.yaml
ChrisFraun Jul 26, 2022
0cb74b1
Update templates/_helpers.tpl
ChrisFraun Jul 26, 2022
d220baa
Update templates/_helpers.tpl
ChrisFraun Jul 26, 2022
496b254
Update templates/injector-deployment.yaml
ChrisFraun Jul 26, 2022
4f82d42
Update templates/injector-deployment.yaml
ChrisFraun Jul 26, 2022
f8a5c01
test: adding unit test for securityContext
ChrisFraun Jul 26, 2022
95e27fe
fix: changing unit test parameters
ChrisFraun Jul 26, 2022
95736a8
Update templates/_helpers.tpl
ChrisFraun Jul 27, 2022
d7eb423
Update test/unit/injector-deployment.bats
ChrisFraun Jul 27, 2022
7faeee8
Update test/unit/injector-deployment.bats
ChrisFraun Jul 27, 2022
250beef
feat: changing unit tests and adding backward compatibility test
ChrisFraun Jul 27, 2022
bbd5927
fix: unit tests
ChrisFraun Jul 28, 2022
2a8e442
fix: unit test
ChrisFraun Jul 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/}}
Expand Down
8 changes: 2 additions & 6 deletions templates/injector-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ 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
{{ template "injector.resources" . }}
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
Expand Down
116 changes: 116 additions & 0 deletions test/unit/injector-deployment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down