Trivy: Container Vulnerability Scanning
Author: Fabrice JAMMES (LinkedIn).
Objective
- Scan container images for known vulnerabilities (CVE) with Trivy
- Find and remove the running pods whose images have critical vulnerabilities: a classic CKS task
- Generate a Software Bill of Materials (SBOM), and scan it
The CKS curriculum lists “Perform static analysis of user workloads and container images” and “Understand your supply chain (SBOM, CI/CD, artifact repositories)” in the Supply chain security domain.
Prerequisites
trivy is installed on the training server. The lab works on the shared cluster, in a namespace of your own.
Exercise 1: scan an image
Scan the image nginx:1.19 and display only its CRITICAL vulnerabilities. Then count the CRITICAL vulnerabilities of nginx:1.19 and of nginx:1.31-alpine-slim.
Exercise 2: remove the vulnerable pods
Run three pods in your namespace: web-old (nginx:1.19), web-new (nginx:1.31-alpine-slim) and pause (registry.k8s.io/pause:3.10).
Then, as in the exam: find the images of the running pods of the namespace, scan them, and delete the pods whose image has CRITICAL vulnerabilities.
Tip
trivy image --exit-code 1 --severity CRITICAL returns 1 if it finds a critical vulnerability: use it in a if.
Exercise 3: SBOM
Generate the SBOM of nginx:1.31-alpine-slim in the CycloneDX format. Which packages does the image contain? Then scan the SBOM instead of the image.
Exercise 4: a gate in the CI
How do you stop a CI pipeline when an image has CRITICAL vulnerabilities which have a fix?
Questions
- An image was scanned without vulnerability when it was built. Is it still safe 6 months later?
- How do you scan continuously the images running in a cluster?
Clean up
The 1-trivy.sh script of the k8s-advanced repository runs similar scans.