DevSecOps, part 11 of 11

Vulnerability Scanning

By , , 2 min read

  1. Secrets in GitOps: SOPS, Sealed Secrets and External Secrets Operator
  2. IaC Security (Checkov and TFLint)
  3. Introduction to Cybersecurity in DevOps
  4. Security Principles
  5. Threat Modeling
  6. Authentication and Authorization
  7. Secrets Management
  8. Linux Server Hardening
  9. SSH Security
  10. Firewall and Network Security
  11. Vulnerability Scanning

The problem

A Docker image that was clean yesterday can carry a critical CVE in OpenSSL or an untouched transitive dependency today. If you do not scan before deploying — and periodically in the registry — you only find out when it is already too late. Here we look at scanning images, containers and dependencies with Trivy, Clair and Snyk, and wiring it into CI/CD so vulnerable builds stop before release.

Prerequisites

  • Basic knowledge of Docker and containers.
  • Access to an environment with Docker installed.
  • Familiarity with CI/CD pipelines (GitHub Actions, GitLab CI).

Main Tools

Trivy

Fast and versatile scanner for vulnerabilities in containers, images, filesystems and repositories.

Installation

# Using brew (macOS)
brew install trivy

# Or download the binary
wget https://github.com/aquasecurity/trivy/releases/latest/download/trivy_$(uname -s)-$(uname -m).tar.gz
tar -xzf trivy_*.tar.gz
sudo mv trivy /usr/local/bin/

Basic Usage

# Scan Docker image
trivy image nginx:latest

# Scan running container
trivy container my-container

# Scan filesystem
trivy fs /path/to/project

# JSON output
trivy image --format json --output results.json nginx:latest

CI/CD Integration

# .github/workflows/security-scan.yml
name: Security Scan
on: [push]
jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Run Trivy
        uses: aquasecurity/trivy-action@master
        with:
          scan-type: 'fs'
          scan-ref: '.'

Clair

Static vulnerability scanner for container images, developed by Red Hat.

Installation and Usage

Clair requires a PostgreSQL database and is more complex to configure. Trivy is recommended for simple cases.

# Using Docker
docker run -d --name clair-db -e POSTGRES_PASSWORD=password postgres:13
docker run -d --name clair --link clair-db:postgres -p 6060:6060 quay.io/projectquay/clair:latest

Snyk

Commercial tool with a free tier that scans code, dependencies and containers for vulnerabilities.

Installation

npm install -g snyk
snyk auth  # Authenticate

Usage

# Scan dependencies
snyk test

# Scan Docker image
snyk container test nginx:latest

# Monitor project
snyk monitor

Best Practices

  • Regular Scanning: Integrate into pipelines for every commit/PR.
  • False Positives: Configure exceptions for vulnerabilities that do not apply.
  • Updates: Keep base images up to date.
  • SBOM: Generate a Software Bill of Materials for tracking.

Advanced Examples

Complete scan script

#!/bin/bash
# scan.sh

echo "Scanning for vulnerabilities..."

# Trivy on images
for image in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep -v '<none>'); do
  echo "Scanning $image"
  trivy image "$image" --exit-code 1 --no-progress
done

# Snyk on dependencies
if [ -f "package.json" ]; then
  snyk test --severity-threshold=high
fi

echo "Scan completed"

References

Search