mirror of
https://github.com/element-hq/ess-helm.git
synced 2026-11-21 09:31:28 -08:00
Manifest Tests: synthesise Secrets that are generated in-cluster or externally provided
This commit is contained in:
1 parent
4f2e47c9c1
commit
de87796c8a
4 files changed
+82
-5
No files matched your search
@@ -0,0 +1 @@
|
||||
Support synthesising Secrets for external and generated Secrets.
|
||||
@@ -22,7 +22,7 @@ def get_configmap(templates, configmap_name):
|
||||
raise ValueError(f"ConfigMap {configmap_name} not found")
|
||||
|
||||
|
||||
def get_secret(templates, secret_name):
|
||||
def get_secret(templates, other_secrets, secret_name):
|
||||
"""
|
||||
Get the content of a Secret with the given name.
|
||||
:param secret_name: The name of the Secret to retrieve.
|
||||
@@ -31,6 +31,9 @@ def get_secret(templates, secret_name):
|
||||
for t in templates:
|
||||
if t["kind"] == "Secret" and t["metadata"]["name"] == secret_name:
|
||||
return t
|
||||
for s in other_secrets:
|
||||
if s["metadata"]["name"] == secret_name:
|
||||
return s
|
||||
raise ValueError(f"Secret {secret_name} not found")
|
||||
|
||||
|
||||
@@ -63,7 +66,7 @@ def match_in_content(container_name, mounted_secret_keys, mount_path, match_in):
|
||||
|
||||
@pytest.mark.parametrize("values_file", values_files_to_test)
|
||||
@pytest.mark.asyncio_cooperative
|
||||
async def test_secrets_consistency(templates):
|
||||
async def test_secrets_consistency(templates, other_secrets):
|
||||
"""
|
||||
Test to ensure that all secrets are properly mounted and consistent across the cluster.
|
||||
|
||||
@@ -88,7 +91,7 @@ async def test_secrets_consistency(templates):
|
||||
current_volume = get_volume_from_mount(template, volume_mount)
|
||||
if "secret" in current_volume:
|
||||
# Extract the paths where this volume's secrets are mounted
|
||||
secret = get_secret(templates, current_volume["secret"]["secretName"])
|
||||
secret = get_secret(templates, other_secrets, current_volume["secret"]["secretName"])
|
||||
if "subPath" in volume_mount:
|
||||
# When using subPath, the key is mounted as the mountPath itself
|
||||
mounted_secret_keys.append(f"{volume_mount['mountPath']}")
|
||||
|
||||
@@ -9,9 +9,11 @@ from . import values_files_to_test
|
||||
|
||||
@pytest.mark.parametrize("values_file", values_files_to_test)
|
||||
@pytest.mark.asyncio_cooperative
|
||||
async def test_volumes_mounts_exists(templates):
|
||||
async def test_volumes_mounts_exists(templates, other_secrets):
|
||||
configmaps_names = [t["metadata"]["name"] for t in templates if t["kind"] == "ConfigMap"]
|
||||
secrets_names = [t["metadata"]["name"] for t in templates if t["kind"] == "Secret"]
|
||||
secrets_names = [t["metadata"]["name"] for t in templates if t["kind"] == "Secret"] + [
|
||||
s["metadata"]["name"] for s in other_secrets
|
||||
]
|
||||
for template in templates:
|
||||
if template["kind"] in ["Deployment", "StatefulSet", "Job"]:
|
||||
volumes_names = []
|
||||
|
||||
@@ -46,6 +46,77 @@ async def templates(chart: pyhelm3.Chart, release_name: str, values: dict[str, A
|
||||
return list([template for template in await helm_template(chart, release_name, values) if template is not None])
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def other_secrets(release_name, values, templates):
|
||||
return list(generated_secrets(release_name, values, templates)) + list(external_secrets(values))
|
||||
|
||||
|
||||
def generated_secrets(release_name: str, values: Any | None, helm_generated_templates: list[Any]) -> Iterator[Any]:
|
||||
if values.get("initSecrets", {}).get("enabled", False):
|
||||
init_secrets_job = None
|
||||
for template in helm_generated_templates:
|
||||
if template["kind"] == "Job" and template["metadata"]["name"] == f"{release_name}-init-secrets":
|
||||
init_secrets_job = template
|
||||
assert init_secrets_job is not None, (
|
||||
"initSecrets.enabled=true but no init-secrets job found to generate Secret for"
|
||||
)
|
||||
|
||||
command_line = (
|
||||
init_secrets_job.get("spec", {})
|
||||
.get("template", {})
|
||||
.get("spec", {})
|
||||
.get("containers", [{}])[0]
|
||||
.get("command", {})
|
||||
)
|
||||
assert len(command_line) == 6, "Unexpected command line in the init-secrets job"
|
||||
assert command_line[2] == "-secrets", "Can't find the secrets args for the init-secrets job"
|
||||
assert command_line[4] == "-labels", "Can't find the labels args for the init-secrets job"
|
||||
|
||||
requested_secrets = command_line[3].split(",")
|
||||
requested_labels = {label.split("=")[0]: label.split("=")[1] for label in command_line[5].split(",")}
|
||||
generated_secrets_to_keys = {}
|
||||
for requested_secret in requested_secrets:
|
||||
secret_parts = requested_secret.split(":")
|
||||
generated_secrets_to_keys.setdefault(secret_parts[0], []).append(secret_parts[1])
|
||||
|
||||
for secret_name, secret_keys in generated_secrets_to_keys.items():
|
||||
yield {
|
||||
"kind": "Secret",
|
||||
"metadata": {"name": secret_name, "labels": requested_labels},
|
||||
"data": {
|
||||
secret_key: "".join(random.choices(string.ascii_lowercase, k=10)) for secret_key in secret_keys
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def external_secrets(values):
|
||||
def find_credential(values_fragment):
|
||||
for value in values_fragment.values():
|
||||
if isinstance(value, dict):
|
||||
if "secret" in value and "secretKey" in value and len(value) == 2:
|
||||
yield (value["secret"], value["secretKey"])
|
||||
# We don't care about credentials in the Helm values as those will
|
||||
# be added to the Secret generated by the chart and won't be external
|
||||
else:
|
||||
yield from find_credential(value)
|
||||
elif isinstance(value, list):
|
||||
for inner in value:
|
||||
yield from find_credential(inner)
|
||||
|
||||
external_secrets_to_keys = {}
|
||||
for secret_name, secretKey in find_credential(values):
|
||||
external_secrets_to_keys.setdefault(secret_name, []).append(secretKey)
|
||||
|
||||
for secret_name, secret_keys in external_secrets_to_keys.items():
|
||||
yield {
|
||||
"kind": "Secret",
|
||||
"metadata": {
|
||||
"name": secret_name,
|
||||
},
|
||||
"data": {secret_key: "".join(random.choices(string.ascii_lowercase, k=10)) for secret_key in secret_keys},
|
||||
}
|
||||
|
||||
|
||||
async def helm_template(chart: pyhelm3.Chart, release_name: str, values: Any | None) -> Iterator[Any]:
|
||||
"""Generate template with ServiceMonitor API Versions enabled
|
||||
|
||||
|
||||
Reference in new issue
Block a user