diff --git a/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/.pre-commit-config.yml b/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/.pre-commit-config.yml new file mode 100644 index 000000000..4d3227e6c --- /dev/null +++ b/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/.pre-commit-config.yml @@ -0,0 +1,10 @@ +repos: + - repo: local + hooks: + - id: grype-cve-scan + name: Grype Vulnerability Scan + description: Scans for dependency vulnerabilities. Fails if CRITICAL vulnerabilities detected. + entry: python -c "import os; import subprocess; import sys; os.environ['GRYPE_DB_AUTO_UPDATE'] = 'false'; result=subprocess.run(['grype', 'dir:.', '--fail-on', 'critical'], capture_output=True); print(result.stdout.decode()); print('CRITICAL level vulnerabilities found. To address issues, run scan via `grype dir:.`, then `git add` followed by `git commit` your fix or ignore via `git commit --no-verify`') if result.returncode != 0 else print('No CRITICAL level vulnerabilities found.'); sys.exit(result.returncode)" + language: system + verbose: true + stages: [pre-push] \ No newline at end of file diff --git a/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/README.mdx b/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/README.mdx new file mode 100644 index 000000000..2ce60499e --- /dev/null +++ b/docs/guides/software-lifecycle/security/dependency-vulnerability-scanning/README.mdx @@ -0,0 +1,157 @@ + +import CodeBlock from '@theme/CodeBlock'; +import PreCommitConfigSource from '!!raw-loader!./.pre-commit-config.yml'; + +# Container Vulnerability Scanning + +
A guide to scanning containers and container repositories for security vulnerabilities both manually and automatically.+ +![banner-image](/img/vulnerability-scanning-screen.png) + +## Introduction + +**Background**: To maintain the integrity and security of your containers in production environments, it's essential to monitor dependency vulnerabilities. Third-party software dependencies can harbor security vulnerabilities. This guide focuses on utilizing Grype, an open source vulnerability scanner, to proactively detect vulnerabilities in dependencies defined within container images as well as generally within repositories that use package managers. + +**Use Cases**: +- Scanning container images for vulnerabilities during the development phase +- Ensuring base container images are as vulnerability-free as possible +- Scanning package-manager defined software dependencies (e.g. NPM, YARN, Maven, etc.) for vulnerabilities during the development phase +- Automating vulnerability detection in repositories + +--- + +## Prerequisites +**Software:** +- OCI compliant containers (e.g. Docker, Podman) or other package-manager software dependencies +- `pre-commit` framework + +**Skills:** +- Basic knowledge of Git hooks and Docker commands +- Understanding of YAML for pre-commit configuration + +--- + +## Quick Start + +**Run a local scan of your container's repository (folder containing the Dockerfile) using [Grype](https://github.com/anchore/grype)** + +```bash +grype dir:. +``` + +**Run a local scan of a Docker image using Grype** + +First, build the Docker image: +```bash +docker build -t my-app:latest . +``` + +Then, scan the built Docker image: +```bash +grype my-app:latest +``` + +**⬇️ [Grype Scanning .pre-commit-config.yml](.pre-commit-config.yml)** + +Download the file above to access the pre-commit configuration file, which includes an example hook for Grype vulnerability scanning. This file should be placed within your local Git repository after installing the pre-commit framework. + +**⬇️ [Grype GitHub Action](https://github.com/marketplace/actions/anchore-container-scan)** + +GitHub Action for Grype vulnerability scanning. + +--- + +## Step-by-Step Guide + +### Step 1: Scan Locally for Container Vulnerabilities + +1. Ensure Grype is installed on your system. You can install Grype from [the official repository](https://github.com/anchore/grype). + + ```bash + grype version + ``` + +2. Perform a scan of the local repository for vulnerabilities. The below checks for vulnerabilities via any common package managers that are detected in your repository. See [Grype supported sources](https://github.com/anchore/grype?tab=readme-ov-file#supported-sources) for more information. + + ```bash + grype dir:. + ``` + +3. To scan a Docker image, first build the Docker image: + + ```bash + docker build -t my-app:latest . + ``` + +4. Then, perform a scan of the built Docker image: + + ```bash + grype my-app:latest + ``` + +5. If you find vulnerabilities, fix them via your package manager. + +### Step 2: Setup Automated Local Scanning of Container Vulnerabilities + +⚠️ NOTE: We recommend installing this pre-commit hook only if you have downloaded grype, already scanned your repository and addressed any vulnerabilities. + +⚠️ NOTE: The automated scan described below will NOT check for image vulnerabilities, rather, it uses the package dependency capability of Grype to look for third-party dependencies via `grype dir:.` + +The below steps, once enacted, will ensure that any local `git push` actions taken will be followed by an automated vulnerability scan. If vulnerabilities at the CRITICAL level are found, the push will be blocked by default. + +1. Install the pre-commit framework via Python: + ```bash + pip install pre-commit + ``` +2. Create a `.pre-commit-config.yaml` file in the root directory of your Git repository with the following content for Grype scanning: +