Pulse Beta

User guide

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 #

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.

  1. 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.
  2. 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_PROTECTED tests 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 #

Next: Jenkins