-
Notifications
You must be signed in to change notification settings - Fork 0
feat(actions): add trivy-tofu-scan reusable workflow #64
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dcfc1eb
docs: add trivy-tofu-scan design spec
262ab32
feat(actions): add trivy-tofu-scan reusable workflow for OpenTofu IaC…
c580b8f
feat(actions): add pr-checks-actions workflow for syntax and secret s…
06be15a
fix(actions): replace rhysd/actionlint-action with devops-actions/act…
75409dd
fix(actions): resolve actionlint and shellcheck errors across workflows
0399de6
fix(actions): run actionlint via CLI to support -ignore flag
930125a
chore(actions): bump actionlint to v1.7.12
948072e
Merge remote-tracking branch 'origin/main' into feat/tofutrivy
6201ecb
fix(actions): replace metadata-file with step output in docker-build-…
97493c5
Merge branch 'main' into feat/tofutrivy
davidf-null File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| name: pr-checks-actions | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| actionlint: | ||
| name: Validate Actions syntax | ||
| runs-on: ubuntu-24.04 | ||
| env: | ||
| FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Install actionlint | ||
| run: | | ||
| wget -q https://github.com/rhysd/actionlint/releases/download/v1.7.12/actionlint_1.7.12_linux_amd64.tar.gz | ||
| tar xzf actionlint_1.7.12_linux_amd64.tar.gz actionlint | ||
|
|
||
| - name: Run actionlint | ||
| run: | | ||
| ./actionlint -ignore 'unknown permission scope "models"' | ||
|
|
||
| secret-scan: | ||
| name: Scan for credentials | ||
| runs-on: ubuntu-24.04 | ||
| env: | ||
| FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Run Trivy secret scan | ||
| uses: aquasecurity/trivy-action@v0.36.0 | ||
| with: | ||
| scan-type: 'fs' | ||
| scan-ref: '.' | ||
| scanners: 'secret' | ||
| format: 'table' | ||
| exit-code: '1' |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| name: trivy-tofu-scan | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| upload_sarif: | ||
| description: 'Upload SARIF results to GitHub Security tab' | ||
| required: false | ||
| type: boolean | ||
| default: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
| security-events: write | ||
|
|
||
| jobs: | ||
| trivy-iac: | ||
| name: Trivy IaC Scan | ||
| runs-on: ubuntu-24.04 | ||
| env: | ||
| FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Find OpenTofu/Terraform files | ||
| id: find | ||
| run: | | ||
| DIRS=$(find . -name "*.tf" -not -path "./.terraform/*" -exec dirname {} \; | sort -u | tr '\n' ' ') | ||
| if [ -z "$DIRS" ]; then | ||
| echo "No .tf files found, skipping scan" | ||
| echo "has_tf_files=false" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "Found .tf files in: $DIRS" | ||
| echo "has_tf_files=true" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Run Trivy IaC scan | ||
| if: steps.find.outputs.has_tf_files == 'true' | ||
| id: scan | ||
| uses: aquasecurity/trivy-action@v0.36.0 | ||
| with: | ||
| scan-type: 'config' | ||
| scan-ref: '.' | ||
| format: 'json' | ||
| output: 'trivy-results.json' | ||
| exit-code: '1' | ||
| severity: 'CRITICAL,HIGH' | ||
| skip-dirs: '.terraform' | ||
| continue-on-error: true | ||
|
|
||
| - name: Generate SARIF report | ||
| if: steps.find.outputs.has_tf_files == 'true' && inputs.upload_sarif | ||
| uses: aquasecurity/trivy-action@v0.36.0 | ||
| with: | ||
| scan-type: 'config' | ||
| scan-ref: '.' | ||
| format: 'sarif' | ||
| output: 'results.sarif' | ||
| severity: 'CRITICAL,HIGH' | ||
| skip-dirs: '.terraform' | ||
|
|
||
| - name: Upload SARIF to GitHub Security tab | ||
| if: steps.find.outputs.has_tf_files == 'true' && inputs.upload_sarif && hashFiles('results.sarif') != '' | ||
| uses: github/codeql-action/upload-sarif@v4 | ||
| with: | ||
| sarif_file: results.sarif | ||
| category: trivy-iac | ||
| continue-on-error: true | ||
|
|
||
| - name: Upload scan results artifact | ||
| if: steps.find.outputs.has_tf_files == 'true' | ||
| uses: actions/upload-artifact@v7 | ||
| with: | ||
| name: trivy-iac-scan-results | ||
| path: trivy-results.json | ||
|
|
||
| - name: Fail if misconfigurations found | ||
| if: steps.scan.outcome == 'failure' | ||
| run: exit 1 |
This file contains hidden or 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
91 changes: 91 additions & 0 deletions
91
docs/superpowers/specs/2026-05-20-trivy-tofu-scan-design.md
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Design: trivy-tofu-scan reusable workflow | ||
|
|
||
| **Date:** 2026-05-20 | ||
| **Author:** David Fernandez | ||
| **Status:** Approved | ||
|
|
||
| ## Summary | ||
|
|
||
| Replace `tfsec.yml` with a new reusable workflow `trivy-tofu-scan.yml` that uses Trivy to scan OpenTofu/Terraform IaC code for misconfigurations. The workflow generates a full report persisted in four ways: SARIF upload to the GitHub Security tab, JSON artifact, Job Summary, and PR comment. | ||
|
|
||
| ## Context | ||
|
|
||
| The repo already has `tfsec.yml` for IaC security scanning. Trivy covers the same surface (OpenTofu/Terraform misconfigurations) via `--scanners misconfig` and provides richer output options, active maintenance, and a unified tool already used in `docker-security-scan.yml` and `ecr-security-scan.yml`. | ||
|
|
||
| ## Architecture | ||
|
|
||
| A single file `.github/workflows/trivy-tofu-scan.yml` with `on: workflow_call`. Follows the exact pattern of `tfsec.yml`, `docker-security-scan.yml`, and `ecr-security-scan.yml`. | ||
|
|
||
| ### Permissions | ||
|
|
||
| ```yaml | ||
| permissions: | ||
| contents: read | ||
| security-events: write # SARIF upload to GitHub Security tab | ||
| pull-requests: write # PR comment | ||
| ``` | ||
|
|
||
| ### Inputs | ||
|
|
||
| | Input | Type | Default | Description | | ||
| |---|---|---|---| | ||
| | `upload_sarif` | boolean | `true` | Upload SARIF to GitHub Security tab | | ||
| | `post_comment` | boolean | `true` | Post comment on PR if findings found | | ||
|
|
||
| Severity is hardcoded to `CRITICAL,HIGH` — not configurable. | ||
|
|
||
| ## Job: `trivy-iac` | ||
|
|
||
| Runner: `ubuntu-24.04`. Env: `FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true`. | ||
|
|
||
| ### Steps | ||
|
|
||
| 1. **Checkout** — `actions/checkout@v6` | ||
|
|
||
| 2. **Find .tf files** — `find . -name "*.tf" -not -path "./.terraform/*"`. Sets `has_tf_files` output. Early exits (skip remaining steps) if no `.tf` files found. | ||
|
|
||
| 3. **Run Trivy IaC scan** — Installs Trivy CLI (pinned version), runs: | ||
| ``` | ||
| trivy config . --scanners misconfig --severity CRITICAL,HIGH \ | ||
| --format json --output trivy-results.json \ | ||
| --exit-code 1 | ||
| ``` | ||
| Uses `continue-on-error: true` to allow subsequent reporting steps to run. Captures exit code in step output. | ||
|
|
||
| 4. **Run Trivy SARIF export** — Re-runs Trivy with `--format sarif --output results.sarif --soft-fail`. Only runs if `has_tf_files == 'true'` and `inputs.upload_sarif`. | ||
|
|
||
| 5. **Upload SARIF** — `github/codeql-action/upload-sarif@v4` with `category: trivy-iac`. Runs if `upload_sarif` input is true and `results.sarif` exists. | ||
|
|
||
| 6. **Generate Job Summary** — Bash script parses `trivy-results.json` with `jq`, builds a markdown table of findings (ID, severity, resource, message) and writes to `$GITHUB_STEP_SUMMARY`. Always runs if `has_tf_files == 'true'`. | ||
|
|
||
| 7. **Upload artifact** — `actions/upload-artifact@v4` uploads `trivy-results.json` as `trivy-iac-scan-results`. Always runs if `has_tf_files == 'true'` (even on clean scans — artifact confirms the scan ran). | ||
|
|
||
| 8. **Post PR comment** — `actions/github-script@v9` posts a comment with finding count and link to the run. Runs if `post_comment` is true, `github.event_name == 'pull_request'`, and findings were found. | ||
|
|
||
| 9. **Fail if findings** — `run: exit 1` if Trivy step exit code was non-zero. | ||
|
|
||
| ## Error handling | ||
|
|
||
| - No `.tf` files: steps 3–9 are skipped via `if: steps.find.outputs.has_tf_files == 'true'`. Workflow exits green. | ||
| - SARIF upload failure: `continue-on-error: true` so it doesn't block the fail step. | ||
| - Trivy install failure: the job fails immediately (no `continue-on-error`). | ||
|
|
||
| ## Migration from tfsec | ||
|
|
||
| Callers replace: | ||
| ```yaml | ||
| uses: nullplatform/actions-nullplatform/.github/workflows/tfsec.yml@main | ||
| ``` | ||
| with: | ||
| ```yaml | ||
| uses: nullplatform/actions-nullplatform/.github/workflows/trivy-tofu-scan.yml@main | ||
| ``` | ||
|
|
||
| The `minimum_severity` input from `tfsec.yml` has no equivalent — severity is fixed to `CRITICAL,HIGH`. | ||
|
|
||
| ## Files | ||
|
|
||
| | Action | File | | ||
| |---|---| | ||
| | Create | `.github/workflows/trivy-tofu-scan.yml` | | ||
| | Delete (or deprecate) | `.github/workflows/tfsec.yml` | |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.