Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions checkov/terraform/plan_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,21 @@ def _handle_complex_after_unknown(k: str, resource_conf: dict[str, Any], v: Any)
continue
if inner_key not in resource_conf[k]:
if isinstance(resource_conf[k][0], dict):
resource_conf[k][0][inner_key] = _clean_simple_type_list([TRUE_AFTER_UNKNOWN])
if _validate_after_unknown_list_not_empty(inner_key, k, resource_conf):
resource_conf[k][0][inner_key] = _clean_simple_type_list([TRUE_AFTER_UNKNOWN])
elif isinstance(resource_conf[k][0], list) and isinstance(resource_conf[k][0][0], dict):
resource_conf[k][0][0][inner_key] = _clean_simple_type_list([TRUE_AFTER_UNKNOWN])
if _validate_after_unknown_list_not_empty(inner_key, k, resource_conf):
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we really invoke the same condition in both cases?
I would expect the second condition to be invoked with k[0], since the second one is operating at a deeper level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

right, fixing

resource_conf[k][0][0][inner_key] = _clean_simple_type_list([TRUE_AFTER_UNKNOWN])


def _validate_after_unknown_list_not_empty(inner_key: str, k: str, resource_conf: dict[str, Any]) -> bool:
"""
If the inner key is a list - we want to check it's not empty, if not we handle it in the original way.
"""
value = resource_conf[k][0]
return ((inner_key not in value) or
(inner_key in value and not isinstance(value[inner_key], list)) or
(isinstance(value[inner_key], list) and value[inner_key] != [] and value[inner_key][0] != []))


def _find_child_modules(
Expand Down
17 changes: 17 additions & 0 deletions tests/terraform/parser/test_plan_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ def test_handle_complex_after_unknown(self):
_handle_complex_after_unknown(key, resource, value)
assert resource == {'tags': [[{'custom_tags': ['true_after_unknown']}]]}

def test_handle_complex_after_unknown_with_empty_list(self):
resource = {"network_configuration": [
{
"endpoint_configuration": [
]
}
]}
key: str = 'network_configuration'
value: list = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we have more than one item in this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the function to handle case where there are more than 1 by going in for loop

{
"endpoint_configuration": [
]
}
]
_handle_complex_after_unknown(key, resource, value)
assert resource == {'network_configuration': [{"endpoint_configuration": []}]}


def test_large_file(mocker: MockerFixture):
# given
Expand Down
Loading