|
| 1 | +from collections import defaultdict |
| 2 | +from typing import List, Dict |
| 3 | + |
| 4 | +import click |
| 5 | +from texttable import Texttable |
| 6 | + |
| 7 | +from cycode.cli.consts import LICENSE_COMPLIANCE_POLICY_ID, PACKAGE_VULNERABILITY_POLICY_ID |
| 8 | +from cycode.cli.models import DocumentDetections, Detection |
| 9 | +from cycode.cli.printers.base_table_printer import BaseTablePrinter |
| 10 | + |
| 11 | +SEVERITY_COLUMN = 'Severity' |
| 12 | +LICENSE_COLUMN = 'License' |
| 13 | +UPGRADE_COLUMN = 'Upgrade' |
| 14 | +REPOSITORY_COLUMN = 'Repository' |
| 15 | +CVE_COLUMN = 'CVE' |
| 16 | + |
| 17 | +PREVIEW_DETECTIONS_COMMON_HEADERS = [ |
| 18 | + 'File Path', |
| 19 | + 'Ecosystem', |
| 20 | + 'Dependency Name', |
| 21 | + 'Direct Dependency', |
| 22 | + 'Development Dependency' |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +class SCATablePrinter(BaseTablePrinter): |
| 27 | + def _print_results(self, results: List[DocumentDetections]) -> None: |
| 28 | + detections_per_detection_type_id = self._extract_detections_per_detection_type_id(results) |
| 29 | + self._print_detection_per_detection_type_id(detections_per_detection_type_id) |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def _extract_detections_per_detection_type_id(results: List[DocumentDetections]) -> Dict[str, List[Detection]]: |
| 33 | + detections_per_detection_type_id = defaultdict(list) |
| 34 | + |
| 35 | + for document_detection in results: |
| 36 | + for detection in document_detection.detections: |
| 37 | + detections_per_detection_type_id[detection.detection_type_id].append(detection) |
| 38 | + |
| 39 | + return detections_per_detection_type_id |
| 40 | + |
| 41 | + def _print_detection_per_detection_type_id( |
| 42 | + self, detections_per_detection_type_id: Dict[str, List[Detection]] |
| 43 | + ) -> None: |
| 44 | + for detection_type_id in detections_per_detection_type_id: |
| 45 | + detections = detections_per_detection_type_id[detection_type_id] |
| 46 | + headers = self._get_table_headers() |
| 47 | + |
| 48 | + title = None |
| 49 | + rows = [] |
| 50 | + |
| 51 | + if detection_type_id == PACKAGE_VULNERABILITY_POLICY_ID: |
| 52 | + title = "Dependencies Vulnerabilities" |
| 53 | + |
| 54 | + headers = [SEVERITY_COLUMN] + headers |
| 55 | + headers.extend(PREVIEW_DETECTIONS_COMMON_HEADERS) |
| 56 | + headers.append(CVE_COLUMN) |
| 57 | + headers.append(UPGRADE_COLUMN) |
| 58 | + |
| 59 | + for detection in detections: |
| 60 | + rows.append(self._get_upgrade_package_vulnerability(detection)) |
| 61 | + elif detection_type_id == LICENSE_COMPLIANCE_POLICY_ID: |
| 62 | + title = "License Compliance" |
| 63 | + |
| 64 | + headers.extend(PREVIEW_DETECTIONS_COMMON_HEADERS) |
| 65 | + headers.append(LICENSE_COLUMN) |
| 66 | + |
| 67 | + for detection in detections: |
| 68 | + rows.append(self._get_license(detection)) |
| 69 | + |
| 70 | + if rows: |
| 71 | + self._print_table_detections(detections, headers, rows, title) |
| 72 | + |
| 73 | + def _get_table_headers(self) -> list: |
| 74 | + if self._is_git_repository(): |
| 75 | + return [REPOSITORY_COLUMN] |
| 76 | + |
| 77 | + return [] |
| 78 | + |
| 79 | + def _print_table_detections( |
| 80 | + self, detections: List[Detection], headers: List[str], rows, title: str |
| 81 | + ) -> None: |
| 82 | + self._print_summary_issues(detections, title) |
| 83 | + text_table = Texttable() |
| 84 | + text_table.header(headers) |
| 85 | + |
| 86 | + self.set_table_width(headers, text_table) |
| 87 | + |
| 88 | + for row in rows: |
| 89 | + text_table.add_row(row) |
| 90 | + |
| 91 | + click.echo(text_table.draw()) |
| 92 | + |
| 93 | + @staticmethod |
| 94 | + def set_table_width(headers: List[str], text_table: Texttable) -> None: |
| 95 | + header_width_size_cols = [] |
| 96 | + for header in headers: |
| 97 | + header_len = len(header) |
| 98 | + if header == CVE_COLUMN: |
| 99 | + header_width_size_cols.append(header_len * 5) |
| 100 | + elif header == UPGRADE_COLUMN: |
| 101 | + header_width_size_cols.append(header_len * 2) |
| 102 | + else: |
| 103 | + header_width_size_cols.append(header_len) |
| 104 | + text_table.set_cols_width(header_width_size_cols) |
| 105 | + |
| 106 | + @staticmethod |
| 107 | + def _print_summary_issues(detections: List, title: str) -> None: |
| 108 | + click.echo(f'⛔ Found {len(detections)} issues of type: {click.style(title, bold=True)}') |
| 109 | + |
| 110 | + def _get_common_detection_fields(self, detection: Detection) -> List[str]: |
| 111 | + row = [ |
| 112 | + detection.detection_details.get('file_name'), |
| 113 | + detection.detection_details.get('ecosystem'), |
| 114 | + detection.detection_details.get('package_name'), |
| 115 | + detection.detection_details.get('is_direct_dependency_str'), |
| 116 | + detection.detection_details.get('is_dev_dependency_str') |
| 117 | + ] |
| 118 | + |
| 119 | + if self._is_git_repository(): |
| 120 | + row = [detection.detection_details.get('repository_name')] + row |
| 121 | + |
| 122 | + return row |
| 123 | + |
| 124 | + def _get_upgrade_package_vulnerability(self, detection: Detection) -> List[str]: |
| 125 | + alert = detection.detection_details.get('alert') |
| 126 | + row = [ |
| 127 | + detection.detection_details.get('advisory_severity'), |
| 128 | + *self._get_common_detection_fields(detection), |
| 129 | + detection.detection_details.get('vulnerability_id') |
| 130 | + ] |
| 131 | + |
| 132 | + upgrade = '' |
| 133 | + if alert.get("first_patched_version"): |
| 134 | + upgrade = f'{alert.get("vulnerable_requirements")} -> {alert.get("first_patched_version")}' |
| 135 | + row.append(upgrade) |
| 136 | + |
| 137 | + return row |
| 138 | + |
| 139 | + def _get_license(self, detection: Detection) -> List[str]: |
| 140 | + row = self._get_common_detection_fields(detection) |
| 141 | + row.append(f'{detection.detection_details.get("license")}') |
| 142 | + return row |
0 commit comments