4
4
import base64
5
5
import requests
6
6
7
- INTERLYNK_API_URL = 'https://api.interlynk.io/lynkapi'
7
+ # INTERLYNK_API_URL = 'https://api.interlynk.io/lynkapi'
8
+
9
+ INTERLYNK_API_URL = 'http://localhost:3000/lynkapi'
8
10
9
11
INTERLYNK_API_TIMEOUT = 100
10
12
77
79
projectId: $projectId
78
80
}
79
81
) {
82
+ id
80
83
errors
81
84
}
82
85
}
@@ -226,16 +229,20 @@ def resolve_env(self):
226
229
227
230
def resolve_ver (self ):
228
231
env = self .env or 'default'
232
+ self .data = self ._fetch_context ()
233
+
229
234
if not self .ver_id :
230
235
for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
231
236
if product ['id' ] == self .prod_id :
232
237
for env in product ['environments' ]:
233
238
if env ['id' ] == self .env_id :
234
239
for ver in env ['versions' ]:
235
- if ver ['primaryComponent' ][ 'version' ] == self .ver :
240
+ if ver . get ( 'primaryComponent' ) and ver ['primaryComponent' ]. get ( 'version' ) == self .ver :
236
241
self .ver_id = ver ['id' ]
237
242
self .ver_status = self .vuln_status_to_status (
238
- ver ['vulnRunStatus' ])
243
+ ver ['vulnRunStatus' ]
244
+ )
245
+
239
246
empty_ver = False
240
247
if not self .ver :
241
248
for product in self .data .get ('data' , {}).get ('organization' , {}).get ('productNodes' , {}).get ('products' , []):
@@ -244,11 +251,13 @@ def resolve_ver(self):
244
251
if env ['id' ] == self .env_id :
245
252
for ver in env ['versions' ]:
246
253
if ver ['id' ] == self .ver_id :
247
- self .ver = ver ['primaryComponent' ]['version' ]
254
+ if ver .get ('primaryComponent' ):
255
+ self .ver = ver ['primaryComponent' ].get ('version' )
248
256
if not self .ver :
249
257
empty_ver = True
250
258
self .ver_status = self .vuln_status_to_status (
251
- ver ['vulnRunStatus' ])
259
+ ver ['vulnRunStatus' ]
260
+ )
252
261
253
262
return (empty_ver or self .ver ) and self .ver_id
254
263
@@ -290,7 +299,7 @@ def status(self):
290
299
291
300
def download (self ):
292
301
logging .debug ("Downloading SBOM for environment ID %s, sbom ID %s" ,
293
- self .env_id , self .ver_id )
302
+ self .env_id , self .ver_id )
294
303
295
304
variables = {
296
305
"envId" : self .env_id ,
@@ -304,10 +313,10 @@ def download(self):
304
313
}
305
314
306
315
response = requests .post (self .api_url ,
307
- headers = {
308
- "Authorization" : "Bearer " + self .token },
309
- json = request_data ,
310
- timeout = INTERLYNK_API_TIMEOUT )
316
+ headers = {
317
+ "Authorization" : "Bearer " + self .token },
318
+ json = request_data ,
319
+ timeout = INTERLYNK_API_TIMEOUT )
311
320
312
321
if response .status_code == 200 :
313
322
try :
@@ -319,19 +328,32 @@ def download(self):
319
328
return None
320
329
321
330
sbom = data .get ('data' , {}).get ('sbom' , {})
322
- if sbom is None :
331
+ if not sbom :
323
332
print ('No SBOM matched with the given ID' )
324
- logging .debug (data )
333
+ logging .debug ("Response data: %s" , data )
325
334
return None
335
+
326
336
b64data = sbom .get ('download' )
327
- decoded_content = base64 .b64decode (b64data )
328
- logging .debug ('Completed download and decoding' )
329
- return decoded_content .decode ('utf-8' )
337
+ if not b64data :
338
+ print ('SBOM data is not available for download.' )
339
+ logging .debug ("SBOM details: %s" , sbom )
340
+ return None
341
+
342
+ try :
343
+ decoded_content = base64 .b64decode (b64data )
344
+ logging .debug ('Completed download and decoding' )
345
+ return decoded_content .decode ('utf-8' )
346
+ except (TypeError , ValueError ) as e :
347
+ logging .error ("Error decoding SBOM content: %s" , e )
348
+ return None
349
+
330
350
except json .JSONDecodeError :
331
351
logging .error ("Failed to parse JSON response." )
352
+ return None
332
353
else :
333
354
logging .error ("Failed to send GraphQL request. Status code: %s" ,
334
- response .status_code )
355
+ response .status_code )
356
+ return None
335
357
336
358
def upload (self , sbom_file ):
337
359
if os .path .isfile (sbom_file ) is False :
@@ -373,11 +395,22 @@ def upload(self, sbom_file):
373
395
timeout = INTERLYNK_API_TIMEOUT )
374
396
if response .status_code == 200 :
375
397
resp_json = response .json ()
398
+ version_id = resp_json .get ('data' , {}).get ('sbomUpload' , {}).get ('id' )
399
+
376
400
errors = resp_json .get ('data' , {}).get (
377
401
'sbomUpload' , {}).get ('errors' )
402
+
378
403
if errors :
379
404
print (f"Error uploading sbom: { errors } " )
380
405
return 1
406
+
407
+ if version_id :
408
+ self .ver_id = version_id
409
+ logging .debug ("SBOM upload response: %s" , response .text )
410
+ else :
411
+ print ("Error: SBOM ID not returned in the response." )
412
+ return 0
413
+
381
414
print ('Uploaded successfully' )
382
415
logging .debug ("SBOM Uploading response: %s" , response .text )
383
416
return 0
0 commit comments