-
Notifications
You must be signed in to change notification settings - Fork 55
CM-23720 - Add table output for all scan types #122
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
Merged
MarshalX
merged 7 commits into
main
from
CM-23720-Add-table-output-formatting-for-all-scan-types
Jun 13, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfbf1ed
Add table output for all scan types
MarshalX f3354d3
New table management
MarshalX 4deda5f
Merge branch 'main' into CM-23720-Add-table-output-formatting-for-all…
MarshalX 27c7d89
fix naming
MarshalX 40d1c69
code refactoring
MarshalX 1d4dcac
rework management of column's widths
MarshalX 530689d
fix possible exception
MarshalX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import abc | ||
| from typing import List | ||
|
|
||
| import click | ||
|
|
||
| from cycode.cli.printers.text_printer import TextPrinter | ||
| from cycode.cli.models import DocumentDetections, CliError, CliResult | ||
| from cycode.cli.printers.base_printer import BasePrinter | ||
|
|
||
|
|
||
| class BaseTablePrinter(BasePrinter, abc.ABC): | ||
| def __init__(self, context: click.Context): | ||
| super().__init__(context) | ||
| self.context = context | ||
| self.scan_id: str = context.obj.get('scan_id') | ||
| self.scan_type: str = context.obj.get('scan_type') | ||
| self.show_secret: bool = context.obj.get('show_secret', False) | ||
|
|
||
| def print_result(self, result: CliResult) -> None: | ||
| TextPrinter(self.context).print_result(result) | ||
|
|
||
| def print_error(self, error: CliError) -> None: | ||
| TextPrinter(self.context).print_error(error) | ||
|
|
||
| def print_scan_results(self, results: List[DocumentDetections]): | ||
| click.secho(f'Scan Results: (scan_id: {self.scan_id})') | ||
|
|
||
| if not results: | ||
| click.secho('Good job! No issues were found!!! 👏👏👏', fg=self.GREEN_COLOR_NAME) | ||
| return | ||
|
|
||
| self._print_results(results) | ||
|
|
||
| report_url = self.context.obj.get('report_url') | ||
| if report_url: | ||
| click.secho(f'Report URL: {report_url}') | ||
|
|
||
| def _is_git_repository(self) -> bool: | ||
| return self.context.obj.get('remote_url') is not None | ||
|
|
||
| @abc.abstractmethod | ||
| def _print_results(self, results: List[DocumentDetections]) -> None: | ||
| raise NotImplementedError | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| from collections import defaultdict | ||
| from typing import List, Dict | ||
|
|
||
| import click | ||
| from texttable import Texttable | ||
|
|
||
| from cycode.cli.consts import LICENSE_COMPLIANCE_POLICY_ID, PACKAGE_VULNERABILITY_POLICY_ID | ||
| from cycode.cli.models import DocumentDetections, Detection | ||
| from cycode.cli.printers.base_table_printer import BaseTablePrinter | ||
|
|
||
| SEVERITY_COLUMN = 'Severity' | ||
| LICENSE_COLUMN = 'License' | ||
| UPGRADE_COLUMN = 'Upgrade' | ||
| REPOSITORY_COLUMN = 'Repository' | ||
| CVE_COLUMN = 'CVE' | ||
|
|
||
| PREVIEW_DETECTIONS_COMMON_HEADERS = [ | ||
| 'File Path', | ||
| 'Ecosystem', | ||
| 'Dependency Name', | ||
| 'Direct Dependency', | ||
| 'Development Dependency' | ||
| ] | ||
|
|
||
|
|
||
| class SCATablePrinter(BaseTablePrinter): | ||
| def _print_results(self, results: List[DocumentDetections]) -> None: | ||
| detections_per_detection_type_id = self._extract_detections_per_detection_type_id(results) | ||
| self._print_detection_per_detection_type_id(detections_per_detection_type_id) | ||
|
|
||
| @staticmethod | ||
| def _extract_detections_per_detection_type_id(results: List[DocumentDetections]) -> Dict[str, List[Detection]]: | ||
| detections_per_detection_type_id = defaultdict(list) | ||
|
|
||
| for document_detection in results: | ||
| for detection in document_detection.detections: | ||
| detections_per_detection_type_id[detection.detection_type_id].append(detection) | ||
|
|
||
| return detections_per_detection_type_id | ||
|
|
||
| def _print_detection_per_detection_type_id( | ||
| self, detections_per_detection_type_id: Dict[str, List[Detection]] | ||
| ) -> None: | ||
| for detection_type_id in detections_per_detection_type_id: | ||
| detections = detections_per_detection_type_id[detection_type_id] | ||
| headers = self._get_table_headers() | ||
|
|
||
| title = None | ||
| rows = [] | ||
|
|
||
| if detection_type_id == PACKAGE_VULNERABILITY_POLICY_ID: | ||
| title = "Dependencies Vulnerabilities" | ||
|
|
||
| headers = [SEVERITY_COLUMN] + headers | ||
| headers.extend(PREVIEW_DETECTIONS_COMMON_HEADERS) | ||
| headers.append(CVE_COLUMN) | ||
| headers.append(UPGRADE_COLUMN) | ||
|
|
||
| for detection in detections: | ||
| rows.append(self._get_upgrade_package_vulnerability(detection)) | ||
| elif detection_type_id == LICENSE_COMPLIANCE_POLICY_ID: | ||
| title = "License Compliance" | ||
|
|
||
| headers.extend(PREVIEW_DETECTIONS_COMMON_HEADERS) | ||
| headers.append(LICENSE_COLUMN) | ||
|
|
||
| for detection in detections: | ||
| rows.append(self._get_license(detection)) | ||
|
|
||
| if rows: | ||
| self._print_table_detections(detections, headers, rows, title) | ||
|
|
||
| def _get_table_headers(self) -> list: | ||
| if self._is_git_repository(): | ||
| return [REPOSITORY_COLUMN] | ||
|
|
||
| return [] | ||
|
|
||
| def _print_table_detections( | ||
| self, detections: List[Detection], headers: List[str], rows, title: str | ||
| ) -> None: | ||
| self._print_summary_issues(detections, title) | ||
| text_table = Texttable() | ||
| text_table.header(headers) | ||
|
|
||
| self.set_table_width(headers, text_table) | ||
|
|
||
| for row in rows: | ||
| text_table.add_row(row) | ||
|
|
||
| click.echo(text_table.draw()) | ||
|
|
||
| @staticmethod | ||
| def set_table_width(headers: List[str], text_table: Texttable) -> None: | ||
| header_width_size_cols = [] | ||
| for header in headers: | ||
| header_len = len(header) | ||
| if header == CVE_COLUMN: | ||
| header_width_size_cols.append(header_len * 5) | ||
| elif header == UPGRADE_COLUMN: | ||
| header_width_size_cols.append(header_len * 2) | ||
| else: | ||
| header_width_size_cols.append(header_len) | ||
| text_table.set_cols_width(header_width_size_cols) | ||
|
|
||
| @staticmethod | ||
| def _print_summary_issues(detections: List, title: str) -> None: | ||
| click.echo(f'⛔ Found {len(detections)} issues of type: {click.style(title, bold=True)}') | ||
|
|
||
| def _get_common_detection_fields(self, detection: Detection) -> List[str]: | ||
| row = [ | ||
| detection.detection_details.get('file_name'), | ||
| detection.detection_details.get('ecosystem'), | ||
| detection.detection_details.get('package_name'), | ||
| detection.detection_details.get('is_direct_dependency_str'), | ||
| detection.detection_details.get('is_dev_dependency_str') | ||
| ] | ||
|
|
||
| if self._is_git_repository(): | ||
| row = [detection.detection_details.get('repository_name')] + row | ||
|
|
||
| return row | ||
|
|
||
| def _get_upgrade_package_vulnerability(self, detection: Detection) -> List[str]: | ||
| alert = detection.detection_details.get('alert') | ||
| row = [ | ||
| detection.detection_details.get('advisory_severity'), | ||
| *self._get_common_detection_fields(detection), | ||
| detection.detection_details.get('vulnerability_id') | ||
| ] | ||
|
|
||
| upgrade = '' | ||
| if alert.get("first_patched_version"): | ||
| upgrade = f'{alert.get("vulnerable_requirements")} -> {alert.get("first_patched_version")}' | ||
| row.append(upgrade) | ||
|
|
||
| return row | ||
|
|
||
| def _get_license(self, detection: Detection) -> List[str]: | ||
| row = self._get_common_detection_fields(detection) | ||
| row.append(f'{detection.detection_details.get("license")}') | ||
| return row |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from typing import List, Dict, Optional, TYPE_CHECKING | ||
| from texttable import Texttable | ||
|
|
||
| if TYPE_CHECKING: | ||
| from cycode.cli.printers.table_models import ColumnInfo, ColumnWidths | ||
|
|
||
|
|
||
| class Table: | ||
| """Helper class to manage columns and their values in the right order and only if the column should be presented.""" | ||
|
|
||
| def __init__(self, column_infos: Optional[List['ColumnInfo']] = None): | ||
| self._column_widths = None | ||
|
|
||
| self._columns: Dict['ColumnInfo', List[str]] = dict() | ||
| if column_infos: | ||
| self._columns: Dict['ColumnInfo', List[str]] = {columns: list() for columns in column_infos} | ||
|
|
||
| def add(self, column: 'ColumnInfo') -> None: | ||
| self._columns[column] = list() | ||
|
|
||
| def set(self, column: 'ColumnInfo', value: str) -> None: | ||
| # we push values only for existing columns what were added before | ||
| if column in self._columns: | ||
| self._columns[column].append(value) | ||
|
|
||
| def _get_ordered_columns(self) -> List['ColumnInfo']: | ||
| # we are sorting columns by index to make sure that columns will be printed in the right order | ||
| return sorted(self._columns, key=lambda column_info: column_info.index) | ||
|
|
||
| def get_columns_info(self) -> List['ColumnInfo']: | ||
| return self._get_ordered_columns() | ||
|
|
||
| def get_headers(self) -> List[str]: | ||
| return [header.name for header in self._get_ordered_columns()] | ||
|
|
||
| def get_rows(self) -> List[str]: | ||
| column_values = [self._columns[column_info] for column_info in self._get_ordered_columns()] | ||
| return list(zip(*column_values)) | ||
|
|
||
| def set_cols_width(self, column_widths: 'ColumnWidths') -> None: | ||
| header_width_size = [] | ||
| for header in self.get_columns_info(): | ||
| width_multiplier = 1 | ||
| if header in column_widths: | ||
| width_multiplier = column_widths[header] | ||
|
|
||
| header_width_size.append(len(header.name) * width_multiplier) | ||
|
|
||
| self._column_widths = header_width_size | ||
|
|
||
| def get_table(self, max_width: int = 80) -> Texttable: | ||
| table = Texttable(max_width) | ||
| table.header(self.get_headers()) | ||
|
|
||
| for row in self.get_rows(): | ||
| table.add_row(row) | ||
|
|
||
| if self._column_widths: | ||
| table.set_cols_width(self._column_widths) | ||
|
|
||
| return table |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from typing import NamedTuple, Dict | ||
|
|
||
|
|
||
| class ColumnInfoBuilder: | ||
| _index = 0 | ||
|
|
||
| @staticmethod | ||
| def build(name: str) -> 'ColumnInfo': | ||
| column_info = ColumnInfo(name, ColumnInfoBuilder._index) | ||
| ColumnInfoBuilder._index += 1 | ||
| return column_info | ||
|
|
||
|
|
||
| class ColumnInfo(NamedTuple): | ||
| name: str | ||
| index: int # Represents the order of the columns, starting from the left | ||
|
|
||
|
|
||
| ColumnWidths = Dict[ColumnInfo, int] | ||
| ColumnWidthsConfig = Dict[str, ColumnWidths] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.