Skip to content
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
26 changes: 11 additions & 15 deletions pkg/iac/scanners/terraform/parser/load_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

import (
"context"
"errors"
"fmt"
"io/fs"
"path"
Expand Down Expand Up @@ -43,7 +44,9 @@ func (e *evaluator) loadModules(ctx context.Context) []*ModuleDefinition {
}
moduleDefinition, err := e.loadModule(ctx, moduleBlock)
if err != nil {
e.logger.Error("Failed to load module. Maybe try 'terraform init'?", log.Err(err))
rng := moduleBlock.GetMetadata().Range()
e.logger.Error("Failed to load module. Maybe try 'terraform init'?",
log.String("loc", rng.String()), log.Err(err))
continue
}

Expand All @@ -56,25 +59,18 @@ func (e *evaluator) loadModules(ctx context.Context) []*ModuleDefinition {

// takes in a module "x" {} block and loads resources etc. into e.moduleBlocks - additionally returns variables to add to ["module.x.*"] variables
func (e *evaluator) loadModule(ctx context.Context, b *terraform.Block) (*ModuleDefinition, error) {

metadata := b.GetMetadata()

if b.Label() == "" {
return nil, fmt.Errorf("module without label at %s", metadata.Range())
return nil, errors.New("module without label")
}

var source string
attrs := b.Attributes()
for _, attr := range attrs {
if attr.Name() == "source" {
sourceVal := attr.Value()
if sourceVal.Type() == cty.String {
source = sourceVal.AsString()
}
}
sourceVal := b.GetAttribute("source").Value()
if !sourceVal.IsKnown() || sourceVal.Type() != cty.String {
return nil, errors.New("source is not a string")
}

source := sourceVal.AsString()
if source == "" {
return nil, fmt.Errorf("could not read module source attribute at %s", metadata.Range().String())
return nil, errors.New("source is empty")
}

if e.moduleMetadata != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac/scanners/terraform/parser/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func buildGraph(blocks terraform.Blocks, paths []string) modulesGraph {

for _, block := range moduleBlocks {
sourceVal := block.GetAttribute("source").Value()
if sourceVal.Type() != cty.String {
if !sourceVal.IsKnown() || sourceVal.Type() != cty.String {
continue
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/iac/scanners/terraform/parser/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ module "this" {
"code/infra2",
},
},
{
name: "unknown module source",
files: map[string]string{
"main.tf": `module "test" {
source = "${path.module}/modules/foo"
}`,
},
expected: []string{"."},
},
}

for _, tt := range tests {
Expand Down
23 changes: 23 additions & 0 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2854,3 +2854,26 @@ locals {
attr := bucket.GetAttribute("test")
require.Equal(t, "some_value", attr.Value().AsString())
}

func Test_UnknownModuleSource(t *testing.T) {
files := map[string]string{
`main.tf`: `
module "test" {
source = "${foo}/modules/foo"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this valid to do? Regardless we shouldn't panic so this PR makes sense but just curious.

}
`,
}

fsys := testutil.CreateFS(t, files)
parser := New(fsys, "",
OptionWithSkipCachedModules(true),
OptionStopOnHCLError(true),
)
err := parser.ParseFS(t.Context(), ".")
require.NoError(t, err)

modules, err := parser.EvaluateAll(t.Context())
require.NoError(t, err)

require.Len(t, modules, 1)
}
Loading