From 52241ef5e7e95e71f9d88f58e4a00a80768cbe39 Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 16 Dec 2024 10:48:17 +0000 Subject: [PATCH] Switch seccompProfile from container to Pod securityContext and add tests --- .../source/common/sub_schema_values.yaml.j2 | 8 +- charts/matrix-stack/values.yaml | 32 +++--- tests/manifests/test_pod_securityContext.py | 100 ++++++++++++++++++ 3 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 tests/manifests/test_pod_securityContext.py diff --git a/charts/matrix-stack/source/common/sub_schema_values.yaml.j2 b/charts/matrix-stack/source/common/sub_schema_values.yaml.j2 index 9111757c..c58e2377 100644 --- a/charts/matrix-stack/source/common/sub_schema_values.yaml.j2 +++ b/charts/matrix-stack/source/common/sub_schema_values.yaml.j2 @@ -49,8 +49,8 @@ imagePullSecrets: [] ## To set the Seccomp profile for a Container, include the seccompProfile field in the securityContext section of your Pod or Container manifest. ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. Valid options for type include RuntimeDefault, Unconfined, and Localhost. ## localhostProfile must only be set set if type Localhost. It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - seccompProfile: - type: RuntimeDefault + # seccompProfile: + # type: RuntimeDefault {%- endmacro %} {% macro credential(credential_description, key) %} @@ -222,9 +222,9 @@ labels: {} ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. ## Valid options for type include RuntimeDefault, Unconfined, and Localhost. localhostProfile must only be set set if type Localhost. ## It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - # seccompProfile: + seccompProfile: # localhostProfile: - # type: + type: RuntimeDefault ## A list of groups applied to the first process run in each container, in addition to the container's primary GID. ## If unspecified, no groups will be added to any container. diff --git a/charts/matrix-stack/values.yaml b/charts/matrix-stack/values.yaml index 9603865c..2a112481 100644 --- a/charts/matrix-stack/values.yaml +++ b/charts/matrix-stack/values.yaml @@ -146,8 +146,8 @@ elementWeb: ## To set the Seccomp profile for a Container, include the seccompProfile field in the securityContext section of your Pod or Container manifest. ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. Valid options for type include RuntimeDefault, Unconfined, and Localhost. ## localhostProfile must only be set set if type Localhost. It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - seccompProfile: - type: RuntimeDefault + # seccompProfile: + # type: RuntimeDefault ## NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ # nodeSelector: {} ## A subset of PodSecurityContext. PodSecurityContext holds pod-level security attributes and common container settings @@ -193,9 +193,9 @@ elementWeb: ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. ## Valid options for type include RuntimeDefault, Unconfined, and Localhost. localhostProfile must only be set set if type Localhost. ## It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - # seccompProfile: + seccompProfile: # localhostProfile: - # type: + type: RuntimeDefault ## A list of groups applied to the first process run in each container, in addition to the container's primary GID. ## If unspecified, no groups will be added to any container. @@ -587,8 +587,8 @@ synapse: ## To set the Seccomp profile for a Container, include the seccompProfile field in the securityContext section of your Pod or Container manifest. ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. Valid options for type include RuntimeDefault, Unconfined, and Localhost. ## localhostProfile must only be set set if type Localhost. It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - seccompProfile: - type: RuntimeDefault + # seccompProfile: + # type: RuntimeDefault ## Defines additional environment variables to be injected onto this workload ## e.g. ## extraEnv: @@ -652,9 +652,9 @@ synapse: ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. ## Valid options for type include RuntimeDefault, Unconfined, and Localhost. localhostProfile must only be set set if type Localhost. ## It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - # seccompProfile: + seccompProfile: # localhostProfile: - # type: + type: RuntimeDefault ## A list of groups applied to the first process run in each container, in addition to the container's primary GID. ## If unspecified, no groups will be added to any container. @@ -766,8 +766,8 @@ synapse: ## To set the Seccomp profile for a Container, include the seccompProfile field in the securityContext section of your Pod or Container manifest. ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. Valid options for type include RuntimeDefault, Unconfined, and Localhost. ## localhostProfile must only be set set if type Localhost. It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - seccompProfile: - type: RuntimeDefault + # seccompProfile: + # type: RuntimeDefault ## NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ # nodeSelector: {} ## A subset of PodSecurityContext. PodSecurityContext holds pod-level security attributes and common container settings @@ -813,9 +813,9 @@ synapse: ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. ## Valid options for type include RuntimeDefault, Unconfined, and Localhost. localhostProfile must only be set set if type Localhost. ## It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - # seccompProfile: + seccompProfile: # localhostProfile: - # type: + type: RuntimeDefault ## A list of groups applied to the first process run in each container, in addition to the container's primary GID. ## If unspecified, no groups will be added to any container. @@ -926,8 +926,8 @@ synapse: ## To set the Seccomp profile for a Container, include the seccompProfile field in the securityContext section of your Pod or Container manifest. ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. Valid options for type include RuntimeDefault, Unconfined, and Localhost. ## localhostProfile must only be set set if type Localhost. It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - seccompProfile: - type: RuntimeDefault + # seccompProfile: + # type: RuntimeDefault ## NodeSelector is a selector which must be true for the pod to fit on a node. Selector which must match a node's labels for the pod to be scheduled on that node. More info: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/ # nodeSelector: {} ## A subset of PodSecurityContext. PodSecurityContext holds pod-level security attributes and common container settings @@ -973,9 +973,9 @@ synapse: ## The seccompProfile field is a SeccompProfile object consisting of type and localhostProfile. ## Valid options for type include RuntimeDefault, Unconfined, and Localhost. localhostProfile must only be set set if type Localhost. ## It indicates the path of the pre-configured profile on the node, relative to the kubelet's configured Seccomp profile location (configured with the --root-dir flag). - # seccompProfile: + seccompProfile: # localhostProfile: - # type: + type: RuntimeDefault ## A list of groups applied to the first process run in each container, in addition to the container's primary GID. ## If unspecified, no groups will be added to any container. diff --git a/tests/manifests/test_pod_securityContext.py b/tests/manifests/test_pod_securityContext.py new file mode 100644 index 00000000..94a88935 --- /dev/null +++ b/tests/manifests/test_pod_securityContext.py @@ -0,0 +1,100 @@ +# Copyright 2024 New Vector Ltd +# +# SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial + +import pytest + +from . import component_details, values_files_to_test + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_sets_nonRoot_uids_gids_in_pod_securityContext_by_default(templates): + for template in templates: + if template["kind"] in ["Deployment", "StatefulSet"]: + id = f"{template["kind"]}/{template["metadata"]["name"]}" + + assert ( + "securityContext" in template["spec"]["template"]["spec"] + ), f"Pod securityContext unexpectedly absent for {id}" + + pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] + + for idKey in ["runAsUser", "runAsGroup", "fsGroup"]: + assert idKey in pod_securityContext, f"No {idKey} in {id}'s Pod securityContext" + assert pod_securityContext[idKey] > 1000, f"Low {idKey} in {id}'s Pod securityContext" + + assert "runAsNonRoot" in pod_securityContext, f"No runAsNonRoot in {id}'s Pod securityContext" + assert pod_securityContext["runAsNonRoot"], f"{id} is running as root" + + assert ( + pod_securityContext["runAsUser"] == pod_securityContext["runAsGroup"] + ), f"{id} has distinct uid and gid in the Pod securityContext" + assert ( + pod_securityContext["runAsGroup"] == pod_securityContext["fsGroup"] + ), f"{id} has distinct run and FS gids in the Pod securityContext" + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_can_nuke_pod_securityContext_ids(component, values, make_templates): + values[component].setdefault("podSecurityContext", {"runAsUser": None, "runAsGroup": None, "fsGroup": None}) + for sub_component in component_details[component]["sub_components"]: + values[component].setdefault(sub_component, {}).setdefault( + "podSecurityContext", {"runAsUser": None, "runAsGroup": None, "fsGroup": None} + ) + + for template in await make_templates(values): + if template["kind"] in ["Deployment", "StatefulSet"]: + id = f"{template["kind"]}/{template["metadata"]["name"]}" + + assert ( + "securityContext" in template["spec"]["template"]["spec"] + ), f"Pod securityContext unexpectedly absent for {id}" + + pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] + + for idKey in ["runAsUser", "runAsGroup", "fsGroup"]: + assert idKey not in pod_securityContext, f"{idKey} set in {id}'s Pod securityContext" + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_sets_seccompProfile_in_pod_securityContext_by_default(templates): + for template in templates: + if template["kind"] in ["Deployment", "StatefulSet"]: + id = f"{template["kind"]}/{template["metadata"]["name"]}" + + assert ( + "securityContext" in template["spec"]["template"]["spec"] + ), f"Pod securityContext unexpectedly absent for {id}" + + pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] + + assert "seccompProfile" in pod_securityContext, f"No seccompProfile in {id}'s Pod securityContext" + assert ( + "type" in pod_securityContext["seccompProfile"] + ), f"No type in {id}'s Pod securityContext.seccompProfile" + assert ( + pod_securityContext["seccompProfile"]["type"] == "RuntimeDefault" + ), f"{id} has unexpected seccompProfile type" + + +@pytest.mark.parametrize("values_file", values_files_to_test) +@pytest.mark.asyncio_cooperative +async def test_can_nuke_pod_securityContext_seccompProfile(component, values, make_templates): + values[component].setdefault("podSecurityContext", {"seccompProfile": None}) + for sub_component in component_details[component]["sub_components"]: + values[component].setdefault(sub_component, {}).setdefault("podSecurityContext", {"seccompProfile": None}) + + for template in await make_templates(values): + if template["kind"] in ["Deployment", "StatefulSet"]: + id = f"{template["kind"]}/{template["metadata"]["name"]}" + + assert ( + "securityContext" in template["spec"]["template"]["spec"] + ), f"Pod securityContext unexpectedly absent for {id}" + + pod_securityContext = template["spec"]["template"]["spec"]["securityContext"] + + assert "seccompProfile" not in pod_securityContext, f"seccompProfile set in {id}'s Pod securityContext"