Enforcing trust at the cluster door
Use Kubernetes admission control with Kyverno ImageValidatingPolicy to verify Sigstore signatures and SLSA provenance before a Pod is admitted, restrict the registries a cluster accepts, and know how a failing webhook can block a cluster.
Contentsand lesson path
On this page
- The Cluster Door: Kubernetes Dynamic Admission Webhooks
- Declarative Admission Policy: Kyverno ImageValidatingPolicy
- Worked Example: Admission Enforcement in Action
- Check your understanding
- Apply it
- Review prompt
Lessons of Supply Chain Security
- How build pipelines get attacked Concept · 20 min
- SLSA in practice: hardened builds and provenance Concept · 20 min
- Lab: SBOMs and a vulnerability triage policy Lab · 25 min
- Lab: keyless signing and provenance in CI Lab · 25 min
- Enforcing trust at the cluster door Worked example · 20 min
- Check: supply-chain audit of a sample pipeline Knowledge check · 6 questions
What you will learn
- Sign images and attach build provenance with Sigstore cosign in CI, then verify signatures and attestations before deployment with an admission policy.
01. The Cluster Door: Kubernetes Dynamic Admission Webhooks
Continuous integration pipelines and container registry scanners provide valuable early-warning security signals. However, CI pipelines operate far upstream from production compute infrastructure. If an attacker compromises a developer credential, gains write access to the container registry, or bypasses the GitOps workflow by running kubectl run evil-app --image=untrusted-repo/malware:latest, the CI gates never see the image at all.
The last place where you can still stop such an image is the cluster door: the admission control phase of the Kubernetes API server. When any client submits a Pod creation request (kubectl apply, Helm install, or ArgoCD sync), the Kubernetes API server intercepts the deserialized object through Dynamic Admission Webhooks:
-
Mutating Webhook Phase: Evaluates the Pod specification and injects default security contexts, sidecar proxies, or rewrites image tags into content-addressed digests (
sha256:...). -
Validating Webhook Phase: Evaluates the request against declarative cluster policies. If the Pod violates any security constraint, the admission controller rejects the API request before
etcdpersists the object and before kube-scheduler schedules the container to a worker node.
To enforce cryptographic supply chain trust at the cluster door, SRE teams deploy dedicated admission controllers such as Kyverno or the Sigstore Policy Controller. These controllers receive Pod admission requests, fetch the signatures and in-toto attestations attached to each image in the registry, check their transparency-log entries in Rekor, and check that the certificate identity names an approved repository and workflow.
In modern Kubernetes versions (1.30+), clusters also support Common Expression Language (CEL) via ValidatingAdmissionPolicies. However, because signature and attestation verification requires external OCI registry network round-trips and cryptographic certificate path validation, dedicated webhook controllers remain the usual choice for supply chain enforcement. Verification has a latency cost: a cached result is fast, but a cold verification needs registry and Rekor round-trips, so set the webhook timeout with that in mind (Kyverno's default is 10 seconds). Kyverno now writes these checks as CEL-based policies in the policies.kyverno.io API group, and this lesson uses its ImageValidatingPolicy (policies.kyverno.io/v1). The older kyverno.io/v1 ClusterPolicy with verifyImages rules, which many tutorials still show, was deprecated in Kyverno 1.19, and Kyverno 1.20 no longer enforces it. After an upgrade to 1.20, existing ClusterPolicy and Policy objects are kept, so you can still read, export or delete them, but they are marked NotReady and check no Pod at all. Creating new ones or changing their spec is refused, and the 1.20 CLI rejects them in kyverno apply. The Helm chart stops the upgrade while such policies exist, unless you set upgrade.acknowledgeLegacyPoliciesNotEnforced=true to accept that they will stop protecting the cluster. The project plans to remove the legacy CRDs in 1.21. So move image rules to ImageValidatingPolicy before you upgrade to 1.20; Kyverno's migration guide maps each verifyImages field to the new type.
02. Declarative Admission Policy: Kyverno ImageValidatingPolicy
Gatekeeper policies are written in Rego, a language of their own. Kyverno policies are ordinary Kubernetes resources, and their checks are written in CEL, the expression language the API server itself uses. The manifest below holds two policies.
-
restrict-registriesis aValidatingPolicy. Its CEL expression joins the containers, init containers and ephemeral containers of the Pod (.?initContainers.orValue([])gives an empty list when the field is missing) and requires every image to start withregistry.internal/. It is needed because the image policy only checks images that match itsmatchImageReferences: an image from any other registry would otherwise be admitted unverified. -
verify-image-trustis anImageValidatingPolicyfor images that matchregistry.internal/payments*. Itsattestorsentry says who may sign: a keyless certificate issued for a GitHub Actions token (issuer) whose identity matchessubjectRegExp. The regex pins the repository (org/payments), the workflow file (release.yaml) and release tags. A looser subject such ashttps://github.com/org/payments*would also accept any branch or pull-request ref, and any repository whose name starts withpayments, such aspayments-fork. -
Signature and provenance checks are the two
validations.verifyImageSignaturesreturns a number above zero when a signature from the attestor verifies, and.all(...)requires that for every matching container image.verifyAttestationSignaturesdoes the same for the SLSA provenance attestation, andextractPayloadthen reads the verified in-toto statement, so the second check can require that the build came fromhttps://github.com/org/payments. Kyverno finds signatures in both layouts from lesson 04: the Sigstore bundles that cosign 3 andactions/attestattach as OCI referrers, and the older.sigand.atttags. -
Digest resolution: with
mutateDigest: true, Kyverno resolves a tag such asregistry.internal/payments:v2.4.0to its digest, verifies that digest and writes it into the Pod spec. The node then pulls exactly the image that was checked, so a tag moved between admission and pull (a time-of-check to time-of-use, or TOCTOU, race) changes nothing.verifyDigest: truechecks that the image is given by digest. -
Caching: a cold verification needs registry and Rekor round-trips, hence the 15-second webhook timeout. Kyverno's admission controller also keeps a TTL cache of verified images (
--imageVerifyCacheEnabled, on by default, entries kept up to 60 minutes), so further pods with the same digest, for example during a scale-out, are usually admitted without repeating those calls.
Read the complete manifest below.
# 1. Only the trusted registry, for every kind of container in the Pod
apiVersion: policies.kyverno.io/v1
kind: ValidatingPolicy
metadata:
name: restrict-registries
spec:
validationActions: [Deny]
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values: ["production", "payments"]
validations:
- expression: >-
(object.spec.containers
+ object.spec.?initContainers.orValue([])
+ object.spec.?ephemeralContainers.orValue([]))
.all(c, c.image.startsWith("registry.internal/"))
message: "Images must come from registry.internal/."
---
# 2. Signature and provenance from the release workflow of org/payments
apiVersion: policies.kyverno.io/v1
kind: ImageValidatingPolicy
metadata:
name: verify-image-trust
spec:
validationActions: [Deny]
webhookConfiguration:
timeoutSeconds: 15 # cold verification needs registry and Rekor round-trips
matchConstraints:
resourceRules:
- apiGroups: [""]
apiVersions: ["v1"]
operations: ["CREATE", "UPDATE"]
resources: ["pods"]
namespaceSelector:
matchExpressions:
- key: kubernetes.io/metadata.name
operator: In
values: ["production", "payments"]
matchImageReferences:
- glob: "registry.internal/payments*"
validationConfigurations:
mutateDigest: true # rewrite the tag to the digest that was verified
verifyDigest: true # and check that the Pod then uses a digest
required: true
attestors:
- name: release
cosign:
keyless:
identities:
- issuer: https://token.actions.githubusercontent.com
# One regex, split to fit this page. In double quotes, `\\.` is the
# regex `\.`, and the trailing `\` joins the next line without a
# space; drop the `\` and YAML inserts a space, so no subject matches.
subjectRegExp: "^https://github\\.com/org/payments/\\.github/workflows/\
release\\.yaml@refs/tags/v[0-9]+\\.[0-9]+\\.[0-9]+$"
ctlog:
url: https://rekor.sigstore.dev
attestations:
- name: provenance
intoto:
type: https://slsa.dev/provenance/v1
validations:
- expression: >-
images.containers.map(i, verifyImageSignatures(i, [attestors.release]))
.all(n, n > 0)
message: "Not signed by the release workflow of org/payments."
- expression: >-
images.containers.map(i,
verifyAttestationSignatures(i, attestations.provenance,
[attestors.release]) > 0
&& extractPayload(i, attestations.provenance).predicate
.buildDefinition.externalParameters.workflow.repository
== "https://github.com/org/payments")
.all(ok, ok)
message: "No SLSA provenance from org/payments."03. Worked Example: Admission Enforcement in Action
In this worked example, we apply both policies to a cluster running Kyverno 1.20 and try four images in the payments namespace. Each attempt tests one check, and every rejection is a negative test you should run yourself after any policy change:
docker.io/library/busyboxis rejected byrestrict-registries. Without that policy,verify-image-trustwould simply skip it, because it only checks images that matchmatchImageReferences.- An image pushed to
registry.internal/paymentsby a branch build, or signed by any workflow other thanrelease.yamlon a release tag, is rejected by the signature check: the certificate identity does not matchsubjectRegExp. - An image that is signed correctly but has no SLSA provenance, or provenance from another repository, is rejected by the provenance check.
- The release image passes both policies and the Pod is created.
The cluster output below is illustrative: it needs a cluster, a registry and a signed release, and Kyverno's message wording varies between versions. The policies themselves were run without a cluster, using the Kyverno 1.20 CLI (kyverno apply), which still contacts the registry and Sigstore to check real signatures: restrict-registries rejected a Pod with a Docker Hub image and one with a Docker Hub init container, and policies of the same shape as verify-image-trust, pointed at real signed images, accepted the Trivy release image signed by reusable-release.yaml, rejected it when the expected workflow was release.yaml, and accepted the SLSA provenance of a Kyverno release only when the required repository was kyverno/kyverno.
$ kubectl apply -f k8s/policies/verify-image-trust.yaml
validatingpolicy.policies.kyverno.io/restrict-registries created
imagevalidatingpolicy.policies.kyverno.io/verify-image-trust created
$ kubectl run x -n payments --image=docker.io/library/busybox:1.37
Error from server: admission webhook "...kyverno.svc..." denied the request:
... restrict-registries: Images must come from registry.internal/.
$ kubectl run y -n payments --image=registry.internal/payments:branch-build
Error from server: admission webhook "...kyverno.svc..." denied the request:
... verify-image-trust: Not signed by the release workflow of org/payments.
... expected SAN value to match regex "^https://github\.com/org/payments/...",
got "https://github.com/org/payments/.github/workflows/ci.yaml@refs/heads/feat"
$ kubectl run z -n payments --image=registry.internal/payments@sha256:<release digest>
pod/z created04. Check your understanding
05. Apply it
Implement these cluster door admission policies across your Kubernetes clusters:
- Deploy Kyverno in High Availability: Run Kyverno with at least three replicas spread across distinct nodes with PodDisruptionBudgets.
- Configure Namespace Exclusions: Exclude critical system namespaces (
kube-system,kyverno) from signature enforcement to prevent bootstrapping deadlocks. - Enforce Tag-to-Digest Mutation: Set
mutateDigest: trueso Kyverno writes the verified digest (sha256:...) into each Pod at admission. - Verify Both Issuer and Subject: Mandate keyless verification rules checking the exact GitHub Actions OIDC issuer and repository identity.
- Enforce SLSA Provenance Attestations: Require in-toto provenance verification ensuring images originated from trusted release workflows.
- Start with Audit Mode: Deploy new policies with
validationActions: [Audit]in staging, read the policy reports, and switch to[Deny]in production. - Migrate Legacy Policies Before 1.20: If you still run
kyverno.io/v1ClusterPolicyimage rules, move them toImageValidatingPolicybefore you upgrade to Kyverno 1.20, following Kyverno's migration guide, because 1.20 stops enforcing them. Setupgrade.acknowledgeLegacyPoliciesNotEnforced=trueonly after the new policies are in place and checked in Audit mode.
06. Review prompt
Explain why admission control is the last place to stop an untrusted image, and describe how a Kyverno ImageValidatingPolicy verifies Sigstore signatures and in-toto provenance before a Pod is admitted. Outline the operational dangers of misconfiguring admission webhook failurePolicy settings, and describe why mutating image tags to immutable digests during admission is critical to preventing time-of-check to time-of-use (TOCTOU) attacks.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.