Skip to content

Commit 2a57d95

Browse files
Alex-Waringtjwald
andauthored
fix(terraform): correctly evaluate CKV_AWS_37 when there's a dynamic … (#6792)
* fix(terraform): correctly evaluate CKV_AWS_37 when there's a dynamic block * Update EKSControlPlaneLogging.py fix linting and access pattern --------- Co-authored-by: Tj <[email protected]>
1 parent e36a9aa commit 2a57d95

File tree

3 files changed

+99
-5
lines changed

3 files changed

+99
-5
lines changed

checkov/terraform/checks/resource/aws/EKSControlPlaneLogging.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,15 @@ def scan_resource_conf(self, conf):
1919
:return: <CheckResult>
2020
"""
2121
log_types = ["api", "audit", "authenticator", "controllerManager", "scheduler"]
22-
if "enabled_cluster_log_types" in conf.keys() and conf["enabled_cluster_log_types"] and \
23-
conf["enabled_cluster_log_types"][0] is not None \
24-
and all(elem in conf["enabled_cluster_log_types"][0] for elem in log_types):
25-
return CheckResult.PASSED
22+
enabled_cluster_log_types = conf.get("enabled_cluster_log_types")
23+
if enabled_cluster_log_types and enabled_cluster_log_types[0] is not None:
24+
enabled_cluster_log_types = enabled_cluster_log_types[0]
25+
if isinstance(enabled_cluster_log_types[0], str):
26+
if all(elem in enabled_cluster_log_types for elem in log_types):
27+
return CheckResult.PASSED
28+
elif isinstance(enabled_cluster_log_types[0], list):
29+
if all([elem] in enabled_cluster_log_types for elem in log_types):
30+
return CheckResult.PASSED
2631
return CheckResult.FAILED
2732

2833
def get_evaluated_keys(self) -> List[str]:
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# pass
2+
3+
resource "aws_eks_cluster" "fully_enabled" {
4+
name = "example"
5+
role_arn = "aws_iam_role.arn"
6+
7+
enabled_cluster_log_types = [
8+
"api",
9+
"audit",
10+
"authenticator",
11+
"controllerManager",
12+
"scheduler"
13+
]
14+
}
15+
16+
resource "aws_eks_cluster" "fully_enabled_with_dynamic_block" {
17+
name = "example"
18+
role_arn = "aws_iam_role.arn"
19+
20+
enabled_cluster_log_types = [
21+
"api",
22+
"audit",
23+
"authenticator",
24+
"controllerManager",
25+
"scheduler"
26+
]
27+
28+
dynamic "encryption_config" {
29+
for_each = [1]
30+
31+
content {
32+
provider {
33+
key_arn = "aws/kms/key"
34+
}
35+
resources = ["secrets"]
36+
}
37+
}
38+
}
39+
40+
# fail
41+
42+
resource "aws_eks_cluster" "partially_enabled" {
43+
name = "example"
44+
role_arn = "aws_iam_role.arn"
45+
46+
enabled_cluster_log_types = [
47+
"api",
48+
"audit"
49+
]
50+
}
51+
52+
resource "aws_eks_cluster" "not_configured" {
53+
name = "example"
54+
role_arn = "aws_iam_role.arn"
55+
}

tests/terraform/checks/resource/aws/test_EKSControlPlaneLogging.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import unittest
2+
from pathlib import Path
23

4+
from checkov.runner_filter import RunnerFilter
35
from checkov.terraform.checks.resource.aws.EKSControlPlaneLogging import check
6+
from checkov.terraform.runner import Runner
47
from checkov.common.models.enums import CheckResult
58

69

@@ -23,12 +26,43 @@ def test_success(self):
2326
scan_result = check.scan_resource_conf(conf=resource_conf)
2427
self.assertEqual(CheckResult.PASSED, scan_result)
2528

26-
def test_success(self):
29+
def test_failure_not_enabled(self):
2730
resource_conf = {'name': ['testcluster'], 'enabled_cluster_log_types': []}
2831

2932
scan_result = check.scan_resource_conf(conf=resource_conf)
3033
self.assertEqual(CheckResult.FAILED, scan_result)
3134

35+
def test_file(self):
36+
# given
37+
test_files_dir = Path(__file__).parent / "example_EKSControlPlaneLogging"
38+
39+
# when
40+
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[check.id]))
41+
42+
# then
43+
summary = report.get_summary()
44+
45+
passing_resources = {
46+
"aws_eks_cluster.fully_enabled",
47+
"aws_eks_cluster.fully_enabled_with_dynamic_block"
48+
}
49+
failing_resources = {
50+
"aws_eks_cluster.partially_enabled",
51+
"aws_eks_cluster.not_configured"
52+
}
53+
54+
passed_check_resources = {c.resource for c in report.passed_checks}
55+
failed_check_resources = {c.resource for c in report.failed_checks}
56+
57+
self.assertEqual(summary["passed"], 2)
58+
self.assertEqual(summary["failed"], 2)
59+
self.assertEqual(summary["skipped"], 0)
60+
self.assertEqual(summary["parsing_errors"], 0)
61+
62+
self.assertEqual(passing_resources, passed_check_resources)
63+
self.assertEqual(failing_resources, failed_check_resources)
64+
65+
3266

3367
if __name__ == '__main__':
3468
unittest.main()

0 commit comments

Comments
 (0)