Sub topic 10
Using KMS-backed keys instead of local key files
This chapter assumes you can already sign and verify an image with a local cosign.key and cosign.pub pair. It also assumes you can read a short IAM policy and have used a CI system such as GitHub Actions. The examples use AWS KMS. The ideas apply equally to Google Cloud KMS, Azure Key Vault and HashiCorp Vault.
Why local key files fall short
A cosign.key file is an encrypted private key. It works, but it has weaknesses once more than one person or pipeline needs to sign:
- Anyone who holds the file and its password can sign as your team. You cannot tell who used it.
- The file gets copied to laptops, CI secret stores and backups. Each copy is another place to leak it.
- Rotating the key means redistributing a new file to every signer.
- The password sits next to the file in most CI setups, so the encryption adds little.
A key management service (KMS) removes the file. The private key is created inside the service and never leaves it. Cosign sends the data to be signed to the KMS, and the KMS returns a signature. Access is controlled by IAM policy and recorded in an audit log such as AWS CloudTrail.
The working model
With a KMS key, only the location of the private key changes. The signature format, the storage in the registry and the verification logic stay the same.
Verification does not need KMS access if you export the public key. Anyone with cosign.pub can verify offline from the signing service. Cosign can also fetch the public key from the KMS at verification time, which requires kms:GetPublicKey.
KMS key URIs
Cosign selects a KMS key through a URI passed to --key. The scheme names the provider.
| Provider | URI format |
|---|---|
| AWS KMS | `awskms:///alias/NAME`, `awskms:///KEY_ID` or `awskms:///KEY_ARN` |
| Google Cloud KMS | `gcpkms://projects/P/locations/L/keyRings/R/cryptoKeys/K/versions/V` |
| Azure Key Vault | `azurekms://VAULT_NAME.vault.azure.net/KEY_NAME` |
| HashiCorp Vault | `hashivault://KEY_NAME` |
The AWS URI has three slashes because the part between the second and third slash is an optional custom endpoint. Leaving it empty uses the default regional endpoint.
Creating an AWS KMS signing key
Cosign needs an asymmetric key with the SIGN_VERIFY usage. The usual choice is the elliptic curve NIST P-256, which matches the algorithm Cosign uses for its own generated keys.
# Create an asymmetric signing key (ECDSA P-256)
KEY_ID=$(aws kms create-key \
--key-spec ECC_NIST_P256 \
--key-usage SIGN_VERIFY \
--description "cosign image signing key" \
--query KeyMetadata.KeyId \
--output text)
# Give it a stable, readable name
aws kms create-alias \
--alias-name alias/cosign-images \
--target-key-id "$KEY_ID"
# Export the public key in the format Cosign verifies with
cosign public-key --key awskms:///alias/cosign-images > cosign.pubYou can also run cosign generate-key-pair --kms awskms:///alias/cosign-images. That command creates the key and writes cosign.pub. The explicit aws kms commands above give you control over the key spec, tags and key policy.
Signing and verifying
The signing and verification commands look like the key-based flow from the earlier chapter. Only the --key value changes.
# Resolve the digest first; sign and verify by digest, not by tag
IMAGE=registry.example.com/team/app@sha256:<digest>
# Sign: the payload is sent to KMS, the private key never leaves it
cosign sign --key awskms:///alias/cosign-images "$IMAGE"
# Verify with the exported public key (no AWS access needed)
cosign verify --key cosign.pub "$IMAGE"
# Or verify through KMS (needs kms:GetPublicKey)
cosign verify --key awskms:///alias/cosign-images "$IMAGE"Attestations use the same flag, for example cosign attest --key awskms:///alias/cosign-images --type spdxjson --predicate sbom.spdx.json "$IMAGE".
Least-privilege IAM permissions
Give signers and verifiers separate permissions. Signing needs kms:Sign. Fetching the public key needs kms:GetPublicKey. Cosign may also call kms:DescribeKey to check the key. A policy for a signing role looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CosignSign",
"Effect": "Allow",
"Action": [
"kms:Sign",
"kms:GetPublicKey",
"kms:DescribeKey"
],
"Resource": "arn:aws:kms:eu-west-1:111122223333:key/KEY_ID"
}
]
}Scope Resource to the key ARN. A policy on an alias ARN does not grant access to the key, because IAM checks the key resource itself. Verifiers that use the exported cosign.pub need no KMS permissions at all, which keeps the number of principals who can touch the key small.
Signing from CI without stored credentials
A KMS key removes the key file but not the credential problem. If the pipeline stores long-lived AWS access keys, you have moved the secret rather than removed it. The better pattern is to let the CI job assume an IAM role through OIDC. The role is trusted for a specific repository and branch, and it receives short-lived credentials.
name: build-and-sign-kms
on:
push:
branches: [main]
permissions:
contents: read
packages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
id: build
with:
push: true
tags: ghcr.io/${{ github.repository }}:latest
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::111122223333:role/cosign-signer
aws-region: eu-west-1
- uses: sigstore/cosign-installer@v3
- name: Sign the image by digest with KMS
run: cosign sign --yes --key awskms:///alias/cosign-images ghcr.io/${{ github.repository }}@${{ steps.build.outputs.digest }}The cosign-signer role's trust policy should require the GitHub OIDC token's sub claim to match your repository and branch. Without that condition, any workflow in your account's GitHub organization could assume the role.
Hardware-backed keys
KMS is not the only way to keep a key out of a file. Cosign also supports hardware tokens, such as a YubiKey over PIV or a PKCS#11 device, through the --sk flag and cosign piv-tool. The key lives on the device and signing needs physical presence, usually a touch. This suits individual maintainers and release managers. It does not suit CI, because a pipeline cannot touch a button. Cloud KMS suits automation, and hardware tokens suit people.
AWS KMS keys are protected by hardware security modules (HSMs) validated under FIPS 140. Using a KMS key therefore also gives you HSM-backed storage with no extra setup.
Choosing between local keys, KMS and keyless
| Situation | Best fit | Reason |
|---|---|---|
| Local experiments, demos | Local key pair | Fast, no cloud account needed |
| Production in one cloud, central control and audit | KMS key | IAM access control, audit log, HSM storage |
| Open source or public images, GitHub Actions | Keyless | No key to manage, identity recorded in Rekor |
| Air-gapped or offline verification | KMS or local key with exported `cosign.pub` | Verification needs no network access to Fulcio or Rekor |
| Individual maintainer release signing | Hardware token | Private key never exported, touch required |
KMS and keyless are not exclusive. Some teams sign with keyless in CI and keep a KMS key for release images that must be verified in disconnected clusters.
Common errors and gotchas
- Wrong key usage. A key created with
ENCRYPT_DECRYPTusage cannot sign. Cosign fails with an error from KMS about an invalid key usage. Create a new key with--key-usage SIGN_VERIFY. You cannot change the usage of an existing key. - Wrong region. An alias exists in one region only. If
AWS_REGIONpoints elsewhere, Cosign reports that the alias or key was not found. Set the region explicitly or encode it in the ARN. AccessDeniedException. The caller lackskms:Signorkms:GetPublicKey, or the key policy does not trust the IAM principal. KMS needs both the key policy and IAM policy to allow the action, unless the key policy delegates to IAM.- Alias retargeting. An alias can be repointed to a different key. If verifiers use
awskms:///alias/cosign-images, an attacker withkms:UpdateAliascan swap the key behind it. Distributecosign.pub, or verify with the full key ARN, so verification does not depend on a mutable name. - No automatic rotation. AWS does not support automatic rotation for asymmetric keys. Rotation means creating a new key and updating the verification side. The old public key must stay trusted until every image signed with it has been replaced or retired.
- Cost and quotas. Each signature is a KMS API call, billed per request and subject to rate limits. Signing a few images per build is trivial. Signing thousands in parallel can throttle.
- Deleting the key. Scheduled key deletion is irreversible after the waiting period. Signatures made with a deleted key still verify if you kept
cosign.pub. If you did not, you lose the ability to verify them.
Summary of the trade-off
A KMS key gives you central access control, audit logging and HSM-backed storage, and it removes the private key from developer machines and CI secret stores. In return you depend on a cloud provider, pay per request, and must manage IAM carefully. Your pipeline still needs credentials to reach the KMS, so use OIDC role assumption rather than stored access keys.
Exercise
A team signs images in CI with cosign sign --key awskms:///alias/cosign-images, and clusters verify with the same alias URI. A colleague points out a risk in the verification step. Name the risk and describe one change that removes it.
Show answer
An alias is a mutable name. Anyone with kms:UpdateAlias can point alias/cosign-images at a different key that they control. Images signed with that key would then pass verification. To remove the risk, verify with the exported public key file cosign.pub, or with the full key ARN (awskms:///arn:aws:kms:REGION:ACCOUNT:key/KEY_ID), and restrict kms:UpdateAlias to administrators.
Exercise
Cosign fails to sign in CI with a KMS error saying the key was not found, although the alias works on your laptop. List two likely causes and how you would check each.
Show answer
First, the region differs. The alias exists only in the region where it was created, so check that the job's aws-region matches, for example by running aws kms describe-key --key-id alias/cosign-images --region REGION. Second, the job assumed a different AWS account or role than you expected. Check with aws sts get-caller-identity in the job, and confirm the role is allowed kms:DescribeKey and kms:Sign on the key. A missing permission can also appear as a not-found or access-denied error.