-
Notifications
You must be signed in to change notification settings - Fork 69
/
graph_validator.py
418 lines (369 loc) · 15.5 KB
/
graph_validator.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
from collections import defaultdict
from pathlib import Path
from typing import List, Optional
from demisto_sdk.commands.common.constants import PACKS_DIR
from demisto_sdk.commands.common.content.content import Content
from demisto_sdk.commands.common.errors import Errors
from demisto_sdk.commands.common.hook_validations.base_validator import (
BaseValidator,
error_codes,
)
from demisto_sdk.commands.common.tools import (
get_all_content_objects_paths_in_dir,
get_marketplace_to_core_packs,
get_pack_name,
replace_incident_to_alert,
)
from demisto_sdk.commands.content_graph.commands.update import update_content_graph
from demisto_sdk.commands.content_graph.interface import (
ContentGraphInterface,
)
from demisto_sdk.commands.content_graph.objects.content_item import ContentItem
class GraphValidator(BaseValidator):
"""GraphValidator makes validations on the content graph."""
def __init__(
self,
specific_validations: list = None,
git_files: list = None,
input_files: list = None,
update_graph: bool = True,
include_optional_deps: bool = False,
):
super().__init__(specific_validations=specific_validations)
self.include_optional = include_optional_deps
self.graph = ContentGraphInterface()
if update_graph:
update_content_graph(
self.graph,
use_git=True,
output_path=self.graph.output_path,
)
self.file_paths: List[str] = git_files or get_all_content_objects_paths_in_dir(
input_files
)
self.pack_ids: List[str] = []
for file_path in self.file_paths:
pack_name: Optional[str] = get_pack_name(file_path)
if pack_name and pack_name not in self.pack_ids:
self.pack_ids.append(pack_name)
def __enter__(self):
return self
def __exit__(self, *args):
return self.graph.__exit__()
def is_valid_content_graph(self) -> bool:
is_valid = (
self.validate_hidden_packs_do_not_have_mandatory_dependencies(),
self.validate_dependencies(),
self.validate_marketplaces_fields(),
self.validate_fromversion_fields(),
self.validate_toversion_fields(),
self.is_file_using_unknown_content(),
self.is_file_display_name_already_exists(),
self.validate_duplicate_ids(),
self.validate_unique_script_name(),
self.validate_deprecated_items_usage(),
)
return all(is_valid)
def validate_dependencies(self):
"""Validating the pack dependencies"""
is_valid = []
is_valid.append(self.are_core_pack_dependencies_valid())
return all(is_valid)
@error_codes("GR105")
def validate_duplicate_ids(self):
is_valid = True
for content_item, duplicates in self.graph.validate_duplicate_ids(
self.file_paths
):
for duplicate in duplicates:
error_message, error_code = Errors.duplicated_id(
content_item.object_id, duplicate.path
)
if self.handle_error(
error_message,
error_code,
file_path=content_item.path,
drop_line=True,
):
is_valid = False
return is_valid
@error_codes("PA124")
def are_core_pack_dependencies_valid(self):
"""Validates, for each marketplace version, that its core packs don't depend on non-core packs.
On `validate -a`, all core packs are checked.
Note: if at the first-level dependency of core packs there are only core packs, for every
core pack, then it is true for all levels. Thus, checking only the first level of
DEPENDS_ON relationships suffices for this validation.
"""
is_valid = True
mp_to_core_packs = get_marketplace_to_core_packs()
for marketplace, mp_core_packs in mp_to_core_packs.items():
if not self.pack_ids:
pack_ids_to_check = list(mp_core_packs)
else:
pack_ids_to_check = [
pack_id for pack_id in self.pack_ids if pack_id in mp_core_packs
]
for core_pack_node in self.graph.find_core_packs_depend_on_non_core_packs(
pack_ids_to_check, marketplace, list(mp_core_packs)
):
non_core_pack_dependencies = [
relationship.content_item_to.object_id
for relationship in core_pack_node.depends_on
if not relationship.is_test
]
error_message, error_code = Errors.invalid_core_pack_dependencies(
core_pack_node.object_id, non_core_pack_dependencies
)
if self.handle_error(
error_message, error_code, file_path=core_pack_node.path
):
is_valid = False
return is_valid
@error_codes("GR100")
def validate_marketplaces_fields(self):
"""
Source's marketplaces field is a subset of the target's marketplaces field
"""
is_valid = True
content_item: ContentItem
for content_item in self.graph.find_uses_paths_with_invalid_marketplaces(
self.pack_ids
):
used_content_items = [
relationship.content_item_to.object_id
for relationship in content_item.uses
]
error_message, error_code = Errors.uses_items_not_in_marketplaces(
content_item.name, content_item.marketplaces, used_content_items
)
if self.handle_error(error_message, error_code, content_item.path):
is_valid = False
return is_valid
@error_codes("GR101")
def validate_fromversion_fields(self):
"""Validates that source's fromversion >= target's fromversion."""
is_valid = []
# Returns warnings - for non supported versions
content_items_with_invalid_fromversion: List[
ContentItem
] = self.graph.find_uses_paths_with_invalid_fromversion(
self.file_paths, for_supported_versions=False
)
for content_item in content_items_with_invalid_fromversion:
is_valid.append(self.handle_invalid_fromversion(content_item, warning=True))
# Returns errors - for supported versions
content_items_with_invalid_fromversion = (
self.graph.find_uses_paths_with_invalid_fromversion(
self.file_paths, for_supported_versions=True
)
)
for content_item in content_items_with_invalid_fromversion:
is_valid.append(self.handle_invalid_fromversion(content_item))
return all(is_valid)
def handle_invalid_fromversion(
self, content_item: ContentItem, warning: bool = False
):
is_valid = True
"""Handles a single invalid fromversion query result"""
used_content_items = [
relationship.content_item_to.object_id for relationship in content_item.uses
]
error_message, error_code = Errors.uses_items_with_invalid_fromversion(
content_item.name, content_item.fromversion, used_content_items
)
if self.handle_error(
error_message, error_code, content_item.path, warning=warning
):
is_valid = False
return is_valid
@error_codes("GR102")
def validate_toversion_fields(self):
"""Validate that source's toversion <= target's toversion."""
is_valid = []
# Returns warnings - for non supported versions
content_items_with_invalid_versions: List[
ContentItem
] = self.graph.find_uses_paths_with_invalid_toversion(
self.file_paths, for_supported_versions=False
)
for content_item in content_items_with_invalid_versions:
is_valid.append(self.handle_invalid_toversion(content_item, warning=True))
# Returns errors - for supported versions
content_items_with_invalid_versions = (
self.graph.find_uses_paths_with_invalid_toversion(
self.file_paths, for_supported_versions=True
)
)
for content_item in content_items_with_invalid_versions:
is_valid.append(self.handle_invalid_toversion(content_item))
return all(is_valid)
def handle_invalid_toversion(
self, content_item: ContentItem, warning: bool = False
):
"""Handles a single invalid toversion query result"""
is_valid = True
used_content_items = [
relationship.content_item_to.object_id for relationship in content_item.uses
]
error_message, error_code = Errors.uses_items_with_invalid_toversion(
content_item.name, content_item.toversion, used_content_items
)
if self.handle_error(
error_message, error_code, content_item.path, warning=warning
):
is_valid = False
return is_valid
@error_codes("GR107")
def validate_deprecated_items_usage(self):
"""Validates there are no items used deprecated items.
For existing content, a warning is raised.
"""
is_valid = True
new_files = Content.git_util().added_files()
items: List[dict] = self.graph.find_items_using_deprecated_items(
self.file_paths
)
for item in items:
deprecated_command = item.get("deprecated_command")
deprecated_content = item.get("deprecated_content")
items_using_deprecated = item.get("object_using_deprecated") or []
for item_using_deprecated in items_using_deprecated:
item_using_deprecated_path = Path(item_using_deprecated)
error_message, error_code = Errors.deprecated_items_usage(
deprecated_command or deprecated_content,
str(item_using_deprecated_path.absolute()),
item.get("deprecated_content_type"),
)
if self.handle_error(
error_message,
error_code,
str(item_using_deprecated_path.absolute()),
warning=(
item_using_deprecated_path not in new_files
), # we raise error only for new content
):
is_valid &= False
return is_valid
@error_codes("GR103")
def is_file_using_unknown_content(self):
"""Validates that there is no usage of unknown content items.
The validation runs twice:
1. Cases where a warning should be raised - if the using content item is a test playbook/test script,
or if the dependency is optional.
2. Cases where an error should be raised - the complementary case.
"""
is_valid = [
self._find_unknown_content_uses(raises_error=False),
self._find_unknown_content_uses(raises_error=True),
]
if self.include_optional:
is_valid.append(
self._find_unknown_content_uses(
raises_error=True, include_optional=True
)
)
return all(is_valid)
def _find_unknown_content_uses(
self, raises_error: bool, include_optional: bool = False
) -> bool:
"""Validates that there is no usage of unknown content items.
Note: if self.file_paths is empty, the validation runs on all files - in this case, returns a warning.
otherwise, returns an error iff raises_error is True.
"""
is_valid = True
content_item: ContentItem
for content_item in self.graph.get_unknown_content_uses(
self.file_paths,
raises_error=raises_error,
include_optional=include_optional,
):
unknown_content_names = [
relationship.content_item_to.object_id or relationship.content_item_to.name # type: ignore
for relationship in content_item.uses
]
error_message, error_code = Errors.using_unknown_content(
content_item.name, unknown_content_names
)
if self.handle_error(
error_message,
error_code,
content_item.path,
warning=not include_optional or not raises_error,
):
is_valid = False
return is_valid
@error_codes("GR104")
def is_file_display_name_already_exists(self):
"""
Validate that there are no duplicate display names in the repo
"""
is_valid = True
query_results = self.graph.get_duplicate_pack_display_name(self.file_paths)
if query_results:
for content_id, duplicate_names_id in query_results:
(
error_message,
error_code,
) = Errors.multiple_packs_with_same_display_name(
content_id, duplicate_names_id
)
if self.handle_error(error_message, error_code, ""):
is_valid = False
return is_valid
@error_codes("GR106")
def validate_unique_script_name(self):
"""
Validate that there are no duplicate names of scripts
when the script name included `alert`.
"""
is_valid = True
query_results = self.graph.get_duplicate_script_name_included_incident(
self.file_paths
)
if query_results:
for script_name, file_path in query_results.items():
(error_message, error_code,) = Errors.duplicated_script_name(
replace_incident_to_alert(script_name), script_name
)
if self.handle_error(
error_message,
error_code,
file_path,
):
is_valid = False
return is_valid
@error_codes("GR108")
def validate_hidden_packs_do_not_have_mandatory_dependencies(self):
"""
Validate that hidden pack(s) do not have dependant packs which the
hidden pack is a mandatory dependency for them.
"""
is_valid = True
if dependant_packs := self.graph.find_mandatory_hidden_packs_dependencies(
pack_ids=self.pack_ids
):
hidden_pack_id_to_dependant_pack_ids: dict = defaultdict(set)
for pack in dependant_packs:
for relationship in pack.depends_on:
hidden_pack_id = relationship.content_item_to.object_id
hidden_pack_id_to_dependant_pack_ids[hidden_pack_id].add(
pack.object_id
)
for pack_id in hidden_pack_id_to_dependant_pack_ids:
if dependant_packs_ids := hidden_pack_id_to_dependant_pack_ids.get(
pack_id
):
(
error_message,
error_code,
) = Errors.hidden_pack_not_mandatory_dependency(
hidden_pack=pack_id, dependant_packs_ids=dependant_packs_ids
)
if self.handle_error(
error_message=error_message,
error_code=error_code,
file_path=f"{PACKS_DIR}/{pack_id}",
):
is_valid = False
return is_valid