Signing releases

A signing key only protected tags can use, and what a branch job gets.

Advanced14 min · lesson 16 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (18 files, 7 KB): sign.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: 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: 2ea730edc570c7ccd95bbb3e86b7671b220b46e2dd9bf3abfed579b17d2fb06c

A 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:

cosign/Dockerfile
# 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 cosign
FROM secopslog-py:3.15
COPY --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:

ubuntu@secopslog-cicd:~/lab/sign · GitLab 19.5.3
$ ./up.sh && docker build -q -t secopslog-cosign:3.1.4 cosign >/dev/null && docker run --rm secopslog-cosign:3.1.4 cosign version 2>/dev/null | grep GitVersion
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929 GitVersion: v3.1.4
$ mkdir -p keys out && openssl rand -hex 24 > keys/password && docker run --rm -e COSIGN_PASSWORD="$(cat keys/password)" -v "$PWD/keys:/keys" -w /keys --user "$(id -u):$(id -g)" secopslog-cosign:3.1.4 cosign generate-key-pair >/dev/null 2>&1; ls keys
cosign.key cosign.pub password
$ ./seed.sh app app/ >/dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=COSIGN_KEY -d variable_type=file -d protected=true -d environment_scope=release --data-urlencode value@keys/cosign.key -o /dev/null && ./gl.sh /projects/root%2Fapp/variables -X POST -d key=COSIGN_PASSWORD -d masked=true -d protected=true -d environment_scope=release --data-urlencode "value=$(cat keys/password)" -o /dev/null && ./gl.sh /projects/root%2Fapp/protected_tags -X POST -d 'name=v*' -d create_access_level=40 -o /dev/null && cp keys/cosign.pub app/ && rm keys/cosign.key keys/password && ./commit.sh app/ 'add the public key' && ls keys
pushed: bd4f55b add the public key to main cosign.pub

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.

app/.gitlab-ci.yml
# 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: build
image: secopslog-py:3.15
script:
- mkdir -p dist
- tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz calc.py
artifacts:
paths: [dist/]
sign:
stage: sign
image: secopslog-cosign:3.1.4
needs: [build]
rules:
- if: $CI_COMMIT_TAG =~ /^v/
environment: # the key variables are scoped to "release", so only this job gets them
name: release
action: prepare
variables:
GIT_DEPTH: 0
script:
# 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.gz
artifacts:
paths: [dist/]
verify:
stage: verify
image: secopslog-cosign:3.1.4
needs: [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
ubuntu@secopslog-cicd:~/lab/sign · GitLab 19.5.3
$ ./pipeline.sh app/ | grep -E "^(--- job|pipeline)"
--- job build (build): success pipeline #2: success

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

ubuntu@secopslog-cicd:~/lab/sign · GitLab 19.5.3
$ ./gl.sh /projects/root%2Fapp/repository/tags -X POST -d tag_name=v1.0.0 -d ref=main -o /dev/null && sleep 3 && SHA=$(git -C app rev-parse HEAD) ./pipeline.sh app/
--- job build (build): success $ mkdir -p dist $ tar --sort=name --mtime=@0 --owner=0 --group=0 -czf dist/app.tar.gz calc.py Uploading artifacts... dist/: found 2 matching artifact files and directories Uploading artifacts as "archive" to coordinator... 201 Created correlation_id=01M73VADF5CEHVZZJ6T31TFV4X id=3 status=201 token=64_jBh8gG --- job sign (sign): success Downloading artifacts $ 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" v1.0.0 is on main $ cosign signing-config create --out signing.json $ cosign sign-blob --yes --key "$COSIGN_KEY" --signing-config signing.json --bundle dist/app.tar.gz.sigstore.json dist/app.tar.gz Using payload from: dist/app.tar.gz Signing artifact... Wrote bundle to file dist/app.tar.gz.sigstore.json Uploading artifacts... dist/: found 3 matching artifact files and directories Uploading artifacts as "archive" to coordinator... 201 Created correlation_id=01M73VANXBEVYFNQE9AP74PHAK id=4 status=201 token=64_zDa4r4 --- job verify (verify): success Removing signing.json Downloading artifacts $ cosign verify-blob --key cosign.pub --insecure-ignore-tlog=true --bundle dist/app.tar.gz.sigstore.json dist/app.tar.gz WARNING: Skipping tlog verification is an insecure practice that lacks transparency and auditability verification for the blob. Verified OK pipeline #3: success
$ for f in app.tar.gz app.tar.gz.sigstore.json; do ./gl.sh "/projects/root%2Fapp/jobs/artifacts/v1.0.0/raw/dist/$f?job=sign" -o out/$f; done && cp app/cosign.pub out/ && docker run --rm -v "$PWD/out:/out:ro" secopslog-cosign:3.1.4 cosign verify-blob --key /out/cosign.pub --insecure-ignore-tlog=true --bundle /out/app.tar.gz.sigstore.json /out/app.tar.gz 2>&1 | tail -1
Verified OK

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:

evil/.gitlab-ci.yml
# 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.4
script:
- 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
ubuntu@secopslog-cicd:~/lab/sign · GitLab 19.5.3
$ ./user.sh dev >/dev/null && ./member.sh app dev 30 >/dev/null && cp evil/.gitlab-ci.yml app/ && ./as.sh dev ./commit.sh app/ 'sign it myself' feature/sign && ./pipeline.sh app/ | sed -n '/--- job/,$p'; exit ${PIPESTATUS[0]}
pushed: b3fe806 sign it myself to feature/sign --- job sign-anyway (test): failed $ echo "COSIGN_KEY='$COSIGN_KEY' COSIGN_PASSWORD length ${#COSIGN_PASSWORD}" COSIGN_KEY='' COSIGN_PASSWORD length 0 $ 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 Generating ephemeral keys... Non-interactive mode detected, using device flow. Error: signing x.txt: getting keypair and token: retrieving ID token: selecting OIDC issuer: no matching service found for API versions [1] and current time 2026-11-10 23:05:39.282722745 +0000 UTC m=+1.849891404 error during command execution: signing x.txt: getting keypair and token: retrieving ID token: selecting OIDC issuer: no matching service found for API versions [1] and current time 2026-11-10 23:05:39.282722745 +0000 UTC m=+1.849891404 ERROR: Job failed: exit code 1 pipeline #4: failed

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.

LayerHereIf it were missing
Protected v* tags, Maintainers onlyOnly Maintainers create release tagsIf Developers could create v* tags, any Developer could start a pipeline that receives the key
Key scoped to releaseOnly the sign job receives itEvery job on main and in tag pipelines would hold the key
Tag must be on mainThe job refuses an unmerged commitA Maintainer, or a stolen Maintainer token, could sign an unreviewed branch
Protected key variablesOnly protected refs receive the keyAny branch job could sign anything
Rules on the sign jobThe job exists only in tag pipelinesNothing important: rules organise, protection secures
Key not in GitLab at all (KMS)Not in this labA Maintainer can still read or replace the key variable
Watch out
Maintainers can read and replace variables, so this design trusts every Maintainer with the signing key. To narrow that further, keep the key in a KMS or HSM that the pipeline reaches with an ID token, as in the OpenBao lesson, so even Maintainers never see it. Run the signing job on a protected runner that never takes branch jobs, as in the runner isolation lesson.

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.

Quick check
01Why store a Cosign private key as a protected file variable instead of a masked variable?
Incorrect — Speed is not the issue.
Correct — It has several lines, so masking is not possible, and protection keeps it from unprotected refs. And file type means the job gets a path, not the key text in its environment.
Incorrect — Variables are stored the same way. The difference is how the job receives them.
Incorrect — It can, but a multi-line key in an environment variable is easier to leak.
02A Developer pushes a branch with a job that runs cosign sign-blob --key "$COSIGN_KEY". What happens?
Incorrect — The image is not the protection. The key is never given to the job.
Incorrect — GitLab does not inspect scripts.
Correct — A branch pipeline receives no protected variables. Protection decides which pipelines get the key.
Incorrect — There is no such marking. Without the key it cannot sign with your key. At most Cosign tries keyless signing, which never verifies against your public key.
03Who can still obtain the signing key in this design?
Correct — They can also create release tags. A KMS reached with an ID token removes even that access.
Incorrect — Maintainers can read variables that are not hidden, and they can replace any variable.
Incorrect — A Developer pipeline does not receive the key at all.
Incorrect — The tarball and bundle contain the signature, not the key.

Related