Signing releases
A signing key only protected tags can use, and what a branch job gets.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/sign.tar.gz && tar -xzf sign.tar.gz, which creates ~/lab/sign/. SHA-256: 2ea730edc570c7ccd95bbb3e86b7671b220b46e2dd9bf3abfed579b17d2fb06cA signing key in CI is the most valuable secret the pipeline holds, because customers trust whatever it signs. So the question isn't only how to sign, but which jobs can sign at all. In GitLab the answer can be narrow. Protected variables go to every job on every protected ref, main included, so protection alone isn't enough. Scoping the key to an environment that only the sign job uses leaves exactly one job that receives it, and that job runs only for release tags, which only Maintainers can create.
A key only the sign job receives
The job image adds Cosign 3.1.4 to the lab's Python image, copying the binary from the official Cosign image pinned by digest:
# Job image with Cosign: the binary comes from the official Cosign image, pinned by digest.FROM ghcr.io/sigstore/cosign/cosign:v3.1.4@sha256:0f06154836d2de34f887a5c273506d52ca43edaa1d9e53e6510fa78a48f69c2d AS cosignFROM secopslog-py:3.15COPY --from=cosign /ko-app/cosign /usr/local/bin/cosign
Generate a key pair outside GitLab, store the private key as a protected file variable and its password as a protected masked variable, both scoped to the environment release, protect the v* tags, and commit only the public key:
Nothing secret is left on disk. A private key can't be masked, since it has several lines, which is one more reason it must be protected and scoped. File type also means the key never appears in a job's environment as text: the job gets a path to a temporary file.
# Every pipeline builds. Only a v* tag (protected, so only maintainers can create one) signs.# The key variables are protected and scoped to the "release" environment: only the sign job# of a pipeline on a protected ref receives them.stages: [build, sign, verify]build:stage: buildimage: secopslog-py:3.15script:- mkdir -p dist- tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz calc.pyartifacts:paths: [dist/]sign:stage: signimage: secopslog-cosign:3.1.4needs: [build]rules:- if: $CI_COMMIT_TAG =~ /^v/environment: # the key variables are scoped to "release", so only this job gets themname: releaseaction: preparevariables:GIT_DEPTH: 0script:# sign only commits that are already on main, whoever made the tag- git fetch -q origin +refs/heads/main:refs/remotes/origin/main- git merge-base --is-ancestor "$CI_COMMIT_SHA" origin/main && echo "$CI_COMMIT_TAG is on main"- cosign signing-config create --out signing.json # no transparency log: keeps the lab offline- cosign sign-blob --yes --key "$COSIGN_KEY" --signing-config signing.json --bundle dist/app.tar.gz.sigstore.json dist/app.tar.gzartifacts:paths: [dist/]verify:stage: verifyimage: secopslog-cosign:3.1.4needs: [sign]rules:- if: $CI_COMMIT_TAG =~ /^v/script:- cosign verify-blob --key cosign.pub --insecure-ignore-tlog=true --bundle dist/app.tar.gz.sigstore.json dist/app.tar.gz
On main, only build ran: the signing jobs' rules require a v tag. main is a protected ref, though, so without the environment scope its build job would have received the key too, and so would every job in a tag pipeline, including build, which in a real project runs your dependencies' install scripts. sign declares environment: release with action: prepare, which gives it the scoped variables without recording a deployment. Cosign reads the key password from COSIGN_PASSWORD in the environment, so the script never mentions it. Before signing, the job also checks that the tagged commit is already on main: a protected tag limits who creates tags, not which commit they point at.
A release tag signs
The tag pipeline ran all three jobs. build made the tarball, sign confirmed v1.0.0 is on main, received the key file and its password, wrote a Sigstore bundle next to the tarball, and verify checked it with the committed public key: Verified OK. The fetch step did the same check from outside GitLab, the way a customer would, with the tarball and bundle downloaded from the tag's artifacts.
cosign signing-config create with no services means nothing is sent to a public transparency log or timestamp service. That keeps this lab offline, and it has a cost, which is why verify printed a warning and needed --insecure-ignore-tlog=true. Without a log or a signed timestamp, nothing proves when a signature was made, so if the key leaks, old signatures and forged ones look the same, and the only remedy is a new key and re-signing every release. In production, use a transparency log (Sigstore's public Rekor or your own) or at least a timestamp authority, and have a written plan for rotating the key.
The verify job is a sanity check, not proof for customers: it uses cosign.pub from the same commit, and whoever can change the commit can change the key. Customers must get the public key from somewhere an attacker can't also change, such as your website over TLS or a key built into the deployer. Publish releases in GitLab's Releases or package registry rather than as job artifacts, which expire.
A developer tries to sign from a branch
dev can push branches, so dev can write a job that tries to use the key:
# A developer's attempt to sign from a branch. The empty signing config keeps the lab# offline: without it, cosign falls back to keyless signing with public Sigstore.sign-anyway:image: secopslog-cosign:3.1.4script:- echo "COSIGN_KEY='$COSIGN_KEY' COSIGN_PASSWORD length ${#COSIGN_PASSWORD}"- echo test > x.txt- cosign signing-config create --out signing.json- cosign sign-blob --yes --key "$COSIGN_KEY" --signing-config signing.json --bundle x.sigstore.json x.txt
The branch pipeline got nothing: COSIGN_KEY was empty and the password had length 0. With no key, Cosign fell back to keyless signing (Generating ephemeral keys...), which the empty signing config then stopped, because it lists no identity service. Without that config, the job would have tried public keyless signing, which needs an interactive login that dev would control. That signature would never verify against your public key, but it's one more reason to verify with the key, not merely to check that a signature exists.
| Layer | Here | If it were missing |
|---|---|---|
Protected v* tags, Maintainers only | Only Maintainers create release tags | If Developers could create v* tags, any Developer could start a pipeline that receives the key |
Key scoped to release | Only the sign job receives it | Every job on main and in tag pipelines would hold the key |
Tag must be on main | The job refuses an unmerged commit | A Maintainer, or a stolen Maintainer token, could sign an unreviewed branch |
| Protected key variables | Only protected refs receive the key | Any branch job could sign anything |
| Rules on the sign job | The job exists only in tag pipelines | Nothing important: rules organise, protection secures |
| Key not in GitLab at all (KMS) | Not in this lab | A Maintainer can still read or replace the key variable |
Hands-on
The fetch step left the v1.0.0 tarball, its bundle and the public key in out/. Append one byte with printf x >> out/app.tar.gz and run the docker run ... cosign verify-blob command from the fetch step again. Then switch back with git -C app checkout main (the dev step left app/ on feature/sign), create the tag v1.0.1 on main as root, the way the tag step did, download its bundle, and compare the two bundles' messageSignature: the messageDigest.digest and the signature fields (jq .messageSignature). Are they the same? Finish with ./down.sh.
What you should see: after one extra byte, verification fails with invalid signature. For v1.0.1, the bundle records the same digest as v1.0.0, KGgq5LIe..., because the tarball is built reproducibly from the same commit, but the signature itself differs (MEUCIQDZTO/... and MEUCIQDw+C5...). ECDSA signatures include a random value, so signing the same bytes twice gives two different, equally valid signatures. Compare digests to see whether two releases contain the same thing, not signatures.
Remember
Sign only in pipelines for protected tags, keep the key in protected variables or a KMS, publish the public key by a separate path, and test from a Developer account that a branch job gets nothing.
cosign sign-blob --key "$COSIGN_KEY". What happens?