Sub topic 12
Enforcing signatures at deploy time with admission policies
Signing an image does nothing unless something checks the signature before the image runs. This chapter shows how a Kubernetes admission controller does that check at deploy time. It assumes you can already run cosign verify with a public key or with a certificate identity and issuer, and that you know basic Kubernetes objects such as Pods and Deployments.
Why verification must happen in the cluster
Engineers can run cosign verify by hand, but a manual step gets skipped. An admission controller is a component the Kubernetes API server calls before it stores a new object. The controller can accept the request, change it, or reject it. If it rejects every Pod whose image lacks a valid signature, an unsigned image cannot start, whoever applies the manifest.
Common choices are Kyverno, Sigstore's policy-controller, and Gatekeeper with an external data provider. This chapter uses Kyverno because its policy format maps closely onto the cosign verify flags you already know.
A keyless policy with Kyverno
The policy below accepts only images from one repository that were signed by one GitHub Actions workflow on the main branch. These are the same identity and issuer values you pinned in cosign verify.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-signed-images
spec:
validationFailureAction: Enforce
webhookTimeoutSeconds: 30
rules:
- name: verify-ghcr-signature
match:
any:
- resources:
kinds:
- Pod
verifyImages:
- imageReferences:
- "ghcr.io/OWNER/REPO*"
mutateDigest: true
verifyDigest: true
required: true
attestors:
- entries:
- keyless:
subject: "https://github.com/OWNER/REPO/.github/workflows/build.yml@refs/heads/main"
issuer: "https://token.actions.githubusercontent.com"
rekor:
url: https://rekor.sigstore.devEach field has a purpose:
imageReferencesselects which images the rule applies to. An image that matches no pattern is not checked at all.attestorsholds the trust anchor. Here it is a keyless identity. For key-based signing, use akeysentry with the contents ofcosign.pub.rekorrequires a matching entry in the transparency log.mutateDigestrewritesapp:1.0.0toapp@sha256:...after verification. The Pod then runs the exact bytes that were verified, not whatever the tag points to later.validationFailureAction: Enforceblocks violations.Auditonly records them. Newer Kyverno releases also accept this setting under a different field name, so check the docs for your version.
Testing the policy
Apply the policy, then try an unsigned image.
kubectl apply -f require-signed-images.yaml
# An image that was never signed should be rejected
kubectl run unsigned --image=ghcr.io/OWNER/REPO@sha256:<unsigned-digest>
# A correctly signed image should be admitted
kubectl run signed --image=ghcr.io/OWNER/REPO@sha256:<signed-digest>The first command fails with an admission error that names the policy and the rule. Read that message first when a deploy breaks.
Policy design decisions
- Fail open or fail closed. If the admission webhook is unreachable, the API server either allows or denies requests, depending on the webhook's
failurePolicy. Fail closed is safer, but then the controller needs several replicas. - Pin identity, not just "signed". A policy that accepts any valid Fulcio certificate only proves that someone signed the image. Set
subjectandissuerso only your workflow counts. - Registry availability. Verification reads from the registry at admission time. Cache results, or use a registry mirror close to the cluster.
- Attestations. Kyverno can also require a signed SBOM or provenance attestation through an
attestationsblock. This lets you reject images that lack an SBOM.
Exercise
A policy uses imageReferences: ["ghcr.io/acme/*"] with mutateDigest: true. A team deploys docker.io/library/nginx:latest, which is unsigned. What happens, and why is mutateDigest useful for the images that do match?
Show answer
The nginx Pod is admitted, because its image matches no pattern in imageReferences, so the rule never evaluates it. To block it, add a rule that denies images from other registries. For images that do match, mutateDigest rewrites the tag to the verified digest. That stops the tag from being moved to different content after verification and before the container pulls the image.