Skip to main content

Containers and Kubernetes · Advanced · 22 min

Sign and verify container images with cosign

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · cosign 3.1.6 · crane 0.22.1 · Syft 1.57.2 · distribution registry 3.1.2

You will sign an image by digest with a password-protected cosign key, prove the signature verifies and that tampering fails, attach a signed SBOM attestation, and have a GitHub Actions workflow ready that signs keylessly through Sigstore.

On this page

A registry tells you that an image called shop/api:1.4 exists. It does not tell you who built it, or whether the bytes behind that tag are the bytes your pipeline pushed yesterday. Tags can be moved by anyone with push access, and a compromised credential is enough to swap a production image for something else. Signing fixes that: your build signs the image's digest, and anything that runs the image checks the signature first.

cosign is the Sigstore project's tool for this. It signs an image digest, stores the signature next to the image in the same registry, and verifies it later. It works in two modes. With a key pair, you hold a private key and publish the public key. Keyless, a CI job proves its identity to Sigstore through OpenID Connect, gets a short-lived certificate, and the signature is tied to "this workflow in this repository" instead of to a key you have to guard.

You will do the key-pair flow end to end against a practice registry on your own machine, see two kinds of tampering fail, sign an SBOM as an attestation, then set up the keyless flow in GitHub Actions. Everything lives in ~/cosign-demo.

Step 1 · Install cosign, crane and Syft

You need three tools. cosign signs and verifies. crane, from Google's go-containerregistry, copies images between registries and prints digests without a Docker daemon. Syft generates the SBOM you will attest to later. All three ship as single binaries on GitHub, and each release publishes a checksum file, so check every download before installing it.

First the packages the commands below rely on:

Bash
sudo apt update
sudo apt install -y curl ca-certificates jq

Then cosign 3.1.6. dpkg --print-architecture prints amd64 or arm64, which matches cosign's file names:

Bash
mkdir -p ~/cosign-demo/downloads
cd ~/cosign-demo/downloads
ARCH=$(dpkg --print-architecture)
COSIGN_VERSION=3.1.6
curl -fsSLO "https://github.com/sigstore/cosign/releases/download/v${COSIGN_VERSION}/cosign-linux-${ARCH}"
curl -fsSLO "https://github.com/sigstore/cosign/releases/download/v${COSIGN_VERSION}/cosign_checksums.txt"
grep " cosign-linux-${ARCH}\$" cosign_checksums.txt | sha256sum -c -
sudo install -m 755 "cosign-linux-${ARCH}" /usr/local/bin/cosign

crane names its archives x86_64 and arm64, so map the architecture first:

Bash
cd ~/cosign-demo/downloads
CRANE_VERSION=0.22.1
case "$ARCH" in
  amd64) CRANE_ARCH=x86_64 ;;
  arm64) CRANE_ARCH=arm64 ;;
esac
curl -fsSLO "https://github.com/google/go-containerregistry/releases/download/v${CRANE_VERSION}/go-containerregistry_Linux_${CRANE_ARCH}.tar.gz"
curl -fsSL -o crane_checksums.txt "https://github.com/google/go-containerregistry/releases/download/v${CRANE_VERSION}/checksums.txt"
grep " go-containerregistry_Linux_${CRANE_ARCH}.tar.gz\$" crane_checksums.txt | sha256sum -c -
tar -xzf "go-containerregistry_Linux_${CRANE_ARCH}.tar.gz" crane
sudo install -m 755 crane /usr/local/bin/crane

And Syft 1.57.2:

Bash
cd ~/cosign-demo/downloads
SYFT_VERSION=1.57.2
curl -fsSLO "https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz"
curl -fsSLO "https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_checksums.txt"
grep " syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz\$" "syft_${SYFT_VERSION}_checksums.txt" | sha256sum -c -
tar -xzf "syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz" syft
sudo install -m 755 syft /usr/local/bin/syft

These binaries live in /usr/local/bin and get no apt security updates. To upgrade, run the same commands with the new version number; install replaces the old file. The three downloads come to well over 100 MB together, so on a prepaid mobile connection fetch them once, on Wi-Fi if you can, and keep the downloads folder until you have installed everything.

Check it worked

Each sha256sum -c line ends in OK. If one prints FAILED, delete the file and download it again before installing. Then confirm the versions:

Bash
cosign version 2>&1 | grep -E '^GitVersion:'
crane version
syft version | grep -E '^Version:'

You see GitVersion: v3.1.6, 0.22.1 (crane may print it with a leading v) and Version: 1.57.2.

Step 2 · Put an image in a practice registry

Signatures live in the registry next to the image, so you need a registry you can push to. Run the official distribution registry on your own machine, bound to localhost only:

Bash
docker run -d --name cosign-demo-registry -p 127.0.0.1:5000:5000 registry:3.1.2

This registry has no authentication and speaks plain HTTP. That is fine on localhost, where crane and cosign accept HTTP without extra flags, and wrong anywhere else.

Now copy a small public image into it. --platform linux/amd64 copies one image instead of every architecture BusyBox publishes, which keeps the download to a couple of megabytes. Then ask the registry for the digest, the SHA-256 of the image manifest. The digest is the image's real identity; the tag is only a label that points at it.

Bash
cd ~/cosign-demo
IMAGE=localhost:5000/cosign-demo/busybox
crane copy --platform linux/amd64 docker.io/library/busybox:1.37.0 "$IMAGE:1.37.0"
DIGEST=$(crane digest "$IMAGE:1.37.0")
echo "$DIGEST"

Keep this terminal open: the later steps use $IMAGE and $DIGEST.

Check it worked

curl -s http://localhost:5000/v2/ prints {}. echo "$DIGEST" prints sha256: followed by 64 hex characters, and the tag list shows the tag you pushed:

Bash
crane ls "$IMAGE"
case "$DIGEST" in sha256:*) echo "digest looks right" ;; *) echo "no digest"; false ;; esac

Step 3 · Create a password-protected key pair

cosign generate-key-pair writes two files: cosign.key, the private key, encrypted with a password you choose, and cosign.pub, the public key you hand to anyone who needs to verify. cosign asks for the password twice and never echoes it, so it does not end up in your shell history.

Bash
cd ~/cosign-demo
cosign generate-key-pair

Choose a long password and store it in your password manager. Anyone with both the key file and the password can sign as you. In CI, keep the key and the password in the CI system's secret store and expose the password to the job as the COSIGN_PASSWORD environment variable, which cosign reads instead of prompting. Never commit cosign.key.

Check it worked

Bash
ls -l cosign.key cosign.pub
head -n 1 cosign.key
head -n 1 cosign.pub

cosign.key is readable by you only (-rw-------). Its first line is -----BEGIN ENCRYPTED SIGSTORE PRIVATE KEY-----, and cosign.pub starts with -----BEGIN PUBLIC KEY-----.

Step 4 · Sign the image by digest

By default cosign 3 also records every signature in Rekor, Sigstore's public transparency log, and gets a timestamp from Sigstore's timestamp authority. That is what you want for software you publish: anyone can later see that the signature existed and when. The log is public and entries cannot be removed, so for a practice image on localhost, or for a private image whose existence you do not want on a public log, sign without it. cosign 3 does that through a signing config file that lists no services:

Bash
cd ~/cosign-demo
cosign signing-config create --out local-signing-config.json
cat local-signing-config.json

Now sign. Always sign image@digest, never image:tag. If you sign a tag, cosign resolves it at that moment, and someone who moves the tag between your build and your signing command gets their image signed by you.

Bash
cosign sign --yes --key cosign.key --signing-config local-signing-config.json "$IMAGE@$DIGEST"

cosign asks for the key password, then pushes the signature to the registry as an OCI referrer of the image: a separate small artifact that points at your digest. --yes skips confirmation prompts; you have already decided to sign.

For an image you publish, drop --signing-config and cosign uses the public Sigstore services it fetches through its signed TUF metadata. Do not run this against the practice image: it would write a permanent public Rekor entry for it. Replace the image name and digest with your own published image:

Bash
cosign sign --key cosign.key registry.example.com/team/app@sha256:<digest>

Check it worked

cosign tree lists the supply-chain artifacts attached to an image:

Bash
cosign tree "$IMAGE@$DIGEST"

You see something like a heading for the image followed by one signature bundle entry with its own sha256: digest. Before you signed, the tree was empty.

Step 5 · Verify the signature, then watch tampering fail

Verify with the public key. Because you signed without the transparency log, tell cosign not to look for a log entry with --insecure-ignore-tlog=true. The flag name is deliberately alarming: drop it once you sign with the public log.

Bash
cd ~/cosign-demo
cosign verify --key cosign.pub --insecure-ignore-tlog=true "$IMAGE@$DIGEST" > verify.json
jq . verify.json
grep -q "${DIGEST#sha256:}" verify.json
echo "signature covers $DIGEST"

cosign prints a warning about skipping the log, a list of the checks it ran, and a JSON document on standard output that names the digest it covers. The last line confirms the digest in that document is yours.

Now try two kinds of tampering. First, a key that is not yours. Generate a second key pair and verify with its public key. cosign asks for a password twice again; any throwaway password will do, because this key is deleted at cleanup:

Bash
cosign generate-key-pair --output-key-prefix stranger
if cosign verify --key stranger.pub --insecure-ignore-tlog=true "$IMAGE@$DIGEST" > /dev/null 2>&1; then
  echo "unexpected: a stranger's key verified"; false
else
  echo "rejected: not signed by stranger.pub"
fi

Second, a moved tag. Push a different BusyBox release over the 1.37.0 tag, the way a leaked push credential would, and verify by tag:

Bash
crane copy --platform linux/amd64 docker.io/library/busybox:1.36.1 "$IMAGE:1.37.0"
crane digest "$IMAGE:1.37.0"
if cosign verify --key cosign.pub --insecure-ignore-tlog=true "$IMAGE:1.37.0" > /dev/null 2>&1; then
  echo "unexpected: the moved tag verified"; false
else
  echo "rejected: the tag now points at an unsigned image"
fi
cosign verify --key cosign.pub --insecure-ignore-tlog=true "$IMAGE@$DIGEST" > /dev/null
echo "the original digest still verifies"

Check it worked

You see three outcomes in order: rejected: not signed by stranger.pub, a new digest from crane digest that differs from $DIGEST, then rejected: the tag now points at an unsigned image and the original digest still verifies. The signature belongs to the digest. Whatever the tag says, only those exact bytes pass. This is also why deployments should name images by digest; see Pin container images by digest.

Step 6 · Attach a signed SBOM attestation

A signature says "I vouch for this image". An attestation says "I vouch for this statement about this image": an SBOM listing what is inside it, a vulnerability scan, or build provenance. cosign wraps the statement in an in-toto envelope, signs it with the same key and stores it as another referrer.

Generate an SPDX SBOM for the signed digest with Syft. SYFT_REGISTRY_INSECURE_USE_HTTP=true lets Syft talk plain HTTP to the practice registry; leave it out for a real registry.

Bash
cd ~/cosign-demo
SYFT_REGISTRY_INSECURE_USE_HTTP=true syft scan "registry:$IMAGE@$DIGEST" -o spdx-json=sbom.spdx.json
jq -r '.spdxVersion, .name' sbom.spdx.json

Generate an SBOM for a container image with Syft covers the formats and what Syft finds. Attest it:

Bash
cosign attest --yes --key cosign.key --signing-config local-signing-config.json \
  --type spdxjson --predicate sbom.spdx.json "$IMAGE@$DIGEST"

Check it worked

Verify the attestation and read the statement back out of the envelope:

Bash
cosign verify-attestation --key cosign.pub --insecure-ignore-tlog=true \
  --type spdxjson "$IMAGE@$DIGEST" > attestation.json
jq -r '.payload' attestation.json | head -n 1 | base64 -d > statement.json
jq -r '.predicateType' statement.json
jq -r '.subject[0].digest.sha256' statement.json
test "$(jq -r '.predicateType' statement.json)" = "https://spdx.dev/Document"
test "$(jq -r '.subject[0].digest.sha256' statement.json)" = "${DIGEST#sha256:}"
echo "attestation is an SPDX SBOM for $DIGEST"
cosign tree "$IMAGE@$DIGEST"

The predicate type is https://spdx.dev/Document, the subject digest is the hex part of $DIGEST (the two test lines fail the block if either is wrong, so you see attestation is an SPDX SBOM for your digest), and cosign tree now lists two entries: the signature and the attestation. Anyone with cosign.pub can now fetch the SBOM and know it came from you, unchanged.

Step 7 · Sign keylessly from GitHub Actions

Keys have to be stored, rotated and revoked. Keyless signing removes the key: the workflow asks GitHub for an OIDC token, Sigstore's certificate authority, Fulcio, swaps it for a certificate valid for ten minutes that names the workflow, and cosign signs with a throwaway key and records the signature in Rekor. Verification then checks the identity, not a key.

This needs a GitHub repository with a Dockerfile; Your first GitHub Actions workflow covers the basics. Save this as .github/workflows/build-and-sign.yml:

YAML
name: build-and-sign

on:
  push:
    branches: [main]

permissions:
  contents: read
  packages: write # push to GitHub Container Registry
  id-token: write # let the job request an OIDC token for Sigstore

jobs:
  image:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7

      - uses: sigstore/cosign-installer@v4.1.4
        with:
          cosign-release: 'v3.1.6'

      - uses: docker/setup-buildx-action@v4

      - uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - id: build
        uses: docker/build-push-action@v7
        with:
          push: true
          tags: ghcr.io/${{ github.repository }}:${{ github.sha }}

      - name: Sign the pushed digest
        env:
          IMAGE: ghcr.io/${{ github.repository }}
          DIGEST: ${{ steps.build.outputs.digest }}
        run: cosign sign --yes "${IMAGE}@${DIGEST}"

The sign step uses the digest that build-push-action reports, never the tag. GitHub Container Registry wants lower-case names, so if your account or repository name has capitals, write the image name out in lower case in both places.

Check it worked

After the workflow runs on main, verify from your machine. Replace OWNER/REPO with your repository and DIGEST with the digest from the build step's log:

Bash
cosign verify \
  --certificate-identity "https://github.com/OWNER/REPO/.github/workflows/build-and-sign.yml@refs/heads/main" \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com \
  "ghcr.io/OWNER/REPO@DIGEST"

It passes only for signatures made by that workflow file on main. Change the identity to another branch and it fails, which is the point: a signature from a fork or a feature branch does not count.

Step 8 · Clean up

Stop the practice registry, remove its image and remove the demo folder. This deletes both key pairs, so copy cosign.pub and cosign.key somewhere safe first if you want to keep using them.

Bash
docker rm -f cosign-demo-registry
docker rmi registry:3.1.2
Bash
cd ~
rm -rf ~/cosign-demo
unset IMAGE DIGEST

cosign, crane and Syft stay installed for the related tutorials. To remove them too, run sudo rm /usr/local/bin/cosign /usr/local/bin/crane /usr/local/bin/syft.

Check it worked

curl -s http://localhost:5000/v2/ now fails to connect, docker image ls registry lists nothing, and ls ~/cosign-demo reports that the folder does not exist.

Next steps

Signing only protects you if something refuses unsigned images. In Kubernetes that is an admission controller: Kyverno can verify cosign signatures and attestations before a pod is admitted, and the Sigstore policy-controller does the same with a ClusterImagePolicy. Start either in audit mode, check which running images would fail, then switch to enforce.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.