Scan container images

  • Tier: Premium, Ultimate
  • Offering: GitLab.com
  • Status: Beta

Scan a container image in Artifact Central for known vulnerabilities with Container Scanning. The Container Scanning job pulls the image from Artifact Central, scans it, and reports the findings in your project.

Container Scanning is available on all tiers. The security reports in merge requests and the vulnerability report need GitLab Ultimate.

Prerequisites

  • A Docker or OCI repository that holds the image.
  • The Artifact Viewer role or higher on the repository for the user who runs the pipeline.

In the examples on this page, replace the following placeholders:

  • <registry_host>: Enter the hostname from your repository’s setup instructions. To find it, go to your repository and select More actions > View setup instructions.
  • <registry_handle>: Enter your organization’s registry handle.
  • <repository_name>: Enter the name of your Docker or OCI repository.
  • <policy_project_path>: Enter the full path of your security policy project, such as my-group/security-policies.

Scan an image

Point the Container Scanning job at the image in Artifact Central, and give it an Artifact Central token as the registry password.

The Container Scanning image has no curl, so the token exchange runs in Ruby instead.

To scan an image:

  1. In .gitlab-ci.yml, include the Container Scanning template and override the container_scanning job:

    yaml
    include:
      - template: Jobs/Container-Scanning.gitlab-ci.yml
    
    container_scanning:
      variables:
        CS_IMAGE: <registry_host>/<registry_handle>/container/<repository_name>/app:$CI_COMMIT_SHA
        CS_REGISTRY_USER: __token__
      before_script:
        - |
          CS_REGISTRY_PASSWORD=$(ruby -rnet/http -rjson -e '
            uri = URI("#{ENV["CI_API_V4_URL"]}/token_exchange?audience=gitlab-artifact-registry&expires_in=3600")
            res = Net::HTTP.post(uri, "", "JOB-TOKEN" => ENV["CI_JOB_TOKEN"])
            abort("token exchange failed: #{res.code}") unless res.is_a?(Net::HTTPSuccess)
            print JSON.parse(res.body).fetch("token")
          ')
          test -n "$CS_REGISTRY_PASSWORD"
          export CS_REGISTRY_PASSWORD
  2. If a job in the same pipeline pushes the image, make sure container_scanning runs after it. The template puts container_scanning in the test stage. Push the image in an earlier stage, such as build.

Set CS_REGISTRY_PASSWORD to the exchanged token, not to $CI_JOB_TOKEN. Artifact Central accepts only Artifact Central tokens, so a GitLab token fails with 401 Unauthorized.

The job also writes a CycloneDX software bill of materials (SBOM) to gl-sbom-report.cdx.json. You can attach the SBOM to the image as a signed attestation. For more information, see sign and verify container images.

Scan images in every project in a group

  • Tier: Ultimate

Use a pipeline execution policy to add the scan job to every pipeline in a group. Each project sets the image to scan in a CI/CD variable.

Use a pipeline execution policy, not a scan execution policy. A pipeline execution policy runs a job you define, so the job can run the token exchange.

Prerequisites:

  • A security policy project linked to the group.
  • The Maintainer role in the security policy project and in each project to scan.

To scan images in every project in a group:

  1. In the security policy project, add a file such as ar-container-scanning.yml. The job runs only on the default branch of projects that set AR_SCAN_IMAGE:

    yaml
    include:
      - template: Jobs/Container-Scanning.gitlab-ci.yml
    
    container_scanning:
      stage: .pipeline-policy-post
      rules:
        - if: $AR_SCAN_IMAGE && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      variables:
        CS_IMAGE: $AR_SCAN_IMAGE
        CS_REGISTRY_USER: __token__
      before_script:
        - |
          CS_REGISTRY_PASSWORD=$(ruby -rnet/http -rjson -e '
            uri = URI("#{ENV["CI_API_V4_URL"]}/token_exchange?audience=gitlab-artifact-registry&expires_in=3600")
            res = Net::HTTP.post(uri, "", "JOB-TOKEN" => ENV["CI_JOB_TOKEN"])
            abort("token exchange failed: #{res.code}") unless res.is_a?(Net::HTTPSuccess)
            print JSON.parse(res.body).fetch("token")
          ')
          test -n "$CS_REGISTRY_PASSWORD"
          export CS_REGISTRY_PASSWORD
  2. In .gitlab/security-policies/policy.yml in the security policy project, add a policy that includes the file:

    yaml
    pipeline_execution_policy:
    - name: Scan Artifact Central images
      description: Scans the image in AR_SCAN_IMAGE.
      enabled: true
      pipeline_config_strategy: inject_policy
      content:
        include:
        - project: <policy_project_path>
          file: ar-container-scanning.yml
  3. In each project to scan, add a CI/CD variable named AR_SCAN_IMAGE with the full image path, such as <registry_host>/<registry_handle>/container/<repository_name>/app:latest.

The .pipeline-policy-post stage runs after every other stage, so a project that pushes the image in the same pipeline scans the new image.

If the users who run pipelines can’t read the security policy project, turn on access to the policy project’s CI/CD configuration.

Known issues

  • Findings are stored in the project that runs the pipeline, not on the image in Artifact Central. Artifact Central does not show scan results.
  • Only the images your pipelines scan get results. Artifact Central does not scan images when they are pushed. Scanning in Artifact Central is planned in issue 629881.
  • A scan execution policy cannot scan Artifact Central images. The job it adds does not run the token exchange. Use a pipeline execution policy instead.
  • Container scanning scans container images only. To scan Maven and npm packages, see scan packages from Artifact Central.