Vulnerability Scanning with Trivy #31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Vulnerability Scanning with Trivy | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 0 * * *' # Test Trivy daily at midnight | |
permissions: | |
contents: read | |
security-events: write # for uploading SARIF results to the security tab | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }} | |
cancel-in-progress: true | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
trivy-repo: | |
name: Trivy - Repository | |
runs-on: ubuntu-22.04 | |
if: ${{ github.ref_name == 'main' }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
ref: main | |
- name: Install Trivy | |
uses: canonical/lxd/.github/actions/install-trivy@main | |
- name: Download Trivy DB | |
id: db_download | |
run: trivy fs --download-db-only --cache-dir /home/runner/vuln-cache | |
continue-on-error: true | |
- name: Cache Trivy vulnerability database | |
if: ${{ steps.db_download.outcome == 'success' }} | |
uses: actions/cache/save@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 | |
with: | |
path: /home/runner/vuln-cache | |
key: trivy-cache-${{ github.run_id }} | |
- name: Use previously downloaded database instead | |
if: ${{ steps.db_download.outcome == 'failure' }} | |
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 | |
with: | |
path: /home/runner/vuln-cache | |
key: download-failed # Use a non existing key to fallback to restore-keys | |
restore-keys: trivy-cache- | |
- name: Run Trivy vulnerability scanner | |
run: | | |
trivy fs --skip-db-update \ | |
--scanners vuln,secret,misconfig \ | |
--format sarif \ | |
--cache-dir /home/runner/vuln-cache \ | |
--severity LOW,MEDIUM,HIGH,CRITICAL \ | |
--output trivy-microcloud-repo-scan-results.sarif . | |
- name: Upload Trivy scan results to GitHub Security tab | |
uses: github/codeql-action/upload-sarif@df409f7d9260372bd5f19e5b04e83cb3c43714ae # v3.27.9 | |
with: | |
sarif_file: "trivy-microcloud-repo-scan-results.sarif" | |
sha: ${{ github.sha }} | |
ref: refs/heads/main | |
trivy-snap: | |
name: Trivy - Snap | |
runs-on: ubuntu-22.04 | |
if: ${{ github.ref_name == 'main' }} | |
needs: trivy-repo | |
strategy: | |
matrix: | |
include: | |
- channel: "3/edge" | |
branch: "main" | |
- channel: "2/stable" | |
branch: "v2-edge" | |
- channel: "1/stable" | |
branch: "v1-edge" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
ref: ${{ matrix.branch }} | |
- name: Install Trivy | |
uses: canonical/lxd/.github/actions/install-trivy@main | |
- name: Restore cached Trivy vulnerability database | |
uses: actions/cache/restore@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 | |
with: | |
path: /home/runner/vuln-cache | |
key: download-failed # Use a non existing key to fallback to restore-keys | |
restore-keys: trivy-cache- | |
- name: Download snap for scan | |
run: | | |
snap download microcloud --channel=${{ matrix.channel }} | |
unsquashfs ./microcloud*.snap | |
- name: Run Trivy vulnerability scanner | |
run: | | |
trivy rootfs --skip-db-update \ | |
--scanners vuln,secret,misconfig \ | |
--format sarif \ | |
--cache-dir /home/runner/vuln-cache \ | |
--severity LOW,MEDIUM,HIGH,CRITICAL \ | |
--output snap-scan-results.sarif squashfs-root | |
- name: Flag snap scanning alerts | |
run: | | |
jq '.runs[].tool.driver.rules[] |= (.shortDescription.text |= "Snap scan - " + .)' snap-scan-results.sarif > tmp.json | |
mv tmp.json snap-scan-results.sarif | |
- name: Upload Trivy scan results to GitHub Security tab | |
uses: github/codeql-action/upload-sarif@df409f7d9260372bd5f19e5b04e83cb3c43714ae # v3.27.9 | |
with: | |
sarif_file: "snap-scan-results.sarif" | |
sha: ${{ github.sha }} | |
ref: refs/heads/${{ matrix.branch }} |