-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(terraform): add CKV_NCP_1 about lb target group health check, CKV_NCP_2 about access control group description #3569
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5afbd24
[22.09.27][추가] CKV_NCP_1
pj991207 4d48fe1
[22.09.27][추가] CKV_NCP_2
Floodnut 9011d54
[22.09.27][Merge]
Floodnut ea829d9
Apply suggestions from code review
pj991207 0e76a1f
Apply suggestions from code review
pj991207 5150177
Apply suggestions from code review
pj991207 d2b322f
Create main.yml
pj991207 a7e3000
[22.09.28][수정] Lint test
Floodnut 2f7dcdf
Merge branch 'master' of https://github.com/init-cloud/checkov
Floodnut f8e7357
Delete main.yml
pj991207 52cb35d
[22.09.29][수정]testcode 수정
pj991207 b1555cb
[22.09.29][수정] 테스트 코드 수정
Floodnut e77773d
[22.09.29][수정] 테스트코드 수정
Floodnut 51d2b71
Merge branch 'master' into master
pj991207 1ccffed
[22.09.29][수정] add test resource for 'ncloud_access_control_group_rule'
Floodnut 49fb76a
Merge branch 'master' of https://github.com/init-cloud/checkov
Floodnut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 47 additions & 0 deletions
47
checkov/terraform/checks/resource/ncp/AccessControlGroupRuleDescription.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,47 @@ | ||
from checkov.common.models.enums import CheckResult, CheckCategories | ||
from checkov.terraform.checks.resource.base_resource_check import BaseResourceCheck | ||
|
||
|
||
class AccessControlGroupRuleDescription(BaseResourceCheck): | ||
def __init__(self): | ||
name = "Ensure every access control groups rule has a description" | ||
id = "CKV_NCP_002" | ||
supported_resource = [ | ||
'ncloud_access_control_group', | ||
'ncloud_access_control_group_rule', | ||
] | ||
categories = [CheckCategories.NETWORKING] | ||
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resource) | ||
|
||
def scan_resource_conf(self, conf): | ||
""" | ||
https://registry.terraform.io/providers/NaverCloudPlatform/ncloud/latest/docs/resources/access_control_group | ||
:return: <CheckResult> | ||
""" | ||
group_result = self.check_rule(rule_type='group_or_rule_description', conf=conf) | ||
if 'type' not in conf.keys(): | ||
outbound_result = self.check_rule(rule_type='outbound', conf=conf) | ||
inbound_result = self.check_rule(rule_type='inbound', conf=conf) | ||
if group_result == CheckResult.PASSED or (outbound_result == CheckResult.PASSED and inbound_result == CheckResult.PASSED): | ||
return CheckResult.PASSED | ||
return CheckResult.FAILED | ||
|
||
return group_result | ||
|
||
def check_rule(self, rule_type, conf): | ||
|
||
if rule_type == 'group_or_rule_description': | ||
self.evaluated_keys = ['description'] | ||
if conf.get('description'): | ||
return CheckResult.PASSED | ||
return CheckResult.FAILED | ||
|
||
if rule_type in conf.keys(): | ||
for rule in conf[rule_type]: | ||
if isinstance(rule, dict) and rule.get('description'): | ||
self.evaluated_keys.append(f'{rule_type}/[{conf[rule_type].index(rule)}]') | ||
return CheckResult.PASSED | ||
return CheckResult.FAILED | ||
|
||
|
||
check = AccessControlGroupRuleDescription() |
24 changes: 24 additions & 0 deletions
24
checkov/terraform/checks/resource/ncp/LBTargetGroupDefinesHealthCheck.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 checkov.common.models.enums import CheckResult, CheckCategories | ||
from checkov.terraform.checks.resource.base_resource_check import BaseResourceCheck | ||
|
||
|
||
class LBTargetGroupDefinesHealthCheck(BaseResourceCheck): | ||
def __init__(self): | ||
name = "Ensure HTTP HTTPS Target group defines Healthcheck" | ||
id = "CKV_NCP_1" | ||
supported_resources = ['ncloud_lb_target_group'] | ||
categories = [CheckCategories.GENERAL_SECURITY] | ||
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources) | ||
|
||
def scan_resource_conf(self, conf): | ||
if conf.get("protocol") in (["HTTP"], ["HTTPS"]): | ||
health_checks = conf.get("health_check") | ||
if health_checks and isinstance(health_checks, list): | ||
healthcheck = health_checks[0] | ||
if isinstance(healthcheck, dict) and healthcheck.get("url_path"): | ||
return CheckResult.PASSED | ||
return CheckResult.FAILED | ||
return CheckResult.UNKNOWN | ||
|
||
|
||
check = LBTargetGroupDefinesHealthCheck() |
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,4 @@ | ||
from pathlib import Path | ||
|
||
modules = Path(__file__).parent.glob("*.py") | ||
__all__ = [f.stem for f in modules if f.is_file() and not f.stem == "__init__"] |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[pytest] | ||
addopts = -n 2 --dist loadfile | ||
asyncio_mode = auto | ||
|
Empty file.
78 changes: 78 additions & 0 deletions
78
tests/terraform/checks/resource/ncp/example_AccessControlGroupRuleDescription/main.tf
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,78 @@ | ||
resource "ncloud_access_control_group" "pass" { | ||
name = "example-acg" | ||
vpc_no = data.ncloud_vpc.selected.id | ||
description = "description" | ||
} | ||
|
||
|
||
resource "ncloud_access_control_group" "fail" { | ||
name = "example-acg" | ||
vpc_no = data.ncloud_vpc.selected.id | ||
} | ||
|
||
|
||
resource "ncloud_access_control_group_rule" "pass" { | ||
access_control_group_no = ncloud_access_control_group.acg.id | ||
|
||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "22" | ||
description = "inbound 22" | ||
} | ||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "80" | ||
description = "inbound 80" | ||
} | ||
outbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "1-65535" | ||
description = "accept 1-65535 port" | ||
} | ||
} | ||
|
||
|
||
resource "ncloud_access_control_group_rule" "fail" { | ||
access_control_group_no = ncloud_access_control_group.acg.id | ||
|
||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "22" | ||
description = "inbound 22" | ||
} | ||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "80" | ||
} | ||
outbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "1-65535" | ||
} | ||
} | ||
|
||
|
||
resource "ncloud_access_control_group_rule" "fail2" { | ||
access_control_group_no = ncloud_access_control_group.acg.id | ||
|
||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "22" | ||
} | ||
inbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "80" | ||
} | ||
outbound { | ||
protocol = "TCP" | ||
ip_block = "0.0.0.0/0" | ||
port_range = "1-65535" | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/terraform/checks/resource/ncp/example_LBTargetGroupDefinesHealthCheck/main.tf
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,26 @@ | ||
|
||
resource "ncloud_lb_target_group" "pass" { | ||
vpc_no = ncloud_vpc.main.vpc_no | ||
protocol = "HTTP" | ||
target_type = "VSVR" | ||
port = 8080 | ||
description = "for test" | ||
health_check { | ||
protocol = "HTTP" | ||
http_method = "GET" | ||
port = 8080 | ||
url_path = "/monitor/l7check" | ||
cycle = 30 | ||
up_threshold = 2 | ||
down_threshold = 2 | ||
} | ||
algorithm_type = "RR" | ||
} | ||
resource "ncloud_lb_target_group" "fail" { | ||
vpc_no = ncloud_vpc.main.vpc_no | ||
protocol = "HTTP" | ||
target_type = "VSVR" | ||
port = 8080 | ||
description = "for test" | ||
algorithm_type = "RR" | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/terraform/checks/resource/ncp/test_AccessControlGroupRuleDescription.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,43 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
from checkov.runner_filter import RunnerFilter | ||
from checkov.terraform.checks.resource.ncp.AccessControlGroupRuleDescription import check | ||
from checkov.terraform.runner import Runner | ||
|
||
|
||
class TestAccessControlGroupRuleDescription(unittest.TestCase): | ||
def test(self): | ||
# given | ||
test_files_dir = Path(__file__).parent / "example_AccessControlGroupRuleDescription" | ||
|
||
# when | ||
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[check.id])) | ||
|
||
# then | ||
summary = report.get_summary() | ||
|
||
passing_resources = { | ||
"ncloud_access_control_group.pass", | ||
"ncloud_access_control_group_rule.pass", | ||
} | ||
failing_resources = { | ||
"ncloud_access_control_group.fail", | ||
"ncloud_access_control_group_rule.fail", | ||
"ncloud_access_control_group_rule.fail2", | ||
} | ||
|
||
passed_check_resources = {c.resource for c in report.passed_checks} | ||
failed_check_resources = {c.resource for c in report.failed_checks} | ||
|
||
self.assertEqual(summary["passed"], 2) | ||
self.assertEqual(summary["failed"], 3) | ||
self.assertEqual(summary["skipped"], 0) | ||
self.assertEqual(summary["parsing_errors"], 0) | ||
|
||
self.assertEqual(passing_resources, passed_check_resources) | ||
self.assertEqual(failing_resources, failed_check_resources) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
40 changes: 40 additions & 0 deletions
40
tests/terraform/checks/resource/ncp/test_LBTargetGroupDefinesHealthCheck.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,40 @@ | ||
import unittest | ||
from pathlib import Path | ||
|
||
from checkov.runner_filter import RunnerFilter | ||
from checkov.terraform.checks.resource.ncp.LBTargetGroupDefinesHealthCheck import check | ||
from checkov.terraform.runner import Runner | ||
|
||
|
||
class TestLBTargetGroupDefinesHealthCheck(unittest.TestCase): | ||
def test(self): | ||
# given | ||
test_files_dir = Path(__file__).parent / "example_LBTargetGroupDefinesHealthCheck" | ||
|
||
# when | ||
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[check.id])) | ||
|
||
# then | ||
summary = report.get_summary() | ||
|
||
passing_resources = { | ||
"ncloud_lb_target_group.pass", | ||
} | ||
failing_resources = { | ||
"ncloud_lb_target_group.fail", | ||
} | ||
|
||
passed_check_resources = {c.resource for c in report.passed_checks} | ||
failed_check_resources = {c.resource for c in report.failed_checks} | ||
|
||
self.assertEqual(summary["passed"], 1) | ||
self.assertEqual(summary["failed"], 1) | ||
self.assertEqual(summary["skipped"], 0) | ||
self.assertEqual(summary["parsing_errors"], 0) | ||
|
||
self.assertEqual(passing_resources, passed_check_resources) | ||
self.assertEqual(failing_resources, failed_check_resources) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you also add a passing + failing resource for
ncloud_access_control_group_rule
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll check additional resource
ncloud_access_control_group_rule
!! thanks!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add resource
ncloud_access_control_group_rule
!And Does resources to be tested mean what we commited?