How-to guide
GitLab CI
Scan each pipeline of a GitLab project, fail a merge request only on the findings that it adds, and keep the token safe.
Before you start #
- A token for the pipeline. In your workspace, + Add › Set up CI mints the token of one repository and shows this job, with buttons that copy them. Or make the token yourself: see Make a token for the pipeline. Does one CI template or one CI server build many repositories of your team? Then ask an admin for a department token (see Pick a token for the pipeline).
- The install step of your project, before the scan. The examples use
npm ci. See what the job needs before the scan. - The job also keeps malicious packages out of the install (CLI 0.1.94 or later):
tuxcarectl guard checkandtuxcarectl guard setup --cibefore the install step. See Malicious packages.
Add the job #
Store the token as a CI/CD variable with the name TUXCARE_PORTAL_TOKEN. The CLI reads it from that variable and writes it nowhere.
Add the token under Settings › CI/CD › Variables, with the visibility Masked. Then add a job to .gitlab-ci.yml:
tuxcarectl:
image: node:22 # the image that builds your project
script:
- curl -fsSL https://securechain.tuxcare.com/get/tuxcarectl | sh
- tuxcarectl guard check
- guard_path="$(tuxcarectl guard setup --ci)"
- eval "$guard_path"
- npm ci # the install step of your project, through the guard
- tuxcarectl sca --fail-on high
A Protected variable goes to the pipelines of protected branches only. In other pipelines, the job stops with exit code 3.
Fail a merge request only on new findings #
compare --fail-on-new fails a merge request only on the findings that it adds. The default branch needs a scan of its own, so the first job scans it on each push. See Pull and merge requests.
tuxcarectl:
image: node:22
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
script:
- npm ci # the install step of your project
- curl -fsSL https://securechain.tuxcare.com/get/tuxcarectl | sh
- tuxcarectl sca --fail-on high
tuxcarectl-new-findings:
image: node:22
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- npm ci
- curl -fsSL https://securechain.tuxcare.com/get/tuxcarectl | sh
- tuxcarectl compare --base "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" --fail-on-new
The merge request job runs on the source branch. If TUXCARE_PORTAL_TOKEN is Protected, GitLab gives it to protected branches only, and the job stops with exit code 3. Keep Protected on that variable when its token has wider rights than one repository, or when an admin saved a registry token in the workspace (see below): every branch could then read it. Choose one of these two ways.
- A second token for the merge request job (recommended when the workspace has no registry token). Mint the CI token (one repository) preset for this repository (pick a token). Save it in a variable
TUXCARE_MR_TOKEN: Masked, not Protected. Only the merge request job reads it. The protected token stays for the default branch. - Run the merge request job for protected source branches only. The protected token then works, but a merge request from an ordinary feature branch gets no check. Use this rule:
- if: $CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_REF_PROTECTED == "true". In a merge request pipeline,CI_COMMIT_REF_PROTECTEDtests the source branch.
Here is the job for the first way:
tuxcarectl-new-findings:
image: node:22
variables:
TUXCARE_PORTAL_TOKEN: $TUXCARE_MR_TOKEN
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
script:
- npm ci
- curl -fsSL https://securechain.tuxcare.com/get/tuxcarectl | sh
- tuxcarectl compare --base "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" --fail-on-new
The risk: every branch of the project can read an unprotected variable. Anyone who can push a branch can print the token. The token can upload scans and read the findings of this one repository only. It cannot read other repositories, and it expires in 90 days at most. Mint a new one before it expires.
The token can also get the registry token of the workspace. Every token with the submit permission can get it, also a token of one repository. So anyone who can push a branch to the project can get the registry token from this variable. When an admin saved a registry token in the workspace, use the second way, or ask an admin before you use the first way. The Audit log records the reads of the registry token through the CLI: one row a day for each token and network.
Sign in with no stored token #
GitLab CI gives each job a signed OIDC token. With a trust rule of an admin (CI sign-in), the job trades it for a portal token of one hour, and the project stores no portal token. Change the job as below. PORTAL_URL is the address of your workspace, with no slash at the end. The audience of the OIDC token must be this address.
scan:
variables:
PORTAL_URL: https://acme.example.com
id_tokens:
PORTAL_ID_TOKEN:
aud: https://acme.example.com
script:
- export TUXCARE_PORTAL_TOKEN=$(jq -n --arg t "$PORTAL_ID_TOKEN" '{token: $t}' |
curl -sS --fail-with-body -X POST --data-binary @- "$PORTAL_URL/securechain/v1/ci-token" |
jq -r .access_token)
More #
- Let the job report findings and not stop the pipeline:
allow_failure: exit_codes: [1]. - Record a release from the job of a tag:
tuxcarectl sca --release "$CI_COMMIT_TAG". - Send the results to other tools, for example the JUnit test report of GitLab.
Next: Jenkins