Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python

feat: add security scan report on github workflows (#2294)

Lucas Oliveirab42beb20068d
diff --git a/.github/workflows/security-scan.yml b/.github/workflows/security-scan.yml
new file mode 100644
index 000000000..e479d8b43
--- /dev/null
+++ b/.github/workflows/security-scan.yml
@@ -0,0 +1,202 @@
+name: Nightly Security Scan
+
+on:
+ schedule:
+ # Run nightly at 02:00 UTC
+ - cron: '0 2 * * *'
+ workflow_dispatch:
+ inputs:
+ fail_on_critical:
+ description: 'Fail workflow if Critical vulnerabilities are found'
+ type: boolean
+ default: false
+
+permissions:
+ contents: read
+ security-events: write
+
+jobs:
+ scan-container-images:
+ name: Container Image Scan (${{ matrix.image.name }})
+ runs-on: ubuntu-latest
+ strategy:
+ fail-fast: false
+ matrix:
+ image:
+ - name: backend
+ file: ./Dockerfile.backend
+ context: .
+ - name: frontend
+ file: ./Dockerfile.frontend
+ context: .
+ - name: langflow
+ file: ./Dockerfile.langflow
+ context: .
+ - name: opensearch
+ file: ./Dockerfile
+ context: .
+ - name: operator
+ file: kubernetes/operator/Dockerfile
+ context: kubernetes/operator
+
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+
+ - name: Build local container image
+ uses: docker/build-push-action@v6
+ with:
+ context: ${{ matrix.image.context }}
+ file: ${{ matrix.image.file }}
+ load: true
+ tags: openrag-${{ matrix.image.name }}:local
+ cache-from: type=gha,scope=security-${{ matrix.image.name }}
+ cache-to: type=gha,mode=max,scope=security-${{ matrix.image.name }}
+
+ - name: Run Trivy Vulnerability Scan (Image)
+ uses: aquasecurity/[email protected]
+ with:
+ image-ref: openrag-${{ matrix.image.name }}:local
+ format: json
+ output: trivy-image-${{ matrix.image.name }}.json
+ exit-code: 0
+ ignore-unfixed: false
+ vuln-type: os,library
+ severity: CRITICAL,HIGH,MEDIUM,LOW
+
+ - name: Upload Trivy Image Scan Artifact
+ uses: actions/upload-artifact@v4
+ with:
+ name: scan-trivy-image-${{ matrix.image.name }}
+ path: trivy-image-${{ matrix.image.name }}.json
+ retention-days: 14
+
+ oss-scan:
+ name: OSS Dependency Audit
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: '3.11'
+
+ - name: Run pip-audit for Python Dependencies
+ run: |
+ mkdir -p reports
+ pip install pip-audit
+ pip-audit --format json -o reports/pip-audit-python.json || true
+
+ - name: Set up Node.js
+ uses: actions/setup-node@v4
+ with:
+ node-version: '20'
+
+ - name: Run npm audit for Frontend & Docs
+ run: |
+ mkdir -p reports
+ (cd frontend && npm audit --json > ../reports/npm-audit-frontend.json || true)
+ (cd docs && npm audit --json > ../reports/npm-audit-docs.json || true)
+ (cd sdks/typescript && npm audit --json > ../reports/npm-audit-sdk-ts.json || true)
+
+ - name: Run Trivy Filesystem Scan
+ uses: aquasecurity/[email protected]
+ with:
+ scan-type: fs
+ scan-ref: .
+ format: json
+ output: reports/trivy-fs.json
+ exit-code: 0
+ severity: CRITICAL,HIGH,MEDIUM,LOW
+
+ - name: Upload OSS Audit Artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: scan-oss-results
+ path: reports/*.json
+ retention-days: 14
+
+ static-scan:
+ name: Static Code Security (SAST)
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: '3.11'
+
+ - name: Run Bandit for Python SAST
+ run: |
+ mkdir -p sast-reports
+ pip install bandit
+ bandit -r src/ -f json -o sast-reports/bandit.json || true
+
+ - name: Set up Go
+ uses: actions/setup-go@v5
+ with:
+ go-version-file: kubernetes/operator/go.mod
+
+ - name: Install and Run Gosec for Go SAST
+ run: |
+ mkdir -p sast-reports
+ curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.22.1
+ (cd kubernetes/operator && $(go env GOPATH)/bin/gosec -fmt=json -out=../../sast-reports/gosec.json ./... || true)
+
+ - name: Upload SAST Artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: scan-static-results
+ path: sast-reports/*.json
+ retention-days: 14
+
+ generate-report:
+ name: Consolidate Security Report
+ needs: [scan-container-images, oss-scan, static-scan]
+ if: always()
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout repository
+ uses: actions/checkout@v4
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: '3.11'
+
+ - name: Download all scan artifacts
+ uses: actions/download-artifact@v4
+ with:
+ pattern: 'scan-*'
+ path: security-reports
+ merge-multiple: true
+
+ - name: Generate Consolidated Security Markdown & CSV Reports
+ run: |
+ python3 scripts/ci/generate_security_report.py security-reports/ \
+ -o security-reports/security-report.md \
+ --csv-output security-reports/security-report.csv
+
+ - name: Upload Security Report CSV Artifact
+ uses: actions/upload-artifact@v4
+ with:
+ name: security-report-csv
+ path: security-reports/security-report.csv
+ retention-days: 30
+
+ - name: Upload Consolidated Security Report Artifacts
+ uses: actions/upload-artifact@v4
+ with:
+ name: security-report-summary
+ path: |
+ security-reports/security-report.md
+ security-reports/security-report.csv
+ security-reports/*.json
+ retention-days: 30
diff --git a/scripts/ci/generate_security_report.py b/scripts/ci/generate_security_report.py
new file mode 100644
index 000000000..40822431d
--- /dev/null
+++ b/scripts/ci/generate_security_report.py
@@ -0,0 +1,438 @@
+#!/usr/bin/env python3
+"""Generate a consolidated Security Scan Report from Trivy, Audit, and SAST JSON results.
+
+Parses scan output JSON files produced by Trivy, pip-audit, npm audit, gosec,
+and bandit, writing a unified Markdown report and summary table for GitHub Actions.
+"""
+
+import argparse
+import csv
+import json
+import os
+import sys
+from pathlib import Path
+from typing import Any
+
+SEVERITY_ORDER = {"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3, "UNKNOWN": 4}
+
+
+def parse_trivy_json(path: Path) -> dict[str, Any]:
+ """Parse Trivy JSON output for image or filesystem scan."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except Exception as exc:
+ return {"error": f"Failed to parse {path.name}: {exc}"}
+
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0}
+ findings: list[dict[str, str]] = []
+
+ results = data.get("Results", []) or []
+ for res in results:
+ target = res.get("Target", "Unknown Target")
+ vulns = res.get("Vulnerabilities", []) or []
+ for v in vulns:
+ sev = (v.get("Severity") or "UNKNOWN").upper()
+ if sev in counts:
+ counts[sev] += 1
+ else:
+ counts["UNKNOWN"] += 1
+
+ findings.append(
+ {
+ "target": target,
+ "id": v.get("VulnerabilityID", "N/A"),
+ "pkg": v.get("PkgName", "N/A"),
+ "installed": v.get("InstalledVersion", "N/A"),
+ "fixed": v.get("FixedVersion", "None"),
+ "severity": sev,
+ "title": (v.get("Title") or v.get("Description") or "").splitlines()[0][:100],
+ }
+ )
+
+ return {"counts": counts, "findings": findings, "total": sum(counts.values())}
+
+
+def parse_pip_audit_json(path: Path) -> dict[str, Any]:
+ """Parse pip-audit JSON output."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except Exception as exc:
+ return {"error": f"Failed to parse {path.name}: {exc}"}
+
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0}
+ findings: list[dict[str, str]] = []
+
+ # pip-audit outputs array of package results or dict with 'dependencies'
+ packages = data if isinstance(data, list) else data.get("dependencies", [])
+ for pkg in packages:
+ name = pkg.get("name", "N/A")
+ version = pkg.get("version", "N/A")
+ vulns = pkg.get("vulns", []) or []
+ for v in vulns:
+ counts["HIGH"] += 1 # Default fallback for pip-audit findings
+ findings.append(
+ {
+ "target": f"{name}@{version}",
+ "id": v.get("id", "N/A"),
+ "pkg": name,
+ "installed": version,
+ "fixed": ", ".join(v.get("fix_versions", [])) or "None",
+ "severity": "HIGH",
+ "title": (v.get("description") or "").splitlines()[0][:100],
+ }
+ )
+
+ return {"counts": counts, "findings": findings, "total": len(findings)}
+
+
+def parse_npm_audit_json(path: Path) -> dict[str, Any]:
+ """Parse npm audit JSON output."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except Exception as exc:
+ return {"error": f"Failed to parse {path.name}: {exc}"}
+
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0}
+ findings: list[dict[str, str]] = []
+
+ metadata_counts = data.get("metadata", {}).get("vulnerabilities", {})
+ if metadata_counts:
+ counts["CRITICAL"] = metadata_counts.get("critical", 0)
+ counts["HIGH"] = metadata_counts.get("high", 0)
+ counts["MEDIUM"] = metadata_counts.get("moderate", 0)
+ counts["LOW"] = metadata_counts.get("low", 0)
+
+ vulnerabilities = data.get("vulnerabilities", {})
+ for pkg_name, info in vulnerabilities.items():
+ sev = (info.get("severity") or "UNKNOWN").upper()
+ if sev == "MODERATE":
+ sev = "MEDIUM"
+ findings.append(
+ {
+ "target": pkg_name,
+ "id": f"NPM-{pkg_name}",
+ "pkg": pkg_name,
+ "installed": info.get("range", "N/A"),
+ "fixed": "See advisory",
+ "severity": sev if sev in counts else "UNKNOWN",
+ "title": f"Dependency advisory for {pkg_name}",
+ }
+ )
+
+ return {"counts": counts, "findings": findings, "total": sum(counts.values())}
+
+
+def parse_bandit_json(path: Path) -> dict[str, Any]:
+ """Parse Bandit SAST JSON output."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except Exception as exc:
+ return {"error": f"Failed to parse {path.name}: {exc}"}
+
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0}
+ findings: list[dict[str, str]] = []
+
+ results = data.get("results", []) or []
+ for r in results:
+ sev = (r.get("issue_severity") or "LOW").upper()
+ if sev in counts:
+ counts[sev] += 1
+ else:
+ counts["UNKNOWN"] += 1
+
+ fname = Path(r.get("filename", "")).name
+ line = r.get("line_number", "")
+ findings.append(
+ {
+ "target": f"{fname}:{line}",
+ "id": r.get("test_id", "SAST"),
+ "pkg": r.get("test_name", "Bandit"),
+ "installed": "N/A",
+ "fixed": "Refactor code",
+ "severity": sev,
+ "title": r.get("issue_text", "")[:100],
+ }
+ )
+
+ return {"counts": counts, "findings": findings, "total": sum(counts.values())}
+
+
+def parse_gosec_json(path: Path) -> dict[str, Any]:
+ """Parse Gosec SAST JSON output."""
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except Exception as exc:
+ return {"error": f"Failed to parse {path.name}: {exc}"}
+
+ counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0}
+ findings: list[dict[str, str]] = []
+
+ issues = data.get("Issues", []) or []
+ for issue in issues:
+ sev = (issue.get("severity") or "LOW").upper()
+ if sev in counts:
+ counts[sev] += 1
+ else:
+ counts["UNKNOWN"] += 1
+
+ fname = Path(issue.get("file", "")).name
+ line = issue.get("line", "")
+ findings.append(
+ {
+ "target": f"{fname}:{line}",
+ "id": issue.get("rule_id", "GOSEC"),
+ "pkg": "gosec",
+ "installed": "N/A",
+ "fixed": "Refactor code",
+ "severity": sev,
+ "title": issue.get("details", "")[:100],
+ }
+ )
+
+ return {"counts": counts, "findings": findings, "total": sum(counts.values())}
+
+
+def collect_scan_results(
+ report_dir: Path,
+) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, int]]:
+ """Scan directory for JSON results and return structured scan data.
+
+ Returns:
+ (component_rows, all_findings, total_counts)
+ """
+ component_rows: list[dict[str, Any]] = []
+ all_findings: list[dict[str, Any]] = []
+ total_counts = {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0, "UNKNOWN": 0, "TOTAL": 0}
+
+ def _record_component(name: str, target: str, res: dict[str, Any]):
+ if "error" in res:
+ component_rows.append({"component": name, "target": target, "error": res["error"]})
+ return
+
+ c = res["counts"]
+ tot = res["total"]
+ component_rows.append(
+ {
+ "component": name,
+ "target": target,
+ "total": tot,
+ "counts": c,
+ }
+ )
+ for k in ("CRITICAL", "HIGH", "MEDIUM", "LOW", "UNKNOWN"):
+ total_counts[k] += c.get(k, 0)
+ total_counts["TOTAL"] += tot
+
+ for item in res.get("findings", []):
+ item_copy = dict(item)
+ item_copy["scanner"] = name
+ all_findings.append(item_copy)
+
+ # 1. Container Image Scans (Trivy)
+ for json_file in sorted(report_dir.glob("trivy-image-*.json")):
+ image_name = json_file.stem.replace("trivy-image-", "")
+ res = parse_trivy_json(json_file)
+ _record_component(f"Container Image ({image_name})", image_name, res)
+
+ # 2. Filesystem / OSS Scans
+ trivy_fs = report_dir / "trivy-fs.json"
+ if trivy_fs.exists():
+ res = parse_trivy_json(trivy_fs)
+ _record_component("Repo Filesystem (Trivy FS)", "Repository", res)
+
+ for json_file in sorted(report_dir.glob("pip-audit-*.json")):
+ target_name = json_file.stem.replace("pip-audit-", "")
+ res = parse_pip_audit_json(json_file)
+ _record_component("Python Dependencies (`pip-audit`)", target_name, res)
+
+ for json_file in sorted(report_dir.glob("npm-audit-*.json")):
+ target_name = json_file.stem.replace("npm-audit-", "")
+ res = parse_npm_audit_json(json_file)
+ _record_component("Node.js Dependencies (`npm audit`)", target_name, res)
+
+ # 3. SAST Scans
+ bandit_file = report_dir / "bandit.json"
+ if bandit_file.exists():
+ res = parse_bandit_json(bandit_file)
+ _record_component("Python SAST (`Bandit`)", "src/", res)
+
+ gosec_file = report_dir / "gosec.json"
+ if gosec_file.exists():
+ res = parse_gosec_json(gosec_file)
+ _record_component("Go SAST (`Gosec`)", "kubernetes/operator", res)
+
+ # Sort all findings: CRITICAL -> HIGH -> MEDIUM -> LOW -> UNKNOWN
+ all_findings.sort(
+ key=lambda x: (
+ SEVERITY_ORDER.get(x.get("severity", "UNKNOWN"), 5),
+ x.get("scanner", ""),
+ x.get("target", ""),
+ x.get("id", ""),
+ )
+ )
+
+ return component_rows, all_findings, total_counts
+
+
+def generate_csv_report(findings: list[dict[str, Any]], output_path: Path) -> None:
+ """Generate a comprehensive CSV report of all detected security findings."""
+ fieldnames = [
+ "Scanner",
+ "Target",
+ "Severity",
+ "Vulnerability ID",
+ "Package / Module",
+ "Installed Version",
+ "Fixed Version",
+ "Title",
+ ]
+ output_path.parent.mkdir(parents=True, exist_ok=True)
+ with open(output_path, "w", newline="", encoding="utf-8") as f:
+ writer = csv.DictWriter(f, fieldnames=fieldnames)
+ writer.writeheader()
+ for item in findings:
+ writer.writerow(
+ {
+ "Scanner": item.get("scanner", "N/A"),
+ "Target": item.get("target", "N/A"),
+ "Severity": item.get("severity", "UNKNOWN"),
+ "Vulnerability ID": item.get("id", "N/A"),
+ "Package / Module": item.get("pkg", "N/A"),
+ "Installed Version": item.get("installed", "N/A"),
+ "Fixed Version": item.get("fixed", "None"),
+ "Title": item.get("title", ""),
+ }
+ )
+
+
+def format_markdown_report(
+ component_rows: list[dict[str, Any]],
+ all_findings: list[dict[str, Any]],
+ total_counts: dict[str, int],
+ csv_filename: str | None = "security-report.csv",
+) -> str:
+ """Format Markdown report from collected scan results."""
+ lines: list[str] = [
+ "# πŸ›‘οΈ OpenRAG Security Scan Report",
+ "",
+ "Consolidated security report generated across container images, OSS dependencies, and repo source code.",
+ "",
+ "## Executive Summary",
+ "",
+ "| Component / Scanner | Target | Total | Critical πŸ”΄ | High 🟠 | Medium 🟑 | Low πŸ”΅ |",
+ "| :--- | :--- | :---: | :---: | :---: | :---: | :---: |",
+ ]
+
+ if not component_rows:
+ lines.append("\n> [!NOTE]\n> No JSON scan results found in report directory.")
+ return "\n".join(lines) + "\n"
+
+ for row in component_rows:
+ if "error" in row:
+ lines.append(
+ f"| {row['component']} | `{row['target']}` | Parse Error | - | - | - | - |"
+ )
+ else:
+ c = row["counts"]
+ lines.append(
+ f"| {row['component']} | `{row['target']}` | {row['total']} | {c['CRITICAL']} | {c['HIGH']} | {c['MEDIUM']} | {c['LOW']} |"
+ )
+
+ # Total row
+ lines.append(
+ f"| **Total** | **All Scans** | **{total_counts['TOTAL']}** | **{total_counts['CRITICAL']}** | **{total_counts['HIGH']}** | **{total_counts['MEDIUM']}** | **{total_counts['LOW']}** |"
+ )
+
+ # CSV download callout
+ lines.append("")
+ if csv_filename:
+ lines.append(
+ f"> πŸ“₯ **Full Security Findings Export (CSV):** A comprehensive CSV report containing all **{len(all_findings)}** findings across all severities is available for download in the workflow **Artifacts** section (`security-report-csv` / `{csv_filename}`)."
+ )
+ lines.append("")
+
+ # Filter critical and high findings for display table
+ critical_high = [f for f in all_findings if f.get("severity") in ("CRITICAL", "HIGH")]
+
+ lines.append("## 🚨 Top Vulnerabilities (Critical & High)")
+ lines.append("")
+
+ if not critical_high:
+ lines.append("πŸŽ‰ **No Critical or High severity vulnerabilities detected!**")
+ else:
+ lines.append(
+ "| Severity | Scanner | Target | Vulnerability ID | Package / Module | Fixed Version | Title |"
+ )
+ lines.append("| :--- | :--- | :--- | :--- | :--- | :--- | :--- |")
+ for item in critical_high[:50]: # Limit top 50 in summary
+ sev_icon = "πŸ”΄ CRITICAL" if item["severity"] == "CRITICAL" else "🟠 HIGH"
+ title = (item.get("title") or "").replace("|", "\\|")
+ scanner = item.get("scanner", "N/A")
+ lines.append(
+ f"| {sev_icon} | {scanner} | `{item['target']}` | `{item['id']}` | `{item['pkg']}` | `{item['fixed']}` | {title} |"
+ )
+
+ if len(critical_high) > 50:
+ lines.append("")
+ lines.append(
+ f"*Showing top 50 of {len(critical_high)} Critical & High findings. Download `{csv_filename}` for all {len(all_findings)} findings.*"
+ )
+
+ return "\n".join(lines) + "\n"
+
+
+def generate_markdown_report(report_dir: Path, csv_filename: str = "security-report.csv") -> str:
+ """Scan directory for JSON results and generate unified Markdown report."""
+ component_rows, all_findings, total_counts = collect_scan_results(report_dir)
+ return format_markdown_report(
+ component_rows, all_findings, total_counts, csv_filename=csv_filename
+ )
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument(
+ "directory",
+ nargs="?",
+ default="security-reports",
+ help="Directory containing scan JSON result files (default: security-reports)",
+ )
+ parser.add_argument(
+ "-o",
+ "--output",
+ default=None,
+ help="Output path for the Markdown report (default: <directory>/security-report.md)",
+ )
+ parser.add_argument(
+ "--csv-output",
+ default=None,
+ help="Output path for the CSV report (default: <directory>/security-report.csv)",
+ )
+ args = parser.parse_args()
+
+ report_dir = Path(args.directory)
+ output_md = Path(args.output) if args.output else report_dir / "security-report.md"
+ output_csv = Path(args.csv_output) if args.csv_output else report_dir / "security-report.csv"
+
+ component_rows, all_findings, total_counts = collect_scan_results(report_dir)
+ report_md = format_markdown_report(
+ component_rows, all_findings, total_counts, csv_filename=output_csv.name
+ )
+
+ output_md.parent.mkdir(parents=True, exist_ok=True)
+ output_md.write_text(report_md, encoding="utf-8")
+
+ generate_csv_report(all_findings, output_csv)
+
+ # Append to GitHub Step Summary if environment variable present
+ step_summary = os.environ.get("GITHUB_STEP_SUMMARY")
+ if step_summary:
+ with open(step_summary, "a", encoding="utf-8") as f:
+ f.write(report_md)
+
+ print(f"Security Markdown report generated at: {output_md}")
+ print(f"Security CSV report generated ({len(all_findings)} findings) at: {output_csv}")
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tests/unit/test_security_report.py b/tests/unit/test_security_report.py
new file mode 100644
index 000000000..d7041c256
--- /dev/null
+++ b/tests/unit/test_security_report.py
@@ -0,0 +1,126 @@
+import csv
+import json
+
+from scripts.ci.generate_security_report import (
+ collect_scan_results,
+ format_markdown_report,
+ generate_csv_report,
+ parse_trivy_json,
+)
+
+
+def test_parse_trivy_json(tmp_path):
+ trivy_data = {
+ "Results": [
+ {
+ "Target": "debian:12",
+ "Vulnerabilities": [
+ {
+ "VulnerabilityID": "CVE-2024-0001",
+ "PkgName": "libcrypto",
+ "InstalledVersion": "1.0.0",
+ "FixedVersion": "1.0.1",
+ "Severity": "CRITICAL",
+ "Title": "Crypto flaw",
+ },
+ {
+ "VulnerabilityID": "CVE-2024-0002",
+ "PkgName": "libssl",
+ "InstalledVersion": "1.0.0",
+ "FixedVersion": "None",
+ "Severity": "HIGH",
+ "Description": "SSL issue",
+ },
+ ],
+ }
+ ]
+ }
+ trivy_file = tmp_path / "trivy-image-backend.json"
+ trivy_file.write_text(json.dumps(trivy_data), encoding="utf-8")
+
+ res = parse_trivy_json(trivy_file)
+ assert res["total"] == 2
+ assert res["counts"]["CRITICAL"] == 1
+ assert res["counts"]["HIGH"] == 1
+ assert len(res["findings"]) == 2
+ assert res["findings"][0]["id"] == "CVE-2024-0001"
+
+
+def test_collect_and_generate_reports(tmp_path):
+ # Setup mock scanner outputs
+ trivy_file = tmp_path / "trivy-image-backend.json"
+ trivy_file.write_text(
+ json.dumps(
+ {
+ "Results": [
+ {
+ "Target": "backend:local",
+ "Vulnerabilities": [
+ {
+ "VulnerabilityID": "CVE-2024-1111",
+ "PkgName": "openssl",
+ "InstalledVersion": "1.1",
+ "FixedVersion": "1.2",
+ "Severity": "HIGH",
+ "Title": "OpenSSL bug",
+ }
+ ],
+ }
+ ]
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ pip_file = tmp_path / "pip-audit-python.json"
+ pip_file.write_text(
+ json.dumps(
+ [
+ {
+ "name": "requests",
+ "version": "2.25.0",
+ "vulns": [
+ {
+ "id": "PYSEC-2023-1",
+ "fix_versions": ["2.31.0"],
+ "description": "Requests vuln",
+ }
+ ],
+ }
+ ]
+ ),
+ encoding="utf-8",
+ )
+
+ component_rows, all_findings, totals = collect_scan_results(tmp_path)
+ assert len(component_rows) == 2
+ assert totals["HIGH"] == 2
+ assert totals["TOTAL"] == 2
+ assert len(all_findings) == 2
+
+ # Verify findings contain scanner tag
+ scanners = {f["scanner"] for f in all_findings}
+ assert "Container Image (backend)" in scanners
+ assert "Python Dependencies (`pip-audit`)" in scanners
+
+ # Test CSV Generation
+ csv_file = tmp_path / "security-report.csv"
+ generate_csv_report(all_findings, csv_file)
+ assert csv_file.exists()
+
+ with open(csv_file, newline="", encoding="utf-8") as f:
+ reader = list(csv.DictReader(f))
+ assert len(reader) == 2
+ assert "Scanner" in reader[0]
+ assert "Severity" in reader[0]
+ assert "Vulnerability ID" in reader[0]
+ assert "Fixed Version" in reader[0]
+
+ # Test Markdown formatting
+ md_content = format_markdown_report(
+ component_rows, all_findings, totals, csv_filename="security-report.csv"
+ )
+ assert "Executive Summary" in md_content
+ assert "| **Total** | **All Scans** |" in md_content
+ assert "security-report-csv" in md_content
+ assert "Top Vulnerabilities" in md_content