-
Notifications
You must be signed in to change notification settings - Fork 0
/
lynkctx.py
416 lines (366 loc) · 14.8 KB
/
lynkctx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import os
import json
import logging
import base64
import requests
INTERLYNK_API_URL = 'https://api.interlynk.io/lynkapi'
INTERLYNK_API_TIMEOUT = 100
QUERY_PRODUCTS_TOTAL_COUNT = """
query GetProductsCount($name: String, $enabled: Boolean) {
organization {
productNodes: projectGroups(
search: $name
enabled: $enabled
orderBy: { field: PROJECT_GROUPS_UPDATED_AT, direction: DESC }
) {
prodCount: totalCount
}
}
}
"""
QUERY_PROJECT_COUNT_PARAMS = {
'operationName': 'GetProductsCount',
'variables': {},
'query': QUERY_PRODUCTS_TOTAL_COUNT
}
QUERY_PRODUCTS_LIST = """
query GetProducts($first: Int) {
organization {
productNodes: projectGroups(
enabled: true
first: $first
orderBy: { field: PROJECT_GROUPS_UPDATED_AT, direction: DESC }
) {
prodCount: totalCount
products: nodes {
id
name
updatedAt
enabled
environments: projects {
id: id
name: name
versions: sboms {
id
vulnRunStatus
updatedAt
primaryComponent {
name
version
}
}
}
}
}
}
}
"""
QUERY_PROJECT_PARAMS = {
'operationName': 'GetProducts',
'variables': {},
'query': QUERY_PRODUCTS_LIST
}
QUERY_SBOM_UPLOAD = """
mutation uploadSbom($doc: Upload!, $projectId: ID!) {
sbomUpload(
input: {
doc: $doc,
projectId: $projectId
}
) {
errors
}
}
"""
QUERY_SBOM_DOWNLOAD = """
query downloadSbom($envId: Uuid!, $sbomId: Uuid!, $includeVulns: Boolean) {
sbom(projectId: $envId, sbomId: $sbomId) {
download(
sbomId: $sbomId
includeVulns: $includeVulns
)
__typename
}
}
"""
class LynkContext:
def __init__(self, api_url, token, prod_id, prod, env_id, env, ver_id, ver, output_file):
self.api_url = api_url or INTERLYNK_API_URL
self.token = token
self.prod_id = prod_id
self.prod = prod
self.env_id = env_id
self.env = env
self.ver_id = ver_id
self.ver = ver
self.ver_status = self.vuln_status_to_status('')
self.output_file = output_file
def validate(self):
if not self.token:
print("Security token not found")
return False
self.products_count = self._fetch_product_count()
if not self.products_count or self.products_count.get('errors'):
print("Error getting products count")
print(
"Possible problems: invalid security token, stale pylynk or invalid INTERLYNK_API_URL")
return False
self.data = self._fetch_context()
if not self.data or self.data.get('errors'):
print("Error getting Interlynk data")
print(
"Possible problems: invalid security token, stale pylynk or invalid INTERLYNK_API_URL")
return False
if (self.prod or self.prod_id) and not self.resolve_prod():
self.prod = self.prod_id = None
print('Product not found')
return False
if self.prod and not self.resolve_env(): # There must be a default env
self.env = self.env_id = None
print('Environment not found')
return False
if (self.ver or self.ver_id) and not self.resolve_ver():
self.ver = self.ver_id = None
print('Version not found')
return False
return True
def _fetch_product_count(self):
headers = {"Authorization": "Bearer " + self.token}
try:
response = requests.post(self.api_url,
headers=headers,
data=QUERY_PROJECT_COUNT_PARAMS,
timeout=INTERLYNK_API_TIMEOUT)
if response.status_code == 200:
response_data = response.json()
logging.debug(
"Products count response text: %s", response_data)
return response_data
logging.error("Error fetching products: %s", response.status_code)
except requests.exceptions.RequestException as ex:
logging.error("RequestException: %s", ex)
except json.JSONDecodeError as ex:
logging.error("JSONDecodeError: %s", ex)
return None
def _fetch_context(self):
headers = {"Authorization": "Bearer " + self.token}
product_count = self.products_count.get(
'data', {}).get('organization', {}).get('productNodes', {}).get('prodCount', 0)
variables = {
"first": product_count
}
request_data = {
"query": QUERY_PRODUCTS_LIST,
"variables": variables,
}
try:
response = requests.post(self.api_url,
headers=headers,
json=request_data,
timeout=INTERLYNK_API_TIMEOUT)
if response.status_code == 200:
response_data = response.json()
logging.debug("Products response text: %s", response_data)
return response_data
logging.error("Error fetching products: %s", response.status_code)
except requests.exceptions.RequestException as ex:
logging.error("RequestException: %s", ex)
except json.JSONDecodeError as ex:
logging.error("JSONDecodeError: %s", ex)
return None
def resolve_prod(self):
if not self.prod_id:
nodes = self.data.get('data', {}).get('organization', {}).get(
'productNodes', {}).get('products', [])
self.prod_id = next(
(node['id'] for node in nodes if node['name'] == self.prod), None)
if not self.prod:
nodes = self.data.get('data', {}).get('organization', {}).get(
'productNodes', {}).get('products', [])
self.prod = next(
(node['name'] for node in nodes if node['id'] == self.prod_id), None)
return self.prod and self.prod_id
def resolve_env(self):
env = self.env or 'default'
# For pre-configured environments, use case-insensitive comparison
if env.lower() in ['default', 'development', 'production']:
env = env.lower()
if not self.env_id:
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
if product['id'] == self.prod_id:
self.env_id = next((env_node['id'] for env_node in product.get('environments', [])
if env_node.get('name') == env), None)
if not self.env:
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
if product['id'] == self.prod_id:
self.env = next((env_node['id'] for env_node in product.get('environments', [])
if env_node.get('id') == self.env_id), None)
return self.env and self.env_id
def resolve_ver(self):
env = self.env or 'default'
if not self.ver_id:
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
if product['id'] == self.prod_id:
for env in product['environments']:
if env['id'] == self.env_id:
for ver in env['versions']:
if ver['primaryComponent']['version'] == self.ver:
self.ver_id = ver['id']
self.ver_status = self.vuln_status_to_status(
ver['vulnRunStatus'])
empty_ver = False
if not self.ver:
for product in self.data.get('data', {}).get('organization', {}).get('productNodes', {}).get('products', []):
if product['id'] == self.prod_id:
for env in product['environments']:
if env['id'] == self.env_id:
for ver in env['versions']:
if ver['id'] == self.ver_id:
self.ver = ver['primaryComponent']['version']
if not self.ver:
empty_ver = True
self.ver_status = self.vuln_status_to_status(
ver['vulnRunStatus'])
return (empty_ver or self.ver) and self.ver_id
def __repr__(self):
from pprint import pformat
return pformat(vars(self), indent=1, width=1)
def prods(self):
if (self.data.get('errors')):
logging.error("Query error: GetProducts")
return None
if not self.data:
logging.error("No products found")
return None
prod_nodes = self.data['data']['organization']['productNodes']['products']
prod_list = []
for prod in prod_nodes:
versions = sum(len(env['versions'])
for env in prod['environments'])
prod_list.append({
'name': prod['name'],
'updatedAt': prod['updatedAt'],
'id': prod['id'],
'versions': versions
})
return prod_list
def versions(self):
prod_nodes = self.data['data']['organization']['productNodes']['products']
versions_node = next((env['versions'] for prod in prod_nodes if self.prod_id == prod['id']
for env in prod['environments'] if self.env_id == env['id']), None)
return versions_node
def status(self):
self.resolve_ver()
return self.ver_status
def download(self):
logging.debug("Downloading SBOM for environment ID %s, sbom ID %s",
self.env_id, self.ver_id)
variables = {
"envId": self.env_id,
"sbomId": self.ver_id,
"includeVulns": False
}
request_data = {
"query": QUERY_SBOM_DOWNLOAD,
"variables": variables,
}
response = requests.post(self.api_url,
headers={
"Authorization": "Bearer " + self.token},
json=request_data,
timeout=INTERLYNK_API_TIMEOUT)
if response.status_code == 200:
try:
data = response.json()
if "errors" in data:
logging.error("GraphQL response contains errors:")
for error in data["errors"]:
logging.error(error["message"])
return None
sbom = data.get('data', {}).get('sbom', {})
if sbom is None:
print('No SBOM matched with the given ID')
logging.debug(data)
return None
b64data = sbom.get('download')
decoded_content = base64.b64decode(b64data)
logging.debug('Completed download and decoding')
return decoded_content.decode('utf-8')
except json.JSONDecodeError:
logging.error("Failed to parse JSON response.")
else:
logging.error("Failed to send GraphQL request. Status code: %s",
response.status_code)
def upload(self, sbom_file):
if os.path.isfile(sbom_file) is False:
print(f"SBOM File not found: {sbom_file}")
return
if not self.prod_id:
print(f"Product not found: {self.prod}")
return
if not self.env_id:
print(f"Environment not found: {self.env}")
return
logging.debug("Uploading SBOM to product ID %s", self.prod_id)
headers = {
"Authorization": "Bearer " + self.token
}
operations = json.dumps({
"query": QUERY_SBOM_UPLOAD,
"variables": {"doc": None, "projectId": self.env_id}
})
map_data = json.dumps({"0": ["variables.doc"]})
form_data = {
"operations": operations,
"map": map_data
}
try:
with open(sbom_file, 'rb') as sbom:
files_map = {'0': sbom}
response = requests.post(self.api_url,
headers=headers,
data=form_data,
files=files_map,
timeout=INTERLYNK_API_TIMEOUT)
if response.status_code == 200:
resp_json = response.json()
errors = resp_json.get('data', {}).get(
'sbomUpload', {}).get('errors')
if errors:
print(f"Error uploading sbom: {errors}")
return 1
print('Uploaded successfully')
logging.debug("SBOM Uploading response: %s", response.text)
return 0
logging.error("Error uploading sbom: %d", response.status_code)
except requests.exceptions.RequestException as ex:
logging.error("RequestException: %s", ex)
except FileNotFoundError as ex:
logging.error("FileNotFoundError: %s", ex)
return 1
def vuln_status_to_status(self, status):
result_dict = dict()
result_dict['checksStatus'] = 'UNKNOWN'
result_dict['policyStatus'] = 'UNKNOWN'
result_dict['labelingStatus'] = 'UNKNOWN'
result_dict['automationStatus'] = 'UNKNOWN'
result_dict['vulnScanStatus'] = 'UNKNOWN'
if status == 'NOT_STARTED':
result_dict['vulnScanStatus'] = 'NOT_STARTED'
result_dict['checksStatus'] = 'NOT_STARTED'
result_dict['policyStatus'] = 'NOT_STARTED'
result_dict['labelingStatus'] = 'NOT_STARTED'
result_dict['automationStatus'] = 'NOT_STARTED'
elif status == 'IN_PROGRESS':
result_dict['vulnScanStatus'] = 'IN_PROGRESS'
result_dict['checksStatus'] = 'COMPLETED'
result_dict['policyStatus'] = 'COMPLETED'
result_dict['labelingStatus'] = 'COMPLETED'
result_dict['automationStatus'] = 'COMPLETED'
elif status == 'FINISHED':
result_dict['vulnScanStatus'] = 'COMPLETED'
result_dict['checksStatus'] = 'COMPLETED'
result_dict['policyStatus'] = 'COMPLETED'
result_dict['labelingStatus'] = 'COMPLETED'
result_dict['automationStatus'] = 'COMPLETED'
return result_dict