Sub topic 2
Tags are mutable, digests are not
This chapter assumes you can build, tag, push and pull images, and that you know what a container registry is. You don't need Cosign yet. The goal here is to understand what a signature must point at, so that later chapters make sense.
Why a tag is a weak identifier
A tag such as app:1.0.0 is a label that a registry maps to an image. Anyone with push access to the repository can move that label to different content at any time. Nothing in the name tells you it moved.
This causes real problems:
- A re-push of
app:1.0.0silently replaces the image behind it. - A compromised CI job or stolen registry credential can retarget
latestto a malicious build. - Two machines pulling the same tag a minute apart can run different code.
If a signature were attached to a tag, it would say nothing about which bytes you actually run. The tag can change while the signature still looks attached.
What a digest is
A digest is a cryptographic hash of the image manifest, written as sha256: followed by 64 hex characters. The manifest is a small JSON document that lists the image config and each layer, and each of those is itself referenced by its own digest.
This gives a chain of content addressing:
- Changing a layer changes that layer's digest.
- The manifest lists the layer digest, so the manifest changes too.
- The manifest digest changes, so the image has a new identity.
A digest therefore names exactly one piece of content. If the content differs, the digest differs.
Resolving the digest
After you push, ask the registry which digest the tag points to right now:
# 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 @sha256:... form is a digest reference. Registries return the same content for it every time, or an error if the content is gone.
You can also read the digest from the build step. docker buildx build --push --metadata-file meta.json writes it to the containerimage.digest field, and docker/build-push-action exposes it as the digest output. In CI, prefer these over resolving a tag afterward, because the tag may already have moved.
Why signatures must reference digests
A signature is only meaningful if it covers a specific, unchangeable thing. Cosign handles this for you. If you pass a tag, it resolves the tag to a digest and signs that digest. It also prints a warning that signing by tag is risky, because the tag could have moved between your build and the signing step.
| Reference | Example | Stable? | Safe to sign or verify? |
|---|---|---|---|
| Tag | `app:1.0.0` | No, can be repointed | Risky |
| Digest | `app@sha256:...` | Yes, tied to content | Yes |
| Tag plus digest | `app:1.0.0@sha256:...` | Digest wins, tag is a hint | Yes, but the tag is ignored |
Common mistakes
- Deploying a tag in a Kubernetes manifest after verifying a digest. The cluster pulls what the tag points to, which may differ from what you checked. Deploy the digest.
- Treating a "version" tag as immutable. Some registries offer tag immutability settings, but they are per-repository configuration and not a guarantee you can assume elsewhere.
- Copying an image between registries with a tool that rewrites the manifest. A different manifest means a different digest, and the original signature will not match.
Exercise
A teammate runs cosign verify against registry.example.com/team/app:latest, sees success, and then deploys app:latest to production. Why is this still unsafe, and what should they do instead?
Show answer
The tag can be repointed between the verification and the pull, so the deployed image may not be the one that was verified. They should resolve latest to a digest once, verify that digest reference (app@sha256:...), and deploy the same digest reference.
Exercise
If you change one byte in one layer of an image, which digests change?
Show answer
The layer's digest changes. Because the manifest lists that digest, the manifest changes and its digest changes too. The image then has a different identity, so a signature made for the old manifest digest does not apply to it.