-
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(terraform_plan): Expose field changes to python checks (#5112)
* first pass at adding functionality to detect changed fields in a terraform resource * no cool one-liners with relevant_attributes, apparently that is not what I thought it was. back to basic diffing * debug * ofc they make dicts None instead of empty when they are empty * remove whitespace * pr feedback, hopefully a test * docs * import hopefully properly * lint * mypy does not seem to understand how i made lines 256-259 safe * i still have failed to figure out my imports :upside_down: * files not classes? * fix tests and mypy * s/each/resource/g --------- Co-authored-by: Anton Grübel <[email protected]>
- Loading branch information
1 parent
15876b4
commit 861c675
Showing
5 changed files
with
333 additions
and
12 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
27 changes: 27 additions & 0 deletions
27
tests/terraform/runner/extra_tf_plan_checks/security_group_rule_protocol_changed.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,27 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from checkov.common.models.enums import CheckResult, CheckCategories | ||
from checkov.terraform.checks.resource.base_resource_check import BaseResourceCheck | ||
from checkov.terraform.plan_parser import TF_PLAN_RESOURCE_CHANGE_ACTIONS, TF_PLAN_RESOURCE_CHANGE_KEYS | ||
|
||
|
||
class SecurityGroupRuleProtocolChanged(BaseResourceCheck): | ||
def __init__(self) -> None: | ||
name = "Ensure security group rule protocol is not being changed" | ||
id = "CUSTOM_CHANGE_1" | ||
supported_resources = ("aws_security_group_rule",) | ||
categories = (CheckCategories.GENERAL_SECURITY,) | ||
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources) | ||
|
||
def scan_resource_conf(self, conf: dict[str, Any]) -> CheckResult: | ||
actions = conf.get(TF_PLAN_RESOURCE_CHANGE_ACTIONS) | ||
if isinstance(actions, list) and "update" in actions: | ||
if "protocol" in conf.get(TF_PLAN_RESOURCE_CHANGE_KEYS): | ||
self.details.append("some great details") | ||
return CheckResult.FAILED | ||
return CheckResult.PASSED | ||
|
||
|
||
check = SecurityGroupRuleProtocolChanged() |
228 changes: 228 additions & 0 deletions
228
tests/terraform/runner/resources/plan_change_keys/tfplan.json
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,228 @@ | ||
{ | ||
"format_version": "1.1", | ||
"terraform_version": "1.4.6", | ||
"planned_values": | ||
{ | ||
"root_module": | ||
{ | ||
"resources": | ||
[ | ||
{ | ||
"address": "aws_security_group_rule.foo", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "foo", | ||
"provider_name": "registry.terraform.io/hashicorp/aws", | ||
"schema_version": 2, | ||
"values": { | ||
"cidr_blocks": null, | ||
"description": "foo", | ||
"from_port": 5432, | ||
"ipv6_cidr_blocks": null, | ||
"prefix_list_ids": [], | ||
"protocol": "tcp", | ||
"security_group_id": "sg-547cc4cd5f94bb695", | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 5433, | ||
"type": "ingress" | ||
}, | ||
"sensitive_values": { | ||
"prefix_list_ids": [] | ||
} | ||
}, | ||
{ | ||
"address": "aws_security_group_rule.bar", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "bar", | ||
"provider_name": "registry.terraform.io/hashicorp/aws", | ||
"schema_version": 2, | ||
"values": { | ||
"cidr_blocks": null, | ||
"description": "foo", | ||
"from_port": 8888, | ||
"ipv6_cidr_blocks": null, | ||
"prefix_list_ids": [], | ||
"protocol": "udp", | ||
"security_group_id": "sg-547cc4cd5f94bb696", | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 8888, | ||
"type": "ingress" | ||
}, | ||
"sensitive_values": { | ||
"prefix_list_ids": [] | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"resource_changes": | ||
[ | ||
{ | ||
"address": "aws_security_group_rule.foo", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "foo", | ||
"provider_name": "registry.terraform.io/hashicorp/aws", | ||
"schema_version": 2, | ||
"change": | ||
{ | ||
"actions": | ||
[ | ||
"update" | ||
], | ||
"before": { | ||
"cidr_blocks": [], | ||
"description": "foo", | ||
"from_port": 5432, | ||
"id": "sgrule-88888888", | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [], | ||
"protocol": "tcp", | ||
"security_group_id": "sg-547cc4cd5f94bb695", | ||
"security_group_rule_id": null, | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 5432, | ||
"type": "ingress" | ||
}, | ||
"after": { | ||
"cidr_blocks": [], | ||
"description": "foo", | ||
"from_port": 5432, | ||
"id": "sgrule-88888888", | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [], | ||
"protocol": "tcp", | ||
"security_group_id": "sg-547cc4cd5f94bb695", | ||
"security_group_rule_id": "sgr-5a4b695164c564a8f", | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 5433, | ||
"type": "ingress" | ||
}, | ||
"after_unknown": {}, | ||
"before_sensitive": { | ||
"cidr_blocks": [], | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [] | ||
}, | ||
"after_sensitive": { | ||
"cidr_blocks": [], | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [] | ||
} | ||
} | ||
}, | ||
{ | ||
"address": "aws_security_group_rule.bar", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "bar", | ||
"provider_name": "registry.terraform.io/hashicorp/aws", | ||
"schema_version": 2, | ||
"change": | ||
{ | ||
"actions": | ||
[ | ||
"update" | ||
], | ||
"before": { | ||
"cidr_blocks": [], | ||
"description": "bar", | ||
"from_port": 8888, | ||
"id": "sgrule-88888888", | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [], | ||
"protocol": "tcp", | ||
"security_group_id": "sg-547cc4cd5f94bb696", | ||
"security_group_rule_id": null, | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 8888, | ||
"type": "ingress" | ||
}, | ||
"after": { | ||
"cidr_blocks": [], | ||
"description": "bar", | ||
"from_port": 8888, | ||
"id": "sgrule-88888888", | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [], | ||
"protocol": "udp", | ||
"security_group_id": "sg-547cc4cd5f94bb696", | ||
"security_group_rule_id": "sgr-5a4b695164c564a90", | ||
"self": false, | ||
"source_security_group_id": "sg-8d21ab5963b0e7917", | ||
"timeouts": null, | ||
"to_port": 8888, | ||
"type": "ingress" | ||
}, | ||
"after_unknown": {}, | ||
"before_sensitive": { | ||
"cidr_blocks": [], | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [] | ||
}, | ||
"after_sensitive": { | ||
"cidr_blocks": [], | ||
"ipv6_cidr_blocks": [], | ||
"prefix_list_ids": [] | ||
} | ||
} | ||
} | ||
], | ||
"configuration": | ||
{ | ||
"provider_config": | ||
{ | ||
"aws": | ||
{ | ||
"name": "aws" | ||
} | ||
}, | ||
"root_module": | ||
{ | ||
"resources": | ||
[ | ||
{ | ||
"address": "aws_security_group_rule.foo", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "foo", | ||
"provider_config_key": "aws", | ||
"expressions": | ||
{ | ||
"name": | ||
{ | ||
"constant_value": "foo" | ||
} | ||
}, | ||
"schema_version": 2 | ||
}, | ||
{ | ||
"address": "aws_security_group_rule.bar", | ||
"mode": "managed", | ||
"type": "aws_security_group_rule", | ||
"name": "bar", | ||
"provider_config_key": "aws", | ||
"expressions": | ||
{ | ||
"name": | ||
{ | ||
"constant_value": "bar" | ||
} | ||
}, | ||
"schema_version": 2 | ||
} | ||
] | ||
} | ||
} | ||
} |
Oops, something went wrong.