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: Adding step for install #37276

Merged
merged 2 commits into from
Nov 7, 2024
Merged

Conversation

sagar-qa007
Copy link
Contributor

@sagar-qa007 sagar-qa007 commented Nov 7, 2024

Description

Adding installation step.

Fixes #37036

Automation

/ok-to-test tags=""

🔍 Cypress test results

Caution

If you modify the content in this section, you are likely to disrupt the CI result for your PR.

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Enhanced vulnerability scanning with new checks for Docker Scout and Trivy vulnerabilities.
    • Added automatic logging and failure alerts for detected vulnerabilities.
  • Bug Fixes

    • Improved error handling and control flow during Trivy installation and scanning processes.
  • Chores

    • Updated scripts for better installation logic and output formatting related to vulnerability data.

Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

Walkthrough

The pull request introduces enhancements to the vulnerability scanning process within GitHub Actions. It modifies the workflow file to include a new step for checking vulnerabilities after running the Scout and Trivy scripts. Additionally, the scripts for Scout and Trivy have been updated to improve error handling, installation procedures, and data processing, ensuring that any detected vulnerabilities are properly logged and managed.

Changes

File Change Summary
.github/workflows/test-vulnerabilities-data.yml Added a step to check for new vulnerabilities in Scout and Trivy files, logging and failing the job if vulnerabilities are found.
scripts/scout_vulnerabilities_data.sh Introduced install_docker_scout function, added Docker checks, defined CSV output file, and improved vulnerability processing logic.
scripts/trivy_vulnerabilities_data.sh Enhanced error handling and installation logic for Trivy, added retry logic, and refined data processing and cleanup steps.

Assessment against linked issues

Objective Addressed Explanation
Run Trivy and Scout vulnerabilities scan in GitHub action with image name (#37036)

Possibly related PRs

  • chore: Added scout & trivy scan to github workflow #37022: This PR directly relates to the main PR as it introduces the integration of Scout and Trivy vulnerability scanners into the GitHub workflow, which is the primary focus of the main PR's changes to the test-vulnerabilities-data.yml file.

Suggested labels

Task, skip-changelog, ok-to-test

Suggested reviewers

  • sharat87
  • yatinappsmith

In the realm of code where scanners dwell,
New checks for vulnerabilities ring a bell.
With Docker and Trivy now in the fray,
Scanning for issues, come what may!
A workflow enhanced, robust and bright,
Protecting our code, day and night! 🌟


🪧 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.

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.

@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Nov 7, 2024
Copy link
Contributor

@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: 4

🧹 Outside diff range and nitpick comments (7)
.github/workflows/test-vulnerabilities-data.yml (2)

Line range hint 64-80: Enhance error handling and output formatting in vulnerability check step.

The vulnerability check implementation is good, but could be more robust with these improvements:

 - name: Check for new vulnerabilities in Scout and Trivy files
   if: always()
   run: |
+    # Function to check vulnerability file
+    check_vulnerabilities() {
+      local file=$1
+      local tool=$2
+      
+      if [ ! -f "$file" ]; then
+        echo "::warning ::$tool vulnerability file not found"
+        return 0
+      fi
+      
+      if [ -s "$file" ]; then
+        echo "::error ::$tool vulnerabilities detected:"
+        echo "----------------------------------------"
+        cat "$file"
+        echo "----------------------------------------"
+        return 1
+      fi
+    }
+    
-    # Check if Scout vulnerabilities file is not empty
-    if [ -s "scout_new_vulnerabilities.csv" ]; then
-      echo "Scout vulnerabilities detected."
-      cat scout_new_vulnerabilities.csv
-      exit 1  # Fail the job if data exists
-    fi
-    
-    # Check if Trivy vulnerabilities file is not empty
-    if [ -s "trivy_new_vulnerabilities.csv" ]; then
-      echo "Trivy vulnerabilities detected."
-      cat trivy_new_vulnerabilities.csv
-      exit 1  # Fail the job if data exists
-    fi
+    # Check both vulnerability files
+    check_vulnerabilities "scout_new_vulnerabilities.csv" "Scout" || exit 1
+    check_vulnerabilities "trivy_new_vulnerabilities.csv" "Trivy" || exit 1
🧰 Tools
🪛 yamllint

[error] 74-74: trailing spaces

(trailing-spaces)


64-64: Remove trailing spaces.

There are trailing spaces on lines 64 and 74 that should be removed for consistent code style.

-      
+
-          
+

Also applies to: 74-74

🧰 Tools
🪛 yamllint

[error] 64-64: trailing spaces

(trailing-spaces)

scripts/trivy_vulnerabilities_data.sh (5)

Line range hint 4-11: Consider enhancing credential security.

While the environment variable validation is thorough, consider implementing additional security measures for database credentials:

  • Use a secrets management service
  • Implement credential rotation
  • Add input sanitization for database parameters

Line range hint 20-47: Add version pinning and checksum verification.

The Trivy installation function needs security enhancements:

 TRIVY_VERSION=$(curl -s https://api.github.com/repos/aquasecurity/trivy/releases/latest | grep '"tag_name"' | sed -E 's/.*"v([^"]+)".*/\1/')
+TRIVY_CHECKSUM_URL="https://github.com/aquasecurity/trivy/releases/download/v$TRIVY_VERSION/trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz.sha256"
+EXPECTED_CHECKSUM=$(curl -sL "$TRIVY_CHECKSUM_URL")
 TRIVY_URL="https://github.com/aquasecurity/trivy/releases/download/v$TRIVY_VERSION/trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz"
-curl -sfL "$TRIVY_URL" | tar -xzf - trivy
+curl -sfLO "$TRIVY_URL"
+echo "$EXPECTED_CHECKSUM trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz" | sha256sum -c
+tar -xzf "trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz" trivy

Line range hint 77-89: Add scan timeout and externalize configuration.

The Trivy scan configuration could be improved:

+# Add timeout to prevent hanging
+SCAN_TIMEOUT=300
+
 # Run Trivy scan
 echo "Running Trivy scan for image: $IMAGE..."
-trivy image --db-repository public.ecr.aws/aquasecurity/trivy-db --java-db-repository public.ecr.aws/aquasecurity/trivy-java-db --insecure --format json "$IMAGE" > "trivy_vulnerabilities.json" || {
+timeout $SCAN_TIMEOUT trivy image \
+  --db-repository "${TRIVY_DB_REPO:-public.ecr.aws/aquasecurity/trivy-db}" \
+  --java-db-repository "${TRIVY_JAVA_DB_REPO:-public.ecr.aws/aquasecurity/trivy-java-db}" \
+  --insecure \
+  --format json \
+  "$IMAGE" > "trivy_vulnerabilities.json" || {
     echo "Error: Trivy scan failed for image: $IMAGE"
     exit 1
 }

Line range hint 92-146: Enhance database operations reliability.

The database operations need improvements:

  1. Add batch processing for better performance
  2. Implement connection pooling
  3. Add retry logic for database operations
  4. Add proper error handling for database connection failures

Would you like me to provide a detailed implementation for these improvements?


156-156: Improve cleanup operations.

Add comprehensive cleanup with error handling:

-rm -f "trivy_vulnerabilities.json"
+cleanup() {
+    local exit_code=$?
+    rm -f "trivy_vulnerabilities.json" "$NEW_VULN_FILE" || true
+    exit $exit_code
+}
+trap cleanup EXIT
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d00c15d and 89f775f.

📒 Files selected for processing (3)
  • .github/workflows/test-vulnerabilities-data.yml (2 hunks)
  • scripts/scout_vulnerabilities_data.sh (2 hunks)
  • scripts/trivy_vulnerabilities_data.sh (1 hunks)
🧰 Additional context used
🪛 yamllint
.github/workflows/test-vulnerabilities-data.yml

[error] 64-64: trailing spaces

(trailing-spaces)


[error] 74-74: trailing spaces

(trailing-spaces)

Comment on lines +51 to +54
if ! systemctl is-active --quiet docker; then
echo "Starting Docker..."
sudo systemctl start docker
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid using 'sudo' within scripts; handle Docker daemon status appropriately

Using sudo may prompt for passwords and is not recommended in scripts. Instead, check if the Docker daemon is running and inform the user if it isn't.

Apply this diff:

if ! systemctl is-active --quiet docker; then
     echo "Docker is not running. Please start Docker and try again."
     exit 1
fi
📝 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
if ! systemctl is-active --quiet docker; then
echo "Starting Docker..."
sudo systemctl start docker
fi
if ! systemctl is-active --quiet docker; then
echo "Docker is not running. Please start Docker and try again."
exit 1
fi

Comment on lines +84 to +85
[ -s "$CSV_OUTPUT_FILE" ] || echo "No vulnerabilities found for image: $IMAGE" > "$CSV_OUTPUT_FILE"

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Avoid writing messages into CSV output files

Writing non-CSV messages into the CSV file can break downstream processing. Handle the case separately without altering the CSV file.

Apply this diff:

# Check if the CSV output file is empty
-[ -s "$CSV_OUTPUT_FILE" ] || echo "No vulnerabilities found for image: $IMAGE" > "$CSV_OUTPUT_FILE"
+if [ ! -s "$CSV_OUTPUT_FILE" ]; then
+    echo "No vulnerabilities found for image: $IMAGE"
+fi
📝 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
[ -s "$CSV_OUTPUT_FILE" ] || echo "No vulnerabilities found for image: $IMAGE" > "$CSV_OUTPUT_FILE"
if [ ! -s "$CSV_OUTPUT_FILE" ]; then
echo "No vulnerabilities found for image: $IMAGE"
fi

Comment on lines +30 to +31
curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
sh install-scout.sh &> install_scout_log.txt
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Verify 'curl' command success before executing the installation script

Ensure that the 'curl' command successfully downloads the installation script before executing it to prevent errors from incomplete downloads.

Apply this diff to add an error check:

curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
+if [ $? -ne 0 ]; then
+    echo "Failed to download Docker Scout installation script."
+    exit 1
+fi
sh install-scout.sh &> install_scout_log.txt
📝 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
curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
sh install-scout.sh &> install_scout_log.txt
curl -fsSL https://raw.githubusercontent.com/docker/scout-cli/main/install.sh -o install-scout.sh
if [ $? -ne 0 ]; then
echo "Failed to download Docker Scout installation script."
exit 1
fi
sh install-scout.sh &> install_scout_log.txt

Comment on lines +74 to +81
docker scout cves "$IMAGE" | grep -E "✗ |CVE-" | awk -v product_name="$product_name" -F' ' '
{
# Check for valid vulnerability data and format it correctly
if ($2 != "" && $3 ~ /^CVE-/) {
# Extract severity level, CVE ID, and format output correctly
print $3","product_name",""SCOUT"","$2
}
}' | sort -u > "$CSV_OUTPUT_FILE"
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use structured output for reliable parsing of vulnerabilities

Parsing command-line output is fragile. If available, use JSON output from docker scout cves for more reliable data extraction.

Example adjustment:

-docker scout cves "$IMAGE" | grep -E "✗ |CVE-" | awk -v product_name="$product_name" -F' ' '
-{
-    if ($2 != "" && $3 ~ /^CVE-/) {
-        print $3","product_name",""SCOUT"","$2
-    }
-}' | sort -u > "$CSV_OUTPUT_FILE"
+docker scout cves "$IMAGE" --output json | jq -r '.vulnerabilities[] | "\(.identifiers.CVE[0]),'"$product_name"',SCOUT,\(.severity)"' | sort -u > "$CSV_OUTPUT_FILE"
📝 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
docker scout cves "$IMAGE" | grep -E "✗ |CVE-" | awk -v product_name="$product_name" -F' ' '
{
# Check for valid vulnerability data and format it correctly
if ($2 != "" && $3 ~ /^CVE-/) {
# Extract severity level, CVE ID, and format output correctly
print $3","product_name",""SCOUT"","$2
}
}' | sort -u > "$CSV_OUTPUT_FILE"
docker scout cves "$IMAGE" --output json | jq -r '.vulnerabilities[] | "\(.identifiers.CVE[0]),'"$product_name"',SCOUT,\(.severity)"' | sort -u > "$CSV_OUTPUT_FILE"

@yatinappsmith yatinappsmith merged commit 879fb6d into release Nov 7, 2024
17 checks passed
@yatinappsmith yatinappsmith deleted the chore/addingmissingstep branch November 7, 2024 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants