From 93d1e4bb8ff8ae392b05db8cf17fa8ce82252407 Mon Sep 17 00:00:00 2001 From: Nikos Terlemes Date: Tue, 27 Oct 2026 14:02:55 +0100 Subject: [PATCH] feat(gitops-values-validation): accept a full self-service path in secretsManagerPath (PTFM-26625) Charts before 0.1.318 pass secretsManagerPath to the ExternalSecret as is, so selfservice/k8s//// works at runtime, but their published schemas only allow k8s/ paths. The legacy-secrets migration keeps those services on their chart and points them at self-service this way, without a restart. The hook now widens each such pattern after download; a malformed self-service path is still refused. --- .../gitops_values_validation.py | 29 +++++++++++ ...dev-malformed-selfservice-secret-path.yaml | 17 +++++++ .../values-dev-selfservice-secret-path.yaml | 17 +++++++ tests/test_gitops_values_validation.py | 48 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 tests/test_data/gitops_data/app1/service1/values-dev-malformed-selfservice-secret-path.yaml create mode 100644 tests/test_data/gitops_data/app1/service1/values-dev-selfservice-secret-path.yaml diff --git a/kp_pre_commit_hooks/gitops_values_validation.py b/kp_pre_commit_hooks/gitops_values_validation.py index de2bfad..472f704 100755 --- a/kp_pre_commit_hooks/gitops_values_validation.py +++ b/kp_pre_commit_hooks/gitops_values_validation.py @@ -146,8 +146,37 @@ class SchemaValidationError(Exception): SCHEMA_CACHE_DIR = Path.home() / ".cache" / "pre-commit" / "kp-pre-commit-hooks" / "schemas" +# A secret kp writes, named in full: selfservice/k8s////. +# Charts before 0.1.318 pass secretsManagerPath to the ExternalSecret as is, so they read it like a legacy +# path, but their published schemas only allow k8s/...: the legacy-secrets migration (PTFM-26625) keeps +# those services on their chart and points them at self-service this way, without a restart. +SELFSERVICE_SECRET_PATH = r"selfservice/k8s/(dev|prod|shared|test|adnocuat|adnocprod)/[^/]+/[^/]+/[^/]+$" + + +def accept_selfservice_secret_paths(node): + """Widen every secretsManagerPath pattern that only allows legacy k8s/ paths to accept a self-service one.""" + if isinstance(node, dict): + properties = node.get("properties") + if isinstance(properties, dict): + entry = properties.get("secretsManagerPath") + pattern = entry.get("pattern") if isinstance(entry, dict) else None + if isinstance(pattern, str) and pattern.startswith("^k8s/"): + entry["pattern"] = f"^(?:{pattern[1:]}|{SELFSERVICE_SECRET_PATH})" + for value in node.values(): + accept_selfservice_secret_paths(value) + elif isinstance(node, list): + for value in node: + accept_selfservice_secret_paths(value) + return node + + @cache def download_json_schema(url: str) -> dict: + """Download a JSON schema, widened by accept_selfservice_secret_paths.""" + return accept_selfservice_secret_paths(fetch_json_schema(url)) + + +def fetch_json_schema(url: str) -> dict: """Download JSON schema, using a persistent disk cache for versioned URLs.""" filename = url.removeprefix(SCHEMA_BASE_URL + "/").replace("/", "_") cache_file = SCHEMA_CACHE_DIR / filename diff --git a/tests/test_data/gitops_data/app1/service1/values-dev-malformed-selfservice-secret-path.yaml b/tests/test_data/gitops_data/app1/service1/values-dev-malformed-selfservice-secret-path.yaml new file mode 100644 index 0000000..2a6cc82 --- /dev/null +++ b/tests/test_data/gitops_data/app1/service1/values-dev-malformed-selfservice-secret-path.yaml @@ -0,0 +1,17 @@ +# yaml-language-server: $schema=https://kp-helmchart-stable-shared-main.s3.eu-west-1.amazonaws.com/schema/platform-managed-chart/v0.1.225/schema-platform-managed-chart.json +# Test file: a secrets entry naming a kp-written secret by its full path +platform-managed-chart: + serviceName: service1 + teamOwner: team1 + env: dev + workloadType: stream + + stream: + secrets: + DB_PASSWORD: + secretsManagerPath: selfservice/k8s/qa/app1/env + property: DB_PASSWORD + + image: + repository: dev/my-repo + tag: my-tag diff --git a/tests/test_data/gitops_data/app1/service1/values-dev-selfservice-secret-path.yaml b/tests/test_data/gitops_data/app1/service1/values-dev-selfservice-secret-path.yaml new file mode 100644 index 0000000..e3e8e33 --- /dev/null +++ b/tests/test_data/gitops_data/app1/service1/values-dev-selfservice-secret-path.yaml @@ -0,0 +1,17 @@ +# yaml-language-server: $schema=https://kp-helmchart-stable-shared-main.s3.eu-west-1.amazonaws.com/schema/platform-managed-chart/v0.1.225/schema-platform-managed-chart.json +# Test file: a secrets entry naming a kp-written secret by its full path +platform-managed-chart: + serviceName: service1 + teamOwner: team1 + env: dev + workloadType: stream + + stream: + secrets: + DB_PASSWORD: + secretsManagerPath: selfservice/k8s/dev/app1/service1/env + property: DB_PASSWORD + + image: + repository: dev/my-repo + tag: my-tag diff --git a/tests/test_gitops_values_validation.py b/tests/test_gitops_values_validation.py index 4aa142a..6717e06 100644 --- a/tests/test_gitops_values_validation.py +++ b/tests/test_gitops_values_validation.py @@ -188,3 +188,51 @@ def test_topic_with_hyphenated_suffix_is_accepted( # THEN assert len(errors) == 0, f"Topic with hyphenated suffix should be accepted, got: {errors}" + + +def test_a_secret_named_by_its_full_self_service_path_is_accepted( + create_validator_for_test_file: Callable[[str], ServiceInstanceConfigValidator], +) -> None: + # GIVEN a chart whose published schema only allows k8s/ paths in secretsManagerPath + validator = create_validator_for_test_file("app1/service1/values-dev-selfservice-secret-path.yaml") + + # WHEN + errors = validator.validate_configuration() + + # THEN + assert errors == [], f"Expected no validation errors, got {[e.message for e in errors]}" + + +def test_a_malformed_self_service_path_is_still_refused( + create_validator_for_test_file: Callable[[str], ServiceInstanceConfigValidator], +) -> None: + # GIVEN an unknown environment and a missing level + validator = create_validator_for_test_file("app1/service1/values-dev-malformed-selfservice-secret-path.yaml") + + # WHEN + errors = validator.validate_configuration() + + # THEN + assert len(errors) == 1 and "does not match" in errors[0].message + + +def test_only_legacy_secret_path_patterns_are_widened() -> None: + from kp_pre_commit_hooks.gitops_values_validation import accept_selfservice_secret_paths + import re + + schema = { + "properties": { + "secretsManagerPath": {"type": "string", "pattern": "^k8s/(dev|prod)/(common|main)/"}, + "okta": {"properties": {"secretsManagerPath": {"pattern": "^selfservice/k8s/(dev|prod)/"}}}, + } + } + widened = accept_selfservice_secret_paths(schema)["properties"] + pattern = widened["secretsManagerPath"]["pattern"] + + assert re.match(pattern, "k8s/dev/common/my-app/db") + assert re.match(pattern, "selfservice/k8s/dev/my-app/my-svc/env") + assert re.match(pattern, "selfservice/k8s/prod/my-app/_shared/db-credentials") + assert not re.match(pattern, "selfservice/k8s/dev/my-app/env") + assert not re.match(pattern, "selfservice/k8s/dev/my-app/my-svc/env/extra") + assert not re.match(pattern, "other/k8s/dev/common/x") + assert widened["okta"]["properties"]["secretsManagerPath"]["pattern"] == "^selfservice/k8s/(dev|prod)/"