@@ -91,6 +91,7 @@ def init_project_data(self,args):
91
91
92
92
subprojects = dict ()
93
93
child_spec_list = self .get_child_spec_list (args )
94
+ # pprint (child_spec_list)
94
95
for child_spec in [x .split (':' ) for x in child_spec_list ]:
95
96
subproject = dict ()
96
97
i = iter (child_spec )
@@ -133,57 +134,57 @@ def remove_project_structure(self, project_name, version_name):
133
134
project = self .find_project_by_name (project_name )
134
135
135
136
if not project :
136
- logging .info (f"Project { project_name } does not exist." )
137
+ logging .debug (f"Project { project_name } does not exist." )
137
138
return
138
139
num_versions = self .client .get_resource ('versions' , project , items = False )['totalCount' ]
139
140
version = self .find_project_version_by_name (project ,version_name )
140
141
if not version :
141
- logging .info (f"Project { project_name } with version { version_name } does not exist." )
142
+ logging .debug (f"Project { project_name } with version { version_name } does not exist." )
142
143
return
143
144
components = [
144
145
c for c in self .client .get_resource ('components' ,version ) if c ['componentType' ] == "SUB_PROJECT"
145
146
]
146
- logging .info (f"Project { project_name } :{ version_name } has { len (components )} subprojects" )
147
+ logging .debug (f"Project { project_name } :{ version_name } has { len (components )} subprojects" )
147
148
for component in components :
148
149
component_name = component ['componentName' ]
149
150
component_version_name = component ['componentVersionName' ]
150
- logging .info (f"Removing subproject { component_name } from { project_name } :{ version_name } " )
151
+ logging .debug (f"Removing subproject { component_name } from { project_name } :{ version_name } " )
151
152
component_url = component ['_meta' ]['href' ]
152
153
response = self .client .session .delete (component_url )
153
- logging .info (f"Operation completed with { response } " )
154
+ logging .debug (f"Operation completed with { response } " )
154
155
self .remove_project_structure (component_name , component_version_name )
155
- logging .info (f"Removing { project_name } :{ version_name } " )
156
+ logging .debug (f"Removing { project_name } :{ version_name } " )
156
157
if num_versions > 1 :
157
158
response = self .client .session .delete (version ['_meta' ]['href' ])
158
159
else :
159
160
response = self .client .session .delete (project ['_meta' ]['href' ])
160
- logging .info (f"Operation completed with { response } " )
161
+ logging .debug (f"Operation completed with { response } " )
161
162
162
- def remove_codelocations_recursively (version ):
163
+ def remove_codelocations_recursively (self , version ):
163
164
components = self .client .get_resource ('components' , version )
164
165
subprojects = [x for x in components if x ['componentType' ] == 'SUB_PROJECT' ]
165
- logging .info (f"Found { len (subprojects )} subprojects" )
166
- unmap_all_codelocations (version )
166
+ logging .debug (f"Found { len (subprojects )} subprojects" )
167
+ self . unmap_all_codelocations (version )
167
168
for subproject in subprojects :
168
169
subproject_name = subproject ['componentName' ]
169
170
subproject_version_name = subproject ['componentVersionName' ]
170
- project = find_project_by_name (subproject_name )
171
+ project = self . find_project_by_name (subproject_name )
171
172
if not project :
172
- logging .info (f"Project { subproject_name } does not exist." )
173
+ logging .debug (f"Project { subproject_name } does not exist." )
173
174
return
174
- subproject_version = find_project_version_by_name (project , subproject_version_name )
175
+ subproject_version = self . find_project_version_by_name (project , subproject_version_name )
175
176
if not subproject_version :
176
- logging .info (f"Project { subproject_name } with version { subversion_name } does not exist." )
177
+ logging .debug (f"Project { subproject_name } with version { subproject_version_name } does not exist." )
177
178
return
178
- remove_codelocations_recursively (subproject_version )
179
+ self . remove_codelocations_recursively (subproject_version )
179
180
180
- def unmap_all_codelocations (version ):
181
+ def unmap_all_codelocations (self , version ):
181
182
codelocations = self .client .get_resource ('codelocations' ,version )
182
183
for codelocation in codelocations :
183
- logging .info (f"Unmapping codelocation { codelocation ['name' ]} " )
184
+ logging .debug (f"Un-mapping of code location { codelocation ['name' ]} " )
184
185
codelocation ['mappedProjectVersion' ] = ""
185
186
response = self .client .session .put (codelocation ['_meta' ]['href' ], json = codelocation )
186
- pprint ( response )
187
+ logging . debug ( f"Un-mapping of code location { codelocation [ 'name' ] } completed with { response } " )
187
188
188
189
189
190
def find_or_create_project_group (self , group_name ):
@@ -316,8 +317,7 @@ def create_and_add_child_projects(self,version):
316
317
self .log ('debug' ,f"Adding { child_name } : { version_name } to parent project completed with { response } " , child )
317
318
except Exception as e :
318
319
self .log ('debug' ,f"Adding { child_name } : { version_name } to parent project completed with exception { e } " , child )
319
-
320
- # remove_codelocations_recursively(version)
320
+ self .remove_codelocations_recursively (version )
321
321
else :
322
322
response = self .create_project_version (child_name ,version_name , nickname = container_spec )
323
323
self .log ('debug' ,f"Creating project { child_name } : { version_name } completed with { response } " , child )
@@ -335,7 +335,7 @@ def create_and_add_child_projects(self,version):
335
335
response = self .client .session .post (version_url ,json = {'component' : child_version_url })
336
336
self .log ('debug' ,f"Adding { child_name } : { version_name } to parent project completed with { response } " , child )
337
337
338
- def create_project_structures (self ):
338
+ def create_project_structure (self ):
339
339
project_name = self .project_data ['project_name' ]
340
340
version_name = self .project_data ['version_name' ]
341
341
@@ -369,6 +369,21 @@ def create_project_structures(self):
369
369
self .log ('debug' ,f"Checking/Adding subprojects to { project_name } : { version ['versionName' ]} " , self .project_data )
370
370
self .create_and_add_child_projects (version )
371
371
372
+ def validate_project_structure (self ):
373
+ parent_project = self .find_project_by_name (self .project_data ['project_name' ])
374
+ parent_version = self .find_project_version_by_name (parent_project , self .project_data ['version_name' ])
375
+ components = [c for c in self .client .get_resource ('components' , parent_version ) if c ['componentType' ] == 'SUB_PROJECT' ]
376
+ for spn , sp in self .project_data ['subprojects' ].items ():
377
+ spn = sp ['project_name' ]
378
+ spvn = sp ['version_name' ]
379
+ ca = [c for c in components if spn == c ['componentName' ] and spvn == c ['componentVersionName' ]]
380
+ if len (ca ) == 1 :
381
+ sp ['status' ] = 'PRESENT'
382
+ self .log ('debug' ,f"Sub-project project { spn } : { spvn } is present in the BOM " , sp )
383
+ else :
384
+ sp ['status' ] = 'ABSENT'
385
+ self .log ('debug' ,f"Sub-project project { spn } : { spvn } could not be added to the BOM " , sp )
386
+
372
387
def scan_container_images (self ):
373
388
from scan_docker_image_lite import scan_container_image
374
389
from blackduck .HubRestApi import HubInstance
@@ -388,7 +403,7 @@ def scan_container_images(self):
388
403
if project_group :
389
404
detect_options += f" --detect.project.group.name=\" { project_group } \" "
390
405
try :
391
- scan_container_image (
406
+ results = scan_container_image (
392
407
image_name ,
393
408
None ,
394
409
None ,
@@ -399,6 +414,7 @@ def scan_container_images(self):
399
414
hub = hub ,
400
415
binary = False
401
416
)
417
+ child ['scan_results' ] = results
402
418
except Exception :
403
419
import traceback
404
420
traceback .print_exc ()
@@ -410,7 +426,8 @@ def proceed(self):
410
426
version_name = self .project_data ['version_name' ]
411
427
self .remove_project_structure (project_name , version_name )
412
428
else :
413
- self .create_project_structures ()
429
+ self .create_project_structure ()
430
+ self .validate_project_structure ()
414
431
self .scan_container_images ()
415
432
416
433
@@ -439,33 +456,7 @@ def main():
439
456
mipm = MultiImageProjectManager (args )
440
457
logging .info (f"Parsed { len (mipm .project_data ['subprojects' ])} projects from specification data" )
441
458
mipm .proceed ()
442
- pprint (mipm .project_data )
443
- sys .exit (1 )
444
-
445
- log_config (args .debug )
446
- global scan_params , skipped_scans
447
- scan_params = []
448
- skipped_scans = []
449
- logging .debug (f"{ args } " )
450
- structure = define_project_structure (args )
451
-
452
- if args .remove :
453
- remove_project_structure (args .project_name , args .version_name , structure )
454
- else :
455
- create_project_structure (structure )
456
- sys .exit (10 )
457
- if args .dry_run :
458
- logging .info (f"{ pformat (scan_params )} " )
459
- else :
460
- logging .info ("Now executing scans" )
461
- from blackduck .HubRestApi import HubInstance
462
- hub = HubInstance (args .base_url , api_token = access_token , insecure = True , debug = False )
463
- scan_container_images (scan_params , hub )
464
- if len (skipped_scans ) > 0 :
465
- logging .info (f"The following images were not scanned" )
466
- logging .info (f"{ pformat (skipped_scans )} " )
467
-
468
-
459
+
469
460
470
461
if __name__ == "__main__" :
471
462
sys .exit (main ())
0 commit comments