How to Sign Container Images with Cosign
Attach a cryptographic signature to container images so a cluster can verify provenance before it runs a tag.
On this page
Introduction
A container image tag is mutable. The tag myapp:latest can point at a trusted image today and a compromised one tomorrow, because pushing a new image to the same tag is ordinary operation. A registry credential leak or a compromised CI job is enough to publish an image your cluster will happily run. Image signing closes that gap by binding a cryptographic signature to the image digest, so the runtime can verify that the bytes it is about to execute were signed by a key you trust, regardless of what the tag currently points to. This article covers keyless signing with OIDC identities, verification policy, and how to wire it into CI.
Why Mutable Tags Are Not Enough
Deployments typically reference a tag, and admission controllers typically allow whatever the tag resolves to. The digest is the immutable identity of the image contents; the tag is a pointer that can be repointed. An attacker who can push to the registry can overwrite a tag with malicious layers, and because the manifest changes, the digest changes too, but nothing in a tag-based deployment notices.
Pinning by digest prevents silent replacement, but digest pinning alone does not tell you who built the image or whether the build was trustworthy. Signing adds that: the signature asserts that a specific identity produced this digest. Verification checks the signature against a policy that names the identities allowed, so a digest pushed by an unauthorized builder is rejected even if the tag looks right.
Keyless Signing with OIDC
Managing long-lived signing keys is the hard part of signing: the key must be protected, rotated and revoked. Keyless signing removes the key from your infrastructure. The CI job obtains a short-lived OIDC token proving its workload identity, exchanges it for a short-lived signing certificate, signs the image, and records the signature and certificate in a transparency log. The private key lives only for the signing operation and is never stored.
# Sign the freshly built image by digest using the CI job's OIDC identity
cosign sign --yes \
ghcr.io/acme/orders@sha256:$(crane digest ghcr.io/acme/orders:latest)
# Inspect the recorded signatures for an image
cosign tree ghcr.io/acme/orders:latest
# Verify against an expected identity and issuer (keyless policy)
cosign verify ghcr.io/acme/orders:latest \
--certificate-identity-regexp '^https://github.com/acme/orders/.github/workflows/.*' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com
What the Signature Proves
A signature binds three things: the image digest, the signing identity, and the time of signing. It does not prove the image is free of vulnerabilities or that the source was reviewed; it proves who published this exact digest. That is the useful, verifiable claim. Combine it with an SBOM attestation to also assert what is inside the image, and with policy that source must come from your repository.
Signatures are stored as OCI artifacts alongside the image in the registry, or in a transparency log. Storing them in the registry keeps verification self-contained: an admission controller can fetch the signature from the same registry it pulls the image from. The transparency log gives an auditable record that a signature was created, which deters a compromised key from signing quietly.
Enforcing Verification at Admission
Signing is only useful if something verifies. The strongest place is the admission controller, which rejects a pod whose image lacks a valid signature from an allowed identity. That way no deployment path, however rushed, can run an unsigned image. Pair the admission policy with a build pipeline that always signs, so the only way an image enters the cluster is through the verified path.
Start the policy in audit mode: log images that would fail without blocking, fix the pipeline until the log is clean, then switch to enforce. Turning on enforcement first breaks deployments and tempts people to add broad exceptions that defeat the policy. This staged rollout is the same caution used for any cluster-wide control.
Hooking Signing into CI
Sign as the last step of the build, after the image digest is final, because signing a tag that later changes signs the wrong bytes. Capture the digest from the push step and sign that. Use the CI job’s OIDC identity so the policy can bind to the workflow path rather than a shared secret, and reject builds that push an image without a signature.
# GitHub Actions: build, push, then sign by digest using OIDC
permissions:
contents: read
packages: write
id-token: write # required for keyless signing
jobs:
build:
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- id: build
run: |
docker build -t ghcr.io/acme/orders:${{ github.sha }} .
docker push ghcr.io/acme/orders:${{ github.sha }}
echo "digest=$(crane digest ghcr.io/acme/orders:${{ github.sha }})" >> "$GITHUB_OUTPUT"
- uses: sigstore/cosign-installer@v3
- run: cosign sign --yes ghcr.io/acme/orders@${{ steps.build.outputs.digest }}
Rotating and Revoking
Even with keyless signing, the identities that may sign must be managed. Restrict which workflows can obtain the OIDC token, and keep the verification policy narrow so a new repository cannot impersonate an existing one. If a signing identity is compromised, remove it from the allowed set in the policy and the images it signed stop verifying.
Signature verification should fail closed: an unreachable signature, an unknown identity, or an expired certificate is a rejection, not a bypass. A verifier that falls back to allowing the image when it cannot check the signature is worse than no verifier, because it gives the appearance of control without the substance.
Conclusion
Signing container images binds a trusted identity to an immutable digest, which mutable tags cannot express. Use keyless signing so there is no long-lived key to protect, sign by digest at the end of the build, verify against a policy that names the allowed identities and issuers, and enforce verification in the admission controller after an audit period. Fail closed when verification cannot complete. The result is a cluster that runs only images whose provenance you can prove.
Sources: Sigstore Cosign, Sigstore keyless signing, Sigstore policy controller.
FAQ
- How is signing different from pinning an image by digest?
- Pinning by digest stops silent replacement but says nothing about who built the image. Signing asserts that a specific trusted identity produced that digest, so verification can reject an image pushed by an unauthorized builder even if the digest is correct.
- What is keyless signing?
- Instead of a stored private key, the signer obtains a short-lived certificate from its OIDC workload identity, signs, and records the result in a transparency log. No long-lived key exists to protect, rotate or leak.
Report a problem with this articleOpens a pre-filled issue on GitHub.