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): Added a check to make sure dynamic "blocks" are of the expected type #5642

Merged
merged 4 commits into from
Oct 18, 2023
Merged
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
18 changes: 14 additions & 4 deletions checkov/terraform/parser_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@

from checkov.common.util.data_structures_utils import pickle_deepcopy
from checkov.common.util.type_forcers import convert_str_to_bool
from checkov.common.util.parser_utils import eval_string, split_merge_args, string_to_native, to_string
from checkov.common.util.parser_utils import (
eval_string,
split_merge_args,
string_to_native,
to_string,
)

#
# Functions defined in this file implement terraform functions.
Expand Down Expand Up @@ -121,7 +126,7 @@ def toset(original: str, **_: Any) -> set[Any] | str:

def tomap(original: str, **_: Any) -> dict[Hashable, Any] | str:
# https://www.terraform.io/docs/language/functions/tomap.html
original = original.replace(":", "=") # converted to colons by parser #shrug
original = original.replace(":", "=") # converted to colons by parser #shrug

altered_value = eval_string(original)
if altered_value is None or not isinstance(altered_value, dict):
Expand All @@ -136,7 +141,7 @@ def map(original: str, **_: Any) -> dict[Hashable, Any] | str:
# the issue, act like it's a list (to allow comma separation) and let the HCL
# parser deal with it. Then iterating the list is easy.
converted_to_list = eval_string(f"[{original}]")
if converted_to_list is None or len(converted_to_list) & 1: # none or odd number of args
if converted_to_list is None or len(converted_to_list) & 1: # none or odd number of args
return FUNCTION_FAILED

return create_map(converted_to_list)
Expand Down Expand Up @@ -190,8 +195,13 @@ def handle_dynamic_values(conf: Dict[str, List[Any]], has_dynamic_block: bool =


def process_dynamic_values(conf: Dict[str, List[Any]]) -> bool:
dynamic_conf: Union[List[Any], Dict[str, List[Any]]] = conf.get("dynamic", {})

if not isinstance(dynamic_conf, list):
return False

has_dynamic_block = False
for dynamic_element in conf.get("dynamic", {}):
for dynamic_element in dynamic_conf:
if isinstance(dynamic_element, str):
try:
dynamic_element = json.loads(dynamic_element)
Expand Down