Sign a container image¶
Sign an image you have already pushed, so a consumer can prove it came from you and has not been altered. The signature binds the image digest, so moving a tag onto that digest later needs no fresh signature.
Image signing lives in its own module,
gitlab.com/phpboyscout/go/signing-sigstore,
so a binary that only signs release files never links a registry client.
Get a signer¶
SignImage takes any crypto.Signer, so a signer from any backend works. The
key decides the algorithm:
| Key | Signs | Who verifies it |
|---|---|---|
| ECDSA P-256 | a SHA-256 digest, ASN.1 DER signature | every verifier surveyed for spec 0001, which records the conclusion in D5; use this |
| Ed25519 | the whole payload, pure Ed25519 | most of those verifiers, but not Argo CD Image Updater or Ratify's older key setting |
| RSA 2048, 3072 or 4096 | a SHA-256 digest, PKCS#1 v1.5 | cosign; other verifiers vary, so check yours |
Any other key, including ECDSA on another curve, is refused with
sigstore.ErrUnsupportedKey before the registry is touched.
For production, use an ECC_NIST_P256 key in AWS KMS through the
signing-aws-kms backend.
Export its public key for verifiers. The examples on this page use
eu-west-2, so use your own key's region:
aws kms get-public-key --region eu-west-2 --key-id alias/app-image \
--query PublicKey --output text | base64 -d \
| openssl pkey -pubin -inform DER -out image.pub
For a test or a local registry, the local backend
reads an unencrypted PKCS#8 P-256 key:
openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out image.key
openssl pkey -in image.key -pubout -out image.pub
Sign the digest¶
import (
"github.com/google/go-containerregistry/pkg/authn"
sigstore "gitlab.com/phpboyscout/go/signing-sigstore"
)
sig, err := sigstore.SignImage(ctx,
"registry.gitlab.com/group/project/app@sha256:<hex>",
signer,
sigstore.WithAuthenticator(&authn.Basic{Username: user, Password: token}),
)
if err != nil {
return err
}
fmt.Println(sig.SignatureTag) // .../app:sha256-<hex>.sig
fmt.Println(sig.Bundle) // .../app@sha256:<bundle manifest digest>
Pass a digest, not a tag. A tag reference is refused with
sigstore.ErrNotDigest, because signing what a tag points at now races
whoever moves the tag next. For a multi-arch image, sign the index digest the
tag resolves to, as cosign does.
Without WithAuthenticator or WithKeychain, the registry is reached
anonymously. sigstore.WithKeychain(authn.DefaultKeychain) reads the Docker
config that docker login writes.
One call writes both cosign forms, described under The container image signatures. Both signatures are made, and checked against the signer's public key, before either is pushed.
The legacy signature is pushed first. If the bundle push then fails, call
SignImage again: with the same key it leaves the legacy signature alone and
adds the bundle referrer. Every successful call adds one bundle referrer.
Sign a digest from one job at a time. Adding a legacy signature is a read and then a write, so two signers running at once on one digest can lose one of them.
From the command line¶
sigillum has sign image from v0.6.0,
with the same refusals:
echo "$CI_REGISTRY_PASSWORD" | sigillum sign image \
--backend aws-kms --kms-region eu-west-2 --key-id alias/app-image \
--registry-username "$CI_REGISTRY_USER" --registry-password-stdin \
registry.gitlab.com/group/project/app@sha256:<hex>
The password is read from stdin so it never appears in a process list. Without
--registry-username, the Docker config is used. sigillum keys generate-image
writes a local P-256 keypair as image.key and image.pub.
Verify it¶
Nothing is uploaded to a transparency log, so a verifier checks the published public key with the log check turned off:
# either form
cosign verify --key image.pub --insecure-ignore-tlog registry.gitlab.com/group/project/app@sha256:<hex>
# the legacy form only
cosign verify --key image.pub --insecure-ignore-tlog --new-bundle-format=false registry.gitlab.com/group/project/app@sha256:<hex>
The first command prefers the bundle and falls back to the legacy signature, so a pass does not prove the bundle is there.
This page covers cosign only. An admission controller or a node-level verifier has its own setting for skipping the log check, so follow its documentation.
cosign 3 warns that --new-bundle-format is deprecated. Verify the legacy form
only if a verifier of yours still reads it.
A signature does not bind the repository, so an image signed with a key passes in any repository that trusts that key. Use one key per image repository.
Keep the signature tags¶
A registry cleanup policy treats sha256-<hex>.sig as an ordinary tag and
deletes it once its age and count rules match. A verifier that reads only the
legacy form then fails on that digest. Keep the tags with a keep regex such as:
That also keeps the sha256-<hex> fallback index, which lists the bundle on a
registry without the referrers API.
Related¶
- The container image signatures.
- Sign with AWS KMS.
- cosign, which the module's tests verify every key type and form against.
- API reference: pkg.go.dev/gitlab.com/phpboyscout/go/signing-sigstore.