Skip to content

Commit 3a150df

Browse files
author
Glenn Snyder
committed
adding another sample
1 parent 3d7e303 commit 3a150df

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

examples/get_bom_component_vulnerability_info.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
vuln_details_response = hub.execute_get(vuln_url, custom_headers={'Accept': 'application/json'})
8888
vuln_details = vuln_details_response.json()
8989

90+
import pdb; pdb.set_trace()
91+
9092
vuln['additional_vuln_info'] = vuln_details
9193
if source == 'BDSA':
9294
# get related vulnerability info, i.e. CVE

examples/get_unmatched_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
version = hub.get_project_version_by_name(args.project_name, args.version)
2323

24-
matched_files_url = version['_meta']['href'] + "/matched-files?limit=9999&filter=bomMatchType:unmatched"
24+
matched_files_url = version['_meta']['href'] + "/matched-files?limit=99999&filter=bomMatchType:unmatched"
2525

2626
unmatched_files = hub.execute_get(matched_files_url).json().get('items', [])
2727

examples/update_vuln_status.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)