From 357c358fb14bbc027bca9b6a4167322f1703036f Mon Sep 17 00:00:00 2001 From: Nikita Pivkin Date: Thu, 9 May 2024 09:06:34 +0600 Subject: [PATCH] refactor(misconf): remove extrafs (#6656) --- pkg/extrafs/extrafs.go | 54 --------------------- pkg/iac/scanners/terraform/parser/parser.go | 24 +-------- pkg/iac/scanners/terraform/scanner.go | 19 +------- 3 files changed, 2 insertions(+), 95 deletions(-) delete mode 100644 pkg/extrafs/extrafs.go diff --git a/pkg/extrafs/extrafs.go b/pkg/extrafs/extrafs.go deleted file mode 100644 index e3956c193bbe..000000000000 --- a/pkg/extrafs/extrafs.go +++ /dev/null @@ -1,54 +0,0 @@ -package extrafs - -import ( - "io/fs" - "os" - "path/filepath" -) - -/* - Go does not currently support symlinks in io/fs. - We work around this by wrapping the fs.FS returned by os.DirFS with our own type which bolts on the ReadLinkFS -*/ - -type OSFS interface { - fs.FS - fs.StatFS -} - -type ReadLinkFS interface { - ResolveSymlink(name, dir string) (string, error) -} - -type FS interface { - OSFS - ReadLinkFS -} - -type filesystem struct { - root string - underlying OSFS -} - -func OSDir(path string) FS { - return &filesystem{ - root: path, - underlying: os.DirFS(path).(OSFS), - } -} - -func (f *filesystem) Open(name string) (fs.File, error) { - return f.underlying.Open(name) -} - -func (f *filesystem) Stat(name string) (fs.FileInfo, error) { - return f.underlying.Stat(name) -} - -func (f *filesystem) ResolveSymlink(name, dir string) (string, error) { - link, err := os.Readlink(filepath.Join(f.root, dir, name)) - if err == nil { - return filepath.Join(dir, link), nil - } - return name, nil -} diff --git a/pkg/iac/scanners/terraform/parser/parser.go b/pkg/iac/scanners/terraform/parser/parser.go index b5b50dc913d7..b7de6dd4ba08 100644 --- a/pkg/iac/scanners/terraform/parser/parser.go +++ b/pkg/iac/scanners/terraform/parser/parser.go @@ -15,7 +15,6 @@ import ( "github.com/hashicorp/hcl/v2/hclparse" "github.com/zclconf/go-cty/cty" - "github.com/aquasecurity/trivy/pkg/extrafs" "github.com/aquasecurity/trivy/pkg/iac/debug" "github.com/aquasecurity/trivy/pkg/iac/ignore" "github.com/aquasecurity/trivy/pkg/iac/scanners/options" @@ -185,28 +184,7 @@ func (p *Parser) ParseFS(ctx context.Context, dir string) error { var paths []string for _, info := range fileInfos { realPath := path.Join(dir, info.Name()) - if info.Type()&os.ModeSymlink != 0 { - extra, ok := p.moduleFS.(extrafs.FS) - if !ok { - // we can't handle symlinks in this fs type for now - p.debug.Log("Cannot resolve symlink '%s' in '%s' for this fs type", info.Name(), dir) - continue - } - realPath, err = extra.ResolveSymlink(info.Name(), dir) - if err != nil { - p.debug.Log("Failed to resolve symlink '%s' in '%s': %s", info.Name(), dir, err) - continue - } - info, err := extra.Stat(realPath) - if err != nil { - p.debug.Log("Failed to stat resolved symlink '%s': %s", realPath, err) - continue - } - if info.IsDir() { - continue - } - p.debug.Log("Resolved symlink '%s' in '%s' to '%s'", info.Name(), dir, realPath) - } else if info.IsDir() { + if info.IsDir() { continue } paths = append(paths, realPath) diff --git a/pkg/iac/scanners/terraform/scanner.go b/pkg/iac/scanners/terraform/scanner.go index f5a3554d002d..1f051a166595 100644 --- a/pkg/iac/scanners/terraform/scanner.go +++ b/pkg/iac/scanners/terraform/scanner.go @@ -11,7 +11,6 @@ import ( "strings" "sync" - "github.com/aquasecurity/trivy/pkg/extrafs" "github.com/aquasecurity/trivy/pkg/iac/debug" "github.com/aquasecurity/trivy/pkg/iac/framework" "github.com/aquasecurity/trivy/pkg/iac/rego" @@ -281,24 +280,8 @@ func (s *Scanner) findModules(target fs.FS, scanDir string, dirs ...string) []st continue } for _, file := range files { - realPath := path.Join(dir, file.Name()) - if symFS, ok := target.(extrafs.ReadLinkFS); ok { - realPath, err = symFS.ResolveSymlink(realPath, scanDir) - if err != nil { - s.debug.Log("failed to resolve symlink '%s': %s", file.Name(), err) - continue - } - } if file.IsDir() { - others = append(others, realPath) - } else if statFS, ok := target.(fs.StatFS); ok { - info, err := statFS.Stat(filepath.ToSlash(realPath)) - if err != nil { - continue - } - if info.IsDir() { - others = append(others, realPath) - } + others = append(others, path.Join(dir, file.Name())) } } }