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): fix an issue with dynamics replacing a whole block #3846

Merged
merged 1 commit into from
Nov 11, 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
3 changes: 2 additions & 1 deletion checkov/terraform/parser_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def process_dynamic_values(conf: Dict[str, List[Any]]) -> bool:
conf[element_name] = element_value["content"]
else:
# this should be the result of a successful dynamic block rendering
conf[element_name] = element_value
# in some cases a whole dict is added, which doesn't have a list around it
conf[element_name] = element_value if isinstance(element_value, list) else [element_value]

has_dynamic_block = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ resource "aws_lambda_function" "lambda" {
target_arn = dead_letter_config.value.target_arn
}
}

dynamic "environment" {
for_each = var.environment == null ? [] : [var.environment]
content {
variables = environment.value.variables
}
}
}
30 changes: 30 additions & 0 deletions tests/terraform/graph/runner/test_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pathlib import Path

from checkov.runner_filter import RunnerFilter
from checkov.terraform.runner import Runner


def test_dynamics():
# given
test_files_dir = Path(__file__).parent.parent / "resources/dynamic_lambda_function"

# when
report = Runner().run(
root_folder=str(test_files_dir),
runner_filter=RunnerFilter(
checks=[
"CKV_AWS_45",
"CKV_AWS_116",
"CKV_AWS_173",
"CKV_AWS_272",
]
),
)

# then
summary = report.get_summary()

assert summary["passed"] == 2
assert summary["failed"] == 2
assert summary["skipped"] == 0
assert summary["parsing_errors"] == 0