Signing and Verifying Container Images with Cosign
Pinning images by digest tells you which image runs, but not who built it. Learn how to sign container images with Cosign in CI, verify them with tightly scoped identities, enforce verification at deploy time and protect the signing process itself.
Signing and Verifying Container Images with Cosign
A container registry will happily store and serve any image pushed to it. If an attacker gains push access, through a leaked CI token, a compromised developer account or a misconfigured registry, they can replace an image your clusters pull, and nothing in a standard deployment pipeline will notice. Pinning images by digest helps you know exactly which image runs, but it does not tell you whether that image came from your build system.
Image signing answers that second question. A signature ties an image digest to an identity, such as a specific CI workflow, so consumers can verify that the image they are about to run was built by a trusted process and has not been altered.
This guide shows how to sign container images with Cosign, part of the Sigstore project, verify them with tightly scoped identities, enforce verification at deploy time and protect the signing process itself. Signing and verification commands are shown with their effects; registry writes are clearly noted.
How Cosign Signing Works
Cosign signs an image digest, not a tag, and stores the signature in the registry alongside the image as an OCI artifact. It supports two main modes.
Key-based signing uses a key pair you manage. You sign with the private key and verify with the public key.
Keyless signing uses short-lived certificates tied to an OpenID Connect identity, such as a CI workflow identity. In the public Sigstore setup, a certificate authority issues a short-lived certificate for that identity, the signature is recorded in a public transparency log, and verifiers check that the signature came from the expected identity and issuer. There is no long-lived private key to protect or rotate.
Keyless signing from CI is often the simplest secure option, because the identity is the build workflow itself. Key-based signing is useful where public services cannot be used, or where you want keys held in a key management service.
Step 1: Sign Images in CI
Always sign by digest, using the digest your build produced, so you sign exactly the image you pushed rather than whatever a tag points to at the time.
Keyless Signing from GitHub Actions
The workflow needs permission to request an OIDC token:
jobs:
release:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- uses: actions/checkout@<full-commit-sha>
- uses: sigstore/cosign-installer@<full-commit-sha>
- name: Build and push
id: build
uses: docker/build-push-action@<full-commit-sha>
with:
push: true
tags: registry.example.internal/api:${{ github.sha }}
- name: Sign image
run: cosign sign --yes registry.example.internal/api@${{ steps.build.outputs.digest }}
The id-token: write permission lets the job obtain the token Cosign uses for keyless signing. The --yes flag skips the interactive confirmation. Note: signing writes a signature artifact to the registry and, with the public Sigstore setup, records an entry in the public transparency log.
Key-Based Signing
Generate a key pair once:
cosign generate-key-pair
This creates cosign.key, an encrypted private key, and cosign.pub, the public key. Sign an image by digest:
cosign sign --key cosign.key registry.example.internal/api@sha256:<digest>
For production, avoid keeping private keys as files. Cosign can use keys stored in cloud key management services and HashiCorp Vault through key references such as awskms://, gcpkms://, azurekms:// and hashivault://, so the private key never leaves the key service.
Step 2: Verify Signatures
Verification checks that a valid signature exists and that it was produced by an identity you trust.
Verifying Keyless Signatures
For keyless signatures, you must specify the expected identity and OIDC issuer:
cosign verify registry.example.internal/api@sha256:<digest> \
--certificate-identity='https://github.com/my-org/api/.github/workflows/release.yml@refs/heads/main' \
--certificate-oidc-issuer='https://token.actions.githubusercontent.com'
This read-only command succeeds only if the image was signed by the release.yml workflow in the my-org/api repository, running on the main branch. A signature from any other repository, workflow or branch fails verification.
This is the most important detail in the whole setup. Verifying that an image is signed is not enough; you must verify who signed it. Anyone can obtain a valid keyless signature for their own identity. Only the identity check makes the signature meaningful.
Cosign also supports regular-expression matching for identities. Use it carefully: a loose pattern can accept signatures from repositories or branches you did not intend to trust.
Verifying Key-Based Signatures
cosign verify --key cosign.pub registry.example.internal/api@sha256:<digest>
This succeeds only if the image was signed with the matching private key.
Step 3: Enforce Verification at Deploy Time
Verifying in a pipeline is useful, but it does not stop someone from deploying an unsigned image directly. Enforcement belongs at the point of deployment.
In Kubernetes, admission controllers can verify signatures before pods are created. Options include the Sigstore policy-controller, Kyverno's image verification rules and policy engines that call external verification services.
Illustrative example of a Kyverno policy that requires keyless signatures from a specific workflow for images from an internal registry:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: verify-internal-images
spec:
webhookTimeoutSeconds: 30
rules:
- name: require-release-signature
match:
any:
- resources:
kinds:
- Pod
verifyImages:
- imageReferences:
- "registry.example.internal/*"
failureAction: Audit
attestors:
- entries:
- keyless:
subject: "https://github.com/my-org/api/.github/workflows/release.yml@refs/heads/main"
issuer: "https://token.actions.githubusercontent.com"
Field names and defaults differ between Kyverno versions, so check the documentation for the version you run. Start in audit mode, which reports violations without blocking, so you can see which workloads would fail. Once unsigned and incorrectly signed images are fixed, switch to enforcement.
Warning: an enforcing admission policy that cannot reach its verification dependencies, or that has a configuration error, can block all matching pod creation, including during incidents. Test in non-production clusters, scope policies to the registries and namespaces you intend, and plan how to handle failures in the admission webhook.
Step 4: Sign More Than Images
The same tooling can attach signed attestations to an image: statements such as an SBOM, vulnerability scan results or build provenance. Verifying attestations lets you require, for example, that an image has provenance from your build system, not just a signature. Start with image signatures, then add attestations once verification is routine.
Air-Gapped and Private Environments
The public Sigstore services are not always appropriate. Organizations with strict network controls or confidentiality requirements can:
- Use key-based signing with keys held in a key management service.
- Run private Sigstore infrastructure, including their own certificate authority and transparency log.
Either way, verification policies must reference the keys or identities and services you actually use.
What About Docker Content Trust?
Docker has an older signing mechanism, Docker Content Trust, enabled with the DOCKER_CONTENT_TRUST environment variable and based on Notary. It signs tags rather than working well with digest-based workflows and has seen limited adoption across the wider ecosystem. For new setups, Sigstore-based signing is generally the more widely supported choice across registries, CI systems and Kubernetes policy tools.
Security Considerations
- A signature proves origin, not safety. It shows an image came from your build process, not that the image is free of vulnerabilities. Combine signing with scanning and SBOMs.
- Protect the signing identity. With keyless signing, whoever can run the trusted workflow on the trusted branch can produce valid signatures. Require reviews for changes to release workflows and restrict who can push to protected branches.
- Scope identities tightly. Verify the exact repository, workflow and branch. Wildcards that match whole organizations or all branches let pull request builds or forks produce acceptable signatures.
- Public transparency logs are public. Keyless signing with the public Sigstore instance records metadata such as repository and workflow names in a public log. If that is sensitive, use key-based signing or private infrastructure.
- Keep keys in a key service. For key-based signing, store private keys in a KMS or Vault, restrict who can use them and plan for rotation.
- Verify where it counts. A check in CI that can be skipped is weaker than an admission policy in the cluster. Enforce at deploy time.
Keeping Signing Status Visible
As signing rolls out, some workloads will still run unsigned images, images signed by unexpected identities or images from registries outside the policy. Knowing which running workloads meet the policy, across clusters and environments, is what makes a gradual rollout safe. That kind of configuration visibility is the area AtGlance focuses on; the verification commands in this guide work with any tooling.
Conclusion
Digest pinning tells you which image runs; signing tells you who built it. Sign images by digest in CI, using keyless signing tied to your release workflow or keys held in a key management service. Verify signatures with exact identities and issuers, not just their presence. Enforce verification at deploy time with an admission policy, starting in audit mode, and protect the release workflow and keys that produce trusted signatures. Together with scanning and SBOMs, signing makes a replaced or tampered image something your platform rejects rather than runs.