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

chore(ci): Save Trivy cache to avoid Trivy TOOMANYREQUESTS errors #182

Merged
merged 1 commit into from
Oct 9, 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
38 changes: 38 additions & 0 deletions .github/workflows/update-trivy-cache.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Note: This workflow only updates the cache. You should create a separate workflow for your actual Trivy scans.
# In your scan workflow, set TRIVY_SKIP_DB_UPDATE=true and TRIVY_SKIP_JAVA_DB_UPDATE=true.
name: Update Trivy Cache

on:
schedule:
- cron: '0 1 * * *' # Run daily at midnight UTC
workflow_dispatch: # Allow manual triggering

jobs:
update-trivy-db:
runs-on: ubuntu-latest
steps:
- name: Get current date
id: date
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT

- name: Download and extract the vulnerability DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/db
oras copy ghcr.io/aquasecurity/trivy-db:2 ghcr.io/tiryoh/aquasecurity/trivy-db:2 || echo "err"
oras pull ghcr.io/aquasecurity/trivy-db:2 || oras pull ghcr.io/tiryoh/aquasecurity/trivy-db:2
tar -xzf db.tar.gz -C $GITHUB_WORKSPACE/.cache/trivy/db
rm db.tar.gz
Comment on lines +18 to +24
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance error handling and use double quotes for variables.

The fallback mechanism for database download is a good practice. However, there are a few areas that could be improved:

  1. Error handling: Instead of echo "err", consider using proper error messages and exit codes.
  2. Variable quoting: Use double quotes around variables to prevent word splitting and globbing issues.

Consider updating the step as follows:

- name: Download and extract the vulnerability DB
  run: |
    mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/db"
    if ! oras copy ghcr.io/aquasecurity/trivy-db:2 ghcr.io/tiryoh/aquasecurity/trivy-db:2; then
      echo "Failed to copy trivy-db, falling back to direct pull"
    fi
    if ! (oras pull ghcr.io/aquasecurity/trivy-db:2 || oras pull ghcr.io/tiryoh/aquasecurity/trivy-db:2); then
      echo "Failed to pull trivy-db from both sources" >&2
      exit 1
    fi
    tar -xzf db.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/db"
    rm db.tar.gz

This update improves error handling and adds proper quoting for variables.

🧰 Tools
🪛 actionlint

19-19: shellcheck reported issue in this script: SC2086:info:1:10: Double quote to prevent globbing and word splitting

(shellcheck)


19-19: shellcheck reported issue in this script: SC2086:info:4:23: Double quote to prevent globbing and word splitting

(shellcheck)


- name: Download and extract the Java DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/java-db
oras copy ghcr.io/aquasecurity/trivy-java-db:1 ghcr.io/tiryoh/aquasecurity/trivy-java-db:1 || echo "err"
oras pull ghcr.io/aquasecurity/trivy-java-db:1 || oras pull ghcr.io/tiryoh/aquasecurity/trivy-java-db:1
tar -xzf javadb.tar.gz -C $GITHUB_WORKSPACE/.cache/trivy/java-db
rm javadb.tar.gz
Comment on lines +26 to +32
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Apply similar improvements to the Java DB download step.

This step could benefit from the same enhancements suggested for the previous step:

  1. Improved error handling
  2. Proper variable quoting

Consider updating the step as follows:

- name: Download and extract the Java DB
  run: |
    mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/java-db"
    if ! oras copy ghcr.io/aquasecurity/trivy-java-db:1 ghcr.io/tiryoh/aquasecurity/trivy-java-db:1; then
      echo "Failed to copy trivy-java-db, falling back to direct pull"
    fi
    if ! (oras pull ghcr.io/aquasecurity/trivy-java-db:1 || oras pull ghcr.io/tiryoh/aquasecurity/trivy-java-db:1); then
      echo "Failed to pull trivy-java-db from both sources" >&2
      exit 1
    fi
    tar -xzf javadb.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/java-db"
    rm javadb.tar.gz

This update aligns the Java DB step with the improvements suggested for the vulnerability DB step.

🧰 Tools
🪛 actionlint

27-27: shellcheck reported issue in this script: SC2086:info:1:10: Double quote to prevent globbing and word splitting

(shellcheck)


27-27: shellcheck reported issue in this script: SC2086:info:4:27: Double quote to prevent globbing and word splitting

(shellcheck)


- name: Cache DBs
uses: actions/cache/save@v4
with:
path: ${{ github.workspace }}/.cache/trivy
key: cache-trivy-${{ steps.date.outputs.date }}