33import logging
44from logging import StreamHandler
55import shutil
6+ import json
67
78run_directory = os .environ ['RAFT_TOOL_RUN_DIRECTORY' ]
89raft_libs_dir = os .path .join (run_directory , '..' , '..' , 'libs' , 'python3' )
@@ -32,6 +33,30 @@ def emit(self, record):
3233
3334zap = __import__ ("zap-api-scan" )
3435
36+ def post_bugs (target_index ):
37+ if os .path .exists (f'/zap/wrk/{ target_index } -report.json' ):
38+ print (f'Using file { target_index } -report.json for reported bugs.' )
39+ with open (f'/zap/wrk/{ target_index } -report.json' ) as f :
40+ reportData = json .load (f )
41+
42+ # Walk though the report, flattening the alert structure for bug reporting.
43+ # The only nested item is the instances array.
44+ for site in reportData ['site' ]:
45+ print (str (len (site ['alerts' ])) + " bugs found." )
46+ for alert in site ['alerts' ]:
47+ bugDetails = {}
48+ for item in alert :
49+ if item == 'instances' :
50+ instanceList = alert ['instances' ]
51+ for instanceCount in range (0 , len (instanceList )):
52+ for instanceItem in instanceList [instanceCount ]:
53+ bugDetails .update ({"Instance" + str (instanceCount ) + "-" + instanceItem : instanceList [instanceCount ][instanceItem ]})
54+ else :
55+ bugDetails .update ({item : alert [item ]})
56+ raftUtils .report_bug (bugDetails )
57+ else :
58+ print (f'File { target_index } -report.json does NOT exist.' )
59+
3560def run_zap (target_index , targets_total , host , target , token ):
3661 if token :
3762 raftUtils .log_trace ('Authentication token is set' )
@@ -87,12 +112,14 @@ def run_zap(target_index, targets_total, host, target, token):
87112
88113 raftUtils .log_trace (f"ZAP exited with exit code: { r } " )
89114 shutil .copy ('/zap/zap.out' , f'/zap/wrk/{ target_index } -zap.out' )
115+ post_bugs (target_index )
90116
91117 if r <= 2 :
92118 r = 0
93119
94120 if target_index + 1 == targets_total :
95121 raftUtils .report_status_completed (details )
122+
96123 return r
97124
98125def run (target_index , targets_total , host , target , token ):
@@ -104,7 +131,8 @@ def run(target_index, targets_total, host, target, token):
104131 raftUtils .report_status_error ({"Error" : f"{ ex } " })
105132 raise
106133 finally :
107- raftUtils .flush ()
134+ raftUtils .flush ()
135+ os .sys .stdout .flush ()
108136
109137
110138if __name__ == "__main__" :
@@ -128,4 +156,4 @@ def run(target_index, targets_total, host, target, token):
128156 host = args [i + 1 ]
129157 i = i + 1
130158
131- run (target_index , targets_total , host , target , token )
159+ run (target_index , targets_total , host , target , token )
0 commit comments