Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit d6a9da9

Browse files
authored
Track zap complete state (#227)
1 parent 1ad7e08 commit d6a9da9

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

Scripts/Tests/bvt-petstore3.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,11 @@ def bvt(cli, definitions, subs):
151151
print('Validating that bugs posted events matches total bugs found in job status')
152152
total_bugs_found = 0
153153
for r in job_status_events:
154-
if r['Data']['State'] == 'Completed' and r['Data']['AgentName'] != r['Data']['JobId'] and r['Data']['Tool'] == 'RESTler':
155-
total_bugs_found += r['Data']['Metrics']['TotalBugBucketsCount']
154+
if r['Data']['State'] == 'Completed' and r['Data']['AgentName'] != r['Data']['JobId']:
155+
if r['Data']['Tool'] == 'RESTler':
156+
total_bugs_found += r['Data']['Metrics']['TotalBugBucketsCount']
157+
elif r['Data']['Tool'] == 'ZAP':
158+
total_bugs_found += int(r['Data']['Details']['totalBugCount'])
156159

157160
print(f'Total bugs found: {total_bugs_found}')
158161
print(f'Number of Bug found events: {len(bug_found_events)}')

cli/raft-tools/tools/ZAP/scan.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ def emit(self, record):
3030
if i != -1:
3131
self.details["Scan progress"] = txt[i :]
3232
raftUtils.report_status_running(self.details)
33+
else:
34+
progress='Passive scanning complete'
35+
i = txt.find(progress)
36+
if i != -1:
37+
self.details["Scan progress"] = "Active and Passive Scan progress %100"
38+
raftUtils.report_status_running(self.details)
3339

3440
zap = __import__("zap-api-scan")
3541

@@ -57,6 +63,18 @@ def post_bugs(target_index):
5763
else:
5864
print(f'File {target_index}-report.json does NOT exist.')
5965

66+
def count_bugs(target_index):
67+
bugCount = 0
68+
if os.path.exists(f'/zap/wrk/{target_index}-report.json'):
69+
with open(f'/zap/wrk/{target_index}-report.json') as f:
70+
reportData = json.load(f)
71+
72+
# Every alert is a bug
73+
for site in reportData['site']:
74+
bugCount = len(site['alerts'])
75+
76+
return bugCount
77+
6078
def run_zap(target_index, targets_total, host, target, token):
6179
if token:
6280
raftUtils.log_trace('Authentication token is set')
@@ -85,7 +103,7 @@ def run_zap(target_index, targets_total, host, target, token):
85103
pass
86104

87105
try:
88-
details = {"targetIndex": target_index, "numberOfTargets" : targets_total, "target": target}
106+
details = {"targetIndex": target_index, "numberOfTargets" : targets_total, "target": target, "totalBugCount": 0}
89107
print(f"Starting ZAP target: {target} host_config: {host_config}")
90108

91109
if os.path.exists(target):
@@ -94,24 +112,29 @@ def run_zap(target_index, targets_total, host, target, token):
94112

95113
raftUtils.log_trace(f"Starting ZAP")
96114
raftUtils.report_status_running(details)
115+
97116
status_reporter = StatusReporter(details)
98117
logger = logging.getLogger()
99118
logger.addHandler(status_reporter)
119+
100120
zap.main([ '-t', target,
101121
'-f', 'openapi',
102122
'-J', f'{target_index}-report.json',
103123
'-r', f'{target_index}-report.html',
104124
'-w', f'{target_index}-report.md',
105125
'-x', f'{target_index}-report.xml',
106126
'-d'] + zap_auth_config + host_config)
107-
details["Scan progress"] = "Active scan progress %: 100"
108-
raftUtils.report_status_running(details)
109127

110128
except SystemExit as e:
111129
r = e.code
112130

113131
raftUtils.log_trace(f"ZAP exited with exit code: {r}")
114132
shutil.copy('/zap/zap.out', f'/zap/wrk/{target_index}-zap.out')
133+
134+
# Update the status with the total bug count.
135+
details["totalBugCount"] = count_bugs(target_index)
136+
raftUtils.report_status_running(details)
137+
115138
post_bugs(target_index)
116139

117140
if r <= 2:

0 commit comments

Comments
 (0)