Skip to content

Commit 58d5752

Browse files
authored
🐛 terraform: read multiple settings blocks (#5482)
Contributes: https://github.com/mondoohq/server/issues/10794 Signed-off-by: Salim Afiune Maya <afiune@mondoo.com>
1 parent 8bc40c4 commit 58d5752

File tree

1 file changed

+30
-26
lines changed
  • providers/terraform/resources

1 file changed

+30
-26
lines changed

providers/terraform/resources/hcl.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ func initTerraformSettings(runtime *plugin.Runtime, args map[string]*llx.RawData
711711
}
712712

713713
blocks := terraform.terraformBlocks
714-
if len(blocks) != 1 {
714+
if len(blocks) == 0 {
715715
// no terraform settings block found, this is ok for terraform and not an error
716716
// TODO: return modified arguments to load from recording
717717
return nil, &mqlTerraformSettings{
@@ -721,34 +721,38 @@ func initTerraformSettings(runtime *plugin.Runtime, args map[string]*llx.RawData
721721
}, nil
722722
}
723723

724-
settingsBlock := blocks[0]
725-
args["block"] = llx.ResourceData(settingsBlock, "terraform.block")
726-
args["requiredProviders"] = llx.DictData(map[string]interface{}{})
727-
args["backend"] = llx.DictData(map[string]interface{}{})
728-
729-
if settingsBlock.block.State == plugin.StateIsSet {
730-
hb := settingsBlock.block.Data
731-
requireProviderBlock := getBlockByName(hb, "required_providers")
732-
if requireProviderBlock != nil {
733-
attributes, _ := requireProviderBlock.Body.JustAttributes()
734-
dict, err := hclResolvedAttributesToDict(attributes)
735-
if err != nil {
736-
return nil, nil, err
724+
// The block `terraform {}` can be defined multiple times but we don't support that yet,
725+
// we will point to the first block and collect all the settings from all blocks to give
726+
// as much information as possible back
727+
args["block"] = llx.ResourceData(blocks[0], "terraform.block")
728+
args["requiredProviders"] = llx.DictData(map[string]any{})
729+
args["backend"] = llx.DictData(map[string]any{})
730+
731+
for _, settingsBlock := range blocks {
732+
if settingsBlock.block.State == plugin.StateIsSet {
733+
hb := settingsBlock.block.Data
734+
requireProviderBlock := getBlockByName(hb, "required_providers")
735+
if requireProviderBlock != nil {
736+
attributes, _ := requireProviderBlock.Body.JustAttributes()
737+
dict, err := hclResolvedAttributesToDict(attributes)
738+
if err != nil {
739+
return nil, nil, err
740+
}
741+
args["requiredProviders"] = llx.DictData(dict)
737742
}
738-
args["requiredProviders"] = llx.DictData(dict)
739-
}
740743

741-
backendBlock := getBlockByName(hb, "backend")
742-
if backendBlock != nil {
743-
attributes, _ := backendBlock.Body.JustAttributes()
744-
dict, err := hclResolvedAttributesToDict(attributes)
745-
if err != nil {
746-
return nil, nil, err
747-
}
748-
if len(backendBlock.Labels) != 0 {
749-
dict["type"] = backendBlock.Labels[0]
744+
backendBlock := getBlockByName(hb, "backend")
745+
if backendBlock != nil {
746+
attributes, _ := backendBlock.Body.JustAttributes()
747+
dict, err := hclResolvedAttributesToDict(attributes)
748+
if err != nil {
749+
return nil, nil, err
750+
}
751+
if len(backendBlock.Labels) != 0 {
752+
dict["type"] = backendBlock.Labels[0]
753+
}
754+
args["backend"] = llx.DictData(dict)
750755
}
751-
args["backend"] = llx.DictData(dict)
752756
}
753757
}
754758

0 commit comments

Comments
 (0)