2018-03-15 09:14:22 -04:00
|
|
|
# Container Scanning with GitLab CI/CD
|
|
|
|
|
|
|
|
You can check your Docker images (or more precisely the containers) for known
|
|
|
|
vulnerabilities by using [Clair](https://github.com/coreos/clair) and
|
|
|
|
[clair-scanner](https://github.com/arminc/clair-scanner), two open source tools
|
|
|
|
for Vulnerability Static Analysis for containers.
|
|
|
|
|
|
|
|
All you need is a GitLab Runner with the Docker executor (the shared Runners on
|
|
|
|
GitLab.com will work fine). You can then add a new job to `.gitlab-ci.yml`,
|
2018-05-24 10:22:51 -04:00
|
|
|
called `container_scanning`:
|
2018-03-15 09:14:22 -04:00
|
|
|
|
|
|
|
```yaml
|
2018-05-24 10:22:51 -04:00
|
|
|
container_scanning:
|
2018-04-03 15:19:00 -04:00
|
|
|
image: docker:stable
|
2018-03-15 09:14:22 -04:00
|
|
|
variables:
|
|
|
|
DOCKER_DRIVER: overlay2
|
|
|
|
## Define two new variables based on GitLab's CI/CD predefined variables
|
|
|
|
## https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables
|
|
|
|
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG
|
|
|
|
CI_APPLICATION_TAG: $CI_COMMIT_SHA
|
|
|
|
allow_failure: true
|
|
|
|
services:
|
2018-04-03 15:19:00 -04:00
|
|
|
- docker:stable-dind
|
2018-03-15 09:14:22 -04:00
|
|
|
script:
|
|
|
|
- docker run -d --name db arminc/clair-db:latest
|
2018-04-27 04:24:30 -04:00
|
|
|
- docker run -p 6060:6060 --link db:postgres -d --name clair --restart on-failure arminc/clair-local-scan:v2.0.1
|
2018-03-15 09:14:22 -04:00
|
|
|
- apk add -U wget ca-certificates
|
|
|
|
- docker pull ${CI_APPLICATION_REPOSITORY}:${CI_APPLICATION_TAG}
|
|
|
|
- wget https://github.com/arminc/clair-scanner/releases/download/v8/clair-scanner_linux_amd64
|
|
|
|
- mv clair-scanner_linux_amd64 clair-scanner
|
|
|
|
- chmod +x clair-scanner
|
|
|
|
- touch clair-whitelist.yml
|
2018-04-16 12:34:43 -04:00
|
|
|
- while( ! wget -q -O /dev/null http://docker:6060/v1/namespaces ) ; do sleep 1 ; done
|
2018-04-19 08:17:48 -04:00
|
|
|
- retries=0
|
|
|
|
- echo "Waiting for clair daemon to start"
|
|
|
|
- while( ! wget -T 10 -q -O /dev/null http://docker:6060/v1/namespaces ) ; do sleep 1 ; echo -n "." ; if [ $retries -eq 10 ] ; then echo " Timeout, aborting." ; exit 1 ; fi ; retries=$(($retries+1)) ; done
|
2018-05-24 10:22:51 -04:00
|
|
|
- ./clair-scanner -c http://docker:6060 --ip $(hostname -i) -r gl-container-scanning-report.json -l clair.log -w clair-whitelist.yml ${CI_APPLICATION_REPOSITORY}:${CI_APPLICATION_TAG} || true
|
2018-03-15 09:14:22 -04:00
|
|
|
artifacts:
|
2018-05-24 10:22:51 -04:00
|
|
|
paths: [gl-container-scanning-report.json]
|
2018-03-15 09:14:22 -04:00
|
|
|
```
|
|
|
|
|
2018-05-24 10:22:51 -04:00
|
|
|
The above example will create a `container_scanning` job in your CI/CD pipeline, pull
|
2018-03-15 09:14:22 -04:00
|
|
|
the image from the [Container Registry](../../user/project/container_registry.md)
|
|
|
|
(whose name is defined from the two `CI_APPLICATION_` variables) and scan it
|
|
|
|
for possible vulnerabilities. The report will be saved as an artifact that you
|
|
|
|
can later download and analyze.
|
|
|
|
|
|
|
|
If you want to whitelist some specific vulnerabilities, you can do so by defining
|
|
|
|
them in a [YAML file](https://github.com/arminc/clair-scanner/blob/master/README.md#example-whitelist-yaml-file),
|
|
|
|
in our case its named `clair-whitelist.yml`.
|
|
|
|
|
|
|
|
TIP: **Tip:**
|
|
|
|
Starting with [GitLab Ultimate][ee] 10.4, this information will
|
|
|
|
be automatically extracted and shown right in the merge request widget. To do
|
2018-05-24 10:22:51 -04:00
|
|
|
so, the CI/CD job must be named `container_scanning` and the artifact path must be
|
|
|
|
`gl-container-scanning-report.json`.
|
2018-03-15 09:14:22 -04:00
|
|
|
[Learn more on container scanning results shown in merge requests](https://docs.gitlab.com/ee/user/project/merge_requests/container_scanning.html).
|
|
|
|
|
2018-05-24 10:22:51 -04:00
|
|
|
CAUTION: **Caution:**
|
2018-07-05 11:20:00 -04:00
|
|
|
Before GitLab 11.0, Container Scanning was previously using `sast:container` for job name and
|
2018-05-24 10:22:51 -04:00
|
|
|
`gl-sast-container-report.json` for the artifact name. While these old names
|
2018-07-05 11:20:00 -04:00
|
|
|
are still maintained, they have been deprecated with GitLab 11.0 and may be removed
|
2018-05-24 10:22:51 -04:00
|
|
|
in next major release, GitLab 12.0. You are advised to update your current `.gitlab-ci.yml`
|
|
|
|
configuration to reflect that change.
|
|
|
|
|
2018-07-02 20:37:24 -04:00
|
|
|
[ee]: https://about.gitlab.com/pricing/
|