Skip to content

Commit f27c236

Browse files
authoredJul 10, 2024··
fix(misconf): do not evaluate TF when a load error occurs (#7109)
Signed-off-by: nikpivkin <[email protected]>
1 parent 7cbdb0a commit f27c236

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

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

+3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ func (p *Parser) EvaluateAll(ctx context.Context) (terraform.Modules, cty.Value,
268268
e, err := p.Load(ctx)
269269
if errors.Is(err, ErrNoFiles) {
270270
return nil, cty.NilVal, nil
271+
} else if err != nil {
272+
return nil, cty.NilVal, err
271273
}
274+
272275
modules, fsMap := e.EvaluateAll(ctx)
273276
p.debug.Log("Finished parsing module '%s'.", p.moduleName)
274277
p.fsMap = fsMap

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

+20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"sort"
88
"testing"
9+
"testing/fstest"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
@@ -1725,3 +1726,22 @@ func Test_LoadLocalCachedModule(t *testing.T) {
17251726
bucketName := buckets[0].GetAttribute("bucket").Value().AsString()
17261727
assert.Equal(t, "my-s3-bucket", bucketName)
17271728
}
1729+
1730+
func TestTFVarsFileDoesNotExist(t *testing.T) {
1731+
fsys := fstest.MapFS{
1732+
"main.tf": &fstest.MapFile{
1733+
Data: []byte(``),
1734+
},
1735+
}
1736+
1737+
parser := New(
1738+
fsys, "",
1739+
OptionStopOnHCLError(true),
1740+
OptionWithDownloads(false),
1741+
OptionWithTFVarsPaths("main.tfvars"),
1742+
)
1743+
require.NoError(t, parser.ParseFS(context.TODO(), "."))
1744+
1745+
_, _, err := parser.EvaluateAll(context.TODO())
1746+
assert.ErrorContains(t, err, "file does not exist")
1747+
}

0 commit comments

Comments
 (0)
Please sign in to comment.