-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.py
1210 lines (1044 loc) · 40.7 KB
/
config.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import collections.abc
import dataclasses
import datetime
import enum
import logging
import re
import dacite
import github3
import github3.repos
import cnudie.iter
import dso.cvss
import dso.model
import github.compliance.milestone as gcmi
import github.compliance.model
import ocm
import bdba.model
import config_filter
import ctx_util
import lookups
import rescore.model
logger = logging.getLogger(__name__)
class Services(enum.StrEnum):
ARTEFACT_ENUMERATOR = 'artefactEnumerator'
BACKLOG_CONTROLLER = 'backlogController'
BDBA = 'bdba'
CACHE_MANAGER = 'cacheManager'
CLAMAV = 'clamav'
DELIVERY_DB_BACKUP = 'deliveryDbBackup'
ISSUE_REPLICATOR = 'issueReplicator'
@dataclasses.dataclass(frozen=True)
class Component:
component_name: str
version: str
version_filter: str
max_versions_limit: int
ocm_repo: ocm.OciOcmRepository
@dataclasses.dataclass(frozen=True)
class TimeRange:
start_date: datetime.date
end_date: datetime.date
@dataclasses.dataclass(frozen=True)
class ArtefactEnumeratorConfig:
'''
:param str delivery_service_url
:param int compliance_snapshot_grace_period:
time after which inactive compliance snapshots are deleted from the delivery-db
:param tuple[str] artefact_types:
list of artefact types for which compliance snapshots should be created
:param Callable[Node, bool] node_filter:
filter of artefact nodes to explicitly in- or exclude artefacts compliance snapshot creation
:param tuple[Component] components:
components which are classified as "active" and for which compliance snapshots are created
:param TimeRange sprints_time_range:
earliest start and latest end date for which compliance snapshots should be created
'''
delivery_service_url: str
compliance_snapshot_grace_period: int
artefact_types: tuple[str]
node_filter: collections.abc.Callable[[cnudie.iter.Node], bool]
components: tuple[Component]
sprints_time_range: TimeRange
@dataclasses.dataclass(frozen=True)
class ClamAVConfig:
'''
:param str delivery_service_url
:param int lookup_new_backlog_item_interval:
time to wait in case no backlog item was found before searching for new backlog item again
:param int rescan_interval:
time after which an artefact must be re-scanned at latest
:param str aws_cfg_name
cfg-element used to create s3 client to retrieve artefacts
:param tuple[str] artefact_types:
list of artefact types which should be scanned, other artefact types are skipped
'''
delivery_service_url: str
lookup_new_backlog_item_interval: int
rescan_interval: int
aws_cfg_name: str
artefact_types: tuple[str]
@dataclasses.dataclass(frozen=True)
class FindingTypeIssueReplicationCfgBase:
'''
:param str finding_type:
finding type this configuration should be applied for
(see cc-utils dso/model.py for available "Datatype"s)
:param bool enable_issue_assignees
:param bool enable_issue_per_finding:
when set to true issues are created per finding for a
specific artefact as oppsed to a single issue with
all findings
'''
finding_type: str
enable_issue_assignees: bool
enable_issue_per_finding: bool
@dataclasses.dataclass(frozen=True)
class VulnerabilityIssueReplicationCfg(FindingTypeIssueReplicationCfgBase):
'''
:param int cve_threshold:
vulnerability findings below this threshold won't be reported in the issue(s)
'''
cve_threshold: int
@dataclasses.dataclass(frozen=True)
class GithubIssueTemplateCfg:
'''
a github-issue-template specific for an issue-type
note: this class was copy-pasted from https://github.com/gardener/cc-utils (where it is
planned for removal). Should not be changed incompatibly until removal is done upstream.
'''
body: str
type: str
@dataclasses.dataclass
class LicenseCfg:
'''
configures license policies for discovered licences
licenses are configured as lists of regular expressions (matching is done case-insensitive)
'''
prohibited_licenses: list[str] = None
def is_allowed(self, license: str):
if not self.prohibited_licenses:
return True
for prohibited in self.prohibited_licenses:
if re.fullmatch(prohibited, license, re.IGNORECASE):
return False
else:
return True
@dataclasses.dataclass(frozen=True)
class BDBAConfig:
'''
:param str delivery_service_url
:param int rescan_interval:
time after which an artefact must be re-scanned at latest
:param int lookup_new_backlog_item_interval:
time to wait in case no backlog item was found before searching for new backlog item again
:param str cfg_name:
name of config element to use for bdba scanning
:param int group_id:
bdba group id to use for scanning
:param tuple[int] reference_group_ids:
bdba group ids to consider when copying existing assessments
:param CVSSVersion cvss_version
:param str aws_cfg_set_name:
name of config element to use for creating a s3 client
:param ProcessingMode processing_mode:
defines the scanning behaviour in case there is already an existing scan
:param tuple[str] artefact_types:
list of artefact types which should be scanned, other artefact types are skipped
:param Callable[Node, bool] node_filter:
filter of artefact nodes to explicitly in- or exclude artefacts from the bdba scan
:param CveRescoringRuleSet cve_rescoring_ruleset:
these rules are applied to automatically rescore findings below `auto_assess_max_severity`
:param CVESeverity auto_assess_max_severity:
only findings below this severity will be auto-rescored
:param LicenseCfg license_cfg:
required to differentiate between allowed and prohibited licenses
:param int delete_inactive_products_after_seconds:
time after which a bdba product is deleted if the scanned artefact is not active anymore
:param set[str] blacklist_finding_types:
finding types which are provided by BDBA but should _not_ be populated into the delivery-db
'''
delivery_service_url: str
rescan_interval: int
lookup_new_backlog_item_interval: int
cfg_name: str
group_id: int
reference_group_ids: tuple[int]
cvss_version: bdba.model.CVSSVersion
aws_cfg_set_name: str
processing_mode: bdba.model.ProcessingMode
artefact_types: tuple[str]
node_filter: collections.abc.Callable[[cnudie.iter.Node], bool]
cve_rescoring_ruleset: rescore.model.CveRescoringRuleSet | None
auto_assess_max_severity: dso.cvss.CVESeverity
license_cfg: LicenseCfg
delete_inactive_products_after_seconds: int
blacklist_finding_types: set[str]
@dataclasses.dataclass(frozen=True)
class IssueReplicatorConfig:
'''
:param str delivery_service_url
:param str delivery_dashboard_url
:param int replication_interval:
time after which an issue must be updated at latest
:param int lookup_new_backlog_item_interval:
time to wait in case no backlog item was found before searching for new backlog item again
:param LicenseCfg license_cfg:
required to differentiate between allowed and prohibited licenses
:param MaxProcessingTimesDays max_processing_days:
configuration of allowed maximum processing time based on the severity of the findings
:param github_api_lookup
:param Repository github_issues_repository
:param tuple[GithubIssueTemplateCfg] github_issue_template_cfgs:
templates to configure appearance and format of issues based on type of findings
:param set[str] github_issue_labels_to_preserve:
labels matching one of these regexes won't be removed upon an issue update
:param int number_included_closed_issues:
number of closed issues to consider when evaluating creating vs re-opening an issue
:param tuple[str] artefact_types:
list of artefact types for which issues should be created, other artefact types are skipped
:param Callable[Node, bool] node_filter:
filter of artefact nodes to explicitly in- or exclude artefacts from the issue replication
:param CveRescoringRuleSet cve_rescoring_ruleset:
these rules are applied to calculate proposed rescorings which are displayed in the issue
:param tuple[FindingTypeIssueReplicationCfgBase] finding_type_issue_replication_cfgs:
these cfgs are finding type specific and allow fine granular configuration
'''
delivery_service_url: str
delivery_dashboard_url: str
replication_interval: int
lookup_new_backlog_item_interval: int
license_cfg: LicenseCfg
max_processing_days: github.compliance.model.MaxProcessingTimesDays
github_api_lookup: collections.abc.Callable[[str], github3.GitHub]
github_issues_repository: github3.repos.Repository
github_issue_template_cfgs: tuple[GithubIssueTemplateCfg]
github_issue_labels_to_preserve: set[str]
number_included_closed_issues: int
artefact_types: tuple[str]
node_filter: collections.abc.Callable[[cnudie.iter.Node], bool]
cve_rescoring_ruleset: rescore.model.CveRescoringRuleSet | None
finding_type_issue_replication_cfgs: tuple[FindingTypeIssueReplicationCfgBase]
milestone_cfg: gcmi.MilestoneConfiguration
@dataclasses.dataclass(frozen=True)
class CachePruningWeights:
'''
The individual weights determine how much the respective values are being considered when
determining those cache entries which should be deleted next (in case the max cache size is
reached). The greater the weight, the less likely an entry will be considered for deletion.
Negative values may be also used to express a property which determines that an entry should
be deleted. 0 means the property does not affect the priority for the next deletion.
'''
creation_date_weight: float = 0
last_update_weight: float = 0
delete_after_weight: float = 0
keep_until_weight: float = 0
last_read_weight: float = 0
read_count_weight: float = 0
revision_weight: float = 0
costs_weight: float = 0
size_weight: float = 0
class FunctionNames(enum.StrEnum):
COMPLIANCE_SUMMARY = 'compliance-summary'
COMPONENT_VERSIONS = 'component-versions'
@dataclasses.dataclass(frozen=True)
class PrefillFunctionCaches:
components: tuple[Component]
function_names: tuple[FunctionNames]
@dataclasses.dataclass(frozen=True)
class CacheManagerConfig:
'''
:param str delivery_db_cfg_name:
name of config element of the delivery database
:param int max_cache_size_bytes
:param int min_pruning_bytes:
If `max_cache_size_bytes` is reached, existing cache entries will be removed according to
the `cache_pruning_weights` until `min_pruning_bytes` is available again.
:param CachePruningWeights cache_pruning_weights
:param PrefillFunctionCaches prefill_function_caches:
Configures components for which to pre-calculate and cache the desired functions. If no
specific functions are set, all available functions will be considered.
'''
delivery_db_cfg_name: str
max_cache_size_bytes: int
min_pruning_bytes: int
cache_pruning_weights: CachePruningWeights
prefill_function_caches: PrefillFunctionCaches
@dataclasses.dataclass(frozen=True)
class ScanConfiguration:
artefact_enumerator_config: ArtefactEnumeratorConfig
bdba_config: BDBAConfig
issue_replicator_config: IssueReplicatorConfig
clamav_config: ClamAVConfig
cache_manager_config: CacheManagerConfig
def deserialise_component_config(
component_config: dict,
) -> Component:
component_name = deserialise_config_property(
config=component_config,
property_key='component_name',
)
version = deserialise_config_property(
config=component_config,
property_key='version',
absent_ok=True,
on_absent_message=(
'missing version in components config, this will result '
'in the greatest available release version being used'
),
)
if version == 'greatest':
# version = None will be treated from subsequent
# function "cnudie.retrieve.greatest_component_versions" like greatest
version = None
version_filter = deserialise_config_property(
config=component_config,
property_key='version_filter',
absent_ok=True,
on_absent_message=(
'missing version filter in components config, this will result '
'in the default version filter of the delivery service being used'
),
)
max_versions_limit = deserialise_config_property(
config=component_config,
property_key='max_versions_limit',
default_value=1,
)
ocm_repo_raw = deserialise_config_property(
config=component_config,
property_key='ocm_repo',
absent_ok=True,
on_absent_message=(
'missing ocm repo in components config, this will result '
'in the ocm repo lookup of the delivery service being used'
),
)
if ocm_repo_raw:
ocm_repo = ocm.OciOcmRepository(
baseUrl=ocm_repo_raw,
)
else:
ocm_repo = None
return Component(
component_name=component_name,
version=version,
version_filter=version_filter,
max_versions_limit=max_versions_limit,
ocm_repo=ocm_repo,
)
def deserialise_artefact_enumerator_config(
spec_config: dict,
) -> ArtefactEnumeratorConfig | None:
default_config = spec_config.get('defaults', dict())
artefact_enumerator_config = spec_config.get('artefactEnumerator')
if not artefact_enumerator_config:
return None
delivery_service_url = deserialise_config_property(
config=artefact_enumerator_config,
property_key='delivery_service_url',
default_config=default_config,
)
compliance_snapshot_grace_period = deserialise_config_property(
config=artefact_enumerator_config,
property_key='compliance_snapshot_grace_period',
default_value=60 * 60 * 24, # 24h
)
artefact_types = tuple(deserialise_config_property(
config=artefact_enumerator_config,
property_key='artefact_types',
default_config=default_config,
default_value=(
ocm.ArtefactType.OCI_IMAGE,
'application/tar+vm-image-rootfs',
),
))
matching_configs_raw = deserialise_config_property(
config=artefact_enumerator_config,
property_key='matching_configs',
default_config=default_config,
default_value=[],
)
matching_configs = config_filter.matching_configs_from_dicts(
dicts=matching_configs_raw,
)
node_filter = config_filter.filter_for_matching_configs(
configs=matching_configs,
)
components_raw = deserialise_config_property(
config=artefact_enumerator_config,
property_key='components',
default_value=[],
)
components = tuple(
deserialise_component_config(component_config=component_raw)
for component_raw in components_raw
)
sprints_relative_time_range = deserialise_config_property(
config=artefact_enumerator_config,
property_key='sprints_relative_time_range',
default_value=None,
absent_ok=True,
on_absent_message='no time range for sprints specified, all sprints will be considered',
)
if sprints_relative_time_range:
today = datetime.date.today()
days_from = sprints_relative_time_range.get('days_from')
days_to = sprints_relative_time_range.get('days_to')
sprints_time_range = TimeRange(
start_date=today + datetime.timedelta(days=days_from),
end_date=today + datetime.timedelta(days=days_to),
)
else:
sprints_time_range = None
return ArtefactEnumeratorConfig(
delivery_service_url=delivery_service_url,
compliance_snapshot_grace_period=compliance_snapshot_grace_period,
artefact_types=artefact_types,
node_filter=node_filter,
components=components,
sprints_time_range=sprints_time_range,
)
def deserialise_clamav_config(
spec_config: dict,
) -> ClamAVConfig:
default_config = spec_config.get('defaults', dict())
clamav_config = spec_config.get('clamav')
if not clamav_config:
return
delivery_service_url = deserialise_config_property(
config=clamav_config,
property_key='delivery_service_url',
default_config=default_config,
)
lookup_new_backlog_item_interval = deserialise_config_property(
config=clamav_config,
property_key='lookup_new_backlog_item_interval',
default_config=default_config,
default_value=60,
)
rescan_interval = deserialise_config_property(
config=clamav_config,
property_key='rescan_interval',
default_value=86400, # daily
)
aws_cfg_name = deserialise_config_property(
config=clamav_config,
property_key='aws_cfg_name',
absent_ok=True,
on_absent_message='artefacts of access type s3 will not be scanned'
)
artefact_types = tuple(deserialise_config_property(
config=clamav_config,
property_key='artefact_types',
default_config=default_config,
default_value=(
ocm.ArtefactType.OCI_IMAGE,
'application/tar+vm-image-rootfs',
),
))
return ClamAVConfig(
delivery_service_url=delivery_service_url,
lookup_new_backlog_item_interval=lookup_new_backlog_item_interval,
rescan_interval=rescan_interval,
aws_cfg_name=aws_cfg_name,
artefact_types=artefact_types,
)
def deserialise_bdba_config(
spec_config: dict,
) -> BDBAConfig:
default_config = spec_config.get('defaults', dict())
bdba_config = spec_config.get('bdba')
if not bdba_config:
return
delivery_service_url = deserialise_config_property(
config=bdba_config,
property_key='delivery_service_url',
default_config=default_config,
)
rescan_interval = deserialise_config_property(
config=bdba_config,
property_key='rescan_interval',
)
lookup_new_backlog_item_interval = deserialise_config_property(
config=bdba_config,
property_key='lookup_new_backlog_item_interval',
default_config=default_config,
default_value=60,
)
cfg_name = deserialise_config_property(
config=bdba_config,
property_key='cfg_name',
)
group_id = deserialise_config_property(
config=bdba_config,
property_key='group_id',
)
reference_group_ids = tuple(deserialise_config_property(
config=bdba_config,
property_key='referenceGroupIds',
default_value=[],
))
cvss_version_raw = deserialise_config_property(
config=bdba_config,
property_key='cvss_version',
default_value='CVSSv3',
)
cvss_version = bdba.model.CVSSVersion(cvss_version_raw)
aws_cfg_set_name = deserialise_config_property(
config=bdba_config,
property_key='aws_cfg_set_name',
absent_ok=True,
)
processing_mode_raw = deserialise_config_property(
config=bdba_config,
property_key='processing_mode',
default_value=bdba.model.ProcessingMode.RESCAN.value,
)
processing_mode = bdba.model.ProcessingMode(processing_mode_raw)
artefact_types = tuple(deserialise_config_property(
config=bdba_config,
property_key='artefact_types',
default_config=default_config,
default_value=(
ocm.ArtefactType.OCI_IMAGE,
'application/tar+vm-image-rootfs',
),
))
matching_configs_raw = deserialise_config_property(
config=bdba_config,
property_key='matching_configs',
default_config=default_config,
default_value=[],
)
matching_configs = config_filter.matching_configs_from_dicts(
dicts=matching_configs_raw,
)
node_filter = config_filter.filter_for_matching_configs(
configs=matching_configs,
)
rescoring_cfg_raw = deserialise_config_property(
config=bdba_config,
property_key='rescoring',
default_config=default_config,
absent_ok=True,
)
if rescoring_cfg_raw:
# Pylint struggles with generic dataclasses, see: github.com/pylint-dev/pylint/issues/9488
cve_rescoring_rulesets = tuple(
rescore.model.CveRescoringRuleSet( #noqa:E1123
name=rule_set_raw['name'],
description=rule_set_raw.get('description'),
rules=list(
rescore.model.cve_rescoring_rules_from_dicts(rule_set_raw['rules'])
)
)
for rule_set_raw in rescoring_cfg_raw['rescoringRuleSets']
)
default_rule_sets = [
dacite.from_dict(
data_class=rescore.model.DefaultRuleSet,
data=default_rule_set_raw,
config=dacite.Config(
cast=[rescore.model.RuleSetType],
)
)
for default_rule_set_raw in rescoring_cfg_raw['defaultRuleSetNames']
]
default_rule_set = rescore.model.find_default_rule_set_for_type_and_name(
default_rule_set=rescore.model.find_default_rule_set_for_type(
default_rule_sets=default_rule_sets,
rule_set_type=rescore.model.RuleSetType.CVE,
),
rule_sets=cve_rescoring_rulesets,
)
auto_assess_max_severity_raw = deserialise_config_property(
config=bdba_config,
property_key='auto_assess_max_severity',
)
auto_assess_max_severity = dso.cvss.CVESeverity[auto_assess_max_severity_raw]
else:
default_rule_set = None
auto_assess_max_severity = None
logger.info('no cve rescoring rules specified, rescoring will not be available')
prohibited_licenses = deserialise_config_property(
config=bdba_config,
property_key='prohibited_licenses',
default_config=default_config,
default_value=[],
)
license_cfg = LicenseCfg(prohibited_licenses=prohibited_licenses)
delete_inactive_products_after_seconds = deserialise_config_property(
config=bdba_config,
property_key='delete_inactive_products_after_seconds',
default_value=None,
absent_ok=True,
on_absent_message='inactive bdba products will not be deleted',
)
blacklist_finding_types = deserialise_config_property(
config=bdba_config,
property_key='blacklist_finding_types',
default_value=[],
)
if isinstance(blacklist_finding_types, str):
blacklist_finding_types = [blacklist_finding_types]
blacklist_finding_types = set(blacklist_finding_types)
if dso.model.Datatype.VULNERABILITY in blacklist_finding_types:
# BDBA only supports rescorings (i.e. triages) for vulnerabilites, hence we do not have to
# store them if vulnerabilities are blacklisted anyways
blacklist_finding_types.add(dso.model.Datatype.RESCORING)
return BDBAConfig(
delivery_service_url=delivery_service_url,
rescan_interval=rescan_interval,
lookup_new_backlog_item_interval=lookup_new_backlog_item_interval,
cfg_name=cfg_name,
group_id=group_id,
reference_group_ids=reference_group_ids,
cvss_version=cvss_version,
aws_cfg_set_name=aws_cfg_set_name,
processing_mode=processing_mode,
artefact_types=artefact_types,
node_filter=node_filter,
cve_rescoring_ruleset=default_rule_set,
auto_assess_max_severity=auto_assess_max_severity,
license_cfg=license_cfg,
delete_inactive_products_after_seconds=delete_inactive_products_after_seconds,
blacklist_finding_types=blacklist_finding_types,
)
def deserialise_finding_type_issue_replication_cfg(
finding_type_issue_replication_cfg: dict,
) -> FindingTypeIssueReplicationCfgBase | VulnerabilityIssueReplicationCfg:
finding_type = deserialise_config_property(
config=finding_type_issue_replication_cfg,
property_key='finding_type',
)
enable_issue_assignees = deserialise_config_property(
config=finding_type_issue_replication_cfg,
property_key='enable_issue_assignees',
default_value=False,
)
enable_issue_per_finding = deserialise_config_property(
config=finding_type_issue_replication_cfg,
property_key='enable_issue_per_finding',
default_value=False,
)
match finding_type:
case dso.model.Datatype.LICENSE, dso.model.Datatype.DIKI_FINDING:
return FindingTypeIssueReplicationCfgBase(
finding_type=finding_type,
enable_issue_assignees=enable_issue_assignees,
enable_issue_per_finding=enable_issue_per_finding,
)
case dso.model.Datatype.VULNERABILITY:
cve_threshold = deserialise_config_property(
config=finding_type_issue_replication_cfg,
property_key='cve_threshold',
)
return VulnerabilityIssueReplicationCfg(
finding_type=finding_type,
enable_issue_assignees=enable_issue_assignees,
enable_issue_per_finding=enable_issue_per_finding,
cve_threshold=cve_threshold,
)
case _:
return FindingTypeIssueReplicationCfgBase(
finding_type=finding_type,
enable_issue_assignees=enable_issue_assignees,
enable_issue_per_finding=enable_issue_per_finding,
)
def deserialise_milestone_cfg(
milestone_cfg_raw: dict | None,
) -> gcmi.MilestoneConfiguration:
if not milestone_cfg_raw:
return gcmi.MilestoneConfiguration()
milestone_title_cfg = milestone_cfg_raw.get('title')
milestone_due_date_cfg = milestone_cfg_raw.get('due_date')
if milestone_title_cfg:
title_prefix = milestone_title_cfg.get('prefix')
title_suffix = milestone_title_cfg.get('suffix')
title_sprint_cfg = milestone_title_cfg.get('sprint')
else:
title_prefix = gcmi.MilestoneConfiguration.title_prefix
title_suffix = gcmi.MilestoneConfiguration.title_suffix
title_sprint_cfg = None
if title_sprint_cfg:
sprint_value_type = title_sprint_cfg.get('value_type')
if sprint_value_type == 'name':
title_callback = lambda sprint: sprint.name
elif sprint_value_type == 'date':
name = title_sprint_cfg.get('date_name', 'end_date')
str_format = title_sprint_cfg.get('date_string_format', '%Y-%m-%d')
title_callback = lambda sprint: sprint.find_sprint_date(name).value.strftime(str_format)
else:
raise ValueError(f'invalid milestone sprint value type {sprint_value_type}')
else:
title_callback = gcmi.MilestoneConfiguration.title_callback
if milestone_due_date_cfg:
name = milestone_due_date_cfg.get('date_name')
due_date_callback = lambda sprint: sprint.find_sprint_date(name).value
else:
due_date_callback = gcmi.MilestoneConfiguration.due_date_callback
return gcmi.MilestoneConfiguration(
title_callback=title_callback,
title_prefix=title_prefix,
title_suffix=title_suffix,
due_date_callback=due_date_callback,
)
def deserialise_issue_replicator_config(
spec_config: dict,
) -> IssueReplicatorConfig:
default_config = spec_config.get('defaults', dict())
issue_replicator_config = spec_config.get('issueReplicator')
if not issue_replicator_config:
return
delivery_service_url = deserialise_config_property(
config=issue_replicator_config,
property_key='delivery_service_url',
default_config=default_config,
)
delivery_dashboard_url = deserialise_config_property(
config=issue_replicator_config,
property_key='delivery_dashboard_url',
default_config=default_config,
absent_ok=True,
)
replication_interval = deserialise_config_property(
config=issue_replicator_config,
property_key='replication_interval',
)
lookup_new_backlog_item_interval = deserialise_config_property(
config=issue_replicator_config,
property_key='lookup_new_backlog_item_interval',
default_config=default_config,
default_value=60,
)
prohibited_licenses = deserialise_config_property(
config=issue_replicator_config,
property_key='prohibited_licenses',
default_config=default_config,
default_value=[],
)
license_cfg = LicenseCfg(prohibited_licenses=prohibited_licenses)
max_processing_days_raw = deserialise_config_property(
config=issue_replicator_config,
property_key='max_processing_days',
default_value={},
)
max_processing_days = dacite.from_dict(
data_class=github.compliance.model.MaxProcessingTimesDays,
data=max_processing_days_raw,
)
github_issues_target_repository_url = deserialise_config_property(
config=issue_replicator_config,
property_key='github_issues_target_repository_url',
)
github_api_lookup = lookups.github_api_lookup(
cfg_factory=ctx_util.cfg_factory(),
)
github_repo_lookup = lookups.github_repo_lookup(github_api_lookup)
try:
github_issues_repository = github_repo_lookup(github_issues_target_repository_url)
except Exception as e:
# repo is only required for issue replicator -> ignore error here to allow other services
# to run (issue replicator will fail soon enough)
logger.warning(
'failed to access GitHub issue repository (note: this error can be safely ignored by '
f'all extensions except the issue replicator); {e}'
)
github_issues_repository = None
github_issue_templates = deserialise_config_property(
config=issue_replicator_config,
property_key='github_issue_templates',
)
github_issue_template_cfgs = tuple(
dacite.from_dict(
data_class=GithubIssueTemplateCfg,
data=ghit,
) for ghit in github_issue_templates
)
github_issue_labels_to_preserve = set(deserialise_config_property(
config=issue_replicator_config,
property_key='github_issue_labels_to_preserve',
default_value=set(),
))
number_included_closed_issues = deserialise_config_property(
config=issue_replicator_config,
property_key='number_included_closed_issues',
default_value=0,
)
artefact_types = tuple(deserialise_config_property(
config=issue_replicator_config,
property_key='artefact_types',
default_config=default_config,
default_value=(
ocm.ArtefactType.OCI_IMAGE,
'application/tar+vm-image-rootfs',
),
))
matching_configs_raw = deserialise_config_property(
config=issue_replicator_config,
property_key='matching_configs',
default_config=default_config,
default_value=[],
)
matching_configs = config_filter.matching_configs_from_dicts(
dicts=matching_configs_raw,
)
node_filter = config_filter.filter_for_matching_configs(
configs=matching_configs,
)
cve_rescoring_cfg_raw = deserialise_config_property(
config=default_config,
property_key='rescoring',
absent_ok=True,
)
if cve_rescoring_cfg_raw:
# Pylint struggles with generic dataclasses, see: github.com/pylint-dev/pylint/issues/9488
cve_rescoring_rulesets = tuple(
rescore.model.CveRescoringRuleSet( #noqa:E1123
name=rule_set_raw['name'],
description=rule_set_raw.get('description'),
rules=list(
rescore.model.cve_rescoring_rules_from_dicts(rule_set_raw['rules'])
)
)
for rule_set_raw in cve_rescoring_cfg_raw['rescoringRuleSets']
)
default_rule_sets = [
dacite.from_dict(
data_class=rescore.model.DefaultRuleSet,
data=default_rule_set_raw,
config=dacite.Config(
cast=[rescore.model.RuleSetType],
)
)
for default_rule_set_raw in cve_rescoring_cfg_raw['defaultRuleSetNames']
]
default_rule_set = rescore.model.find_default_rule_set_for_type_and_name(
default_rule_set=rescore.model.find_default_rule_set_for_type(
default_rule_sets=default_rule_sets,
rule_set_type=rescore.model.RuleSetType.CVE,
),
rule_sets=cve_rescoring_rulesets,
)
else:
default_rule_set = None
finding_type_issue_replication_cfgs_raw = deserialise_config_property(
config=issue_replicator_config,
property_key='finding_type_issue_replication_configs',
default_config=default_config,
default_value=[],
)
finding_type_issue_replication_cfgs = tuple(
deserialise_finding_type_issue_replication_cfg(
finding_type_issue_replication_cfg=finding_type_issue_replication_cfg_raw,
)
for finding_type_issue_replication_cfg_raw in finding_type_issue_replication_cfgs_raw
)
milestone_cfg_raw = deserialise_config_property(
config=issue_replicator_config,
property_key='milestones',
absent_ok=True,
)
milestone_cfg = deserialise_milestone_cfg(
milestone_cfg_raw=milestone_cfg_raw,
)
return IssueReplicatorConfig(
delivery_service_url=delivery_service_url,
delivery_dashboard_url=delivery_dashboard_url,
replication_interval=replication_interval,
lookup_new_backlog_item_interval=lookup_new_backlog_item_interval,
license_cfg=license_cfg,
max_processing_days=max_processing_days,
github_api_lookup=github_api_lookup,
github_issues_repository=github_issues_repository,
github_issue_template_cfgs=github_issue_template_cfgs,
github_issue_labels_to_preserve=github_issue_labels_to_preserve,
number_included_closed_issues=number_included_closed_issues,
artefact_types=artefact_types,
node_filter=node_filter,
cve_rescoring_ruleset=default_rule_set,
finding_type_issue_replication_cfgs=finding_type_issue_replication_cfgs,
milestone_cfg=milestone_cfg,
)
def deserialise_cache_manager_config(
spec_config: dict,
) -> CacheManagerConfig | None:
cache_manager_config = spec_config.get('cacheManager')
if not cache_manager_config:
return None
delivery_db_cfg_name = deserialise_config_property(