Rendering a dynamic block drops the static blocks of the same name
Describe the issue
Rendering a dynamic block replaces the statically written blocks of the same name instead of adding to them. A resource that declares both rule { ... } and dynamic "rule" { ... } ends up with only the dynamic rules in the graph, so any check that reads rule sees the static one as absent.
process_dynamic_values() in checkov/terraform/parser_functions.py:197 already appends the unrendered content of each dynamic block to the attribute of the same name, so at that point the attribute holds the static blocks followed by the dynamic ones. _render_dynamic_blocks() in checkov/terraform/graph_builder/variable_rendering/renderer.py:321 then calls
vertex.update_inner_attribute(block_name, vertex.attributes, block_confs)with block_confs holding the rendered dynamic blocks only, and update_inner_attribute assigns over the whole attribute. The static blocks are dropped.
This is the same conclusion @gruebel came to in #5363 two years ago: "Basically the dynamic block overrides the other one, that's why it fails." Reported again in #6028 and, for the same check, in #4733.
Why it keeps coming back
The one test resource that covers a static rule next to a dynamic one, aws_s3_bucket_lifecycle_configuration.resource_with_dynamic_rule_pass4 in tests/terraform/checks/resource/aws/example_S3AbortIncompleteUploads/main.tf, passes whether the bug is present or not. _process_dynamic_blocks() returns {} for that shape, so nothing is rendered and nothing overwrites anything. #5363 was closed on "I'm showing this check passes for your example", and that example is the one shape that never reaches the code path.
Examples
CKV_AWS_300 is where it shows up most often, but the defect is in the graph, not the check. One file, no modules:
variable "versioning" {
default = true
}
resource "aws_s3_bucket_lifecycle_configuration" "x" {
bucket = "x"
rule {
id = "abort-incomplete-uploads"
status = "Enabled"
abort_incomplete_multipart_upload {
days_after_initiation = 7
}
}
dynamic "rule" {
for_each = var.versioning ? [1] : []
content {
id = "expire-noncurrent-versions"
status = "Enabled"
noncurrent_version_expiration {
noncurrent_days = 1
}
}
}
}FAILED for resource: aws_s3_bucket_lifecycle_configuration.xThe rule attribute the check is given, printed from scan_resource_conf:
[{'id': ['expire-noncurrent-versions'], 'noncurrent_version_expiration': {'noncurrent_days': [1]}, 'status': ['Enabled']},
{'id': ['expire-noncurrent-versions'], 'noncurrent_version_expiration': [{'noncurrent_days': [1]}], 'status': ['Enabled']}]Two entries, both the dynamic rule. abort-incomplete-uploads is not in the graph.
Expected: three entries, the static rule first.
Order in the source makes no difference, writing the dynamic block first fails the same way. Two static rules pass, so it is the dynamic one that does it. An empty filter {} and a variable in days_after_initiation are not involved.
Other shapes that reproduce it, all measured on 3.3.19:
- the example in #6028, where the dynamic rule is
Disabledand takes the place of an enabled static one - a
dynamic "rule"withfor_each = var.list_of_mapsandrule.value[...]references, with the static abort rule above it - more than one
dynamic "rule"in the same resource alongside a static one
Version (please complete the following information):
- Checkov Version 3.3.19, also 3.3.16
Source: bridgecrewio/checkov