Switch seccompProfile from container to Pod securityContext and add tests

This commit is contained in:
Ben Banfield-Zanin committed 2024-12-16 12:05:53 +00:00
1 parent 228b0065d8
commit 52241ef5e7
3 files changed
+120 -20

No files matched your search

@@ -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.
+16 -16
View File
@@ -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.
+100
View File
@@ -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"