Skip to content

Implement Basic Trivy Scanning Workflow - #256

Merged
josecelano merged 15 commits into
mainfrom
251-implement-basic-trivy-scanning-workflow
Dec 29, 2025
Merged

josecelano merged 15 commits into
mainfrom
251-implement-basic-trivy-scanning-workflow

Conversation

@josecelano

Copy link
Copy Markdown
Member

Closes #251

Overview

Implements a GitHub Actions workflow that uses Trivy to scan Docker images for vulnerabilities. This initial implementation uses a hardcoded list of images and provides immediate security coverage.

Changes

1. GitHub Actions Workflow (.github/workflows/docker-security-scan.yml)

Created a new workflow with two jobs:

Scan Project-Built Images

  • Builds and scans 2 project-built images:
    • torrust-tracker-deployer/provisioned-instance
    • torrust-tracker-deployer/ssh-server

Scan Third-Party Images

  • Scans 4 third-party images used in docker-compose:
    • torrust/tracker:develop
    • mysql:8.0
    • grafana/grafana:11.4.0
    • prom/prometheus:v3.0.1

Workflow Features:

  • ✅ Scans for HIGH/CRITICAL vulnerabilities only
  • ✅ Fails builds when vulnerabilities detected (exit-code: 1)
  • ✅ Uploads results to GitHub Security tab (SARIF format)
  • ✅ Runs on push to main/develop
  • ✅ Runs on PRs affecting Docker files
  • ✅ Runs daily at 6 AM UTC
  • ✅ Supports manual triggering

Notes:

2. README Update

Added workflow badge to display scan status.

Testing

  • ✅ Pre-commit checks passed (./scripts/pre-commit.sh)
  • ✅ YAML linting passed
  • ✅ All unit tests passed
  • ✅ E2E tests passed

Future Work

This is Phase 1 of the security scanning epic. Phase 2 (#252) will:

  • Dynamically detect images from environment configuration
  • Eliminate need for manual updates when images change
  • Integrate with show command to list scanned images

Related

@github-advanced-security

Copy link
Copy Markdown

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@josecelano
josecelano requested a review from da2ce7 December 23, 2025 17:51
@josecelano
josecelano marked this pull request as draft December 23, 2025 18:34
@josecelano
josecelano marked this pull request as ready for review December 23, 2025 19:23
@josecelano
josecelano marked this pull request as draft December 23, 2025 20:10
@josecelano

Copy link
Copy Markdown
Member Author

Viewing Security Scan Results

The workflow successfully uploads SARIF results to GitHub Security, but the default Security tab view filters by is:open branch:main, which means results from PR branches are not visible in the default view.

How to View Results

  • For this PR: https://github.com/torrust/torrust-tracker-deployer/security/code-scanning?query=pr:256+is:open
  • For PR branch: /security/code-scanning?query=is:open+branch:251-implement-basic-trivy-scanning-workflow
  • After merging: Results will appear in the default Security tab view (/security/code-scanning?query=is:open+branch:main)

All six SARIF uploads are completing successfully:

  • ✅ docker-project-provisioned-instance
  • ✅ docker-project-ssh-server
  • ✅ docker-third-party-mysql-8.0
  • ✅ docker-third-party-torrust-tracker-develop
  • ✅ docker-third-party-grafana-grafana-11.4.0
  • ✅ docker-third-party-prom-prometheus-v3.0.1

This is standard GitHub behavior for code scanning - PR results are filtered separately from main branch results.

@josecelano

Copy link
Copy Markdown
Member Author

Viewing Security Scan Results

The workflow successfully uploads SARIF results to GitHub Security, but the default Security tab view filters by 'is:open branch:main', which means results from PR branches are not visible in the default view.

How to View Results

All six SARIF uploads are completing successfully with unique categories. This is standard GitHub behavior for code scanning - PR results are filtered separately from main branch results.

- Add comment in workflow explaining filter behavior
- Add section in ADR explaining how to view results by branch/PR
- Add PR comment with direct links to view scan results

The default Security tab filters by 'is:open branch:main' which
hides PR branch results. Users must use specific PR/branch filters
to see results before merging to main.
@josecelano

Copy link
Copy Markdown
Member Author

ACK 40dd234

@josecelano
josecelano marked this pull request as ready for review December 23, 2025 20:31
@josecelano

Copy link
Copy Markdown
Member Author

Design Decision: Upload Steps Pattern

The upload job currently uses 6 individual steps instead of a matrix. This is intentional for Phase 1 (hardcoded images).

Why Not Matrix Now?

Matrix would require duplicating the image list:

  • Scan jobs already define matrices for the 6 images
  • Upload job would need to duplicate that list
  • Two sources of truth = synchronization issues
  • Refactoring effort would be throwaway work

Future: Dynamic Array Passing (Issue #252)

Phase 2 will implement dynamic image detection, where:

  • Scan jobs output a JSON array of scanned images
  • Upload job consumes that array via fromJSON(needs.scan.outputs.images)
  • Single source of truth: scan jobs determine what gets uploaded
  • Zero manual maintenance when images change

Example for Phase 2:

scan-images:
  outputs:
    scanned_images: ${{ steps.collect.outputs.images }}

upload-sarif:
  needs: scan-images
  strategy:
    matrix:
      image: ${{ fromJSON(needs.scan-images.outputs.scanned_images) }}

Decision Rationale

Keep current implementation because:

  • ✅ Simple, explicit, easy to debug
  • ✅ Works perfectly for hardcoded lists
  • ✅ Avoids premature optimization
  • ✅ Phase 2 refactoring will be more comprehensive anyway

References:

@josecelano
josecelano merged commit 9d8c4b8 into main Dec 29, 2025
@josecelano
josecelano deleted the 251-implement-basic-trivy-scanning-workflow branch April 15, 2026 16:38

@da2ce7 da2ce7 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a post-merge review of the Docker image scanning workflow from #256 (merge 9d8c4b8), read as it stands at f782b40 on the default branch.

What still runs: .github/workflows/docker-security-scan.yml keeps the shape #256 gave it. It builds the project images locally and scans them, scans the third-party images the deployment uses, and sends one SARIF file per image to code scanning under its own category. It runs on pushes to main and develop, on pull requests that touch the scanned paths, daily at 06:00 UTC and on manual dispatch. Every scan uses exit code 0, as docs/decisions/docker-security-scan-exit-code-zero.md decides, so beyond the job log the Security tab is the only place a finding shows. Since the merge, the project matrix has grown to four images, the extract-images job now produces the third-party list, and the third-party upload has moved into the scan job. Two claims in the description do not match the merged bytes. It says builds fail on findings with exit code 1, which the decision record in the same pull request reverses. It also mentions a TODO for #252, which the workflow does not contain.

[Major] The two project images this repository publishes never reach code scanning. The project matrix builds and scans deployer and tracker-backup (.github/workflows/docker-security-scan.yml:37, :46) and stores their SARIF as artifacts. But upload-sarif-results (:251) has upload steps only for provisioned-instance (:278) and ssh-server (:286), one hand-written step per image as #256 laid it out. ff4a4d9 and bb3d5ea added the matrix entries without adding matching steps. So when the daily run finds a new HIGH or CRITICAL CVE in an image that .github/workflows/container.yaml or .github/workflows/backup-container.yaml publishes, the finding appears only in the log of a green run. The backup image is the one that templates/docker-compose/docker-compose.yml.tera:234 deploys. Meanwhile the two test containers are tracked in the Security tab. docs/security/production/scans/torrust-tracker-backup.md:212 states that the backup image's results are uploaded there. Smallest fix: add two upload steps with the categories docker-project-deployer and docker-project-tracker-backup. A sturdier fix makes the upload job a matrix over the same image names, so a new image cannot be left out again.

[Minor] The job that runs the third-party scanner action holds security-events: write. b397688 added the permission to scan-third-party-images (.github/workflows/docker-security-scan.yml:197), and that job runs [email protected] twice (:208, :218). #256 had confined the permission to a job that runs only GitHub-owned actions (382f430). The action is pinned by a tag, and whoever controls the action's repository can move a tag. On a push or the daily run, code behind a moved tag would get a token that can upload SARIF and change alert states in this repository's code scanning, so it could hide the findings this workflow exists to raise. The same tag pin at :67 and :81 would let such code rewrite the project SARIF artifacts before the upload job reads them. Smallest fix: pin all four uses to the full commit id behind 0.35.0 and keep the tag as a comment. Moving the third-party upload back into a job that runs no third-party action would also restore the separation.

[Minor] Every SARIF upload carries continue-on-error: true (.github/workflows/docker-security-scan.yml:249, :284, :292). Because every scan also uses exit code 0, a run is green whether or not any result reached code scanning. A rejected upload on a push or the daily run therefore leaves the Security tab stale with no signal, even though the decision record makes that tab the enforcement point. This has already happened once. Between af3fc7b and b397688, the third-party uploads were refused with HTTP 422 (as the message of b397688 records) while the runs stayed green, because the error was swallowed. Smallest fix: continue-on-error: ${{ github.event_name == 'pull_request' }}, so only pull request runs tolerate a refused upload.

[Minor] Third-party categories include the image tag. .github/workflows/docker-security-scan.yml:248 builds the category from the sanitised reference with its tag, as the hard-coded docker-third-party-mysql-8.0 and its siblings in #256 did. A version bump therefore starts a new code scanning configuration, and no later run ever analyses the old one again, so the old configuration's open alerts are never closed by a new analysis. After 36fd5c3 moved Caddy from 2.10.2 to 2.11.2, alerts filed under docker-third-party-caddy-2.10.2 stay open on main for an image that is no longer deployed, until someone deletes that configuration by hand. b5ae273 (Grafana) and 5a2b09a (Prometheus) did the same. Smallest fix: derive the category from the repository part of the reference only, for example by cutting at the first : or @ before the tr at :231. Each upload then replaces the previous analysis of the same image.

[Minor] The extract-images job (.github/workflows/docker-security-scan.yml:98) declares no permissions:, and the workflow has no top-level block, so its token gets the repository default. That job runs the Rust toolchain action at :112, which is referenced by a branch and not by a release or commit, the cache action at :115, and a release build of the whole workspace. If the repository default grants write access, all of this runs with that access on every push and every daily run. Smallest fix: a top-level permissions: contents: read, which leaves the three jobs that declare their own unchanged, plus a commit-id pin for the toolchain action.

[Nit] The deployer image is scanned as its debug stage. The build at .github/workflows/docker-security-scan.yml:59 passes no --target, so it builds the last stage, debug (docker/deployer/Dockerfile:173). That stage adds vim, less, procps and net-tools to the release stage that .github/workflows/container.yaml:61 publishes. Nothing shipped is missed, but the comment at :55 (scan exactly what this repository produces) does not hold for this image, and the debug-only packages add findings. Fix: add a target field to the matrix (release for the deployer, production for the backup image) and pass it as --target.

[Nit] docs/security/production/scans/torrust-tracker-backup.md:211 says the scan runs weekly, but the cron at .github/workflows/docker-security-scan.yml:22 runs it daily.

Checked and found sound:

  • the triggers and path filters;
  • the effect of exit code 0: it suppresses only the vulnerability verdict, so a scanner error such as a failed database download or image pull still fails its step and turns the run red;
  • scanners: vuln on the SARIF steps;
  • the project scan job (:31) and the project upload job (:260), which hold only the permissions they use;
  • the if: always() conditions, which keep partial results;
  • artifact names, which are unique per run;
  • the build contexts at :39 to :48, which match each Dockerfile's COPY paths;
  • the matrix.image value interpolated into the shell at :231, which comes from the repository's own source, so a pull request gains nothing it could not already run through the build;
  • the generated third-party list, which matches the images in templates/docker-compose/docker-compose.yml.tera.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Basic Trivy Scanning Workflow

3 participants