ignore target link files based on path (#667)

* ignore target link files based on path

log when files are actually indexed

add test for sym link resolution

golang test nits

Signed-off-by: Jonas Galvão Xavier <jonas.agx@gmail.com>

* nil catalog should act like an empty catalog

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* remove dir path filtering in favor of file type filtering

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* split out addPathToIndex into specialized functions

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add test for nul catalog enumeration

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* conditionally discover MIME types for file based on file resolver index

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* change logging around cataloging

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* add tests to cover possible infinite symlink loop for resolver

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

Co-authored-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Jonas Galvão Xavier
2021-12-13 20:49:11 -05:00
committed by GitHub
co-authored by Alex Goodman
parent 3a10ca7e6a
commit d3804d1a82
23 changed files with 623 additions and 225 deletions
+4
View File
@@ -103,6 +103,10 @@ func (c *Catalog) Enumerate(types ...Type) <-chan Package {
channel := make(chan Package)
go func() {
defer close(channel)
if c == nil {
// we should allow enumerating from a catalog that was never created (which will result in no packages enumerated)
return
}
for ty, ids := range c.idsByType {
if len(types) != 0 {
found := false
+7
View File
@@ -3,6 +3,8 @@ package pkg
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/scylladb/go-set/strset"
"github.com/anchore/syft/syft/source"
@@ -144,3 +146,8 @@ func TestCatalog_PathIndexDeduplicatesRealVsVirtualPaths(t *testing.T) {
}
}
func TestCatalog_EnumerateNilCatalog(t *testing.T) {
var c *Catalog
assert.Empty(t, c.Enumerate())
}
+2 -1
View File
@@ -51,6 +51,7 @@ func Catalog(resolver source.FileResolver, theDistro *distro.Distro, catalogers
var errs error
for _, theCataloger := range catalogers {
// find packages from the underlying raw data
log.Debugf("cataloging with %q", theCataloger.Name())
packages, relationships, err := theCataloger.Catalog(resolver)
if err != nil {
errs = multierror.Append(errs, err)
@@ -59,7 +60,7 @@ func Catalog(resolver source.FileResolver, theDistro *distro.Distro, catalogers
catalogedPackages := len(packages)
log.Debugf("package cataloger %q discovered %d packages", theCataloger.Name(), catalogedPackages)
log.Debugf("discovered %d packages", catalogedPackages)
packagesDiscovered.N += int64(catalogedPackages)
for _, p := range packages {
@@ -208,7 +208,14 @@ func (p PackageJSON) hasNameAndVersionValues() bool {
return p.Name != "" && p.Version != ""
}
func pathContainsNodeModulesDirectory(path string) bool {
isNodeModulesPath, _ := regexp.MatchString("[\\/]node_modules[\\/]", path)
return isNodeModulesPath
// this supports both windows and unix paths
var filepathSeparator = regexp.MustCompile(`[\\/]`)
func pathContainsNodeModulesDirectory(p string) bool {
for _, subPath := range filepathSeparator.Split(p, -1) {
if subPath == "node_modules" {
return true
}
}
return false
}
@@ -6,6 +6,7 @@ import (
"github.com/anchore/syft/syft/pkg"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
)
func TestParsePackageJSON(t *testing.T) {
@@ -160,3 +161,50 @@ func TestParsePackageJSON_Partial(t *testing.T) { // see https://github.com/anch
t.Errorf("no packages should've been returned (but got %d packages)", actualCount)
}
}
func Test_pathContainsNodeModulesDirectory(t *testing.T) {
tests := []struct {
path string
expected bool
}{
// positive
{
path: "something/node_modules/package",
expected: true,
},
{
path: "node_modules/package",
expected: true,
},
{
path: "something/node_modules",
expected: true,
},
{
path: "\\something\\node_modules\\",
expected: true,
},
{
path: "\\something\\node_modules",
expected: true,
},
// negative
{
path: "something/node_bogus_modules",
expected: false,
},
{
path: "something/node_modules_bogus",
expected: false,
},
{
path: "something/node_bogus_modules/package",
expected: false,
},
}
for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
assert.Equal(t, test.expected, pathContainsNodeModulesDirectory(test.path))
})
}
}