Skip to content

Commit bf8d5fd

Browse files
author
Shane Wright
committed
Force custom components and comparisons to use all lowercase. Clean up some stray comments. Make a variable initialization cleaner. Improve some error messages
1 parent 28f3599 commit bf8d5fd

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

examples/client/parse_spdx.py

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def spdx_validate(document):
127127

128128
# TODO is there a way to distinguish between something fatal and something
129129
# BD can deal with?
130-
# TODO - this can take forever, so add an optional --skip-validation flag
131130
for validation_message in validation_messages:
132131
# Just printing these messages intead of exiting. Later when we try to import
133132
# the file to BD, let's plan to exit if it fails. Seeing lots of errors in the
@@ -162,7 +161,7 @@ def poll_for_upload(sbom_name):
162161
# This might be a risk for a race condition
163162
# TODO Annoyingly, the sbom_name is not necessarily precisely our document
164163
# name! Found a case where BD swaps a space for a "-" in the
165-
# document name.
164+
# document name. Need to be more general in the match.
166165
params = {
167166
'q': [f"name:{sbom_name}"],
168167
'sort': ["updatedAt: ASC"]
@@ -280,20 +279,20 @@ def find_comp_in_bom(compname, compver, projver):
280279
# Search BOM for specific component name
281280
comps = bd.get_resource('components', projver, params=params)
282281
for comp in comps:
283-
if comp['componentName'] != compname:
282+
if comp['componentName'].lower() != compname.lower():
284283
# The BD API search is inexact. Force our match to be precise.
285284
continue
286285
# Check component name + version name
287286
try:
288-
if comp['componentVersionName'] == compver:
287+
if comp['componentVersionName'].lower() == compver.lower():
289288
return True
290289
except:
291290
# Handle situation where it's missing the version name for some reason
292291
print(f"comp {compname} in BOM has no version!")
293292
return False
294293
return False
295294

296-
# Verifies if a custom component and version already exist in the system
295+
# Verifies if a custom component and version already exist in the system.
297296
#
298297
# Inputs:
299298
# compname - Component name to locate
@@ -303,15 +302,15 @@ def find_comp_in_bom(compname, compver, projver):
303302
# VerMatch - Contains matched component verison url, None for no match
304303
def find_cust_comp(compname, compver):
305304
params = {
306-
'q': [f"name:{compname}"]
305+
'q': [f"name:{compname.lower()}"]
307306
}
308307

309308
matched_comp = None
310309
matched_ver = None
311310
# Warning: Relies on internal header
312311
headers = {'Accept': 'application/vnd.blackducksoftware.internal-1+json'}
313312
for comp in bd.get_resource('components', params=params, headers=headers):
314-
if compname == comp['name']:
313+
if compname.lower() == comp['name'].lower():
315314
# Force exact match
316315
matched_comp = comp['_meta']['href']
317316
else:
@@ -320,7 +319,7 @@ def find_cust_comp(compname, compver):
320319

321320
# Check version
322321
for version in bd.get_resource('versions', comp):
323-
if compver == version['versionName']:
322+
if compver.lower() == version['versionName'].lower():
324323
# Successfully matched both name and version
325324
matched_ver = version['_meta']['href']
326325
return(matched_comp, matched_ver)
@@ -348,7 +347,10 @@ def get_license_url(license_name):
348347
logging.error(f"Failed to find license {license_name}")
349348
sys.exit(1)
350349

351-
# Create a custom component
350+
# Create a custom component. The Name and Version strings are converted to
351+
# lowercase strings to ensure a reliable experience (avoiding dup names
352+
# with varying CapItaliZation)
353+
#
352354
# Inputs:
353355
# name - Name of component to add
354356
# version - Version of component to add
@@ -358,7 +360,7 @@ def create_cust_comp(name, version, license):
358360
print(f"Adding custom component: {name} {version}")
359361
license_url = get_license_url(license)
360362
data = {
361-
'name': name,
363+
'name': name.lower(),
362364
'version' : {
363365
'versionName' : version,
364366
'license' : {
@@ -383,7 +385,8 @@ def create_cust_comp(name, version, license):
383385
for version in bd.get_items(response.links['versions']['url']):
384386
return(version['_meta']['href'])
385387

386-
# Create a version for a custom component that already exists
388+
# Create a version for a custom component that already exists.
389+
# Force the version string to be lowercase.
387390
#
388391
# Inputs:
389392
# comp_url - API URL of the component to update
@@ -394,7 +397,7 @@ def create_cust_comp(name, version, license):
394397
def create_cust_comp_ver(comp_url, version, license):
395398
license_url = get_license_url(license)
396399
data = {
397-
'versionName' : version,
400+
'versionName' : version.lower(),
398401
'license' : {
399402
'license' : license_url
400403
},
@@ -459,38 +462,10 @@ def add_to_sbom(proj_version_url, comp_ver_url):
459462
global bd
460463
bd = Client(base_url=args.base_url, token=access_token, verify=args.verify)
461464

462-
#pprint(bd.list_resources())
463-
464465
upload_sbom_file(args.spdx_file, args.project_name, args.version_name)
465466
# This will exit if it fails
466467
poll_for_upload(document.creation_info.name)
467468

468-
# some debug/test stubs
469-
# TODO: delete these
470-
#ver="https://purl-validation.saas-staging.blackduck.com/api/projects/c2b4463f-7996-4c45-8443-b69b4f82ef1d/versions/67e4f6f5-2f42-42c4-9b69-e39bad55f907"
471-
#comp = "https://purl-validation.saas-staging.blackduck.com/api/components/fc0a76fe-70a4-4afa-9a94-c3c22d63454f/versions/fabaabb9-3b9a-4b5f-850a-39fe84c4cfc4"
472-
#add_to_sbom(ver, comp)
473-
#quit()
474-
475-
#matchcomp, matchver = find_cust_comp("ipaddress", "1.0.23")
476-
#if matchcomp:
477-
# print("matched comp")
478-
#else:
479-
# print("no comp match")
480-
#if matchver:
481-
# print("matched ver")
482-
#else:
483-
# print("no ver match")
484-
#comp_ver_url = create_cust_comp("MY COMPONENT z", "1", args.license_name)
485-
#
486-
#comp_url = "https://purl-validation.saas-staging.blackduck.com/api/components/886c04d4-28ce-4a27-be4c-f083e73a9f69"
487-
#comp_ver_url = create_cust_comp_ver(comp_url, "701", "NOASSERTION")
488-
#
489-
#pv = "https://purl-validation.saas-staging.blackduck.com/api/projects/14b714d0-fa37-4684-86cc-ed4e7cc64b89/versions/b8426ca3-1e27-4045-843b-003eca72f98e"
490-
#cv = "https://purl-validation.saas-staging.blackduck.com/api/components/886c04d4-28ce-4a27-be4c-f083e73a9f69/versions/56f64b7f-c284-457d-b593-0cf19a272a19"
491-
#add_to_sbom(pv, cv)
492-
#quit()
493-
494469
# Open unmatched component file to save name, spdxid, version, and
495470
# origin/purl for later in json format
496471
# TODO this try/except isn't quite right
@@ -508,6 +483,8 @@ def add_to_sbom(proj_version_url, comp_ver_url):
508483
}
509484
projects = [p for p in bd.get_resource('projects', params=params)
510485
if p['name'] == args.project_name]
486+
assert len(projects) != 0, \
487+
f"Failed to locate project: {args.project_name}"
511488
assert len(projects) == 1, \
512489
f"There should one project named {args.project_name}. Found {len(projects)}"
513490
project = projects[0]
@@ -518,6 +495,8 @@ def add_to_sbom(proj_version_url, comp_ver_url):
518495
}
519496
versions = [v for v in bd.get_resource('versions', project, params=params)
520497
if v['versionName'] == args.version_name]
498+
assert len(versions) != 0, \
499+
f"Failed to find project version: {args.version_name}"
521500
assert len(versions) == 1, \
522501
f"There should be 1 version named {args.version_name}. Found {len(versions)}"
523502
version = versions[0]
@@ -548,9 +527,9 @@ def add_to_sbom(proj_version_url, comp_ver_url):
548527
# Tracking unique package name + version from spdx file
549528
packages[matchname+matchver] = packages.get(matchname+matchver, 0) + 1
550529

530+
kb_match = None
551531
if package.external_references:
552532
foundpurl = False
553-
kb_match = None
554533
for ref in package.external_references:
555534
# There can be multiple extrefs - try to locate a purl
556535
if (ref.reference_type == "purl"):
@@ -573,7 +552,6 @@ def add_to_sbom(proj_version_url, comp_ver_url):
573552
print(f" No KB match for {package.name} {package.version}")
574553
else:
575554
nopurl += 1
576-
kb_match = None
577555
print(f"No pURL provided for {package.name} {package.version}")
578556

579557
if find_comp_in_bom(matchname, matchver, version):

0 commit comments

Comments
 (0)