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

fix(terraform): Handle new resource type for CKV_GCP_73 #7023

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from checkov.common.models.enums import CheckResult, CheckCategories
from checkov.common.util.type_forcers import force_list
from checkov.terraform.checks.resource.base_resource_check import BaseResourceCheck
from checkov.common.graph.graph_builder import CustomAttributes


class CloudArmorWAFACLCVE202144228(BaseResourceCheck):
Expand Down Expand Up @@ -38,6 +39,32 @@ def scan_resource_conf(self, conf: Dict[str, List[Any]]) -> CheckResult:
if rule.get("preview") == [True]:
return CheckResult.FAILED
return CheckResult.PASSED

resource_name = conf.get("name")[0]
Copy link
Contributor

@maxamel maxamel Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we always have a name? I know the name attribute can be omitted from the resource but I am not sure if we populate this field in a default way in this case. I also saw in other locations we check if conf["name"] is null.

connected_rules = [
g1[1] for g1 in self.graph.nodes()
if g1[1].get(CustomAttributes.RESOURCE_TYPE) == "google_compute_security_policy_rule" and
g1[1].get("security_policy") == resource_name
]

for rule in force_list(connected_rules):
match = rule.get("match")
if match and isinstance(match, dict):
expr = match.get("expr")
if expr and isinstance(expr, dict):
if expr.get("expression") == "evaluatePreconfiguredExpr('cve-canary')":
if rule.get("action") == "allow":
return CheckResult.FAILED
if rule.get("preview"):
Copy link
Contributor

@maxamel maxamel Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I saw if preview=True we do not enforce the action, so why are we failing here?
Also, perhaps before the action check we need to first check the preview since in case it's True the action won't kick in.

return CheckResult.FAILED
return CheckResult.PASSED
elif expr.get("expression") == "evaluatePreconfiguredWaf('cve-canary')":
if rule.get("action") == "allow":
return CheckResult.FAILED
if rule.get("preview"):
return CheckResult.FAILED
return CheckResult.PASSED

return CheckResult.FAILED


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,49 @@ resource "google_compute_security_policy" "pass_preconfigwaf" {
}
}

resource "google_compute_security_policy" "pass_separate_resource" {
name = "example_separate"

rule {
description = "Foo"
priority = 1

match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}

action = "deny(404)"
}
}

resource "google_compute_security_policy_rule" "cve_canary_waf" {
security_policy = google_compute_security_policy.pass_separate_resource.name
description = "cve-canary WAF rule"
priority = 1
match {
expr {
expression = "evaluatePreconfiguredExpr('cve-canary')"
}
}
action = "deny(403)"
}

resource "google_compute_security_policy_rule" "rule2" {
security_policy = google_compute_security_policy.pass_separate_resource.name
description = "rule2"
priority = 2
match {
expr {
expression = "evaluatePreconfiguredWaf('xss-canary')"
}
}
action = "allow"
}


# fail

resource "google_compute_security_policy" "allow" {
Expand Down Expand Up @@ -101,4 +144,75 @@ resource "google_compute_security_policy" "pass_preconfigwaf" {
}
}
}
}

resource "google_compute_security_policy" "fail" {

name = "my-policy"

rule {
action = "deny(403)"
priority = "1000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["9.9.9.0/24"]
}
}
description = "Deny access to IPs in 9.9.9.0/24"
}

rule {
action = "allow"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "default rule"
}
}

resource "google_compute_security_policy" "fail_separate_resource" {
name = "example_separate_fail"

rule {
description = "Foo"
priority = 1

match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}

action = "deny(404)"
}
}

resource "google_compute_security_policy_rule" "cve_canary_waf" {
security_policy = google_compute_security_policy.fail_separate_resource.name
description = "cve-canary WAF rule"
priority = 1
match {
expr {
expression = "evaluatePreconfiguredExpr('cve-canary')"
}
}
action = "allow"
}

resource "google_compute_security_policy_rule" "rule2" {
security_policy = google_compute_security_policy.fail_separate_resource.name
description = "rule2"
priority = 2
match {
expr {
expression = "evaluatePreconfiguredWaf('xss-canary')"
}
}
action = "allow"
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ def test(self):
"google_compute_security_policy.enabled_deny_403",
"google_compute_security_policy.enabled_deny_404",
"google_compute_security_policy.pass_preconfigwaf",
"google_compute_security_policy.pass_separate_resource",
}

failing_resources = {
"google_compute_security_policy.allow",
"google_compute_security_policy.preview",
"google_compute_security_policy.different_expr",
"google_compute_security_policy.pass_preconfigwaf"
"google_compute_security_policy.pass_preconfigwaf",
"google_compute_security_policy.fail",
"google_compute_security_policy.fail_separate_resource",
}

passed_check_resources = {c.resource for c in report.passed_checks}
Expand Down
Loading