Skip to content

Sign and verify container images with Sigstore Cosign

Sign container images with Sigstore Cosign from your CI pipeline, then verify each signature before the image runs. You will tie each signature to the exact build that made it, attach a software bill of materials, and have Kubernetes refuse any image that is not signed.

Cosign, a free tool from the Sigstore project, signs container images so you can prove they came from your own build pipeline. It is for teams running containers through continuous integration (CI) and Kubernetes who want a deploy to fail when an image is not theirs. Without a signature, anyone with push access to your registry can replace an image, and nothing downstream notices.

Cosign stores the signature next to the image in the same registry, tied to the image’s digest (its content hash). The recommended mode is keyless. Your CI job proves its identity with a short-lived login token, and Fulcio, Sigstore’s certificate authority, issues a certificate that lasts about ten minutes. The signing event is recorded in Rekor, a public log anyone can audit. If your builders cannot reach Sigstore, a classic key pair works too. At deploy time, an admission controller (a Kubernetes component that accepts or rejects each new workload) checks the signature and blocks images that fail. A signature proves who published an image, not that it is safe. Vulnerability scanning stays a separate job.

What you’ll accomplish

  • Sign an image by digest with keyless Cosign from CI, with no key material stored anywhere.
  • Know when a cosign generate-key-pair key is the right answer instead, and what it costs you.
  • Verify a signature with the identity constraints that make verification mean something.
  • Attach an SBOM as an attestation and verify it in the same check.
  • Refuse unsigned images at admission time, so an unverified image cannot start a pod.

Prerequisites

  • Cosign 2.x. Check with cosign version. This matters more than usual: in Cosign 1.x, keyless mode needed COSIGN_EXPERIMENTAL=1 and cosign verify accepted no identity constraints at all, so a v1-era example copied from a blog post verifies that somebody signed the image and nothing more. Cosign 2 requires --certificate-identity and --certificate-oidc-issuer (or their -regexp variants) for keyless verification, and that is the point.
  • Push access to the registry, and a registry that accepts an extra tag in the same repository. Cosign stores the signature as a tag derived from the digest. Registries that enforce a strict tag naming policy, or repository-level immutability that blocks new tags, will reject it.
  • Outbound HTTPS to fulcio.sigstore.dev and rekor.sigstore.dev if you use the Sigstore public instance. Air-gapped builders need either a private Sigstore deployment or a key.
  • In GitHub Actions, the workflow needs permissions: id-token: write to mint the OIDC token. Without it, keyless signing fails at the token step, not at the signing step, which makes the error confusing.
  • For admission enforcement, Kubernetes 1.25 or later plus a policy engine. This guide uses the Sigstore policy-controller; Kyverno’s verifyImages rule expresses the same checks with different YAML.
  • Optional: syft for SBOM generation, and crane or skopeo for digest resolution.

How it works

A registry repository can hold more than images. Cosign takes the image digest, say sha256:ab12..., and pushes a second, tiny OCI artifact under the tag sha256-ab12....sig in the same repository. That object’s layers carry the signature and, in keyless mode, the signing certificate and the Rekor inclusion proof as annotations. Attestations go to a parallel .att tag. Nothing about the image itself changes — the digest you signed is still the digest you pull.

Keyless mode removes the private key from the problem instead of protecting it. The signer presents an OIDC token proving what it is (a GitHub Actions workflow at a specific path on a specific ref, a GitLab job, a human’s Google account). Fulcio, a certificate authority that only accepts OIDC identities, issues a certificate valid for about ten minutes with that identity in the SAN extension. Cosign generates an ephemeral key pair, signs the digest, records the signature and certificate in Rekor, and throws the private key away. There is no key to rotate because the key existed for the length of one command.

Flow diagram of Cosign keyless signing: a CI job presents an OIDC token, Fulcio issues a short-lived certificate, cosign signs the image digest and pushes the signature to the OCI registry while recording the event in the Rekor transparency log, then a verification pass fetches the signature, checks the certificate identity and issuer, and an admission controller allows or denies the deployment

Verification reverses that. Cosign fetches the signature object, checks the certificate chains to a Fulcio root it trusts, checks the certificate’s SAN and issuer against the constraints you passed, and confirms the Rekor entry — which is what makes a ten-minute certificate useful long after it expired. The log entry is timestamped, so the question becomes “was the certificate valid when the signature was made?” rather than “is it valid now?”.

The key-pair mode is the conventional shape: cosign generate-key-pair writes cosign.key (password-encrypted) and cosign.pub, you sign with the private key and hand the public key to verifiers. You now own a secret, its backups, its distribution and its rotation. Cosign can also address a key held in a KMS — awskms://, gcpkms://, azurekms://, hashivault:// — which is materially better than a file, because the private key never leaves the KMS.

Decision tree for choosing a Cosign identity model: whether the signer can obtain an OIDC token Fulcio trusts, whether a public transparency log entry is acceptable, and whether a KMS is available, pointing to keyless public signing, a private Sigstore deployment, a KMS-held key, or a locally generated key pair

One detail decides whether any of this is worth doing: sign the digest, not the tag. A tag is a mutable pointer. If you sign app:1.4.0 and someone repoints that tag at a different image tomorrow, your signature still verifies — against the old digest, which nobody is pulling any more. Cosign resolves a tag to a digest for you and prints a warning while doing it, but the reference you sign, verify and deploy should be repo@sha256:... end to end.

Step-by-step

  1. Install Cosign and confirm the major version. Package managers lag; the release binaries do not.

    curl -sSfLO https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64
    sudo install -m 0755 cosign-linux-amd64 /usr/local/bin/cosign
    cosign version

    Expected: a version block whose GitVersion starts with v2.. If it starts with v1., stop and upgrade — the flags below do not exist there.

  2. Resolve your tag to a digest. Everything downstream uses this string.

    crane digest ghcr.io/example-org/app:1.4.0

    Expected: a single line, sha256: followed by 64 hex characters. Without crane, docker buildx imagetools inspect ghcr.io/example-org/app:1.4.0 prints a Digest: line, and skopeo inspect docker://ghcr.io/example-org/app:1.4.0 returns it as the Digest field in JSON. Our skopeo guide covers reading registry metadata without pulling.

  3. Authenticate to the registry. Cosign reuses the Docker credential store, but its own login is explicit.

    echo "$GHCR_TOKEN" | cosign login ghcr.io -u "$GITHUB_ACTOR" --password-stdin

    Expected: Login Succeeded. The token needs package write scope — read-only credentials pull images fine and then fail at the signature push, several seconds later.

  4. Sign keylessly from CI. In GitHub Actions the OIDC token is ambient once permissions allow it, so the signing step is one line. The -y flag skips the interactive confirmation about writing to the public transparency log.

    permissions:
      contents: read
      packages: write
      # required for keyless signing: mints the OIDC token
      id-token: write
    
    steps:
      - uses: sigstore/cosign-installer@v3
      - name: Sign the image by digest
        run: cosign sign -y "ghcr.io/example-org/app@${DIGEST}"

    Expected: log lines reading Generating ephemeral keys..., Retrieving signed certificate..., tlog entry created with index: <number>, and Pushing signature to: ghcr.io/example-org/app. The index number is your Rekor entry; it is public.

  5. Verify with identity constraints. This is the step people get wrong. A cosign verify with no identity flags is rejected outright by Cosign 2, and a --certificate-identity-regexp of .* is the same as no check at all.

    cosign verify \
      --certificate-identity "https://github.com/example-org/app/.github/workflows/release.yml@refs/heads/main" \
      --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
      "ghcr.io/example-org/app@${DIGEST}"

    Expected: a Verification for ghcr.io/example-org/app@sha256:... -- banner followed by three checks — the Cosign claims were validated, existence of the claims in the transparency log was verified, and the code-signing certificate was verified against trusted certificate authority certificates — then the signature payload as JSON. Any other output is a failure.

  6. Or sign with a key pair, if keyless does not fit. The password protects the file at rest; COSIGN_PASSWORD supplies it non-interactively in CI.

    cosign generate-key-pair
    cosign sign --key cosign.key "ghcr.io/example-org/app@${DIGEST}"
    cosign verify --key cosign.pub "ghcr.io/example-org/app@${DIGEST}"

    Expected: cosign.key and cosign.pub in the working directory, then the same verification banner as above minus the certificate check. Never commit cosign.key. Better: cosign generate-key-pair k8s://cosign-system/cosign-key writes the pair straight into a Kubernetes Secret, and --key awskms://alias/cosign keeps it in a KMS where an exfiltrated CI runner cannot read it.

  7. Attach an SBOM as an attestation. An attestation is a signed statement about the digest, wrapped in an in-toto envelope, rather than a bare signature.

    syft "ghcr.io/example-org/app@${DIGEST}" -o spdx-json=sbom.spdx.json
    cosign attest -y --predicate sbom.spdx.json --type spdxjson "ghcr.io/example-org/app@${DIGEST}"

    Expected: Using payload from: sbom.spdx.json, another tlog entry created with index: line, and a push to a .att tag. The --type value is one of the known predicate shorthands (spdxjson, cyclonedx, slsaprovenance, vuln, custom) or a full predicate type URI.

  8. Verify the attestation. Same identity constraints, plus the predicate type you expect.

    cosign verify-attestation \
      --type spdxjson \
      --certificate-identity "https://github.com/example-org/app/.github/workflows/release.yml@refs/heads/main" \
      --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
      "ghcr.io/example-org/app@${DIGEST}"

    Expected: Verification for ghcr.io/example-org/app@sha256:... -- with the same three checks, then a base64 DSSE envelope. Decode the payload with jq -r .payload | base64 -d if you want to read the SBOM back.

  9. Enforce at admission. Install the Sigstore policy controller, then declare which images must be signed by whom.

    helm repo add sigstore https://sigstore.github.io/helm-charts
    helm install policy-controller -n cosign-system --create-namespace sigstore/policy-controller
    kubectl label namespace production policy.sigstore.dev/include=true

    Expected: pods running in cosign-system, and the label on production — without that label the webhook ignores the namespace entirely, which is the single most common reason a policy appears to do nothing.

    apiVersion: policy.sigstore.dev/v1beta1
    kind: ClusterImagePolicy
    metadata:
      name: require-signed-app-images
    spec:
      images:
        - glob: "ghcr.io/example-org/**"
      authorities:
        - keyless:
            url: https://fulcio.sigstore.dev
            identities:
              - issuer: https://token.actions.githubusercontent.com
                subject: https://github.com/example-org/app/.github/workflows/release.yml@refs/heads/main

    Expected: kubectl apply -f policy.yaml returns clusterimagepolicy.policy.sigstore.dev/require-signed-app-images created. The glob scopes the rule; images outside it are unaffected, so start narrow.

Validate it works

Three tests, and the two negative ones matter more than the positive one.

Confirm the signature exists in the registry and is addressed to the right digest:

cosign triangulate "ghcr.io/example-org/app@${DIGEST}"

Expected: ghcr.io/example-org/app:sha256-<64 hex chars>.sig, where the hex matches your digest. cosign tree on the same reference lists both the signature and any attestations.

Now prove that verification actually discriminates. Verify with a deliberately wrong identity:

cosign verify \
  --certificate-identity "https://github.com/example-org/app/.github/workflows/nope.yml@refs/heads/main" \
  --certificate-oidc-issuer "https://token.actions.githubusercontent.com" \
  "ghcr.io/example-org/app@${DIGEST}"

Expected: a non-zero exit and Error: no matching signatures. If this passes, your constraints are too loose — usually a -regexp flag with a pattern that matches everything.

Then try to deploy an unsigned image into the labelled namespace:

kubectl -n production run unsigned-test --image=ghcr.io/example-org/app:untagged-build --restart=Never

Expected: the request is rejected by the admission webhook, with a message naming the failing policy and the container image. Delete nothing; the pod was never created. If it starts, check the namespace label and that the image matches your glob.

Troubleshooting

  • Error: no matching signatures on an image you know you signed. Three usual causes. You signed a digest and are verifying a tag that has since moved — resolve and compare both digests. Your identity constraints do not match the certificate: run cosign verify with --certificate-identity-regexp '.*' --certificate-oidc-issuer-regexp '.*' once, as a diagnostic only, and read the actual subject out of the output, then pin to it. Or the signature lives in another repository because COSIGN_REPOSITORY was set at signing time and not at verification time.
  • Keyless signing fails with a Fulcio or token error in CI. Almost always the missing id-token: write permission in GitHub Actions, or a GitLab job without id_tokens configured. The give-away is that the failure happens before any registry traffic. Adding the permission at job level does not inherit to reusable workflows — set it there too.
  • The registry rejects the signature push. UNAUTHORIZED means the credential has pull but not push scope on that repository. TAG_INVALID or a policy rejection means the registry refuses the sha256-....sig tag shape — some Artifactory and Harbor configurations with tag immutability or naming rules do this. Loosen the rule for the repository, or use a dedicated signature repository via COSIGN_REPOSITORY.
  • Verification works on your laptop and fails in the cluster. The policy controller needs to pull the signature from a private registry itself. It uses the pod’s imagePullSecrets, and a secret that exists in the workload namespace but not where the controller resolves it produces a generic “signature not found”. Check the controller logs rather than guessing.
  • Signing is slow or intermittently fails on large matrix builds. Every keyless signature is a Fulcio certificate request plus a Rekor write, and the public instance is rate limited. Sign once per released digest rather than once per architecture per commit, or run a private Sigstore deployment.

When this is the wrong approach

A signature is a provenance claim, not a safety claim. It says an identity you named published this exact digest. It says nothing about what is inside — an image with a five-year-old OpenSSL and a signature is still an image with a five-year-old OpenSSL. Signing and vulnerability scanning answer different questions and you need both; the scan tells you what is in there, the signature tells you the thing you scanned is the thing you deployed.

The chain is also only as strong as who can trigger the signing identity. If any contributor can open a pull request that runs the release workflow, the signature honestly records that the release workflow signed the image, and the release workflow was doing what an attacker asked. Pin the identity to a protected branch or a tag-triggered workflow, and treat that pin as part of the security control rather than a formality.

Keyless on the public instance is the wrong choice when image names are confidential — Rekor entries are public and permanent, so repository paths, image names and workflow paths become public the first time you sign. Run a private Sigstore deployment or use a KMS-backed key instead. It is also wrong on air-gapped builders that cannot reach Fulcio at all. And if all you need is “this is the same image I inspected”, a pinned digest already gives you that without any of this machinery — signing earns its place when a third party has to verify something you cannot hand them out of band.

The reference material worth reading next is the Sigstore documentation for the trust model and the private-deployment path, and the Cosign repository for the full flag surface, which changes faster than any guide.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.