Skip to content

Commit 99c8fc5

Browse files
committed
Adding nodetails and ajustable limit
1 parent 7d882c7 commit 99c8fc5

File tree

1 file changed

+47
-35
lines changed

1 file changed

+47
-35
lines changed

examples/get_bom_component_vulnerability_info.py

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@
4444
parser.add_argument("-s", "--save_dt",
4545
action='store_true',
4646
help="If set, the date/time will be saved to a file named '.last_run' in the current directory which can be used later with the -n option to see vulnerabilities published since the last run.")
47+
parser.add_argument("-l", "--limit", default=9999, help="Set limit on number of vulnerabilitties to retrieve (default 9999)")
48+
parser.add_argument("-nd", type=bool, default=False, help="Disables retrieving details for each vulnerability to reduce execution time")
4749
args = parser.parse_args()
4850

51+
4952
if args.newer_than:
5053
newer_than = timestring.Date(args.newer_than).date
5154
else:
@@ -66,47 +69,51 @@
6669
version = hub.get_version_by_name(project, args.version)
6770
version_id = object_id(version)
6871

69-
vulnerable_components_url = hub.get_link(version, "vulnerable-components") + "?limit=9999"
72+
vulnerablity_limit = "limit?={}".format(args.limit)
73+
74+
vulnerable_components_url = hub.get_link(version, "vulnerable-components") + vulnerablity_limit
7075
custom_headers = {'Accept':'application/vnd.blackducksoftware.bill-of-materials-6+json'}
7176
response = hub.execute_get(vulnerable_components_url, custom_headers=custom_headers)
7277
vulnerable_bom_components = response.json().get('items', [])
7378

7479
bdsa_records = set()
7580
cve_records = set()
7681

77-
for i, vuln in enumerate(vulnerable_bom_components):
78-
source = vuln['vulnerabilityWithRemediation']['source']
79-
vuln_name = vuln['vulnerabilityWithRemediation']['vulnerabilityName']
8082

81-
# Retrieve additional details about the vulnerability
82-
#
83+
if args.nodetails==False:
84+
for i, vuln in enumerate(vulnerable_bom_components):
85+
source = vuln['vulnerabilityWithRemediation']['source']
86+
vuln_name = vuln['vulnerabilityWithRemediation']['vulnerabilityName']
8387

84-
update_guidance_url = vuln['componentVersion'] + "/upgrade-guidance"
85-
update_guidance_results = hub.execute_get(update_guidance_url).json()
86-
vuln['update_guidance'] = update_guidance_results
88+
# Retrieve additional details about the vulnerability
89+
#
8790

88-
logging.debug("Retrieving additional details regarding vuln {}, i={}".format(vuln_name, i))
89-
vuln_url = hub.get_apibase() + "/vulnerabilities/{}".format(vuln_name)
90-
vuln_details_response = hub.execute_get(vuln_url, custom_headers={'Accept': 'application/json'})
91-
vuln_details = vuln_details_response.json()
91+
update_guidance_url = vuln['componentVersion'] + "/upgrade-guidance"
92+
update_guidance_results = hub.execute_get(update_guidance_url).json()
93+
vuln['update_guidance'] = update_guidance_results
9294

93-
vuln['additional_vuln_info'] = vuln_details
95+
logging.debug("Retrieving additional details regarding vuln {}, i={}".format(vuln_name, i))
96+
vuln_url = hub.get_apibase() + "/vulnerabilities/{}".format(vuln_name)
97+
vuln_details_response = hub.execute_get(vuln_url, custom_headers={'Accept': 'application/json'})
98+
vuln_details = vuln_details_response.json()
9499

95-
if source == 'BDSA':
96-
bdsa_records.add(vuln_name)
100+
vuln['additional_vuln_info'] = vuln_details
97101

98-
# get related vulnerability info, i.e. CVE
99-
# note: not all BDSA records will have a corresponding CVE record
100-
cve_url = hub.get_link(vuln_details, "related-vulnerability")
101-
if cve_url:
102-
cve_details_response = hub.execute_get(cve_url, custom_headers={'Accept': 'application/json'})
103-
cve_details = cve_details_response.json()
104-
vuln['related_vulnerability'] = cve_details
105-
cve_records.add(cve_details['name'])
106-
elif source == "NVD":
107-
cve_records.add(vuln_name)
108-
else:
109-
logging.warning(f"source {source} was not recognized")
102+
if source == 'BDSA':
103+
bdsa_records.add(vuln_name)
104+
105+
# get related vulnerability info, i.e. CVE
106+
# note: not all BDSA records will have a corresponding CVE record
107+
cve_url = hub.get_link(vuln_details, "related-vulnerability")
108+
if cve_url:
109+
cve_details_response = hub.execute_get(cve_url, custom_headers={'Accept': 'application/json'})
110+
cve_details = cve_details_response.json()
111+
vuln['related_vulnerability'] = cve_details
112+
cve_records.add(cve_details['name'])
113+
elif source == "NVD":
114+
cve_records.add(vuln_name)
115+
else:
116+
logging.warning(f"source {source} was not recognized")
110117

111118
if vulnerable_bom_components:
112119
vulnerable_bom_components = sorted(
@@ -142,13 +149,18 @@
142149
'by_remediation_status': remediation_counts
143150
}
144151

145-
146-
everything = {
147-
'counts': counts,
148-
'vulnerabilities': vulnerable_bom_components,
149-
'bdsa_records': list(bdsa_records),
150-
'cve_records': list(cve_records),
151-
}
152+
if args.nodetails==False:
153+
everything = {
154+
'counts': counts,
155+
'vulnerabilities': vulnerable_bom_components,
156+
'bdsa_records': list(bdsa_records),
157+
'cve_records': list(cve_records),
158+
}
159+
else:
160+
everything = {
161+
'counts': counts,
162+
'vulnerabilities': vulnerable_bom_components,
163+
}
152164

153165
print(json.dumps(everything))
154166

0 commit comments

Comments
 (0)