|
| 1 | +import pathlib |
| 2 | +import time |
| 3 | +from platform import platform |
| 4 | +from typing import TYPE_CHECKING, Optional |
| 5 | + |
| 6 | +from cycode.cli import consts |
| 7 | +from cycode.cli.commands.report.sbom.sbom_report_file import SbomReportFile |
| 8 | +from cycode.cli.config import configuration_manager |
| 9 | +from cycode.cli.exceptions.custom_exceptions import ReportAsyncError |
| 10 | +from cycode.cli.utils.progress_bar import SbomReportProgressBarSection |
| 11 | +from cycode.cyclient import logger |
| 12 | +from cycode.cyclient.models import ReportExecutionSchema |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from cycode.cli.utils.progress_bar import BaseProgressBar |
| 16 | + from cycode.cyclient.report_client import ReportClient |
| 17 | + |
| 18 | + |
| 19 | +def _poll_report_execution_until_completed( |
| 20 | + progress_bar: 'BaseProgressBar', |
| 21 | + client: 'ReportClient', |
| 22 | + report_execution_id: int, |
| 23 | + polling_timeout: Optional[int] = None, |
| 24 | +) -> ReportExecutionSchema: |
| 25 | + if polling_timeout is None: |
| 26 | + polling_timeout = configuration_manager.get_report_polling_timeout_in_seconds() |
| 27 | + |
| 28 | + end_polling_time = time.time() + polling_timeout |
| 29 | + while time.time() < end_polling_time: |
| 30 | + report_execution = client.get_report_execution(report_execution_id) |
| 31 | + report_label = report_execution.error_message or report_execution.status_message |
| 32 | + |
| 33 | + progress_bar.update_label(report_label) |
| 34 | + |
| 35 | + if report_execution.status == consts.REPORT_STATUS_COMPLETED: |
| 36 | + return report_execution |
| 37 | + |
| 38 | + if report_execution.status == consts.REPORT_STATUS_ERROR: |
| 39 | + raise ReportAsyncError(f'Error occurred while trying to generate report: {report_label}') |
| 40 | + |
| 41 | + time.sleep(consts.REPORT_POLLING_WAIT_INTERVAL_IN_SECONDS) |
| 42 | + |
| 43 | + raise ReportAsyncError(f'Timeout exceeded while waiting for report to complete. Timeout: {polling_timeout} sec.') |
| 44 | + |
| 45 | + |
| 46 | +def send_report_feedback( |
| 47 | + client: 'ReportClient', |
| 48 | + start_scan_time: float, |
| 49 | + report_type: str, |
| 50 | + report_command_type: str, |
| 51 | + request_report_parameters: dict, |
| 52 | + report_execution_id: int, |
| 53 | + error_message: Optional[str] = None, |
| 54 | + request_zip_file_size: Optional[int] = None, |
| 55 | + **kwargs, |
| 56 | +) -> None: |
| 57 | + try: |
| 58 | + request_report_parameters.update(kwargs) |
| 59 | + |
| 60 | + end_scan_time = time.time() |
| 61 | + scan_status = { |
| 62 | + 'report_type': report_type, |
| 63 | + 'report_command_type': report_command_type, |
| 64 | + 'request_report_parameters': request_report_parameters, |
| 65 | + 'operation_system': platform(), |
| 66 | + 'error_message': error_message, |
| 67 | + 'execution_time': int(end_scan_time - start_scan_time), |
| 68 | + 'request_zip_file_size': request_zip_file_size, |
| 69 | + } |
| 70 | + |
| 71 | + client.report_status(report_execution_id, scan_status) |
| 72 | + except Exception as e: |
| 73 | + logger.debug(f'Failed to send report feedback: {e}') |
| 74 | + |
| 75 | + |
| 76 | +def create_sbom_report( |
| 77 | + progress_bar: 'BaseProgressBar', |
| 78 | + client: 'ReportClient', |
| 79 | + report_execution_id: int, |
| 80 | + output_file: Optional[pathlib.Path], |
| 81 | + output_format: str, |
| 82 | +) -> None: |
| 83 | + report_execution = _poll_report_execution_until_completed(progress_bar, client, report_execution_id) |
| 84 | + |
| 85 | + progress_bar.set_section_length(SbomReportProgressBarSection.GENERATION) |
| 86 | + |
| 87 | + report_path = report_execution.storage_details.path |
| 88 | + report_content = client.get_file_content(report_path) |
| 89 | + |
| 90 | + progress_bar.set_section_length(SbomReportProgressBarSection.RECEIVE_REPORT) |
| 91 | + progress_bar.stop() |
| 92 | + |
| 93 | + sbom_report = SbomReportFile(report_path, output_format, output_file) |
| 94 | + sbom_report.write(report_content) |
0 commit comments