@@ -127,7 +127,6 @@ def spdx_validate(document):
127
127
128
128
# TODO is there a way to distinguish between something fatal and something
129
129
# BD can deal with?
130
- # TODO - this can take forever, so add an optional --skip-validation flag
131
130
for validation_message in validation_messages :
132
131
# Just printing these messages intead of exiting. Later when we try to import
133
132
# 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):
162
161
# This might be a risk for a race condition
163
162
# TODO Annoyingly, the sbom_name is not necessarily precisely our document
164
163
# 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.
166
165
params = {
167
166
'q' : [f"name:{ sbom_name } " ],
168
167
'sort' : ["updatedAt: ASC" ]
@@ -280,20 +279,20 @@ def find_comp_in_bom(compname, compver, projver):
280
279
# Search BOM for specific component name
281
280
comps = bd .get_resource ('components' , projver , params = params )
282
281
for comp in comps :
283
- if comp ['componentName' ] != compname :
282
+ if comp ['componentName' ]. lower () != compname . lower () :
284
283
# The BD API search is inexact. Force our match to be precise.
285
284
continue
286
285
# Check component name + version name
287
286
try :
288
- if comp ['componentVersionName' ] == compver :
287
+ if comp ['componentVersionName' ]. lower () == compver . lower () :
289
288
return True
290
289
except :
291
290
# Handle situation where it's missing the version name for some reason
292
291
print (f"comp { compname } in BOM has no version!" )
293
292
return False
294
293
return False
295
294
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.
297
296
#
298
297
# Inputs:
299
298
# compname - Component name to locate
@@ -303,15 +302,15 @@ def find_comp_in_bom(compname, compver, projver):
303
302
# VerMatch - Contains matched component verison url, None for no match
304
303
def find_cust_comp (compname , compver ):
305
304
params = {
306
- 'q' : [f"name:{ compname } " ]
305
+ 'q' : [f"name:{ compname . lower () } " ]
307
306
}
308
307
309
308
matched_comp = None
310
309
matched_ver = None
311
310
# Warning: Relies on internal header
312
311
headers = {'Accept' : 'application/vnd.blackducksoftware.internal-1+json' }
313
312
for comp in bd .get_resource ('components' , params = params , headers = headers ):
314
- if compname == comp ['name' ]:
313
+ if compname . lower () == comp ['name' ]. lower () :
315
314
# Force exact match
316
315
matched_comp = comp ['_meta' ]['href' ]
317
316
else :
@@ -320,7 +319,7 @@ def find_cust_comp(compname, compver):
320
319
321
320
# Check version
322
321
for version in bd .get_resource ('versions' , comp ):
323
- if compver == version ['versionName' ]:
322
+ if compver . lower () == version ['versionName' ]. lower () :
324
323
# Successfully matched both name and version
325
324
matched_ver = version ['_meta' ]['href' ]
326
325
return (matched_comp , matched_ver )
@@ -348,7 +347,10 @@ def get_license_url(license_name):
348
347
logging .error (f"Failed to find license { license_name } " )
349
348
sys .exit (1 )
350
349
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
+ #
352
354
# Inputs:
353
355
# name - Name of component to add
354
356
# version - Version of component to add
@@ -358,7 +360,7 @@ def create_cust_comp(name, version, license):
358
360
print (f"Adding custom component: { name } { version } " )
359
361
license_url = get_license_url (license )
360
362
data = {
361
- 'name' : name ,
363
+ 'name' : name . lower () ,
362
364
'version' : {
363
365
'versionName' : version ,
364
366
'license' : {
@@ -383,7 +385,8 @@ def create_cust_comp(name, version, license):
383
385
for version in bd .get_items (response .links ['versions' ]['url' ]):
384
386
return (version ['_meta' ]['href' ])
385
387
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.
387
390
#
388
391
# Inputs:
389
392
# comp_url - API URL of the component to update
@@ -394,7 +397,7 @@ def create_cust_comp(name, version, license):
394
397
def create_cust_comp_ver (comp_url , version , license ):
395
398
license_url = get_license_url (license )
396
399
data = {
397
- 'versionName' : version ,
400
+ 'versionName' : version . lower () ,
398
401
'license' : {
399
402
'license' : license_url
400
403
},
@@ -459,38 +462,10 @@ def add_to_sbom(proj_version_url, comp_ver_url):
459
462
global bd
460
463
bd = Client (base_url = args .base_url , token = access_token , verify = args .verify )
461
464
462
- #pprint(bd.list_resources())
463
-
464
465
upload_sbom_file (args .spdx_file , args .project_name , args .version_name )
465
466
# This will exit if it fails
466
467
poll_for_upload (document .creation_info .name )
467
468
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
-
494
469
# Open unmatched component file to save name, spdxid, version, and
495
470
# origin/purl for later in json format
496
471
# TODO this try/except isn't quite right
@@ -508,6 +483,8 @@ def add_to_sbom(proj_version_url, comp_ver_url):
508
483
}
509
484
projects = [p for p in bd .get_resource ('projects' , params = params )
510
485
if p ['name' ] == args .project_name ]
486
+ assert len (projects ) != 0 , \
487
+ f"Failed to locate project: { args .project_name } "
511
488
assert len (projects ) == 1 , \
512
489
f"There should one project named { args .project_name } . Found { len (projects )} "
513
490
project = projects [0 ]
@@ -518,6 +495,8 @@ def add_to_sbom(proj_version_url, comp_ver_url):
518
495
}
519
496
versions = [v for v in bd .get_resource ('versions' , project , params = params )
520
497
if v ['versionName' ] == args .version_name ]
498
+ assert len (versions ) != 0 , \
499
+ f"Failed to find project version: { args .version_name } "
521
500
assert len (versions ) == 1 , \
522
501
f"There should be 1 version named { args .version_name } . Found { len (versions )} "
523
502
version = versions [0 ]
@@ -548,9 +527,9 @@ def add_to_sbom(proj_version_url, comp_ver_url):
548
527
# Tracking unique package name + version from spdx file
549
528
packages [matchname + matchver ] = packages .get (matchname + matchver , 0 ) + 1
550
529
530
+ kb_match = None
551
531
if package .external_references :
552
532
foundpurl = False
553
- kb_match = None
554
533
for ref in package .external_references :
555
534
# There can be multiple extrefs - try to locate a purl
556
535
if (ref .reference_type == "purl" ):
@@ -573,7 +552,6 @@ def add_to_sbom(proj_version_url, comp_ver_url):
573
552
print (f" No KB match for { package .name } { package .version } " )
574
553
else :
575
554
nopurl += 1
576
- kb_match = None
577
555
print (f"No pURL provided for { package .name } { package .version } " )
578
556
579
557
if find_comp_in_bom (matchname , matchver , version ):
0 commit comments