-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_checks_parser.py
More file actions
140 lines (107 loc) · 4.72 KB
/
test_checks_parser.py
File metadata and controls
140 lines (107 loc) · 4.72 KB
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
from pathlib import Path
import yaml
from _pytest.logging import LogCaptureFixture
from checkov.common.checks_infra.checks_parser import GraphCheckParser
from checkov.common.checks_infra.resources_types import resources_types as raw_resources_types
from checkov.common.graph.checks_infra.enums import SolverType
EXAMPLES_DIR = Path(__file__).parent / "examples"
def test_validate_check_config(caplog: LogCaptureFixture):
# given
file_path = EXAMPLES_DIR / "valid_check.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert valid
assert len(caplog.messages) == 0
def test_validate_check_config_missing_metadata(caplog: LogCaptureFixture):
# given
file_path = EXAMPLES_DIR / "missing_metadata.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert not valid
assert caplog.messages == [
f"Custom policy {file_path} is missing required fields metadata.id, metadata.name, metadata.category"
]
def test_validate_check_config_missing_metadata_category(caplog: LogCaptureFixture):
# given
file_path = EXAMPLES_DIR / "missing_metadata_category.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert not valid
assert caplog.messages == [f"Custom policy {file_path} is missing required fields metadata.category"]
def test_validate_check_config_missing_definition(caplog: LogCaptureFixture):
# given
file_path = EXAMPLES_DIR / "missing_definition.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert not valid
assert caplog.messages == [f"Custom policy {file_path} is missing required fields definition"]
def test_validate_check_config_invalid_definition(caplog: LogCaptureFixture):
# given
file_path = EXAMPLES_DIR / "invalid_definition.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert not valid
assert caplog.messages == [
f"Custom policy {file_path} has an invalid 'definition' block type 'NoneType', "
"needs to be either a 'list' or 'dict'"
]
def test_parse_taggable_resource_string():
parser = GraphCheckParser()
raw_check = {"resource_types": "taggable"}
providers = ["aws"]
check = parser._parse_raw_check(raw_check, [], providers)
assert check.resource_types == raw_resources_types.get("aws_taggable")
def test_parse_taggable_resource_list():
parser = GraphCheckParser()
raw_check = {"resource_types": ["taggable"]}
providers = ["azure"]
check = parser._parse_raw_check(raw_check, [], providers)
assert check.resource_types == raw_resources_types.get("azure_taggable")
def test_validate_check_config_list_definition(caplog: LogCaptureFixture):
"""A definition that is a list should pass validation."""
# given
file_path = EXAMPLES_DIR / "valid_check_list_definition.yaml"
check_yaml = yaml.safe_load(file_path.read_text())
# when
valid = GraphCheckParser().validate_check_config(file_path=str(file_path), raw_check=check_yaml)
# then
assert valid
assert len(caplog.messages) == 0
def test_parse_raw_check_list_definition():
"""A list-type definition should be treated as an implicit AND of its elements."""
parser = GraphCheckParser()
raw_check = {
"metadata": {"id": "TEST_LIST", "name": "Test List Def", "category": "GENERAL_SECURITY"},
"definition": [
{
"cond_type": "attribute",
"resource_types": ["aws_s3_bucket"],
"attribute": "versioning.enabled",
"operator": "equals",
"value": "true",
},
{
"cond_type": "attribute",
"resource_types": ["aws_s3_bucket"],
"attribute": "server_side_encryption_configuration",
"operator": "exists",
},
],
}
check = parser.parse_raw_check(raw_check)
assert check.id == "TEST_LIST"
assert check.type == SolverType.COMPLEX
assert check.operator == "and"
assert len(check.sub_checks) == 2
assert check.sub_checks[0].attribute == "versioning.enabled"
assert check.sub_checks[1].attribute == "server_side_encryption_configuration"