|
| 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