-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general):
intersects/not_intersects
operators (solvers) (#3482)
* feat: support for intersects/not_intersects solvers * fix: fixed mypy tests * chore: CR comments - removed logging, use Collection as type assertion Co-authored-by: Mor Kadosh <[email protected]>
- Loading branch information
Showing
20 changed files
with
252 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
checkov/common/checks_infra/solvers/attribute_solvers/intersects_attribute_solver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Optional, Any, Dict, Collection | ||
from collections.abc import Iterable | ||
from checkov.common.graph.checks_infra.enums import Operators | ||
from checkov.common.checks_infra.solvers.attribute_solvers.base_attribute_solver import BaseAttributeSolver | ||
|
||
|
||
class IntersectsAttributeSolver(BaseAttributeSolver): | ||
operator = Operators.INTERSECTS # noqa: CCE003 # a static attribute | ||
|
||
def _get_operation(self, vertex: Dict[str, Any], attribute: Optional[str]) -> bool: # type:ignore[override] | ||
attr = vertex.get(attribute) # type:ignore[arg-type] # due to attribute can be None | ||
|
||
# if this value contains an underendered variable, then we cannot evaluate the check, | ||
# so return True (since we cannot return UNKNOWN) | ||
if self._is_variable_dependant(attr, vertex['source_']): | ||
return True | ||
|
||
if isinstance(self.value, str) and isinstance(attr, Iterable): | ||
return self.value in attr | ||
|
||
if isinstance(self.value, Collection) and isinstance(attr, Iterable): | ||
return any(i in self.value for i in attr) | ||
|
||
return False |
11 changes: 11 additions & 0 deletions
11
checkov/common/checks_infra/solvers/attribute_solvers/not_intersects_attribute_solver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from typing import Optional, Any, Dict | ||
|
||
from checkov.common.graph.checks_infra.enums import Operators | ||
from .intersects_attribute_solver import IntersectsAttributeSolver | ||
|
||
|
||
class NotIntersectsAttributeSolver(IntersectsAttributeSolver): | ||
operator = Operators.NOT_INTERSECTS # noqa: CCE003 # a static attribute | ||
|
||
def _get_operation(self, vertex: Dict[str, Any], attribute: Optional[str]) -> bool: # type:ignore[override] | ||
return not super()._get_operation(vertex, attribute) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/ArrayIntersect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
metadata: | ||
id: "ArrayIntersect" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_xyz" | ||
attribute: "arr" | ||
operator: "intersects" | ||
value: | ||
- "allowed1" |
12 changes: 12 additions & 0 deletions
12
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/MivedValue.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
metadata: | ||
id: "MixedValue" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_default_security_group" | ||
attribute: "ingress.cidr_blocks" | ||
operator: "intersects" | ||
value: "0.0.0.0/0" | ||
|
12 changes: 12 additions & 0 deletions
12
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/NoneAttribute.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
metadata: | ||
id: "NoneAttribute" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_subnet" | ||
attribute: "foo" | ||
operator: "intersects" | ||
value: | ||
- "bar" |
13 changes: 13 additions & 0 deletions
13
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/PublicVMs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
metadata: | ||
id: "PublicVMs" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_default_security_group" | ||
attribute: "ingress.cidr_blocks" | ||
operator: "intersects" | ||
value: | ||
- "0.0.0.0/0" | ||
|
11 changes: 11 additions & 0 deletions
11
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/StringAttribute.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
metadata: | ||
id: "StringAttribute" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_subnet" | ||
attribute: "availability_zone" | ||
operator: "intersects" | ||
value: "us-" |
13 changes: 13 additions & 0 deletions
13
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/TagsIntersect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
metadata: | ||
id: "TagsIntersect" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "all" | ||
attribute: "tags" | ||
operator: "intersects" | ||
value: | ||
- "acme" | ||
- "foo" |
Empty file.
65 changes: 65 additions & 0 deletions
65
tests/terraform/graph/checks_infra/attribute_solvers/intersects_solver/test_solver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import os | ||
|
||
from tests.terraform.graph.checks_infra.test_base import TestBaseSolver | ||
|
||
TEST_DIRNAME = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
class TestIntersectsSolver(TestBaseSolver): | ||
def setUp(self): | ||
self.checks_dir = TEST_DIRNAME | ||
super(TestIntersectsSolver, self).setUp() | ||
|
||
def test_simple_array_intersection1(self): | ||
root_folder = '../../../resources/public_virtual_machines' | ||
check_id = "PublicVMs" | ||
should_pass = ['aws_default_security_group.default_security_group_open'] | ||
should_fail = ['aws_default_security_group.default_security_group_closed'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_simple_array_intersection2(self): | ||
root_folder = '../../../resources/array_test' | ||
check_id = "ArrayIntersect" | ||
should_pass = ['aws_xyz.pass1', 'aws_xyz.pass2'] | ||
should_fail = ['aws_xyz.fail2', 'aws_xyz.fail3', 'aws_xyz.pass3'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_none_attribute(self): | ||
root_folder = '../../../resources/public_virtual_machines' | ||
check_id = "NoneAttribute" | ||
should_pass = [] | ||
should_fail = ['aws_subnet.subnet_public_ip', 'aws_subnet.subnet_not_public_ip'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_string_attribute(self): | ||
root_folder = '../../../resources/public_virtual_machines' | ||
check_id = "StringAttribute" | ||
should_pass = ['aws_subnet.subnet_public_ip'] | ||
should_fail = ['aws_subnet.subnet_not_public_ip'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_mixed_value(self): | ||
root_folder = '../../../resources/public_virtual_machines' | ||
check_id = "MixedValue" | ||
should_pass = ['aws_default_security_group.default_security_group_open'] | ||
should_fail = ['aws_default_security_group.default_security_group_closed'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_tags_intersection(self): | ||
root_folder = '../../../resources/tag_includes' | ||
check_id = "TagsIntersect" | ||
should_pass = ['aws_subnet.acme_subnet'] | ||
should_fail = ['aws_instance.some_instance', 'aws_s3_bucket.acme_s3_bucket'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) |
12 changes: 12 additions & 0 deletions
12
...rraform/graph/checks_infra/attribute_solvers/not_intersects_solver/ArrayNotIntersect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
metadata: | ||
id: "ArrayNotIntersect" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_xyz" | ||
attribute: "arr" | ||
operator: "not_intersects" | ||
value: | ||
- "notallowed" |
13 changes: 13 additions & 0 deletions
13
tests/terraform/graph/checks_infra/attribute_solvers/not_intersects_solver/PublicVMs.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
metadata: | ||
id: "PublicVMs" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "aws_default_security_group" | ||
attribute: "ingress.cidr_blocks" | ||
operator: "not_intersects" | ||
value: | ||
- "0.0.0.0/0" | ||
|
13 changes: 13 additions & 0 deletions
13
...erraform/graph/checks_infra/attribute_solvers/not_intersects_solver/TagsNotIntersect.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
metadata: | ||
id: "TagsNotIntersect" | ||
scope: | ||
provider: "AWS" | ||
definition: | ||
cond_type: "attribute" | ||
resource_types: | ||
- "all" | ||
attribute: "tags" | ||
operator: "intersects" | ||
value: | ||
- "acme" | ||
- "bar" |
Empty file.
29 changes: 29 additions & 0 deletions
29
tests/terraform/graph/checks_infra/attribute_solvers/not_intersects_solver/test_solver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
|
||
from tests.terraform.graph.checks_infra.test_base import TestBaseSolver | ||
|
||
TEST_DIRNAME = os.path.dirname(os.path.realpath(__file__)) | ||
|
||
|
||
class TestNotIntersectsSolver(TestBaseSolver): | ||
def setUp(self): | ||
self.checks_dir = TEST_DIRNAME | ||
super(TestNotIntersectsSolver, self).setUp() | ||
|
||
def test_simple_array_no_intersection1(self): | ||
root_folder = '../../../resources/public_virtual_machines' | ||
check_id = "PublicVMs" | ||
should_pass = ['aws_default_security_group.default_security_group_closed'] | ||
should_fail = ['aws_default_security_group.default_security_group_open'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) | ||
|
||
def test_simple_array_no_intersection2(self): | ||
root_folder = '../../../resources/array_test' | ||
check_id = "ArrayNotIntersect" | ||
should_pass = ['aws_xyz.pass1', 'aws_xyz.pass2'] | ||
should_fail = ['aws_xyz.fail2', 'aws_xyz.fail3', 'aws_xyz.pass3'] | ||
expected_results = {check_id: {"should_pass": should_pass, "should_fail": should_fail}} | ||
|
||
self.run_test(root_folder=root_folder, expected_results=expected_results, check_id=check_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters