Skip to content

Commit 84a2569

Browse files
author
Glenn Snyder
authored
Merge pull request #77 from blackducksoftware/gsnyder/generating-vuln-reports
adding example showing how to create and download a vulnerability status report
2 parents 31ec483 + 73d8122 commit 84a2569

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

blackduck/HubRestApi.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,26 @@ def download_report(self, report_id):
557557
url = self.get_urlbase() + "/api/reports/{}".format(report_id)
558558
return self.execute_get(url, {'Content-Type': 'application/zip', 'Accept':'application/zip'})
559559

560+
##
561+
#
562+
# (Global) Vulnerability reports
563+
#
564+
##
565+
valid_vuln_status_report_formats = ["CSV", "JSON"]
566+
def create_vuln_status_report(self, format="CSV"):
567+
assert format in HubInstance.valid_vuln_status_report_formats, "Format must be one of {}".format(HubInstance.valid_vuln_status_report_formats)
568+
569+
post_data = {
570+
"reportFormat": format,
571+
"locale": "en_US"
572+
}
573+
url = self.get_apibase() + "/vulnerability-status-reports"
574+
custom_headers = {
575+
'Content-Type': 'application/vnd.blackducksoftware.report-4+json',
576+
'Accept': 'application/vnd.blackducksoftware.report-4+json'
577+
}
578+
return self.execute_post(url, custom_headers=custom_headers, data=post_data)
579+
560580
##
561581
#
562582
# License stuff

blackduck/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
VERSION = (0, 0, 40)
1+
VERSION = (0, 0, 41)
22

33
__version__ = '.'.join(map(str, VERSION))
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
Created on Jan 15, 2020
3+
4+
@author: gsnyder
5+
6+
Generate vulnerability status report
7+
'''
8+
9+
from blackduck.HubRestApi import HubInstance
10+
11+
import argparse
12+
import json
13+
import time
14+
15+
parser = argparse.ArgumentParser("A program to create a vulnerability status report")
16+
parser.add_argument("--file_name", default="vuln_status_report")
17+
parser.add_argument('-f', '--format', default='CSV', choices=["CSV", "JSON"], help="Report format")
18+
parser.add_argument('-t', '--tries', default=4, type=int, help="How many times to retry downloading the report, i.e. wait for the report to be generated")
19+
parser.add_argument('-s', '--sleep_time', default=5, type=int, help="The amount of time to sleep in-between (re-)tries to download the report")
20+
21+
args = parser.parse_args()
22+
23+
hub = HubInstance()
24+
25+
class FailedReportDownload(Exception):
26+
pass
27+
28+
def download_report(location, report_format, filename, retries=args.tries):
29+
report_id = location.split("/")[-1]
30+
31+
if retries:
32+
print("Retrieving generated report from {}".format(location))
33+
# response = hub.download_vuln_status_report(location)
34+
response = hub.execute_get(location)
35+
36+
if response.status_code == 200:
37+
report_obj = response.json()
38+
download_url = hub.get_link(report_obj, "download") + ".json"
39+
content_url = hub.get_link(report_obj, "content")
40+
if report_format == "CSV":
41+
download_filename = filename + ".zip"
42+
response = hub.execute_get(download_url, {'Content-Type': 'application/zip'})
43+
else:
44+
download_filename = filename + ".json"
45+
response = hub.execute_get(content_url)
46+
47+
if response.status_code == 200:
48+
if report_format == "CSV":
49+
with open(download_filename, "wb") as f:
50+
f.write(response.content)
51+
print("Successfully downloaded zip file to {} for report {}".format(
52+
download_filename, report_id))
53+
else:
54+
with open(download_filename, "w") as f:
55+
json.dump(response.json(), f, indent=3)
56+
print("Successfully downloaded json report data to {} for report {}".format(
57+
download_filename, report_id))
58+
else:
59+
print("Failed to retrieve report {}".format(report_id))
60+
print("Probably not ready yet, waiting 5 seconds then retrying...")
61+
time.sleep(args.sleep_time)
62+
retries -= 1
63+
download_report(location, report_format, filename, retries)
64+
else:
65+
print("Failed to find report information at location {}, status code: {}".format(location, response.status_code))
66+
else:
67+
raise FailedReportDownload("Failed to retrieve report {} after {} retries".format(report_id, args.tries))
68+
69+
response = hub.create_vuln_status_report(format=args.format)
70+
71+
if response.status_code == 201:
72+
print("Successfully created vulnerability status report")
73+
location = response.headers['Location']
74+
download_report(location, args.format, args.file_name)
75+
else:
76+
print("Failed to create vulnerability status report, status code returned: {}".format(response.status_code))

0 commit comments

Comments
 (0)