Skip to content

Vulnerability Scanning with Trivy #15

Vulnerability Scanning with Trivy

Vulnerability Scanning with Trivy #15

Workflow file for this run

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 vulnerability scanner - Repository
runs-on: ubuntu-22.04
if: ${{ ( github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' ) && github.ref_name == 'main' && github.repository == 'canonical/lxd' }}
steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
ref: main
- name: Install Trivy
uses: ./.github/actions/install-trivy
- name: Run Trivy vulnerability scanner
run: |
trivy fs --quiet --scanners vuln,secret,misconfig --format sarif --cache-dir /home/runner/vuln-cache \
--severity LOW,MEDIUM,HIGH,CRITICAL --output trivy-lxd-repo-scan-results.sarif .
- name: Cache Trivy vulnerability database
uses: actions/cache/save@3624ceb22c1c5a301c8db4169662070a689d9ea8 # v4.1.1
with:
path: /home/runner/vuln-cache
key: trivy-cache-${{ github.run_id }}
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
with:
sarif_file: "trivy-lxd-repo-scan-results.sarif"
sha: ${{ github.sha }}
ref: refs/heads/main
trivy-snap:
name: Trivy vulnerability scanner - Snap
runs-on: ubuntu-22.04
needs: trivy-repo
if: ${{ ( github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' ) && github.ref_name == 'main' && github.repository == 'canonical/lxd' }}
strategy:
matrix:
version:
- "latest"
- "5.21"
- "5.0"
- "4.0"
steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- name: Install Trivy
uses: ./.github/actions/install-trivy
- name: Restore cached Trivy vulnerability database
uses: actions/cache/restore@3624ceb22c1c5a301c8db4169662070a689d9ea8 # v4.1.1
with:
path: /home/runner/vuln-cache
key: trivy-cache-${{ github.run_id }}
- name: Download snap for scan
run: |
snap download lxd --channel=${{ matrix.version }}/stable
unsquashfs ./lxd*.snap
- name: Run Trivy vulnerability scanner
run: |
trivy rootfs --quiet --scanners vuln,secret,misconfig --format sarif --cache-dir /home/runner/vuln-cache \
--severity LOW,MEDIUM,HIGH,CRITICAL --output /home/runner/${{ matrix.version }}-stable.sarif squashfs-root
- name: Flag snap scanning alerts
run: |
cd /home/runner
jq '.runs[].tool.driver.rules[] |= (.shortDescription.text |= "Snap scan - " + .)' ${{ matrix.version }}-stable.sarif > tmp.json
mv tmp.json ${{ matrix.version }}-stable.sarif
# Now we checkout to the branch related to the scanned snap to populate github.sha appropriately.
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
with:
ref: ${{ (matrix.version == 'latest' && 'main') || format('stable-{0}', matrix.version) }}
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@c36620d31ac7c881962c3d9dd939c40ec9434f2b # v3.26.12
with:
sarif_file: /home/runner/${{ matrix.version }}-stable.sarif
sha: ${{ github.sha }}
ref: refs/heads/${{ (matrix.version == 'latest' && 'main') || format('stable-{0}', matrix.version) }}