Migrate location-related structs to the file package (#1751)

* migrate location structs to file package

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

* replace source.Location refs with file package call

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

* fix linting

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

* remove hardlink test for file based catalogers

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

* remove hardlink test for all-regular-files testing

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

* migrate file resolver implementations to separate package

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

* fix linting

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

* [wip] migrate resolvers to internal

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

* migrate resolvers to syft/internal

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

---------

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
Signed-off-by: <>
This commit is contained in:
Alex Goodman
2023-05-24 17:06:38 -04:00
committed by GitHub
parent 4bf17a94b9
commit 07e76907f6
313 changed files with 2316 additions and 2191 deletions
+3 -3
View File
@@ -3,8 +3,8 @@ package binary
import (
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
const catalogerName = "binary-cataloger"
@@ -27,7 +27,7 @@ func (c Cataloger) Name() string {
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages
// after analyzing the catalog source.
func (c Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, []artifact.Relationship, error) {
func (c Cataloger) Catalog(resolver file.Resolver) ([]pkg.Package, []artifact.Relationship, error) {
var packages []pkg.Package
var relationships []artifact.Relationship
@@ -68,7 +68,7 @@ func mergePackages(target *pkg.Package, extra *pkg.Package) {
target.Metadata = meta
}
func catalog(resolver source.FileResolver, cls classifier) (packages []pkg.Package, err error) {
func catalog(resolver file.Resolver, cls classifier) (packages []pkg.Package, err error) {
locations, err := resolver.FilesByGlob(cls.FileGlob)
if err != nil {
return nil, err
+21 -20
View File
@@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
@@ -728,12 +729,12 @@ func TestClassifierCataloger_DefaultClassifiers_NegativeCases(t *testing.T) {
assert.Equal(t, 0, len(actualResults))
}
func locations(locations ...string) source.LocationSet {
var locs []source.Location
func locations(locations ...string) file.LocationSet {
var locs []file.Location
for _, s := range locations {
locs = append(locs, source.NewLocation(s))
locs = append(locs, file.NewLocation(s))
}
return source.NewLocationSet(locs...)
return file.NewLocationSet(locs...)
}
// metadata paths are: realPath, virtualPath
@@ -757,8 +758,8 @@ func match(classifier string, paths ...string) pkg.ClassifierMatch {
}
return pkg.ClassifierMatch{
Classifier: classifier,
Location: source.NewVirtualLocationFromCoordinates(
source.Coordinates{
Location: file.NewVirtualLocationFromCoordinates(
file.Coordinates{
RealPath: realPath,
},
virtualPath,
@@ -817,10 +818,10 @@ func assertPackagesAreEqual(t *testing.T, expected pkg.Package, p pkg.Package) {
if len(failMessages) > 0 {
assert.Failf(t, strings.Join(failMessages, "; "), "diff: %s",
cmp.Diff(expected, p,
cmp.Transformer("Locations", func(l source.LocationSet) []source.Location {
cmp.Transformer("Locations", func(l file.LocationSet) []file.Location {
return l.ToSlice()
}),
cmpopts.IgnoreUnexported(pkg.Package{}, source.Location{}),
cmpopts.IgnoreUnexported(pkg.Package{}, file.Location{}),
cmpopts.IgnoreFields(pkg.Package{}, "CPEs", "FoundBy", "MetadataType", "Type"),
))
}
@@ -830,22 +831,22 @@ type panicyResolver struct {
searchCalled bool
}
func (p *panicyResolver) FilesByExtension(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByExtension(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) FilesByBasename(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByBasename(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) FilesByBasenameGlob(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByBasenameGlob(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) FileContentsByLocation(_ source.Location) (io.ReadCloser, error) {
func (p *panicyResolver) FileContentsByLocation(_ file.Location) (io.ReadCloser, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
@@ -854,34 +855,34 @@ func (p *panicyResolver) HasPath(_ string) bool {
return true
}
func (p *panicyResolver) FilesByPath(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByPath(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) FilesByGlob(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByGlob(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) FilesByMIMEType(_ ...string) ([]source.Location, error) {
func (p *panicyResolver) FilesByMIMEType(_ ...string) ([]file.Location, error) {
p.searchCalled = true
return nil, errors.New("not implemented")
}
func (p *panicyResolver) RelativeFileByPath(_ source.Location, _ string) *source.Location {
func (p *panicyResolver) RelativeFileByPath(_ file.Location, _ string) *file.Location {
return nil
}
func (p *panicyResolver) AllLocations() <-chan source.Location {
func (p *panicyResolver) AllLocations() <-chan file.Location {
return nil
}
func (p *panicyResolver) FileMetadataByLocation(_ source.Location) (source.FileMetadata, error) {
return source.FileMetadata{}, errors.New("not implemented")
func (p *panicyResolver) FileMetadataByLocation(_ file.Location) (file.Metadata, error) {
return file.Metadata{}, errors.New("not implemented")
}
var _ source.FileResolver = (*panicyResolver)(nil)
var _ file.Resolver = (*panicyResolver)(nil)
func Test_Cataloger_ResilientToErrors(t *testing.T) {
c := NewCataloger()
+9 -9
View File
@@ -15,9 +15,9 @@ import (
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/cpe"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/internal/unionreader"
"github.com/anchore/syft/syft/source"
)
var emptyPURL = packageurl.PackageURL{}
@@ -53,10 +53,10 @@ type classifier struct {
}
// evidenceMatcher is a function called to catalog Packages that match some sort of evidence
type evidenceMatcher func(resolver source.FileResolver, classifier classifier, location source.Location) ([]pkg.Package, error)
type evidenceMatcher func(resolver file.Resolver, classifier classifier, location file.Location) ([]pkg.Package, error)
func evidenceMatchers(matchers ...evidenceMatcher) evidenceMatcher {
return func(resolver source.FileResolver, classifier classifier, location source.Location) ([]pkg.Package, error) {
return func(resolver file.Resolver, classifier classifier, location file.Location) ([]pkg.Package, error) {
for _, matcher := range matchers {
match, err := matcher(resolver, classifier, location)
if err != nil {
@@ -72,7 +72,7 @@ func evidenceMatchers(matchers ...evidenceMatcher) evidenceMatcher {
func fileNameTemplateVersionMatcher(fileNamePattern string, contentTemplate string) evidenceMatcher {
pat := regexp.MustCompile(fileNamePattern)
return func(resolver source.FileResolver, classifier classifier, location source.Location) ([]pkg.Package, error) {
return func(resolver file.Resolver, classifier classifier, location file.Location) ([]pkg.Package, error) {
if !pat.MatchString(location.RealPath) {
return nil, nil
}
@@ -118,7 +118,7 @@ func fileNameTemplateVersionMatcher(fileNamePattern string, contentTemplate stri
func fileContentsVersionMatcher(pattern string) evidenceMatcher {
pat := regexp.MustCompile(pattern)
return func(resolver source.FileResolver, classifier classifier, location source.Location) ([]pkg.Package, error) {
return func(resolver file.Resolver, classifier classifier, location file.Location) ([]pkg.Package, error) {
contents, err := getContents(resolver, location)
if err != nil {
return nil, fmt.Errorf("unable to get read contents for file: %w", err)
@@ -138,7 +138,7 @@ func fileContentsVersionMatcher(pattern string) evidenceMatcher {
//nolint:gocognit
func sharedLibraryLookup(sharedLibraryPattern string, sharedLibraryMatcher evidenceMatcher) evidenceMatcher {
pat := regexp.MustCompile(sharedLibraryPattern)
return func(resolver source.FileResolver, classifier classifier, location source.Location) (packages []pkg.Package, _ error) {
return func(resolver file.Resolver, classifier classifier, location file.Location) (packages []pkg.Package, _ error) {
libs, err := sharedLibraries(resolver, location)
if err != nil {
return nil, err
@@ -159,7 +159,7 @@ func sharedLibraryLookup(sharedLibraryPattern string, sharedLibraryMatcher evide
}
for _, p := range pkgs {
// set the source binary as the first location
locationSet := source.NewLocationSet(location)
locationSet := file.NewLocationSet(location)
locationSet.Add(p.Locations.ToSlice()...)
p.Locations = locationSet
meta, _ := p.Metadata.(pkg.BinaryMetadata)
@@ -187,7 +187,7 @@ func mustPURL(purl string) packageurl.PackageURL {
return p
}
func getContents(resolver source.FileResolver, location source.Location) ([]byte, error) {
func getContents(resolver file.Resolver, location file.Location) ([]byte, error) {
reader, err := resolver.FileContentsByLocation(location)
if err != nil {
return nil, err
@@ -216,7 +216,7 @@ func singleCPE(cpeString string) []cpe.CPE {
// sharedLibraries returns a list of all shared libraries found within a binary, currently
// supporting: elf, macho, and windows pe
func sharedLibraries(resolver source.FileResolver, location source.Location) ([]string, error) {
func sharedLibraries(resolver file.Resolver, location file.Location) ([]string, error) {
contents, err := getContents(resolver, location)
if err != nil {
return nil, err
+5 -5
View File
@@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/anchore/syft/syft/cpe"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/syft/file"
)
func Test_ClassifierCPEs(t *testing.T) {
@@ -63,12 +63,12 @@ func Test_ClassifierCPEs(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
resolver := source.NewMockResolverForPaths(test.fixture)
locations, err := resolver.FilesByPath(test.fixture)
resolver := file.NewMockResolverForPaths(test.fixture)
ls, err := resolver.FilesByPath(test.fixture)
require.NoError(t, err)
require.Len(t, locations, 1)
require.Len(t, ls, 1)
pkgs, err := test.classifier.EvidenceMatcher(resolver, test.classifier, locations[0])
pkgs, err := test.classifier.EvidenceMatcher(resolver, test.classifier, ls[0])
require.NoError(t, err)
require.Len(t, pkgs, 1)
+3 -3
View File
@@ -4,11 +4,11 @@ import (
"reflect"
"github.com/anchore/syft/syft/cpe"
"github.com/anchore/syft/syft/file"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)
func newPackage(classifier classifier, location source.Location, matchMetadata map[string]string) *pkg.Package {
func newPackage(classifier classifier, location file.Location, matchMetadata map[string]string) *pkg.Package {
version, ok := matchMetadata["version"]
if !ok {
return nil
@@ -26,7 +26,7 @@ func newPackage(classifier classifier, location source.Location, matchMetadata m
p := pkg.Package{
Name: classifier.Package,
Version: version,
Locations: source.NewLocationSet(
Locations: file.NewLocationSet(
location.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation),
),
Type: pkg.BinaryPkg,