Sub topic 10
Using KMS-backed keys instead of local key files
This chapter assumes you can sign and verify an image with a Cosign key pair, and that you know what a cloud account, an IAM role, and a registry are. If cosign generate-key-pair is new to you, review the key-based signing chapter first.
Why move the key out of a file
A cosign.key file is an encrypted private key on disk. It is easy to start with, but it has costs:
- Anyone who copies the file and guesses or steals the password can sign as you.
- The file must be stored somewhere CI can read it, usually as a secret.
- Rotating it means regenerating the pair and distributing a new public key.
A key management service (KMS) holds the private key and never releases it. Cosign sends the data to be signed to the service, and the service returns the signature. Access is controlled by IAM permissions, and every use appears in the audit log. A hardware security module (HSM) goes one step further: the key lives in tamper-resistant hardware, and some KMS tiers are backed by one.
Key reference URIs
Cosign selects a provider from the prefix of the --key value.
| Provider | Example reference |
|---|---|
| AWS KMS | `awskms:///alias/image-signing` |
| Google Cloud KMS | `gcpkms://projects/P/locations/L/keyRings/R/cryptoKeys/K` |
| Azure Key Vault | `azurekms://VAULT.vault.azure.net/KEYNAME` |
| HashiCorp Vault | `hashivault://keyname` |
| PKCS#11 token | `pkcs11:` URI, or `cosign sign --sk` for a FIDO/YubiKey |
AWS references can use a key ID, key ARN, alias name, or alias ARN. The leading /// means "use the default region from the environment".
Worked example with AWS KMS
Cosign can create the key in KMS for you. The key is an asymmetric ECDSA P-256 key with the SIGN_VERIFY usage.
export AWS_REGION=eu-west-1
# Create the key in KMS (needs kms:CreateKey and kms:CreateAlias)
cosign generate-key-pair --kms awskms:///alias/image-signing
# Export the public key so others can verify without AWS access
cosign public-key --key awskms:///alias/image-signing > cosign.pub
# Sign by digest. Cosign calls kms:Sign; the private key never leaves AWS
cosign sign --key awskms:///alias/image-signing "$IMAGE"
# Verify using KMS, or using the exported public key
cosign verify --key awskms:///alias/image-signing "$IMAGE"
cosign verify --key cosign.pub "$IMAGE"Here $IMAGE is the digest reference from earlier chapters, for example registry.example.com/team/app@sha256:<digest>.
Signing needs kms:Sign and kms:GetPublicKey on the key. Verifying through KMS needs kms:GetPublicKey, and Cosign also uses kms:DescribeKey. Verifiers that use the exported cosign.pub need no cloud permissions at all, which is usually the better choice for consumers.
Choosing between local, KMS, and keyless
| Criterion | Local key file | KMS or HSM key | Keyless |
|---|---|---|---|
| Long-lived secret to protect | Yes, file and password | No file, access via IAM | None |
| Works offline or air-gapped | Yes | Needs reachable KMS | Needs Fulcio, OIDC, Rekor |
| Verifier needs | Public key | Public key | Identity and issuer |
| Audit trail | Manual | Cloud audit logs | Rekor log |
| Rotation | Manual, new public key | Key versions or new alias | Automatic, certificates are short-lived |
Use a local key for experiments or isolated environments. Use KMS when you need a stable, long-lived public key, such as for air-gapped clusters or policies that pin a key. Use keyless when your CI has an OIDC identity and you can reach the public Sigstore services.
Common errors
AccessDeniedExceptiononkms:Sign: the role lacks permission on that key, or the key policy blocks it.- Wrong region: a bare alias is resolved in the region from
AWS_REGION, so the key looks missing in another region. - Unsupported key spec: symmetric keys cannot sign. Create an asymmetric signing key.
- Verification fails after rotation: an alias now points to a new key, but old signatures were made with the old key. Verify old images with the old public key.
Exercise
Your team signs images with an AWS KMS key and runs clusters in an account that has no access to that KMS key. How can the clusters still verify signatures, and which permission does the signing role need?
Show answer
Export the public key with cosign public-key --key awskms:///alias/image-signing > cosign.pub and give it to the clusters, which verify with cosign verify --key cosign.pub. The signing role needs kms:Sign and kms:GetPublicKey on the key.