|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import http.client |
| 4 | +# http.client._MAXHEADERS = 1000 |
| 5 | + |
| 6 | +import argparse |
| 7 | +import copy |
| 8 | +from datetime import datetime |
| 9 | +import json |
| 10 | +import logging |
| 11 | +import sys |
| 12 | +import timestring |
| 13 | + |
| 14 | +from blackduck.HubRestApi import HubInstance, object_id |
| 15 | + |
| 16 | +parser = argparse.ArgumentParser("Update vulnerability remediation status for a given vulnerability either using the default status or a user-supplied status") |
| 17 | +parser.add_argument("vulnerability", help="e.g. CVE-2020-8488") |
| 18 | +parser.add_argument("project", help="The project to apply the updates to") |
| 19 | +parser.add_argument("version", help="The version within the project to apply the updates to") |
| 20 | + |
| 21 | +sub_parsers = parser.add_subparsers(help="Update modes") |
| 22 | + |
| 23 | +use_default_parser = sub_parsers.add_parser("use_default") |
| 24 | +user_supplied_parser = sub_parsers.add_parser("user_supplied") |
| 25 | + |
| 26 | +user_supplied_parser.add_argument( |
| 27 | + "status", |
| 28 | + choices=['new', 'review', 'required', 'complete', 'mitigated', 'patched', 'ignored', 'duplicate']) |
| 29 | +user_supplied_parser.add_argument("comment") |
| 30 | + |
| 31 | +args = parser.parse_args() |
| 32 | + |
| 33 | +if args.version and not args.project: |
| 34 | + raise RuntimeError("You must specify a project with (-p, --project) with the version option") |
| 35 | + |
| 36 | +logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', stream=sys.stderr, level=logging.DEBUG) |
| 37 | +logging.getLogger("requests").setLevel(logging.WARNING) |
| 38 | +logging.getLogger("urllib3").setLevel(logging.WARNING) |
| 39 | + |
| 40 | +hub = HubInstance() |
| 41 | + |
| 42 | +project = hub.get_project_by_name(args.project) |
| 43 | + |
| 44 | +version = hub.get_version_by_name(project, args.version) |
| 45 | + |
| 46 | +vulnerable_components_url = hub.get_link(version, "vulnerable-components") + "?limit=9999" |
| 47 | +custom_headers = {'Accept':'application/vnd.blackducksoftware.bill-of-materials-6+json'} |
| 48 | +response = hub.execute_get(vulnerable_components_url, custom_headers=custom_headers) |
| 49 | +vulnerable_bom_components = response.json().get('items', []) |
| 50 | + |
| 51 | +if hasattr(args, "status"): |
| 52 | + # user supplied status |
| 53 | + status = args.status.upper() |
| 54 | + comment = args.comment |
| 55 | +else: |
| 56 | + default_remediation_status_url = hub.get_apibase() + f"/vulnerabilities/{args.vulnerability}/default-remediation-status" |
| 57 | + default_remediation_status = hub.execute_get(default_remediation_status_url).json() |
| 58 | + status = default_remediation_status['remediationStatus'] |
| 59 | + comment = default_remediation_status['comment'] |
| 60 | + |
| 61 | +for i, vuln in enumerate(vulnerable_bom_components): |
| 62 | + vuln_name = vuln['vulnerabilityWithRemediation']['vulnerabilityName'] |
| 63 | + |
| 64 | + if vuln_name == args.vulnerability: |
| 65 | + vuln['remediationStatus'] = status |
| 66 | + vuln['remediationComment'] = comment |
| 67 | + logging.debug(f"Updating vuln {args.vulnerability} in project {project['name']}, version {version['versionName']} with status {status} and comment {comment}") |
| 68 | + result = hub.execute_put(vuln['_meta']['href'], data=vuln) |
| 69 | + if result.status_code == 202: |
| 70 | + logging.info(f"Successfully updated vuln {args.vulnerability} in project {project['name']}, version {version['versionName']} with status {status} and comment {comment}") |
| 71 | + else: |
| 72 | + logging.error(f"Failed to update vuln {args.vulnerability} in project {project['name']}, version {version['versionName']}; http status code: {response.status_code}") |
| 73 | + |
0 commit comments