22
22
from google .cloud import storage
23
23
from slack_sdk .web import WebClient as SlackClient
24
24
25
- from zap import ScanType , zap_compliance_scan , zap_connect
25
+ from zap import ScanType , zap_compliance_scan , zap_shutdown
26
26
27
27
28
28
def fetch_dojo_product_name (defect_dojo , defect_dojo_user , defect_dojo_key , product_id ):
@@ -34,7 +34,7 @@ def fetch_dojo_product_name(defect_dojo, defect_dojo_user, defect_dojo_key, prod
34
34
defect_dojo , defect_dojo_key , defect_dojo_user , debug = False , timeout = 120 )
35
35
max_retries = int (getenv ("MAX_RETRIES" , '6' ))
36
36
retry_delay = 30
37
- for attempt in range (max_retries ):
37
+ for _ in range (max_retries ):
38
38
try :
39
39
product = dojo .get_product (product_id = product_id )
40
40
return product .data ["name" ]
@@ -106,11 +106,21 @@ def defectdojo_upload(product_id: int, zap_filename: str, defect_dojo_key: str,
106
106
date = datetime .today ().strftime ("%Y%m%d%H:%M" )
107
107
lead_id = fetch_dojo_lead_id (dojo , defect_dojo_user )
108
108
109
- engagement = dojo .create_engagement ( name = date , product_id = product_id , lead_id = lead_id ,
110
- target_start = datetime .today ().strftime ("%Y-%m-%d" ),
111
- target_end = datetime .today ().strftime ("%Y-%m-%d" ), status = "In Progress" ,
112
- active = 'True' ,deduplication_on_engagement = 'False' )
113
- engagement_id = engagement .data ["id" ]
109
+ # The call to create_engagement sometimes fails.
110
+ retry_delay = 20
111
+ max_retries = int (getenv ("MAX_RETRIES" , '5' ))
112
+ for attempt in range (max_retries ):
113
+ try :
114
+ engagement = dojo .create_engagement ( name = date , product_id = product_id , lead_id = lead_id ,
115
+ target_start = datetime .today ().strftime ("%Y-%m-%d" ),
116
+ target_end = datetime .today ().strftime ("%Y-%m-%d" ), status = "In Progress" ,
117
+ active = 'True' ,deduplication_on_engagement = 'False' )
118
+ engagement_id = engagement .data ["id" ]
119
+ break
120
+ except Exception : # pylint: disable=broad-except
121
+ sleep (retry_delay )
122
+ if attempt == max_retries - 1 :
123
+ raise RuntimeError ("Maximum retry attempts reached for closing engagement" )
114
124
115
125
dojo_upload = dojo .upload_scan (engagement_id = engagement_id ,
116
126
scan_type = "ZAP Scan" ,
@@ -242,7 +252,6 @@ def get_codedx_initial_report(
242
252
file_name = report_file ,
243
253
filters = filters ,
244
254
)
245
-
246
255
return report_file
247
256
248
257
@@ -372,7 +381,7 @@ def upload_googledrive(scan_type, zap_filename, codedx_project, report_file, sla
372
381
"""
373
382
root_id = os .getenv ('DRIVE_ROOT_ID' , None )
374
383
drive_id = os .getenv ('DRIVE_ID' , None )
375
- if scan_type in (ScanType .BASELINE ):
384
+ if scan_type in (ScanType .BASELINE ):
376
385
return
377
386
try :
378
387
logging .info ('Setting up the google drive API service for uploading reports.' )
@@ -389,7 +398,6 @@ def upload_googledrive(scan_type, zap_filename, codedx_project, report_file, sla
389
398
date = datetime .today ()
390
399
date = drivehelper .adjust_date (date )
391
400
_ , xml_folder_dict , zap_raw_folder = drivehelper .get_upload_folders (folder_structure , date )
392
-
393
401
file = drivehelper .upload_file_to_drive (zap_filename ,
394
402
xml_folder_dict .get ('id' ),
395
403
drive_id ,
@@ -508,10 +516,9 @@ def main(): # pylint: disable=too-many-locals
508
516
dd ,
509
517
target_url
510
518
)
511
- zap = zap_connect ()
512
- zap .core .shutdown ()
519
+ zap_shutdown ()
513
520
return
514
-
521
+
515
522
# upload its results to Code Dx
516
523
cdx = CodeDx (codedx_url , codedx_api_key )
517
524
@@ -533,8 +540,7 @@ def main(): # pylint: disable=too-many-locals
533
540
logging .info ("ready to upload to google drive" )
534
541
upload_googledrive (scan_type , zap_filename , codedx_project , cdx_filename , slack_token , slack_channel )
535
542
536
- zap = zap_connect ()
537
- zap .core .shutdown ()
543
+ zap_shutdown ()
538
544
return
539
545
except Exception as error : # pylint: disable=broad-except
540
546
error_message = f"[RETRY-{ attempt } ] Exception running Zap Scans: { error } "
@@ -546,8 +552,7 @@ def main(): # pylint: disable=too-many-locals
546
552
except :
547
553
logging .error (f"Slack could not post to { slack_channel } " )
548
554
try :
549
- zap = zap_connect ()
550
- zap .core .shutdown ()
555
+ zap_shutdown ()
551
556
except Exception as zap_e : # pylint: disable=broad-except
552
557
error_message = f"Error shutting down zap: { zap_e } "
553
558
error_slack_alert (
0 commit comments