-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbdba.py
317 lines (261 loc) · 9.5 KB
/
bdba.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
import argparse
import atexit
import logging
import os
import signal
import sys
import time
import botocore.client
import ccc.aws
import ccc.oci
import ccc.protecode
import ci.log
import cnudie.iter
import cnudie.retrieve
import delivery.client
import delivery.model
import oci.client
import protecode.client
import protecode.scanning
import protecode.util
import config
import ctx_util
import k8s.backlog
import k8s.logging
import k8s.model
import k8s.util
import lookups
logger = logging.getLogger(__name__)
ci.log.configure_default_logging()
k8s.logging.configure_kubernetes_logging()
own_dir = os.path.abspath(os.path.dirname(__file__))
default_cache_dir = os.path.join(own_dir, '.cache')
ready_to_terminate = True
wants_to_terminate = False
def handle_termination_signal(*args):
global ready_to_terminate, wants_to_terminate
# also terminate if > 1 termination signals were received
if ready_to_terminate or wants_to_terminate:
sys.exit(0)
# grace period to finish current scan is defined in the replica set
# after this period, the scan will be terminated anyways by k8s means
logger.info('termination signal received, will try to finish current scan and then exit')
wants_to_terminate = True
def deserialise_bdba_configuration(
cfg_name: str,
namespace: str,
kubernetes_api: k8s.util.KubernetesApi,
) -> config.BDBAConfig:
scan_cfg_crd = kubernetes_api.custom_kubernetes_api.get_namespaced_custom_object(
group=k8s.model.ScanConfigurationCrd.DOMAIN,
version=k8s.model.ScanConfigurationCrd.VERSION,
plural=k8s.model.ScanConfigurationCrd.PLURAL_NAME,
namespace=namespace,
name=cfg_name,
)
if scan_cfg_crd and (spec := scan_cfg_crd.get('spec')):
bdba_config = config.deserialise_bdba_config(spec_config=spec)
else:
bdba_config = None
if not bdba_config:
logger.warning(
f'no bdba configuration for config elem {cfg_name} set, '
'job is not able to process current scan backlog and will terminate'
)
sys.exit(0)
return bdba_config
def scan(
backlog_item: k8s.backlog.BacklogItem,
bdba_config: config.BDBAConfig,
component_descriptor_lookup: cnudie.retrieve.ComponentDescriptorLookupById,
delivery_client: delivery.client.DeliveryServiceClient,
bdba_client: protecode.client.ProtecodeApi,
oci_client: oci.client.Client,
s3_client: 'botocore.client.S3',
):
resource_node = k8s.backlog.get_resource_node(
backlog_item=backlog_item,
component_descriptor_lookup=component_descriptor_lookup,
)
groups = [[resource_node]]
if not resource_node.resource.type in bdba_config.artefact_types:
return
if not bdba_config.node_filter(resource_node):
return
known_scan_results = protecode.scanning._retrieve_existing_scan_results(
protecode_client=bdba_client,
group_id=bdba_config.group_id,
resource_groups=groups,
oci_client=oci_client,
)
processor = protecode.scanning.ResourceGroupProcessor(
group_id=bdba_config.group_id,
reference_group_ids=bdba_config.reference_group_ids,
protecode_client=bdba_client,
oci_client=oci_client,
)
scan_results = tuple(processor.process(
resource_group=groups[0],
processing_mode=bdba_config.processing_mode,
known_scan_results=known_scan_results,
s3_client=s3_client,
license_cfg=bdba_config.license_cfg,
cve_rescoring_rules=bdba_config.cve_rescoring_rules,
auto_assess_max_severity=bdba_config.auto_assess_max_severity,
use_product_cache=False,
delete_inactive_products_after_seconds=bdba_config.delete_inactive_products_after_seconds,
))
delivery_client.update_metadata(data=scan_results)
logger.info(
f'finished scan of artefact {backlog_item.artefact.artefact.artefact_name}:'
f'{backlog_item.artefact.artefact.artefact_version} of component '
f'{backlog_item.artefact.component_name}:{backlog_item.artefact.component_version}'
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--k8s-cfg-name',
help='specify kubernetes cluster to interact with',
default=os.environ.get('K8S_CFG_NAME'),
)
parser.add_argument(
'--k8s-namespace',
help='specify kubernetes cluster namespace to interact with',
default=os.environ.get('K8S_TARGET_NAMESPACE'),
)
parser.add_argument(
'--cfg-name',
help=(
'specify the context the process should run in, not relevant for the artefact '
'enumerator as well as backlog controller as these are context independent'
),
default=os.environ.get('CFG_NAME'),
)
parser.add_argument(
'--delivery-service-url',
help=(
'specify the url of the delivery service to use instead of the one configured in the '
'respective scan configuration'
),
)
parser.add_argument('--cache-dir', default=default_cache_dir)
parsed_arguments = parser.parse_args()
if not parsed_arguments.k8s_namespace:
raise ValueError(
'k8s namespace must be set, either via argument "--k8s-namespace" '
'or via environment variable "K8S_TARGET_NAMESPACE"'
)
if not parsed_arguments.cfg_name:
raise ValueError(
'name of the to-be-used scan configuration must be set, either via '
'argument "--cfg-name" or via environment variable "CFG_NAME"'
)
return parsed_arguments
def main():
signal.signal(signal.SIGTERM, handle_termination_signal)
signal.signal(signal.SIGINT, handle_termination_signal)
parsed_arguments = parse_args()
cfg_name = parsed_arguments.cfg_name
namespace = parsed_arguments.k8s_namespace
delivery_service_url = parsed_arguments.delivery_service_url
cfg_factory = ctx_util.cfg_factory()
if parsed_arguments.k8s_cfg_name:
kubernetes_cfg = cfg_factory.kubernetes(parsed_arguments.k8s_cfg_name)
kubernetes_api = k8s.util.kubernetes_api(kubernetes_cfg=kubernetes_cfg)
else:
kubernetes_api = k8s.util.kubernetes_api()
k8s.logging.init_logging_thread(
service=config.Services.BDBA,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
atexit.register(
k8s.logging.log_to_crd,
service=config.Services.BDBA,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
bdba_config = deserialise_bdba_configuration(
cfg_name=cfg_name,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
try:
bdba_cfg = cfg_factory.bdba(bdba_config.cfg_name)
except ValueError:
# factory method 'bdba' does not exist, fallback to 'protecode'
bdba_cfg = cfg_factory.protecode(bdba_config.cfg_name)
bdba_client = ccc.protecode.client(
protecode_cfg=bdba_cfg,
group_id=bdba_config.group_id,
base_url=bdba_cfg.base_url(),
cfg_factory=cfg_factory,
)
bdba_client.login()
if not delivery_service_url:
delivery_service_url = bdba_config.delivery_service_url
delivery_client = delivery.client.DeliveryServiceClient(
routes=delivery.client.DeliveryServiceRoutes(
base_url=delivery_service_url,
),
)
oci_client = ccc.oci.oci_client(cfg_factory=cfg_factory)
component_descriptor_lookup = lookups.init_component_descriptor_lookup(
cache_dir=parsed_arguments.cache_dir,
delivery_client=delivery_client,
)
try:
cfg_set = cfg_factory.cfg_set(bdba_config.aws_cfg_set_name)
except ValueError:
logger.info(
f'cfg set {bdba_config.aws_cfg_set_name} not found, '
'trying to create default s3 client'
)
cfg_set = None
s3_client = None
try:
s3_session = ccc.aws.default_session(
cfg_factory=cfg_factory,
cfg_set=cfg_set,
)
if s3_session:
s3_client = s3_session.client('s3')
except RuntimeError:
logger.warning('failed to create s3 client')
global ready_to_terminate, wants_to_terminate
while not wants_to_terminate:
ready_to_terminate = False
backlog_crd = k8s.backlog.get_backlog_crd_and_claim(
service=config.Services.BDBA,
cfg_name=cfg_name,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
if not backlog_crd:
ready_to_terminate = True
sleep_interval = bdba_config.lookup_new_backlog_item_interval
logger.info(f'no open backlog item found, will sleep for {sleep_interval} sec')
time.sleep(sleep_interval)
continue
name = backlog_crd.get('metadata').get('name')
logger.info(f'processing backlog item {name}')
backlog_item = k8s.backlog.BacklogItem.from_dict(
backlog_item=backlog_crd.get('spec'),
)
scan(
backlog_item=backlog_item,
bdba_config=bdba_config,
component_descriptor_lookup=component_descriptor_lookup,
delivery_client=delivery_client,
bdba_client=bdba_client,
oci_client=oci_client,
s3_client=s3_client,
)
k8s.backlog.delete_backlog_crd(
name=name,
namespace=namespace,
kubernetes_api=kubernetes_api,
)
logger.info(f'processed and deleted backlog item {name}')
if __name__ == '__main__':
main()