Skip to content

Commit 4141013

Browse files
authored
chore(terraform): remove os.OpenPath call from terraform file functions (#8737)
1 parent b7cbbdc commit 4141013

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

pkg/iac/scanners/terraform/parser/funcs/filesystem.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,11 @@ func openFile(target fs.FS, baseDir, path string) (fs.File, error) {
368368
// Trivy uses a virtual file system
369369
path = filepath.ToSlash(path)
370370

371-
if target != nil {
372-
return target.Open(path)
371+
if target == nil {
372+
return nil, fmt.Errorf("open file %q, filesystem is nil", path)
373373
}
374-
return os.Open(path)
374+
375+
return target.Open(path)
375376
}
376377

377378
func readFileBytes(target fs.FS, baseDir, path string) ([]byte, error) {

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ func (p *Parser) ParseFile(_ context.Context, fullPath string) error {
143143

144144
// ParseFS parses a root module, where it exists at the root of the provided filesystem
145145
func (p *Parser) ParseFS(ctx context.Context, dir string) error {
146-
146+
if p.moduleFS == nil {
147+
return errors.New("module filesystem is nil, nothing to parse")
148+
}
147149
dir = path.Clean(dir)
148150

149151
if p.projectRoot == "" {

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

+8
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,14 @@ func TestLoadChildModulesFromLocalCache(t *testing.T) {
24282428
assert.Contains(t, buf.String(), "Using module from Terraform cache .terraform/modules\tsource=\"../level_3\"")
24292429
}
24302430

2431+
func TestNilParser(t *testing.T) {
2432+
parser := New(
2433+
nil, "",
2434+
)
2435+
err := parser.ParseFS(t.Context(), ".")
2436+
require.Error(t, err)
2437+
}
2438+
24312439
func TestLogParseErrors(t *testing.T) {
24322440
var buf bytes.Buffer
24332441
slog.SetDefault(slog.New(log.NewHandler(&buf, nil)))

0 commit comments

Comments
 (0)