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): Use Trivy cache to avoid CI error #63

Merged
merged 2 commits into from
Oct 12, 2024
Merged

Conversation

Tiryoh
Copy link
Owner

@Tiryoh Tiryoh commented Oct 10, 2024

Summary by CodeRabbit

  • New Features
    • Introduced a new GitHub Actions workflow to update the Trivy vulnerability database cache automatically on a daily schedule.
  • Chores
    • Added functionality for manual initiation of the cache update process.

Copy link

coderabbitai bot commented Oct 10, 2024

Walkthrough

A new GitHub Actions workflow file named update-trivy-cache.yaml has been introduced. This workflow is scheduled to run daily at 1:30 AM UTC and can also be triggered manually. It includes a job that updates the Trivy vulnerability database cache by downloading and extracting the necessary databases from specified container registries, and caching them for future use in vulnerability scanning workflows.

Changes

File Path Change Summary
.github/workflows/update-trivy-cache.yaml Added a new workflow to update and cache the Trivy vulnerability database. Includes steps for downloading, extracting, and caching databases.

Poem

In the moonlight, the cache shall grow,
With Trivy's secrets, we'll surely know.
Each day at midnight, a dance we’ll do,
Updating our treasures, all shiny and new!
Hops of joy, as we scan with glee,
For safety and security, just wait and see! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
.github/workflows/update-trivy-cache.yaml (3)

14-16: Minor improvement: Use double quotes in shell command.

The shell command looks good, but it's a best practice to use double quotes around variable expansions to prevent potential issues with word splitting or globbing.

Consider updating the command as follows:

- run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
+ run: echo "date=$(date +'%Y-%m-%d')" >> "$GITHUB_OUTPUT"
🧰 Tools
🪛 actionlint

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

(shellcheck)


18-23: Improve shell script robustness and consider error handling.

The step correctly handles potential failures by trying an alternative registry, which is good. However, there are a few improvements we can make:

  1. Use double quotes around variable expansions.
  2. Consider adding error handling for the extraction step.

Here's an improved version of the script:

 run: |
-  mkdir -p $GITHUB_WORKSPACE/.cache/trivy/db
-  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
+  mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/db"
+  if ! (oras pull ghcr.io/aquasecurity/trivy-db:2 || oras pull ghcr.io/tiryoh/aquasecurity/trivy-db:2); then
+    echo "Failed to pull Trivy database" >&2
+    exit 1
+  fi
+  if ! tar -xzf db.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/db"; then
+    echo "Failed to extract Trivy database" >&2
+    exit 1
+  fi
+  rm db.tar.gz

Would you like me to implement these changes or provide any additional enhancements to improve error handling or logging?

🧰 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:3:23: Double quote to prevent globbing and word splitting

(shellcheck)


32-36: LGTM: Caching step is well-implemented.

The caching step is correctly implemented using the actions/cache/save@v4 action. The cache key includes the current date, which ensures that the cache is updated daily. This is a good practice for maintaining up-to-date vulnerability databases.

Consider adding a step to list the contents of the cache directory before saving, which can help with debugging:

- name: List cache contents
  run: ls -R ${{ github.workspace }}/.cache/trivy

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

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9e4caa0 and 2ac28a5.

📒 Files selected for processing (1)
  • .github/workflows/update-trivy-cache.yaml (1 hunks)
🧰 Additional context used
🪛 actionlint
.github/workflows/update-trivy-cache.yaml

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

(shellcheck)


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:3:23: Double quote to prevent globbing and word splitting

(shellcheck)


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

(shellcheck)


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

(shellcheck)

🔇 Additional comments (2)
.github/workflows/update-trivy-cache.yaml (2)

1-8: LGTM: Workflow name and triggers are well-defined.

The workflow name accurately describes its purpose, and the triggers (daily schedule and manual) are appropriate for updating vulnerability databases.


1-36: Overall, this is a well-implemented workflow for updating the Trivy cache.

The workflow successfully achieves its purpose of regularly updating and caching the Trivy vulnerability databases. It includes appropriate triggers, error handling, and caching mechanisms. The suggested improvements are minor and focus on shell scripting best practices and potential refactoring for better maintainability.

Great job on implementing this workflow to enhance the security scanning process in your CI/CD pipeline!

🧰 Tools
🪛 actionlint

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

(shellcheck)


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:3:23: Double quote to prevent globbing and word splitting

(shellcheck)


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

(shellcheck)


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

(shellcheck)

Comment on lines 25 to 30
- name: Download and extract the Java DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/java-db
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
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve shell script and consider refactoring for DRY principle.

This step is structurally similar to the previous one, which is good for consistency. However, we can make some improvements:

  1. Use double quotes around variable expansions.
  2. Consider refactoring this step and the previous one to reduce duplication.

Here's an improved version of the script:

 run: |
-  mkdir -p $GITHUB_WORKSPACE/.cache/trivy/java-db
-  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
+  mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/java-db"
+  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 Java database" >&2
+    exit 1
+  fi
+  if ! tar -xzf javadb.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/java-db"; then
+    echo "Failed to extract Java database" >&2
+    exit 1
+  fi
+  rm javadb.tar.gz

Additionally, to reduce duplication, we could refactor both this step and the previous one into a reusable function or a separate action. Would you like assistance in implementing this refactoring?

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Download and extract the Java DB
run: |
mkdir -p $GITHUB_WORKSPACE/.cache/trivy/java-db
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
- name: Download and extract the Java DB
run: |
mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/java-db"
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 Java database" >&2
exit 1
fi
if ! tar -xzf javadb.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/java-db"; then
echo "Failed to extract Java database" >&2
exit 1
fi
rm javadb.tar.gz
🧰 Tools
🪛 actionlint

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

(shellcheck)


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

(shellcheck)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
.github/workflows/update-trivy-cache.yaml (1)

10-16: Minor improvement: Quote the echo argument.

The job and step definitions look good. However, there's a minor improvement we can make to the echo command in the "Get current date" step:

-        run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
+        run: echo "date=$(date +'%Y-%m-%d')" >> "$GITHUB_OUTPUT"

This change adds quotes around $GITHUB_OUTPUT to prevent potential issues with word splitting or globbing.

🧰 Tools
🪛 actionlint

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

(shellcheck)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2ac28a5 and 94de248.

📒 Files selected for processing (1)
  • .github/workflows/update-trivy-cache.yaml (1 hunks)
🧰 Additional context used
🪛 actionlint
.github/workflows/update-trivy-cache.yaml

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

(shellcheck)

🔇 Additional comments (4)
.github/workflows/update-trivy-cache.yaml (4)

1-8: LGTM: Well-structured workflow configuration.

The workflow name is descriptive, and the trigger configuration is appropriate for the task. The added comments provide valuable context for users, explaining the purpose of this workflow and how it relates to actual Trivy scans.


18-29: LGTM: Well-implemented database download and extraction.

This step is robustly implemented with several noteworthy features:

  1. Proper directory creation with quoted paths.
  2. Fallback mechanism using two registries.
  3. Effective error handling for both pull and extract operations.
  4. Cleanup of the downloaded file after extraction.
  5. Correct use of quoting and error redirection.

These practices contribute to a reliable and maintainable workflow.


44-48: LGTM: Effective use of caching.

This step makes good use of the actions/cache/save@v4 action to cache the downloaded databases. The cache key includes the current date, which ensures that the cache is updated daily. This approach is well-suited for the workflow's purpose of maintaining an up-to-date Trivy cache.


1-48: Overall, well-implemented workflow with minor improvement opportunities.

This new workflow for updating the Trivy cache is well-structured and achieves its intended purpose effectively. Key strengths include:

  1. Clear naming and scheduling.
  2. Robust error handling and fallback mechanisms.
  3. Effective use of GitHub Actions features like outputs and caching.

There are a few minor improvements that could be made:

  1. Quoting the $GITHUB_OUTPUT variable in the "Get current date" step.
  2. Refactoring the database download and extraction steps to reduce duplication.

These changes would further enhance the maintainability and robustness of the workflow. Great job on implementing this important CI/CD improvement!

🧰 Tools
🪛 actionlint

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

(shellcheck)

Comment on lines +31 to +42
- name: Download and extract the Java DB
run: |
mkdir -p "$GITHUB_WORKSPACE/.cache/trivy/java-db"
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 Java database" >&2
exit 1
fi
if ! tar -xzf javadb.tar.gz -C "$GITHUB_WORKSPACE/.cache/trivy/java-db"; then
echo "Failed to extract Java database" >&2
exit 1
fi
rm javadb.tar.gz
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring to reduce duplication.

This step is well-implemented and follows the same robust practices as the previous step. However, there's significant duplication between this step and the "Download and extract the vulnerability DB" step.

To improve maintainability, consider refactoring these steps into a reusable function or a composite action. This would reduce duplication and make future updates easier.

Here's a high-level suggestion for refactoring:

  1. Create a composite action (e.g., download-and-extract-db.yml) with inputs for the database name, directory, and registry URLs.
  2. Replace both steps with calls to this new action.

Would you like assistance in implementing this refactoring?

@Tiryoh Tiryoh merged commit f7da180 into master Oct 12, 2024
@Tiryoh Tiryoh deleted the feature/use-trivy-cache branch October 12, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant