Skip to content

Commit

Permalink
Chart: Do not propagate global security context to statsd and redis (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Aakcht authored Jun 22, 2023
1 parent b99f1b1 commit c48f744
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
23 changes: 23 additions & 0 deletions chart/templates/_helpers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,29 @@ capabilities:
{{- end -}}
{{- end -}}

{{/*
Set the default value for external container securityContext(redis and statsd).
If no value is passed for <node>.securityContexts.container, defaults to deny privileges escallation and dropping all POSIX capabilities.

+-----------------------------------+ +-----------------------------------------------------------+
| <node>.securityContexts.container | -> | allowPrivilegesEscalation: false, capabilities.drop: [ALL]|
+-----------------------------------+ +-----------------------------------------------------------+


The template can be called like so:
include "externalContainerSecurityContext" .Values.statsd
*/}}
{{- define "externalContainerSecurityContext" -}}
{{- if .securityContexts.container -}}
{{ toYaml .securityContexts.container | print }}
{{- else -}}
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
{{- end -}}
{{- end -}}

{{- define "container_extra_envs" -}}
{{- $ := index . 0 -}}
{{- $env := index . 1 -}}
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/redis/redis-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{{- $tolerations := or .Values.redis.tolerations .Values.tolerations }}
{{- $topologySpreadConstraints := or .Values.redis.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $securityContext := include "localPodSecurityContext" .Values.redis }}
{{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.redis) }}
{{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.redis }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
Expand Down
2 changes: 1 addition & 1 deletion chart/templates/statsd/statsd-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{{- $topologySpreadConstraints := or .Values.statsd.topologySpreadConstraints .Values.topologySpreadConstraints }}
{{- $revisionHistoryLimit := or .Values.statsd.revisionHistoryLimit .Values.revisionHistoryLimit }}
{{- $securityContext := include "localPodSecurityContext" .Values.statsd }}
{{- $containerSecurityContext := include "containerSecurityContext" (list . .Values.statsd) }}
{{- $containerSecurityContext := include "externalContainerSecurityContext" .Values.statsd }}
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down
6 changes: 3 additions & 3 deletions chart/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4790,7 +4790,7 @@
"default": []
},
"securityContext": {
"description": "Security context for the StatsD pod (deprecated, use `securityContexts` instead). If not set, the values from `securityContext` will be used.",
"description": "Security context for the StatsD pod (deprecated, use `securityContexts` instead).",
"type": "object",
"$ref": "#/definitions/io.k8s.api.core.v1.PodSecurityContext",
"default": {},
Expand All @@ -4803,7 +4803,7 @@
]
},
"securityContexts": {
"description": "Security context definition for the statsd. If not set, the values from global `securityContexts` will be used.",
"description": "Security context definition for the statsd.",
"type": "object",
"x-docsSection": "Kubernetes",
"properties": {
Expand Down Expand Up @@ -5517,7 +5517,7 @@
]
},
"securityContexts": {
"description": "Security context definition for the redis. If not set, the values from global `securityContexts` will be used.",
"description": "Security context definition for the redis.",
"type": "object",
"x-docsSection": "Kubernetes",
"properties": {
Expand Down
1 change: 0 additions & 1 deletion chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,6 @@ statsd:
uid: 65534
# When not set, `statsd.uid` will be used

# When not set, the values defined in the global securityContext will be used
# (deprecated, use `securityContexts` instead)
securityContext: {}
# runAsUser: 65534
Expand Down
40 changes: 40 additions & 0 deletions tests/charts/security/test_security_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ def test_gitsync_sidecar_and_init_container(self):
docs[index],
)

# Test securityContexts for main containers
def test_global_security_context(self):
ctx_value_pod = {"runAsUser": 7000}
ctx_value_container = {"allowPrivilegeEscalation": False}
docs = render_chart(
values={"securityContexts": {"containers": ctx_value_container, "pod": ctx_value_pod}},
show_only=[
"templates/flower/flower-deployment.yaml",
"templates/scheduler/scheduler-deployment.yaml",
"templates/webserver/webserver-deployment.yaml",
"templates/workers/worker-deployment.yaml",
"templates/jobs/create-user-job.yaml",
"templates/jobs/migrate-database-job.yaml",
"templates/triggerer/triggerer-deployment.yaml",
"templates/statsd/statsd-deployment.yaml",
"templates/redis/redis-statefulset.yaml",
],
)

for index in range(len(docs) - 2):
assert ctx_value_container == jmespath.search(
"spec.template.spec.containers[0].securityContext", docs[index]
)
assert ctx_value_pod == jmespath.search("spec.template.spec.securityContext", docs[index])

# Global security context is not propagated to redis and statsd, so we test default value
default_ctx_value_container = {"allowPrivilegeEscalation": False, "capabilities": {"drop": ["ALL"]}}
default_ctx_value_pod_statsd = {"runAsUser": 65534}
default_ctx_value_pod_redis = {"runAsUser": 0}
for index in range(len(docs) - 2, len(docs)):
assert default_ctx_value_container == jmespath.search(
"spec.template.spec.containers[0].securityContext", docs[index]
)
assert default_ctx_value_pod_statsd == jmespath.search(
"spec.template.spec.securityContext", docs[len(docs) - 2]
)
assert default_ctx_value_pod_redis == jmespath.search(
"spec.template.spec.securityContext", docs[len(docs) - 1]
)

# Test securityContexts for main containers
def test_main_container_setting(self):
ctx_value = {"allowPrivilegeEscalation": False}
Expand Down

0 comments on commit c48f744

Please sign in to comment.