1- import json
21import logging
32import os
43import sys
@@ -99,6 +98,10 @@ def set_issue_detected_by_scan_results(context: click.Context, scan_results: Lis
9998 set_issue_detected (context , any (scan_result .issue_detected for scan_result in scan_results ))
10099
101100
101+ def _should_use_scan_service (scan_type : str , scan_parameters : Optional [dict ] = None ) -> bool :
102+ return scan_type == consts .SECRET_SCAN_TYPE and scan_parameters is not None and scan_parameters ['report' ] is True
103+
104+
102105def _enrich_scan_result_with_data_from_detection_rules (
103106 cycode_client : 'ScanClient' , scan_type : str , scan_result : ZippedFileScanResult
104107) -> None :
@@ -148,14 +151,21 @@ def _scan_batch_thread_func(batch: List[Document]) -> Tuple[str, CliError, Local
148151
149152 scan_id = str (_generate_unique_id ())
150153 scan_completed = False
154+ should_use_scan_service = _should_use_scan_service (scan_type , scan_parameters )
151155
152156 try :
153157 logger .debug ('Preparing local files, %s' , {'batch_size' : len (batch )})
154158 zipped_documents = zip_documents (scan_type , batch )
155159 zip_file_size = zipped_documents .size
156-
157160 scan_result = perform_scan (
158- cycode_client , zipped_documents , scan_type , scan_id , is_git_diff , is_commit_range , scan_parameters
161+ cycode_client ,
162+ zipped_documents ,
163+ scan_type ,
164+ scan_id ,
165+ is_git_diff ,
166+ is_commit_range ,
167+ scan_parameters ,
168+ should_use_scan_service ,
159169 )
160170
161171 _enrich_scan_result_with_data_from_detection_rules (cycode_client , scan_type , scan_result )
@@ -194,6 +204,7 @@ def _scan_batch_thread_func(batch: List[Document]) -> Tuple[str, CliError, Local
194204 zip_file_size ,
195205 command_scan_type ,
196206 error_message ,
207+ should_use_scan_service ,
197208 )
198209
199210 return scan_id , error , local_scan_result
@@ -315,14 +326,13 @@ def scan_commit_range_documents(
315326 local_scan_result = error_message = None
316327 scan_completed = False
317328 scan_id = str (_generate_unique_id ())
318-
319329 from_commit_zipped_documents = InMemoryZip ()
320330 to_commit_zipped_documents = InMemoryZip ()
321331
322332 try :
323333 progress_bar .set_section_length (ScanProgressBarSection .SCAN , 1 )
324334
325- scan_result = init_default_scan_result (scan_id )
335+ scan_result = init_default_scan_result (cycode_client , scan_id , scan_type )
326336 if should_scan_documents (from_documents_to_scan , to_documents_to_scan ):
327337 logger .debug ('Preparing from-commit zip' )
328338 from_commit_zipped_documents = zip_documents (scan_type , from_documents_to_scan )
@@ -428,8 +438,9 @@ def perform_scan(
428438 is_git_diff : bool ,
429439 is_commit_range : bool ,
430440 scan_parameters : dict ,
441+ should_use_scan_service : bool = False ,
431442) -> ZippedFileScanResult :
432- if scan_type in (consts .SCA_SCAN_TYPE , consts .SAST_SCAN_TYPE ):
443+ if scan_type in (consts .SCA_SCAN_TYPE , consts .SAST_SCAN_TYPE ) or should_use_scan_service :
433444 return perform_scan_async (cycode_client , zipped_documents , scan_type , scan_parameters )
434445
435446 if is_commit_range :
@@ -439,12 +450,20 @@ def perform_scan(
439450
440451
441452def perform_scan_async (
442- cycode_client : 'ScanClient' , zipped_documents : 'InMemoryZip' , scan_type : str , scan_parameters : dict
453+ cycode_client : 'ScanClient' ,
454+ zipped_documents : 'InMemoryZip' ,
455+ scan_type : str ,
456+ scan_parameters : dict ,
443457) -> ZippedFileScanResult :
444458 scan_async_result = cycode_client .zipped_file_scan_async (zipped_documents , scan_type , scan_parameters )
445459 logger .debug ('scan request has been triggered successfully, scan id: %s' , scan_async_result .scan_id )
446460
447- return poll_scan_results (cycode_client , scan_async_result .scan_id , scan_type )
461+ return poll_scan_results (
462+ cycode_client ,
463+ scan_async_result .scan_id ,
464+ scan_type ,
465+ scan_parameters .get ('report' ),
466+ )
448467
449468
450469def perform_commit_range_scan_async (
@@ -460,13 +479,16 @@ def perform_commit_range_scan_async(
460479 )
461480
462481 logger .debug ('scan request has been triggered successfully, scan id: %s' , scan_async_result .scan_id )
463- return poll_scan_results (cycode_client , scan_async_result .scan_id , scan_type , timeout )
482+ return poll_scan_results (
483+ cycode_client , scan_async_result .scan_id , scan_type , scan_parameters .get ('report' ), timeout
484+ )
464485
465486
466487def poll_scan_results (
467488 cycode_client : 'ScanClient' ,
468489 scan_id : str ,
469490 scan_type : str ,
491+ should_get_report : bool = False ,
470492 polling_timeout : Optional [int ] = None ,
471493) -> ZippedFileScanResult :
472494 if polling_timeout is None :
@@ -483,7 +505,7 @@ def poll_scan_results(
483505 print_debug_scan_details (scan_details )
484506
485507 if scan_details .scan_status == consts .SCAN_STATUS_COMPLETED :
486- return _get_scan_result (cycode_client , scan_type , scan_id , scan_details )
508+ return _get_scan_result (cycode_client , scan_type , scan_id , scan_details , should_get_report )
487509
488510 if scan_details .scan_status == consts .SCAN_STATUS_ERROR :
489511 raise custom_exceptions .ScanAsyncError (
@@ -735,6 +757,7 @@ def _report_scan_status(
735757 zip_size : int ,
736758 command_scan_type : str ,
737759 error_message : Optional [str ],
760+ should_use_scan_service : bool = False ,
738761) -> None :
739762 try :
740763 end_scan_time = time .time ()
@@ -751,7 +774,7 @@ def _report_scan_status(
751774 'scan_type' : scan_type ,
752775 }
753776
754- cycode_client .report_scan_status (scan_type , scan_id , scan_status )
777+ cycode_client .report_scan_status (scan_type , scan_id , scan_status , should_use_scan_service )
755778 except Exception as e :
756779 logger .debug ('Failed to report scan status, %s' , {'exception_message' : str (e )})
757780
@@ -769,37 +792,49 @@ def _does_severity_match_severity_threshold(severity: str, severity_threshold: s
769792
770793
771794def _get_scan_result (
772- cycode_client : 'ScanClient' , scan_type : str , scan_id : str , scan_details : 'ScanDetailsResponse'
795+ cycode_client : 'ScanClient' ,
796+ scan_type : str ,
797+ scan_id : str ,
798+ scan_details : 'ScanDetailsResponse' ,
799+ should_get_report : bool = False ,
773800) -> ZippedFileScanResult :
774801 if not scan_details .detections_count :
775- return init_default_scan_result (scan_id , scan_details . metadata )
802+ return init_default_scan_result (cycode_client , scan_id , scan_type , should_get_report )
776803
777804 wait_for_detections_creation (cycode_client , scan_type , scan_id , scan_details .detections_count )
778805
779806 scan_detections = cycode_client .get_scan_detections (scan_type , scan_id )
807+
780808 return ZippedFileScanResult (
781809 did_detect = True ,
782810 detections_per_file = _map_detections_per_file (scan_detections ),
783811 scan_id = scan_id ,
784- report_url = _try_get_report_url ( scan_details . metadata ),
812+ report_url = _try_get_report_url_if_needed ( cycode_client , should_get_report , scan_id , scan_type ),
785813 )
786814
787815
788- def init_default_scan_result (scan_id : str , scan_metadata : Optional [str ] = None ) -> ZippedFileScanResult :
816+ def init_default_scan_result (
817+ cycode_client : 'ScanClient' , scan_id : str , scan_type : str , should_get_report : bool = False
818+ ) -> ZippedFileScanResult :
789819 return ZippedFileScanResult (
790- did_detect = False , detections_per_file = [], scan_id = scan_id , report_url = _try_get_report_url (scan_metadata )
820+ did_detect = False ,
821+ detections_per_file = [],
822+ scan_id = scan_id ,
823+ report_url = _try_get_report_url_if_needed (cycode_client , should_get_report , scan_id , scan_type ),
791824 )
792825
793826
794- def _try_get_report_url (metadata_json : Optional [str ]) -> Optional [str ]:
795- if metadata_json is None :
827+ def _try_get_report_url_if_needed (
828+ cycode_client : 'ScanClient' , should_get_report : bool , scan_id : str , scan_type : str
829+ ) -> Optional [str ]:
830+ if not should_get_report :
796831 return None
797832
798833 try :
799- metadata_json = json . loads ( metadata_json )
800- return metadata_json . get ( ' report_url' )
801- except json . JSONDecodeError :
802- return None
834+ report_url_response = cycode_client . get_scan_report_url ( scan_id , scan_type )
835+ return report_url_response . report_url
836+ except Exception as e :
837+ logger . debug ( 'Failed to get report url: %s' , str ( e ))
803838
804839
805840def wait_for_detections_creation (
@@ -856,9 +891,18 @@ def _get_file_name_from_detection(detection: dict) -> str:
856891 if detection ['category' ] == 'SAST' :
857892 return detection ['detection_details' ]['file_path' ]
858893
894+ if detection ['category' ] == 'SecretDetection' :
895+ return _get_secret_file_name_from_detection (detection )
896+
859897 return detection ['detection_details' ]['file_name' ]
860898
861899
900+ def _get_secret_file_name_from_detection (detection : dict ) -> str :
901+ file_path : str = detection ['detection_details' ]['file_path' ]
902+ file_name : str = detection ['detection_details' ]['file_name' ]
903+ return os .path .join (file_path , file_name )
904+
905+
862906def _does_reach_to_max_commits_to_scan_limit (commit_ids : List [str ], max_commits_count : Optional [int ]) -> bool :
863907 if max_commits_count is None :
864908 return False
0 commit comments