|
| 1 | +import csv |
1 | 2 | import datetime |
2 | 3 | import json |
3 | 4 | import logging |
|
8 | 9 | import sys |
9 | 10 | import tempfile |
10 | 11 | import typing |
| 12 | +from io import StringIO |
11 | 13 |
|
12 | 14 | from entrypoint import dockerfile, executor, exporter, installer, pkg_vuln, fixed_vulns |
13 | 15 | from entrypoint.data_model import SBOMOutput, VulnScanOutput |
@@ -506,22 +508,104 @@ def install_sbomgen(sbomgen_version): |
506 | 508 | return 0 |
507 | 509 |
|
508 | 510 |
|
509 | | -def write_pkg_vuln_report_csv(out_scan_csv, scan_result: exporter.InspectorScanResult): |
510 | | - csv_output = exporter.to_csv(scan_result) |
511 | | - |
512 | | - logging.info(f"writing package vulnerability CSV report to: {out_scan_csv}") |
513 | | - with open(out_scan_csv, "w") as f: |
| 511 | +def write_pkg_vuln_report_csv(output_file_path, scan_data): |
| 512 | + # Handle both VulnScanOutput and legacy InspectorScanResult |
| 513 | + if hasattr(scan_data, 'critical_count'): |
| 514 | + # New VulnScanOutput format |
| 515 | + critical_count = scan_data.critical_count |
| 516 | + high_count = scan_data.high_count |
| 517 | + medium_count = scan_data.medium_count |
| 518 | + low_count = scan_data.low_count |
| 519 | + other_count = scan_data.other_count |
| 520 | + total_count = scan_data.total_vulnerabilities |
| 521 | + else: |
| 522 | + # Legacy InspectorScanResult format |
| 523 | + critical_count = scan_data.criticals |
| 524 | + high_count = scan_data.highs |
| 525 | + medium_count = scan_data.mediums |
| 526 | + low_count = scan_data.lows |
| 527 | + other_count = scan_data.others |
| 528 | + total_count = len(scan_data.vulnerabilities) |
| 529 | + |
| 530 | + csv_buffer = StringIO() |
| 531 | + csv_writer = csv.writer(csv_buffer, quoting=csv.QUOTE_ALL) |
| 532 | + |
| 533 | + # Write vulnerability summary header |
| 534 | + vulnerability_summary = [ |
| 535 | + f"#critical_vulnerabilities:{critical_count}", |
| 536 | + f"high_vulnerabilities:{high_count}", |
| 537 | + f"medium_vulnerabilities:{medium_count}", |
| 538 | + f"low_vulnerabilities:{low_count}", |
| 539 | + f"other_vulnerabilities:{other_count}", |
| 540 | + f"total_vulnerabilities:{total_count}" |
| 541 | + ] |
| 542 | + csv_writer.writerow(vulnerability_summary) |
| 543 | + |
| 544 | + # Write CSV column headers |
| 545 | + csv_headers = [ |
| 546 | + "vulnerability_id", |
| 547 | + "severity", |
| 548 | + "cvss_score", |
| 549 | + "package_name", |
| 550 | + "installed_version", |
| 551 | + "fixed_version", |
| 552 | + "description" |
| 553 | + ] |
| 554 | + csv_writer.writerow(csv_headers) |
| 555 | + |
| 556 | + csv_output = csv_buffer.getvalue() |
| 557 | + |
| 558 | + logging.info(f"writing package vulnerability CSV report to: {output_file_path}") |
| 559 | + with open(output_file_path, "w") as f: |
514 | 560 | f.write(csv_output) |
515 | 561 |
|
516 | 562 |
|
517 | | -def write_pkg_vuln_report_markdown(out_scan_markdown, scan_result: exporter.InspectorScanResult): |
518 | | - markdown = exporter.to_markdown(scan_result) |
| 563 | +def write_pkg_vuln_report_markdown(output_file_path, scan_data): |
| 564 | + # Handle both VulnScanOutput and legacy InspectorScanResult |
| 565 | + if hasattr(scan_data, 'critical_count'): |
| 566 | + # New VulnScanOutput format |
| 567 | + critical_count = scan_data.critical_count |
| 568 | + high_count = scan_data.high_count |
| 569 | + medium_count = scan_data.medium_count |
| 570 | + low_count = scan_data.low_count |
| 571 | + other_count = scan_data.other_count |
| 572 | + total_count = scan_data.total_vulnerabilities |
| 573 | + results_file = getattr(scan_data, 'scan_results_file_path', None) |
| 574 | + scan_success = getattr(scan_data, 'scan_success', True) |
| 575 | + else: |
| 576 | + # Legacy InspectorScanResult format |
| 577 | + critical_count = scan_data.criticals |
| 578 | + high_count = scan_data.highs |
| 579 | + medium_count = scan_data.mediums |
| 580 | + low_count = scan_data.lows |
| 581 | + other_count = scan_data.others |
| 582 | + total_count = len(scan_data.vulnerabilities) |
| 583 | + results_file = None |
| 584 | + scan_success = True |
| 585 | + |
| 586 | + # Create simple markdown report |
| 587 | + markdown_content = "# Amazon Inspector Scan Results\n\n" |
| 588 | + |
| 589 | + # Add vulnerability summary |
| 590 | + markdown_content += "## Vulnerability Summary\n\n" |
| 591 | + markdown_content += f"- **Total Vulnerabilities:** {total_count}\n" |
| 592 | + markdown_content += f"- **Critical:** {critical_count}\n" |
| 593 | + markdown_content += f"- **High:** {high_count}\n" |
| 594 | + markdown_content += f"- **Medium:** {medium_count}\n" |
| 595 | + markdown_content += f"- **Low:** {low_count}\n" |
| 596 | + markdown_content += f"- **Other:** {other_count}\n\n" |
| 597 | + |
| 598 | + # Add scan information |
| 599 | + if results_file: |
| 600 | + markdown_content += "## Scan Information\n\n" |
| 601 | + markdown_content += f"- **Results File:** {results_file}\n" |
| 602 | + markdown_content += f"- **Scan Status:** {'Success' if scan_success else 'Failed'}\n\n" |
519 | 603 |
|
520 | | - logging.info(f"writing package vulnerability markdown report to: {out_scan_markdown}") |
521 | | - with open(out_scan_markdown, "w") as f: |
522 | | - f.write(markdown) |
| 604 | + logging.info(f"writing package vulnerability markdown report to: {output_file_path}") |
| 605 | + with open(output_file_path, "w") as f: |
| 606 | + f.write(markdown_content) |
523 | 607 |
|
524 | | - return markdown |
| 608 | + return markdown_content |
525 | 609 |
|
526 | 610 |
|
527 | 611 | def set_env_var_if_vuln_threshold_exceeded(output_config, |
|
0 commit comments