Skip to content
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 rules about server instance and public IP #3857

Merged
merged 17 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions checkov/terraform/checks/resource/ncp/ServerPublicIP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from checkov.common.models.consts import ANY_VALUE
from checkov.common.models.enums import CheckCategories
from checkov.terraform.checks.resource.base_resource_negative_value_check import BaseResourceNegativeValueCheck


class ServerPublicIP(BaseResourceNegativeValueCheck):
def __init__(self):
name = "Ensure Server instance should not have public IP."
id = "CKV_NCP_23"
supported_resource = ('ncloud_public_ip',)
categories = (CheckCategories.NETWORKING,)
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resource)

def get_inspected_key(self):
return 'server_instance_no'

def get_forbidden_values(self):
return [ANY_VALUE]


check = ServerPublicIP()
18 changes: 18 additions & 0 deletions tests/terraform/checks/resource/ncp/example_ServerPublicIP/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "ncloud_server" "server" {
subnet_no = ncloud_subnet.test.id
name = "my-tf-server"
server_image_product_code = "SW.VSVR.OS.LNX64.CNTOS.0703.B050"
server_product_code = "SVR.VSVR.HICPU.C002.M004.NET.SSD.B050.G002"
login_key_name = ncloud_login_key.loginkey.key_name
}

resource "ncloud_public_ip" "pass" {
}

resource "ncloud_public_ip" "fail" {
server_instance_no = ncloud_server.server.instance_no
}

resource "ncloud_public_ip" "fail2" {
server_instance_no = "551212"
}
41 changes: 41 additions & 0 deletions tests/terraform/checks/resource/ncp/test_ServerPublicIP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from pathlib import Path

from checkov.runner_filter import RunnerFilter
from checkov.terraform.checks.resource.ncp.ServerPublicIP import check
from checkov.terraform.runner import Runner


class TestServerPublicIP(unittest.TestCase):
def test(self):
# given
test_files_dir = Path(__file__).parent / "example_ServerPublicIP"

# when
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[check.id]))

# then
summary = report.get_summary()

passing_resources = {
"ncloud_public_ip.pass",
}
failing_resources = {
"ncloud_public_ip.fail",
"ncloud_public_ip.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"], 1)
self.assertEqual(summary["failed"], 2)
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()