diff --git a/k8s/bases/infrastructure/cluster-policies/best-practices/restrict-tenant-secret-stores.yaml b/k8s/bases/infrastructure/cluster-policies/best-practices/restrict-tenant-secret-stores.yaml index 97f0c8786f..87c653a9b9 100644 --- a/k8s/bases/infrastructure/cluster-policies/best-practices/restrict-tenant-secret-stores.yaml +++ b/k8s/bases/infrastructure/cluster-policies/best-practices/restrict-tenant-secret-stores.yaml @@ -90,6 +90,18 @@ spec: - name: not-platform-applied expression: >- request.userInfo.username != 'system:serviceaccount:flux-system:kustomize-controller' + # The External Secrets controller adds its cleanup finalizer to every + # ExternalSecret and PushSecret it reconciles, an UPDATE under its own + # identity. Without this condition a platform-placed resource that names + # the ClusterSecretStore is admitted on create and then never reconciled, + # because that first metadata update is denied. Only an update that leaves + # `spec` unchanged is skipped, so the controller still cannot change which + # store or key a resource reads, and a tenant cannot act as this identity. + - name: not-controller-metadata-update + expression: >- + !(request.userInfo.username == 'system:serviceaccount:external-secrets:external-secrets' && + request.operation == 'UPDATE' && oldObject != null && + object.?spec.orValue({}) == oldObject.?spec.orValue({})) variables: - name: kro expression: "request.userInfo.username == 'system:serviceaccount:kro-system:kro'" diff --git a/scripts/tests/test-restrict-tenant-secret-stores.sh b/scripts/tests/test-restrict-tenant-secret-stores.sh index 7be833fbf0..2ab67d6198 100755 --- a/scripts/tests/test-restrict-tenant-secret-stores.sh +++ b/scripts/tests/test-restrict-tenant-secret-stores.sh @@ -30,4 +30,22 @@ if ! grep -Fq "${expected_summary}" "${output_file}"; then exit 1 fi -echo "Tenant SecretStore policy enforced the expected 5-pass/7-deny boundary." +# The controller carve-out covers only spec-preserving updates. Under the +# controller identity the CLI evaluates each fixture as a create, so the same +# boundary must hold: the controller cannot create a store reference. +controller_user_info="${repo_root}/tests/restrict-tenant-secret-stores/user-info-controller.yaml" +if kyverno apply "${policy}" \ + --resource "${fixtures}" \ + --values-file "${values}" \ + --userinfo "${controller_user_info}" \ + --remove-color >"${output_file}" 2>&1; then + echo "::error::tenant SecretStore policy admitted every fixture created by the External Secrets controller" + exit 1 +fi +if ! grep -Fq "${expected_summary}" "${output_file}"; then + echo "::error::tenant SecretStore policy exempted a create by the External Secrets controller" + sed -n '1,80p' "${output_file}" + exit 1 +fi + +echo "Tenant SecretStore policy enforced the expected 5-pass/7-deny boundary for tenant and controller creates." diff --git a/tests/restrict-tenant-secret-stores/user-info-controller.yaml b/tests/restrict-tenant-secret-stores/user-info-controller.yaml new file mode 100644 index 0000000000..ca1e2b2189 --- /dev/null +++ b/tests/restrict-tenant-secret-stores/user-info-controller.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: cli.kyverno.io/v1alpha1 +kind: UserInfo +metadata: + name: external-secrets-controller +userInfo: + username: system:serviceaccount:external-secrets:external-secrets + groups: + - system:serviceaccounts + - system:serviceaccounts:external-secrets + - system:authenticated