Skip to content

Commit 47307d5

Browse files
committed
add support for download in upload cmd
Signed-off-by: Vivek Kumar Sahu <[email protected]>
1 parent f79dfb9 commit 47307d5

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

lynkctx.py

+49-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import base64
55
import requests
66

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'
810

911
INTERLYNK_API_TIMEOUT = 100
1012

@@ -77,6 +79,7 @@
7779
projectId: $projectId
7880
}
7981
) {
82+
id
8083
errors
8184
}
8285
}
@@ -226,16 +229,20 @@ def resolve_env(self):
226229

227230
def resolve_ver(self):
228231
env = self.env or 'default'
232+
self.data = self._fetch_context()
233+
229234
if not self.ver_id:
230235
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
231236
if product['id'] == self.prod_id:
232237
for env in product['environments']:
233238
if env['id'] == self.env_id:
234239
for ver in env['versions']:
235-
if ver['primaryComponent']['version'] == self.ver:
240+
if ver.get('primaryComponent') and ver['primaryComponent'].get('version') == self.ver:
236241
self.ver_id = ver['id']
237242
self.ver_status = self.vuln_status_to_status(
238-
ver['vulnRunStatus'])
243+
ver['vulnRunStatus']
244+
)
245+
239246
empty_ver = False
240247
if not self.ver:
241248
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
@@ -244,11 +251,13 @@ def resolve_ver(self):
244251
if env['id'] == self.env_id:
245252
for ver in env['versions']:
246253
if ver['id'] == self.ver_id:
247-
self.ver = ver['primaryComponent']['version']
254+
if ver.get('primaryComponent'):
255+
self.ver = ver['primaryComponent'].get('version')
248256
if not self.ver:
249257
empty_ver = True
250258
self.ver_status = self.vuln_status_to_status(
251-
ver['vulnRunStatus'])
259+
ver['vulnRunStatus']
260+
)
252261

253262
return (empty_ver or self.ver) and self.ver_id
254263

@@ -290,7 +299,7 @@ def status(self):
290299

291300
def download(self):
292301
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)
294303

295304
variables = {
296305
"envId": self.env_id,
@@ -304,10 +313,10 @@ def download(self):
304313
}
305314

306315
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)
311320

312321
if response.status_code == 200:
313322
try:
@@ -319,19 +328,32 @@ def download(self):
319328
return None
320329

321330
sbom = data.get('data', {}).get('sbom', {})
322-
if sbom is None:
331+
if not sbom:
323332
print('No SBOM matched with the given ID')
324-
logging.debug(data)
333+
logging.debug("Response data: %s", data)
325334
return None
335+
326336
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+
330350
except json.JSONDecodeError:
331351
logging.error("Failed to parse JSON response.")
352+
return None
332353
else:
333354
logging.error("Failed to send GraphQL request. Status code: %s",
334-
response.status_code)
355+
response.status_code)
356+
return None
335357

336358
def upload(self, sbom_file):
337359
if os.path.isfile(sbom_file) is False:
@@ -373,11 +395,22 @@ def upload(self, sbom_file):
373395
timeout=INTERLYNK_API_TIMEOUT)
374396
if response.status_code == 200:
375397
resp_json = response.json()
398+
version_id = resp_json.get('data', {}).get('sbomUpload', {}).get('id')
399+
376400
errors = resp_json.get('data', {}).get(
377401
'sbomUpload', {}).get('errors')
402+
378403
if errors:
379404
print(f"Error uploading sbom: {errors}")
380405
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+
381414
print('Uploaded successfully')
382415
logging.debug("SBOM Uploading response: %s", response.text)
383416
return 0

pylynk.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,23 @@ def upload_sbom(lynk_ctx, sbom_file):
224224
Returns:
225225
The result of the upload operation.
226226
"""
227-
return lynk_ctx.upload(sbom_file)
227+
# return lynk_ctx.upload(sbom_file)
228+
upload_result = lynk_ctx.upload(sbom_file)
229+
if upload_result != 0:
230+
return 1
231+
232+
if True:
233+
while True:
234+
status = lynk_ctx.status()
235+
236+
if status.get('automationStatus') == "COMPLETED":
237+
download_sbom(lynk_ctx)
238+
break
239+
else:
240+
print("Waiting for automation status to complete...")
241+
time.sleep(5)
242+
243+
return 0
228244

229245

230246
def add_output_format_group(parser):

0 commit comments

Comments
 (0)