Protected branches, tags and variables
Who can change main, who can cut a release, and which pipelines get the secrets.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/protected.tar.gz && tar -xzf protected.tar.gz, which creates ~/lab/protected/. SHA-256: a52bdacfee7d2b8d68559a769843afd9c30d2c227f2bb9e08a2fd7f5d1bfb9ddA production deploy token lives in a CI/CD variable. Any developer can create a branch, and any branch runs the pipeline. Put those two facts together and every developer can read the production token, whatever the deploy job's rules say. GitLab's answer is one idea applied three times: some refs are protected, only some people can change them, and only pipelines on them receive protected secrets.
The setup
A project with a protected variable, PROD_TOKEN, and a second user, dev, who has the Developer role. member.sh adds the membership and waits until GitLab has applied it, since permissions are refreshed in the background. The pipeline only prints which ref it runs on and the length of PROD_TOKEN, never the value:
# Prints which secrets this pipeline received. Lengths only: never print a secret, even masked.where-am-i:image: secopslog-py:3.15script:- echo "ref $CI_COMMIT_REF_NAME, protected=$CI_COMMIT_REF_PROTECTED"- echo "PROD_TOKEN length ${#PROD_TOKEN}"
A new project protects its default branch out of the box (an instance-wide default that administrators can change): only Maintainers may push to main or merge into it, and nobody may force-push.
Who can change main
Refused by a server-side hook. A Developer can still work on a branch and propose the change:
The branch pipeline ran with protected=false and got a PROD_TOKEN of length 0: the variable simply isn't there. The merge request was opened. And dev couldn't merge it, because merging into main needs the Maintainer role here. GitLab's API answers that with 401 Unauthorized, even though dev's token is valid. Now start a pipeline on main itself:
On a protected ref, the pipeline received the token, 21 characters. A secret marked protected is handed only to pipelines running for protected branches or tags, so the people who can run code with it are the people who can change those refs.
Who can make a release tag
Tags matter as much as branches: release pipelines usually run on tags, and a protected tag pipeline also receives protected variables. Protect the v* pattern so only Maintainers can create release tags:
The Developer's tag was refused. The Maintainer's tag v1.0.0 started a pipeline that ran with protected=true and the token. Unprotected tags are a quiet hole: if any developer can push a tag that your release jobs trust, a protected main doesn't stop them.
| Setting | Protects | Free tier |
|---|---|---|
| Protected branch | Who can push, merge and force-push | Yes |
| Protected tag | Who can create matching tags | Yes |
| Protected variable | Only pipelines on protected refs receive it | Yes |
| Protected environment | Who can deploy to an environment | No, Premium and above |
| Required approvals on merge requests | Merges need reviewers | No, Premium and above |
release/* pattern that Developers may push hands them every deployment secret. Keep the set of protected refs small and Maintainer-only.Hands-on
Protect a release branch pattern: ./gl.sh /projects/root%2Fapp/protected_branches -X POST -d 'name=release/*' -d push_access_level=40 -d merge_access_level=40. Change a file (echo "# release" >> app/calc.py) and try to push release/1.0 as dev with ./as.sh dev ./commit.sh app/ "release" release/1.0. Then push the same local branch as root with git -C app push -u origin release/1.0 and run ./pipeline.sh app/. Which pipeline gets PROD_TOKEN? Finish with ./down.sh.
What you should see: as dev, the push of release/1.0 was rejected with You are not allowed to create protected branches on this project. The pattern protects branches that don't exist yet, so nobody below Maintainer can create one. Root pushed the same branch, its pipeline ran with protected=true, and PROD_TOKEN arrived (length 21). A wildcard protects every future release branch at once, so you never have to remember to protect a new one.
Remember
Mark every deployment secret protected, protect the branches and tags that deploy, and test it from a Developer account: the branch pipeline should see an empty variable.
PROD_TOKEN is a protected variable. A Developer pushes feature/x with a job that prints ${#PROD_TOKEN}. What does the job print?PROD_TOKEN length 0 on feature/dev and 21 on main.main is protected. Tags are not protected. What is the risk?v*.