Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trivy vulnerability scanning #14145

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: main

- name: Create Trivy cache directory
run: mkdir -p /home/runner/vuln-cache

- name: Run Trivy vulnerability scanner in repo mode
uses: aquasecurity/trivy-action@master
with:
scan-type: "fs"
format: "sarif"
output: "trivy-lxd-repo-scan-results.sarif"
cache-dir: "/home/runner/vuln-cache"
severity: "LOW,MEDIUM,HIGH,CRITICAL"

- name: Cache trivy and vulnerability database
uses: actions/cache/save@v4
with:
path: /home/runner/vuln-cache
key: trivy-cache

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
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
strategy:
matrix:
version:
- "latest"
- "5.21"
- "5.0"
- "4.0"
steps:
- name: Checkout
uses: actions/checkout@v4
tomponline marked this conversation as resolved.
Show resolved Hide resolved
with:
# Snap scanner results are placed on the the branch related to the scanned channel.
ref: ${{ (matrix.version == 'latest' && 'main') || format('stable-{0}', matrix.version) }}

- name: Restore cached Trivy and vulnerability database
uses: actions/cache/restore@v4
with:
path: /home/runner/vuln-cache
key: trivy-cache

- name: Download snap for scan
run: |
ls /home/runner/vuln-cache
snap download lxd --channel=${{ matrix.version }}/stable
unsquashfs ./lxd*.snap

- name: Run Trivy vulnerability scanner on the snap
uses: aquasecurity/trivy-action@master
with:
scan-type: "rootfs"
tomponline marked this conversation as resolved.
Show resolved Hide resolved
format: "sarif"
scan-ref: squashfs-root
output: "${{ matrix.version }}-stable.sarif"
cache-dir: "/home/runner/vuln-cache"
severity: "LOW,MEDIUM,HIGH,CRITICAL"

- name: Prepend channel to alert name
run: |
jq '.runs[].tool.driver.rules[] |= (.shortDescription.text |= "Snap scan - " + .)' ${{ matrix.version }}-stable.sarif > tmp.json
mv tmp.json ${{ matrix.version }}-stable.sarif

- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "${{ matrix.version }}-stable.sarif"
sha: ${{ github.sha }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to provide this line at all?

Copy link
Contributor Author

@hamistao hamistao Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also find it confusing but I get an error if I don't provide both sha and ref. The error explicitly says both are needed here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can parse the version from the snap somehow, and use the ref of the associated tag perhaps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will try and see if I can get something like this working

ref: refs/heads/${{ (matrix.version == 'latest' && 'main') || format('stable-{0}', matrix.version) }}
Loading