Skip to content
Merged
Show file tree
Hide file tree
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
86 changes: 73 additions & 13 deletions .github/scripts/trivy-summary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,28 @@
set -euo pipefail

# Trivy Summary: Extract and display Trivy security findings from JSON report.
# Supports both CI filesystem scans and CD image scans.
# Supports both CI filesystem scans and CD/CI image scans.
# Used in GitHub Actions to create a human-readable summary in the job output.
#
#
# Usage:
# trivy-summary.sh [report-file] [scan-type]
#
# trivy-summary.sh [report-file] [scan-type] [gate-mode]
#
# Arguments:
# report-file Path to Trivy JSON report (default: reports/trivy.json)
# scan-type "fs" for filesystem scan or "image" for image scan (auto-detected if omitted)
# gate-mode "blocking" (default) if CRITICAL findings fail the job elsewhere,
# "non-blocking" if this scan is informational only. Only affects
# wording, since the actual pass/fail decision lives in the workflow.

REPORT_FILE="${1:-reports/trivy.json}"
SCAN_TYPE="${2:-auto}"
GATE_MODE="${3:-blocking}"

# If report doesn't exist, print warning and exit gracefully
if [[ ! -f "$REPORT_FILE" ]]; then
echo "Trivy report not found at $REPORT_FILE" >> "$GITHUB_STEP_SUMMARY" echo "Trivy report not found at $REPORT_FILE (continuing gracefully)" >&2 exit 0
echo "Trivy report not found at $REPORT_FILE" >> "$GITHUB_STEP_SUMMARY"
echo "Trivy report not found at $REPORT_FILE (continuing gracefully)" >&2
exit 0
fi

# Auto-detect scan type from report filename if not specified
Expand All @@ -43,12 +49,14 @@ MISC_MEDIUM=$(jq '[.Results[]?.Misconfigurations[]? // empty | select(.Severity
SECRETS=$(jq '[.Results[]?.Secrets[]? // empty] | length' "$REPORT_FILE" 2>/dev/null || echo "0")

# Determine overall status based on scan type
# Image scan: only CRITICAL vulnerabilities block deployment
# Filesystem scan: all findings are informational for summary
STATUS="✅ Passed"
if [[ "$SCAN_TYPE" == "image" ]]; then
if [[ "$VULN_CRITICAL" -gt 0 ]]; then
STATUS="❌ Failed"
if [[ "$GATE_MODE" == "non-blocking" ]]; then
STATUS="⚠️ Critical findings (informational, does not block this workflow)"
else
STATUS="❌ Failed"
fi
fi
else
# Filesystem scan: show all but don't fail on HIGH/MEDIUM
Expand All @@ -57,21 +65,73 @@ else
fi
fi

# Build a readable CRITICAL/HIGH findings table for image scans - severity
# counts alone don't say which package or CVE actually needs fixing.
MAX_FINDING_ROWS=25
FINDINGS_TABLE=""
FINDINGS_COUNT=0
if [[ "$SCAN_TYPE" == "image" ]]; then
FINDINGS_TSV=$(jq -r '
[.Results[]?.Vulnerabilities[]? // empty | select(.Severity == "CRITICAL" or .Severity == "HIGH")]
| sort_by(if .Severity == "CRITICAL" then 0 else 1 end)
| .[]
| [.Severity, .PkgName, .InstalledVersion, (.FixedVersion // "-"), .VulnerabilityID, (.PrimaryURL // "")]
| @tsv
' "$REPORT_FILE" 2>/dev/null || true)

if [[ -n "$FINDINGS_TSV" ]]; then
FINDINGS_COUNT=$(printf '%s\n' "$FINDINGS_TSV" | wc -l | tr -d ' ')
ROWS=""
ROW_INDEX=0
while IFS=$'\t' read -r sev pkg installed fixed id url; do
ROW_INDEX=$((ROW_INDEX + 1))
[[ "$ROW_INDEX" -gt "$MAX_FINDING_ROWS" ]] && break
badge="🟠 HIGH"
[[ "$sev" == "CRITICAL" ]] && badge="🔴 CRITICAL"
id_cell="$id"
[[ -n "$url" ]] && id_cell="[$id]($url)"
ROWS+="| $badge | \`$pkg\` | $installed | $fixed | $id_cell |"$'\n'
done <<< "$FINDINGS_TSV"
FINDINGS_TABLE="$ROWS"
fi
fi

# Write summary to GitHub Step Summary
if [[ "$SCAN_TYPE" == "image" ]]; then
# Container image scan summary (CD)
# Container image scan summary (CD and CI)
{
echo "## Container Security"
echo ""
[[ -n "${IMAGE:-}" ]] && echo "Image: $IMAGE"
[[ -n "${IMAGE:-}" ]] && echo "Image: \`$IMAGE\`"
echo ""
echo "| Severity | Count |"
echo "| --- | ---: |"
echo "| Critical | $VULN_CRITICAL |"
echo "| High | $VULN_HIGH |"
echo "| Medium | $VULN_MEDIUM |"

if [[ "$FINDINGS_COUNT" -gt 0 ]]; then
echo ""
echo "<details>"
if [[ "$FINDINGS_COUNT" -gt "$MAX_FINDING_ROWS" ]]; then
echo "<summary>Critical/High findings (showing $MAX_FINDING_ROWS of $FINDINGS_COUNT - full list in the uploaded report artifact)</summary>"
else
echo "<summary>Critical/High findings ($FINDINGS_COUNT)</summary>"
fi
echo ""
echo "| Severity | Package | Installed | Fixed | ID |"
echo "| --- | --- | --- | --- | --- |"
printf '%s' "$FINDINGS_TABLE"
echo ""
echo "</details>"
fi

echo ""
echo "**Security Gate:** $STATUS"
if [[ "$SCAN_TYPE" == "image" && "$GATE_MODE" == "non-blocking" ]]; then
echo "**Status:** $STATUS"
else
echo "**Security Gate:** $STATUS"
fi
} >> "$GITHUB_STEP_SUMMARY"
else
# Filesystem scan summary (CI)
Expand All @@ -84,7 +144,7 @@ else
echo "| Critical | $VULN_CRITICAL |"
echo "| High | $VULN_HIGH |"
echo "| Medium | $VULN_MEDIUM |"

if [[ "$SECRETS" -gt 0 ]] || [[ "$MISC_CRITICAL" -gt 0 ]] || [[ "$MISC_HIGH" -gt 0 ]]; then
echo ""
echo "### Other Findings"
Expand All @@ -93,7 +153,7 @@ else
[[ "$SECRETS" -gt 0 ]] && echo "| Secrets | $SECRETS |"
[[ "$MISC_CRITICAL" -gt 0 || "$MISC_HIGH" -gt 0 ]] && echo "| Misconfigurations | $((MISC_CRITICAL + MISC_HIGH)) |"
fi

echo ""
echo "**Status:** $STATUS"
} >> "$GITHUB_STEP_SUMMARY"
Expand Down
20 changes: 6 additions & 14 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ jobs:
- name: Create reports directory
run: mkdir -p reports

# Security scan for the locally built image.
# CRITICAL vulnerabilities with available fixes will block the image push and deploy.
# Note: Using exit-code: "0" to always write the report; security gate logic is in the summary script
# Security scan for the locally built image. Informational only - findings
# are surfaced in the job summary but never block the push/deploy below.
# Note: Using exit-code: "0" to always write the report and let the job continue.
- name: Scan image with Trivy
uses: aquasecurity/trivy-action@v0.36.0
with:
Expand All @@ -82,17 +82,10 @@ jobs:
vuln-type: "os,library"
exit-code: "0"

# Trivy Summary & Security Gate for image
- name: Trivy Summary & Security Gate
# Trivy Summary (non-blocking)
- name: Trivy Summary
if: always()
run: |
bash .github/scripts/trivy-summary.sh reports/trivy-image.json image
# Check for CRITICAL vulnerabilities and fail if present
CRITICAL=$(jq '[.Results[]?.Vulnerabilities[]? // empty | select(.Severity == "CRITICAL")] | length' reports/trivy-image.json 2>/dev/null || echo "0")
if [[ "$CRITICAL" -gt 0 ]]; then
echo "❌ Security Gate Failed: $CRITICAL CRITICAL vulnerabilities in image"
exit 1
fi
run: bash .github/scripts/trivy-summary.sh reports/trivy-image.json image non-blocking

# Upload Trivy report for detailed analysis
- name: Upload Trivy Report
Expand All @@ -104,7 +97,6 @@ jobs:
retention-days: 30

# Push the exact image that was scanned above.
# This step only runs if Trivy scan succeeded (no continue-on-error).
- name: Push image
run: |
docker push "${{ env.IMAGE }}:${{ env.IMAGE_TAG_SHA }}"
Expand Down