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-pairkey 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 neededCOSIGN_EXPERIMENTAL=1andcosign verifyaccepted 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-identityand--certificate-oidc-issuer(or their-regexpvariants) 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.devandrekor.sigstore.devif 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: writeto 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’sverifyImagesrule expresses the same checks with different YAML. - Optional:
syftfor SBOM generation, andcraneorskopeofor 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.
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.
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
-
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 versionExpected: a version block whose
GitVersionstarts withv2.. If it starts withv1., stop and upgrade — the flags below do not exist there. -
Resolve your tag to a digest. Everything downstream uses this string.
crane digest ghcr.io/example-org/app:1.4.0Expected: a single line,
sha256:followed by 64 hex characters. Withoutcrane,docker buildx imagetools inspect ghcr.io/example-org/app:1.4.0prints aDigest:line, andskopeo inspect docker://ghcr.io/example-org/app:1.4.0returns it as theDigestfield in JSON. Our skopeo guide covers reading registry metadata without pulling. -
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-stdinExpected:
Login Succeeded. The token needs package write scope — read-only credentials pull images fine and then fail at the signature push, several seconds later. -
Sign keylessly from CI. In GitHub Actions the OIDC token is ambient once permissions allow it, so the signing step is one line. The
-yflag 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>, andPushing signature to: ghcr.io/example-org/app. The index number is your Rekor entry; it is public. -
Verify with identity constraints. This is the step people get wrong. A
cosign verifywith no identity flags is rejected outright by Cosign 2, and a--certificate-identity-regexpof.*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. -
Or sign with a key pair, if keyless does not fit. The password protects the file at rest;
COSIGN_PASSWORDsupplies 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.keyandcosign.pubin the working directory, then the same verification banner as above minus the certificate check. Never commitcosign.key. Better:cosign generate-key-pair k8s://cosign-system/cosign-keywrites the pair straight into a Kubernetes Secret, and--key awskms://alias/cosignkeeps it in a KMS where an exfiltrated CI runner cannot read it. -
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, anothertlog entry created with index:line, and a push to a.atttag. The--typevalue is one of the known predicate shorthands (spdxjson,cyclonedx,slsaprovenance,vuln,custom) or a full predicate type URI. -
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 withjq -r .payload | base64 -dif you want to read the SBOM back. -
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=trueExpected: pods running in
cosign-system, and the label onproduction— 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/mainExpected:
kubectl apply -f policy.yamlreturnsclusterimagepolicy.policy.sigstore.dev/require-signed-app-images created. Theglobscopes 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 signatureson 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: runcosign verifywith--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 becauseCOSIGN_REPOSITORYwas 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: writepermission in GitHub Actions, or a GitLab job withoutid_tokensconfigured. 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.
UNAUTHORIZEDmeans the credential has pull but not push scope on that repository.TAG_INVALIDor a policy rejection means the registry refuses thesha256-....sigtag 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 viaCOSIGN_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.