Skip to content

Commit 99485cf

Browse files
aqua-botnikpivkin
andauthored
fix(misconf): check if for-each is known when expanding dyn block [backport: release/v0.62] (#8826)
Signed-off-by: nikpivkin <[email protected]> Co-authored-by: Nikita Pivkin <[email protected]>
1 parent b4fc9e8 commit 99485cf

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pkg/iac/scanners/terraform/parser/parser_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,15 @@ resource "test_resource" "test" {
15881588
bar = foo.value
15891589
}
15901590
}
1591+
}`,
1592+
expected: []any{},
1593+
},
1594+
{
1595+
name: "unknown for-each",
1596+
src: `resource "test_resource" "test" {
1597+
dynamic "foo" {
1598+
for_each = lookup(foo, "") ? [] : []
1599+
}
15911600
}`,
15921601
expected: []any{},
15931602
},

pkg/iac/terraform/block.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ func (b *Block) ExpandBlock() error {
583583
if child.Type() == "dynamic" {
584584
blocks, err := child.expandDynamic()
585585
if err != nil {
586-
errs = multierror.Append(errs, err)
586+
errs = multierror.Append(errs, fmt.Errorf("block %q: %w", child.TypeLabel(), err))
587587
continue
588588
}
589589
expanded = append(expanded, blocks...)
@@ -612,6 +612,10 @@ func (b *Block) expandDynamic() ([]*Block, error) {
612612
return nil, fmt.Errorf("invalid for-each in %s block: %w", b.FullLocalName(), err)
613613
}
614614

615+
if !forEachVal.IsKnown() {
616+
return nil, errors.New("for-each must be known")
617+
}
618+
615619
var (
616620
expanded []*Block
617621
errs error

0 commit comments

Comments
 (0)