@@ -227,6 +227,7 @@ def resolve_env(self):
227
227
228
228
def resolve_ver (self ):
229
229
env = self .env or 'default'
230
+
230
231
if not self .ver_id :
231
232
for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
232
233
if product ['id' ] == self .prod_id :
@@ -237,6 +238,7 @@ def resolve_ver(self):
237
238
self .ver_id = ver ['id' ]
238
239
self .ver_status = self .vuln_status_to_status (
239
240
ver ['vulnRunStatus' ])
241
+
240
242
empty_ver = False
241
243
if not self .ver :
242
244
for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
@@ -245,11 +247,25 @@ def resolve_ver(self):
245
247
if env ['id' ] == self .env_id :
246
248
for ver in env ['versions' ]:
247
249
if ver ['id' ] == self .ver_id :
248
- self .ver = ver ['primaryComponent' ]['version' ]
250
+ if ver .get ('primaryComponent' ):
251
+ self .ver = ver ['primaryComponent' ]['version' ]
249
252
if not self .ver :
250
253
empty_ver = True
251
254
self .ver_status = self .vuln_status_to_status (
252
255
ver ['vulnRunStatus' ])
256
+
257
+
258
+ # if ver is not empty
259
+ if not empty_ver :
260
+ for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
261
+ if product ['id' ] == self .prod_id :
262
+ for env in product ['environments' ]:
263
+ if env ['id' ] == self .env_id :
264
+ for ver in env ['versions' ]:
265
+ if ver ['id' ] == self .ver_id :
266
+ if ver .get ('primaryComponent' ):
267
+ self .ver = ver ['primaryComponent' ]['version' ]
268
+ self .ver_status = self .vuln_status_to_status (ver ['vulnRunStatus' ])
253
269
254
270
return (empty_ver or self .ver ) and self .ver_id
255
271
@@ -286,17 +302,13 @@ def versions(self):
286
302
return versions_node
287
303
288
304
def status (self ):
305
+ self .data = self ._fetch_context ()
289
306
self .resolve_ver ()
290
307
return self .ver_status
291
-
292
- def live_status (self ):
293
- self .resolve_ver_status ()
294
- return self .ver_status
295
-
296
308
297
309
def download (self ):
298
310
logging .debug ("Downloading SBOM for environment ID %s, sbom ID %s" ,
299
- self .env_id , self .ver_id )
311
+ self .env_id , self .ver_id )
300
312
301
313
variables = {
302
314
"envId" : self .env_id ,
@@ -310,10 +322,10 @@ def download(self):
310
322
}
311
323
312
324
response = requests .post (self .api_url ,
313
- headers = {
314
- "Authorization" : "Bearer " + self .token },
315
- json = request_data ,
316
- timeout = INTERLYNK_API_TIMEOUT )
325
+ headers = {
326
+ "Authorization" : "Bearer " + self .token },
327
+ json = request_data ,
328
+ timeout = INTERLYNK_API_TIMEOUT )
317
329
318
330
if response .status_code == 200 :
319
331
try :
@@ -325,32 +337,19 @@ def download(self):
325
337
return None
326
338
327
339
sbom = data .get ('data' , {}).get ('sbom' , {})
328
- if not sbom :
340
+ if sbom is None :
329
341
print ('No SBOM matched with the given ID' )
330
- logging .debug ("Response data: %s" , data )
342
+ logging .debug (data )
331
343
return None
332
-
333
344
b64data = sbom .get ('download' )
334
- if not b64data :
335
- print ('SBOM data is not available for download.' )
336
- logging .debug ("SBOM details: %s" , sbom )
337
- return None
338
-
339
- try :
340
- decoded_content = base64 .b64decode (b64data )
341
- logging .debug ('Completed download and decoding' )
342
- return decoded_content .decode ('utf-8' )
343
- except (TypeError , ValueError ) as e :
344
- logging .error ("Error decoding SBOM content: %s" , e )
345
- return None
346
-
345
+ decoded_content = base64 .b64decode (b64data )
346
+ logging .debug ('Completed download and decoding' )
347
+ return decoded_content .decode ('utf-8' )
347
348
except json .JSONDecodeError :
348
349
logging .error ("Failed to parse JSON response." )
349
- return None
350
350
else :
351
351
logging .error ("Failed to send GraphQL request. Status code: %s" ,
352
- response .status_code )
353
- return None
352
+ response .status_code )
354
353
355
354
def upload (self , sbom_file ):
356
355
if os .path .isfile (sbom_file ) is False :
@@ -393,21 +392,18 @@ def upload(self, sbom_file):
393
392
if response .status_code == 200 :
394
393
resp_json = response .json ()
395
394
version_id = resp_json .get ('data' , {}).get ('sbomUpload' , {}).get ('id' )
396
-
397
395
errors = resp_json .get ('data' , {}).get (
398
396
'sbomUpload' , {}).get ('errors' )
399
-
400
397
if errors :
401
398
print (f"Error uploading sbom: { errors } " )
402
399
return 1
403
-
404
400
if version_id :
405
401
self .ver_id = version_id
402
+ print ("SBOM ID successfully returned in the response: " , self .ver_id )
406
403
logging .debug ("SBOM upload response: %s" , response .text )
407
404
else :
408
405
print ("Error: SBOM ID not returned in the response." )
409
- return 0
410
-
406
+ return 1
411
407
print ('Uploaded successfully' )
412
408
logging .debug ("SBOM Uploading response: %s" , response .text )
413
409
return 0
@@ -444,60 +440,3 @@ def vuln_status_to_status(self, status):
444
440
result_dict ['labelingStatus' ] = 'COMPLETED'
445
441
result_dict ['automationStatus' ] = 'COMPLETED'
446
442
return result_dict
447
-
448
-
449
- def resolve_ver_status (self ):
450
- """
451
- Resolve version ID (ver_id) and version (ver) for the current context.
452
- """
453
- self .data = self ._fetch_context ()
454
-
455
- # ver_id is present
456
- if self .ver_id :
457
- self ._update_ver_status ()
458
- return self .ver_id
459
-
460
- # ver_id is not present
461
- self ._resolve_ver_id ()
462
-
463
- # ver is not present
464
- if not self .ver :
465
- self ._resolve_ver_value ()
466
-
467
- return self .ver_id and self .ver
468
-
469
- def _update_ver_status (self ):
470
- """Update the status of the version based on ver_id."""
471
- for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
472
- if product ['id' ] == self .prod_id :
473
- for env in product ['environments' ]:
474
- if env ['id' ] == self .env_id :
475
- for ver in env ['versions' ]:
476
- if ver ['id' ] == self .ver_id :
477
- self .ver_status = self .vuln_status_to_status (ver ['vulnRunStatus' ])
478
-
479
- def _resolve_ver_id (self ):
480
- """Resolve ver_id based on ver."""
481
- for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
482
- if product ['id' ] == self .prod_id :
483
- for env in product ['environments' ]:
484
- if env ['id' ] == self .env_id :
485
- for ver in env ['versions' ]:
486
- if ver .get ('primaryComponent' ) and ver ['primaryComponent' ].get ('version' ) == self .ver :
487
- self .ver_id = ver ['id' ]
488
- self .ver_status = self .vuln_status_to_status (ver ['vulnRunStatus' ])
489
-
490
- def _resolve_ver_value (self ):
491
- """Resolve the version value (ver) based on ver_id."""
492
- empty_ver = False
493
- for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
494
- if product ['id' ] == self .prod_id :
495
- for env in product ['environments' ]:
496
- if env ['id' ] == self .env_id :
497
- for ver in env ['versions' ]:
498
- if ver ['id' ] == self .ver_id :
499
- if ver .get ('primaryComponent' ):
500
- self .ver = ver ['primaryComponent' ].get ('version' )
501
- if not self .ver :
502
- empty_ver = True
503
- return empty_ver
0 commit comments