Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(misconf): terraform relative paths #4571

Merged
merged 8 commits into from
Jun 8, 2023
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
7 changes: 3 additions & 4 deletions pkg/fanal/analyzer/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/fs"
"os"
"path"
"path/filepath"

"golang.org/x/xerrors"

Expand Down Expand Up @@ -65,14 +64,14 @@ func (c *CompositeFS) CopyFileToTemp(opener Opener, info os.FileInfo) (string, e

// CreateLink creates a link in the virtual filesystem that corresponds to a real file.
// The linked virtual file will have the same path as the real file path provided.
func (c *CompositeFS) CreateLink(analyzerTypes []Type, virtualPath, realPath string, setRoot bool) error {
func (c *CompositeFS) CreateLink(analyzerTypes []Type, rootDir, virtualPath, realPath string) error {
// Create fs.FS for each post-analyzer that wants to analyze the current file
for _, t := range analyzerTypes {
// Since filesystem scanning may require access outside the specified path, (e.g. Terraform modules)
// it allows "../" access with "WithUnderlyingRoot".
var opts []mapfs.Option
if setRoot {
opts = append(opts, mapfs.WithUnderlyingRoot(filepath.Dir(realPath)))
if rootDir != "" {
opts = append(opts, mapfs.WithUnderlyingRoot(rootDir))
}
mfs, _ := c.files.LoadOrStore(t, mapfs.New(opts...))
if d := path.Dir(virtualPath); d != "." {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/artifact/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func (a Artifact) inspectLayer(ctx context.Context, layerInfo LayerInfo, disable
if err != nil {
return xerrors.Errorf("failed to copy file to temp: %w", err)
}
if err = composite.CreateLink(analyzerTypes, filePath, tmpFilePath, false); err != nil {
if err = composite.CreateLink(analyzerTypes, "", filePath, tmpFilePath); err != nil {
return xerrors.Errorf("failed to write a file: %w", err)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/fanal/artifact/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/json"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -52,7 +53,7 @@ func NewArtifact(rootPath string, c cache.ArtifactCache, opt artifact.Option) (a
}

return Artifact{
rootPath: filepath.Clean(rootPath),
rootPath: filepath.ToSlash(filepath.Clean(rootPath)),
cache: c,
walker: walker.NewFS(buildPathsToSkip(rootPath, opt.SkipFiles), buildPathsToSkip(rootPath, opt.SkipDirs),
opt.Slow, opt.WalkOption.ErrorCallback),
Expand Down Expand Up @@ -139,7 +140,7 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
// When the directory is the same as the filePath, a file was given
// instead of a directory, rewrite the file path and directory in this case.
if filePath == "." {
dir, filePath = filepath.Split(a.rootPath)
dir, filePath = path.Split(a.rootPath)
}

if err = a.analyzer.AnalyzeFile(ctx, &wg, limit, result, dir, filePath, info, opener, nil, opts); err != nil {
Expand All @@ -153,7 +154,7 @@ func (a Artifact) Inspect(ctx context.Context) (types.ArtifactReference, error)
}

// Build filesystem for post analysis
if err = composite.CreateLink(analyzerTypes, filePath, filepath.Join(dir, filePath), true); err != nil {
if err = composite.CreateLink(analyzerTypes, dir, filePath, filepath.Join(dir, filePath)); err != nil {
return xerrors.Errorf("failed to create link: %w", err)
}

Expand Down
Loading