Skip to content

Commit 0b8d297

Browse files
author
Jakob Maier
committed
fixed deprecated functions, added support for filename in cl args
1 parent 06e40e9 commit 0b8d297

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

examples/vuln_batch_remediation.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
Each processing step can be turned on or off. At least one step must be run. Default
2727
is to run both.
2828
29-
The script get's it CVE and orign lists from CSV files. The CSV filenames are loaded
29+
The script can get's its CVE and orign lists from CSV files. The CSV filenames are loaded
3030
from Custom Fields in the Black Duck project. This allows different groups of projects to
3131
use different remeidation settings. If a CVE remediation status should apply globally
3232
to all projects, Black Duck's global remediation feature should be used.
3333
34+
The script can also get the CSV filenames from the command line arguments.
35+
3436
Here is an example of the CSV data for the CVE list:
3537
3638
"CVE-2016-1840","IGNORED","Applies only to Apple OS"
@@ -114,9 +116,19 @@ def find_custom_field_value (custom_fields, custom_field_label):
114116
return None
115117
return None
116118

119+
120+
121+
def set_vulnerablity_remediation(hub, vuln, remediation_status, remediation_comment):
122+
url = vuln['_meta']['href']
123+
update={}
124+
update['remediationStatus'] = remediation_status
125+
update['comment'] = remediation_comment
126+
response = hub.execute_put(url, data=update)
127+
return response
128+
117129
def process_vulnerabilities(hub, vulnerable_components, remediation_data=None, exclusion_data=None):
118130
count = 0
119-
print('"Component Name","Component Version","Component OriginID","CVE","Reason","Remeidation Status","HTTP response code"')
131+
print('"Component Name","Component Version","CVE","Reason","Remeidation Status","HTTP response code"')
120132

121133
for vuln in vulnerable_components['items']:
122134
if vuln['vulnerabilityWithRemediation']['remediationStatus'] == "NEW":
@@ -125,6 +137,8 @@ def process_vulnerabilities(hub, vulnerable_components, remediation_data=None, e
125137

126138
if (exclusion_data):
127139
exclusion_action = origin_is_excluded(vuln, exclusion_data)
140+
else:
141+
exclusion_action = None
128142

129143
# If vuln has both a remdiation action and an origin exclusion action, set remdiation status
130144
# to the remdiation action. Append the exclusion action's comment to the overall comment.
@@ -137,13 +151,14 @@ def process_vulnerabilities(hub, vulnerable_components, remediation_data=None, e
137151
reason = 'origin-exclusion'
138152

139153
if (remediation_action):
140-
resp = hub.set_vulnerablity_remediation(vuln, remediation_action[0],remediation_action[1])
154+
resp = set_vulnerablity_remediation(hub, vuln, remediation_action[0],remediation_action[1])
141155
count += 1
142-
print ('\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\"'.
156+
print ('\"{}\",\"{}\",\"{}\",\"{}\",\"{}\",\"{}\"'.
143157
format(vuln['componentName'], vuln['componentVersionName'],
144-
vuln['componentVersionOriginId'],
145158
vuln['vulnerabilityWithRemediation']['vulnerabilityName'],
146159
reason, remediation_action[0], resp.status_code))
160+
161+
147162
print (f'Remediated {count} vulnerabilities.')
148163

149164
def main(argv=None): # IGNORE:C0111
@@ -178,7 +193,9 @@ def main(argv=None): # IGNORE:C0111
178193
parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter)
179194
parser.add_argument("projectname", help="Project nname")
180195
parser.add_argument("projectversion", help="Project vesrsion")
181-
parser.add_argument("--no-process-cve-remediation-list", dest='process_cve_remediation_list', action='store_false', help="Disbable processing CVE-Remediation-list")
196+
parser.add_argument("--remediation-list", dest="local_remediation_list", default=None, help="Filename of cve remediation list csv file")
197+
parser.add_argument("--origin-exclusion-list", dest="local_origin_exclusion_list", default=None, help="Filename of origin exclusion list csv file")
198+
parser.add_argument("--no-process-cve-remediation-list", dest='process_cve_remediation_list', action='store_false', help="Disable processing CVE-Remediation-list")
182199
parser.add_argument("--no-process-origin-exclusion-list", dest='process_origin_exclusion_list', action='store_false', help="Disable processing Origin-Exclusion-List")
183200
parser.add_argument("--cve-remediation-list-custom-field-label", default='CVE Remediation List', help='Label of Custom Field on Black Duck that contains remeidation list file name')
184201
parser.add_argument("--origin-exclusion-list-custom-field-label", default='Origin Exclusion List', help='Label of Custom Field on Black Duck that containts origin exclusion list file name')
@@ -189,6 +206,8 @@ def main(argv=None): # IGNORE:C0111
189206

190207
projectname = args.projectname
191208
projectversion = args.projectversion
209+
local_cve_remediation_file = args.local_remediation_list
210+
local_origin_exclusion_file = args.local_origin_exclusion_list
192211
process_cve_remediation = args.process_cve_remediation_list
193212
process_origin_exclulsion = args.process_origin_exclusion_list
194213

@@ -203,21 +222,34 @@ def main(argv=None): # IGNORE:C0111
203222
hub = HubInstance()
204223
project = hub.get_project_by_name(projectname)
205224
version = hub.get_project_version_by_name(projectname, projectversion)
206-
custom_fields = hub.get_cf_values (project)
225+
226+
custom_fields = hub.get_cf_values(project)
207227

208228
if (process_cve_remediation):
209-
cve_remediation_file = find_custom_field_value (custom_fields, args.cve_remediation_list_custom_field_label)
210-
print (f' Opening: {args.cve_remediation_list_custom_field_label}:{cve_remediation_file}')
229+
230+
if (local_cve_remediation_file):
231+
cve_remediation_file = local_cve_remediation_file
232+
print (f' Opening: {cve_remediation_file}')
233+
else:
234+
cve_remediation_file = find_custom_field_value (custom_fields, args.cve_remediation_list_custom_field_label)
235+
print (f' Opening: {args.cve_remediation_list_custom_field_label}:{cve_remediation_file}')
236+
211237
remediation_data = load_remediation_input(cve_remediation_file)
212238
else:
213239
remediation_data = None
214240

215241
if (process_origin_exclulsion):
216-
exclusion_list_file = find_custom_field_value (custom_fields, args.origin_exclusion_list_custom_field_label)
217-
print (f' Opening: {args.origin_exclusion_list_custom_field_label}:{exclusion_list_file}')
242+
243+
if local_origin_exclusion_file:
244+
exclusion_list_file = local_origin_exclusion_file
245+
print (f' Opening: {exclusion_list_file}')
246+
else:
247+
exclusion_list_file = find_custom_field_value (custom_fields, args.origin_exclusion_list_custom_field_label)
248+
print (f' Opening: {args.origin_exclusion_list_custom_field_label}:{exclusion_list_file}')
218249
exclusion_data = load_remediation_input(exclusion_list_file)
219250
else:
220251
exclusion_data = None
252+
221253

222254
# Retrieve the vulnerabiltites for the project version
223255
vulnerable_components = hub.get_vulnerable_bom_components(version)

0 commit comments

Comments
 (0)