Sub topic 2
Tags are mutable, digests are not
This chapter covers why a signature must point at an image digest and not at a tag. It assumes you can build, tag, push and pull images, know what a container registry is, and are comfortable at the command line. The previous chapter explained what signing is meant to prove. Here you learn what a signature can be attached to.
What a tag really is
A tag is a human-readable label that a registry maps to an image. The label is a pointer, and whoever has push access to the repository can move it. Pushing app:1.0.0 twice with different content leaves one tag and two different images in the history. The tag now points at the second one.
This is how tags are designed to work, and often it is useful. latest and stable are meant to move. The trouble starts when people treat a version-looking tag such as 1.0.0 as a promise that the content never changes. The registry does not enforce that promise unless you turn on tag immutability, which is a per-repository setting on some registries (Amazon ECR and Harbor have it, and Docker Hub does not offer it for ordinary repositories).
Consider what a tag-based signature would mean. If you signed "whatever app:1.0.0 points at right now", the claim would stay true only until someone re-pushed the tag. A later pull of app:1.0.0 would return different bytes, and nothing about the signature would tell you.
What a digest is
A digest is a cryptographic hash of the image manifest. The manifest is a small JSON document that lists the image configuration and every layer, each by its own digest. A digest looks like this:
sha256:3b1c0f6a9d2e4f57a8c1d0e9b7a6f5e4d3c2b1a09f8e7d6c5b4a39281706f5e4Because the manifest contains the digest of each layer, the manifest digest covers the whole image. Change one byte in one layer and the layer digest changes. The manifest then changes, and so does the manifest digest. You cannot produce a different image with the same digest unless you can find a SHA-256 collision, which is not practical today.
A registry stores content under its digest. Asking for app@sha256:... returns exactly the content that hashes to that value, and the client can check this by hashing what it received. That makes a digest a content address, so it cannot be moved or re-pointed.
| Property | Tag (`app:1.0.0`) | Digest (`app@sha256:...`) |
|---|---|---|
| Chosen by | A person or a build script | Computed from the content |
| Can point at different content later | Yes | No |
| Readable by humans | Yes | No |
| Verifiable by the client | No | Yes, by hashing the manifest |
| Safe as a signature target | No | Yes |
Why signatures must reference digests
A signature is a statement that a specific signer vouches for specific bytes. For that statement to mean anything, the "specific bytes" part must be fixed. A digest fixes it. Cosign signs a small payload that contains the digest of the image manifest, so the signature stays valid only for that content.
Cosign accepts a tag on the command line, but it resolves the tag to a digest before signing and then signs the digest. Newer Cosign releases print a warning when you pass a tag, because the resolution happens at one moment and you may not have meant that moment. The following scenario shows the risk:
- Your pipeline pushes
app:1.0.0and a later step signsapp:1.0.0. - Between the push and the sign step, another process re-pushes
app:1.0.0with different content. - Cosign resolves the tag, gets the new digest, and signs it.
You now hold a valid signature over content you never reviewed. The signature is not wrong. It faithfully covers the digest it was given. The mistake was in how you chose that digest.
The same gap exists at verification time. If you run cosign verify against a tag, Cosign resolves the tag to a digest and checks signatures for that digest. If the tag moved, you verify the new content, which may have a valid signature from someone else who signed it. Verifying by digest asks the question you actually care about: "is this exact content signed by the signer I expect?"
Getting the digest
The right place to get the digest is the step that pushes the image, because that step knows exactly what it uploaded. You can also read it from the registry afterward.
# Resolve the digest of the pushed image
docker buildx imagetools inspect registry.example.com/team/app:1.0.0 --format '{{json .Manifest.Digest}}'
# Always sign and verify by digest, not by tag
IMAGE=registry.example.com/team/app@sha256:<digest>Replace <digest> with the value printed by the first command. The output of that command is a JSON string with quotes, so strip them before putting the value into a variable.
Reading the digest from the registry after the fact has the same race described above. If the tag can move between the push and the lookup, you can read the wrong digest. Where you can, take the digest from the push itself. docker buildx build --push --metadata-file meta.json writes the pushed digest to a file under the key containerimage.digest. In GitHub Actions, docker/build-push-action exposes it as the output digest, which a later chapter uses.
You can also read it locally from an image you have pulled:
docker image inspect --format '{{index .RepoDigests 0}}' registry.example.com/team/app:1.0.0This prints a full reference of the form registry.example.com/team/app@sha256:..., ready to use as IMAGE. If the image was built locally and never pushed, RepoDigests is empty and the command fails with an index error. Push first, then read the digest.
Multi-architecture images
Many tags point at an image index (also called a manifest list). The index lists one manifest per platform, such as linux/amd64 and linux/arm64. This creates two kinds of digest:
- The index digest identifies the list as a whole.
docker buildx imagetools inspectreports this one. - Each platform manifest has its own digest.
When you sign the index digest, the signature covers the index, and through its content-addressed entries, every platform image it lists. This is usually what you want, because users pull the index and the runtime picks the platform. Signing only one platform manifest leaves the others unsigned, and a verifier that checks the index digest will find no signature. Decide which level you sign, and verify at the same level that your deployments reference.
Common errors
Signing a tag in a pipeline. The race above is rare, but it is cheap to remove. Always pass repo@sha256:... to cosign sign.
Deploying by tag after signing by digest. You signed app@sha256:aaa..., but your Kubernetes manifest says image: app:1.0.0. The cluster pulls whatever the tag points at when the pod starts. Pin the deployment to the digest too, or use an admission policy (covered in a later chapter) that resolves and verifies tags for you.
Comparing the wrong digest. The digest of a local image ID (docker images --digests shows a different value from docker image inspect --format '{{.Id}}') is the config hash, not the manifest digest. Cosign needs the manifest digest, the one the registry reports.
Re-pushing after signing. A signature is stored in the registry next to the image, as the next chapters explain. If you rebuild and push the same tag, the tag moves to a new digest that has no signature. Verification of the tag then fails, even though the old digest is still signed. This failure is correct behavior. It tells you the new content has not been signed.
Relying on tag immutability alone. Immutable tags stop the tag from moving, but a digest is still the better reference. Immutability is a registry setting that an administrator can turn off, and a signature bound to a digest does not depend on it.
Summary of the working model
- A tag is a movable pointer. A digest is a hash of the manifest and identifies content.
- A signature binds a signer to a digest. A signature over a tag does not exist in practice, because Cosign resolves the tag first.
- Take the digest from the push step, sign it, and verify and deploy using the same digest.
- For multi-architecture images, sign the index digest unless you have a reason to sign individual platform manifests.
Exercise
Your CI job runs docker push app:2.1.0 and then, a minute later, cosign sign app:2.1.0. Explain what could go wrong, and write the change that removes the problem.
Show answer
Cosign resolves the tag to a digest at the moment it runs. If another push moved app:2.1.0 in the minute between the two commands, Cosign signs the digest of the new content, which this job never built or reviewed. The fix is to capture the digest from the push step and sign that, for example cosign sign registry.example.com/team/app@sha256:<digest> with the digest taken from the build output (the digest output of docker/build-push-action, or containerimage.digest in the file written by --metadata-file). Verify and deploy using the same digest.
Exercise
A teammate says "we turned on tag immutability in the registry, so signing by tag is now safe." Give one reason to keep signing and verifying by digest anyway.
Show answer
Immutability is a registry setting that an administrator can change, and not every registry supports it. A digest is derived from the content, so a signature over a digest stays correct whatever the registry configuration is. Digest references also remove any dependence on when Cosign resolves the tag.