Add layer information for go-module-binary-cataloger (#620)

* update functions to pass Location

Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>

* update unit tests to pass new locations

Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>

* fix image source.FileResolvers to include layer info

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

* add non-empty location in golang binary cataloger testing

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

Co-authored-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Christopher Angelo Phillips 2021-11-08 16:05:53 -05:00 committed by GitHub
parent bb0f35bac4
commit 9bbc9ff633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 23 deletions

View File

@ -48,7 +48,7 @@ func (c *Cataloger) Catalog(resolver source.FileResolver) ([]pkg.Package, error)
return pkgs, fmt.Errorf("failed to resolve file contents by location: %w", err)
}
goPkgs, err := parseGoBin(location.RealPath, r)
goPkgs, err := parseGoBin(location, r)
if err != nil {
log.Warnf("could not parse possible go binary: %+v", err)
}

View File

@ -14,7 +14,7 @@ const (
replaceIdentifier = "=>"
)
func parseGoBin(path string, reader io.ReadCloser) ([]pkg.Package, error) {
func parseGoBin(location source.Location, reader io.ReadCloser) ([]pkg.Package, error) {
// Identify if bin was compiled by go
x, err := openExe(reader)
if err != nil {
@ -23,12 +23,12 @@ func parseGoBin(path string, reader io.ReadCloser) ([]pkg.Package, error) {
goVersion, mod := findVers(x)
pkgs := buildGoPkgInfo(path, mod, goVersion)
pkgs := buildGoPkgInfo(location, mod, goVersion)
return pkgs, nil
}
func buildGoPkgInfo(path, mod, goVersion string) []pkg.Package {
func buildGoPkgInfo(location source.Location, mod, goVersion string) []pkg.Package {
pkgsSlice := make([]pkg.Package, 0)
scanner := bufio.NewScanner(strings.NewReader(mod))
@ -48,9 +48,7 @@ func buildGoPkgInfo(path, mod, goVersion string) []pkg.Package {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{
RealPath: path,
},
location,
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{

View File

@ -33,7 +33,10 @@ func TestBuildGoPkgInfo(t *testing.T) {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{},
{
RealPath: "/a-path",
FileSystemID: "layer-id",
},
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{
@ -47,7 +50,10 @@ func TestBuildGoPkgInfo(t *testing.T) {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{},
{
RealPath: "/a-path",
FileSystemID: "layer-id",
},
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{
@ -72,7 +78,10 @@ func TestBuildGoPkgInfo(t *testing.T) {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{},
{
RealPath: "/a-path",
FileSystemID: "layer-id",
},
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{
@ -86,7 +95,10 @@ func TestBuildGoPkgInfo(t *testing.T) {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{},
{
RealPath: "/a-path",
FileSystemID: "layer-id",
},
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{
@ -100,7 +112,10 @@ func TestBuildGoPkgInfo(t *testing.T) {
Language: pkg.Go,
Type: pkg.GoModulePkg,
Locations: []source.Location{
{},
{
RealPath: "/a-path",
FileSystemID: "layer-id",
},
},
MetadataType: pkg.GolangBinMetadataType,
Metadata: pkg.GolangBinMetadata{
@ -115,7 +130,8 @@ func TestBuildGoPkgInfo(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
pkgs := buildGoPkgInfo("", tt.mod, goCompiledVersion)
location := source.Location{RealPath: "/a-path", FileSystemID: "layer-id"}
pkgs := buildGoPkgInfo(location, tt.mod, goCompiledVersion)
assert.Equal(t, tt.expected, pkgs)
})
}

View File

@ -198,7 +198,7 @@ func (r *allLayersResolver) FilesByMIMEType(types ...string) ([]Location, error)
}
for _, ref := range refs {
locations = append(locations, NewLocationFromReference(ref))
locations = append(locations, NewLocationFromImage(string(ref.RealPath), ref, r.img))
}
}

View File

@ -272,3 +272,32 @@ func Test_imageAllLayersResolver_FilesByMIMEType(t *testing.T) {
})
}
}
func Test_imageAllLayersResolver_hasFilesystemIDInLocation(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-duplicate-path")
resolver, err := newAllLayersResolver(img)
assert.NoError(t, err)
locations, err := resolver.FilesByMIMEType("text/plain")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
locations, err = resolver.FilesByGlob("*.txt")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
locations, err = resolver.FilesByPath("/somefile-1.txt")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
}

View File

@ -159,7 +159,7 @@ func (r *imageSquashResolver) FilesByMIMEType(types ...string) ([]Location, erro
var locations []Location
for _, ref := range refs {
locations = append(locations, NewLocationFromReference(ref))
locations = append(locations, NewLocationFromImage(string(ref.RealPath), ref, r.img))
}
return locations, nil

View File

@ -259,3 +259,32 @@ func Test_imageSquashResolver_FilesByMIMEType(t *testing.T) {
})
}
}
func Test_imageSquashResolver_hasFilesystemIDInLocation(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-duplicate-path")
resolver, err := newImageSquashResolver(img)
assert.NoError(t, err)
locations, err := resolver.FilesByMIMEType("text/plain")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
locations, err = resolver.FilesByGlob("*.txt")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
locations, err = resolver.FilesByPath("/somefile-1.txt")
assert.NoError(t, err)
assert.NotEmpty(t, locations)
for _, location := range locations {
assert.NotEmpty(t, location.FileSystemID)
}
}

View File

@ -53,14 +53,6 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
}
}
func NewLocationFromReference(ref file.Reference) Location {
return Location{
VirtualPath: string(ref.RealPath),
RealPath: string(ref.RealPath),
ref: ref,
}
}
func (l Location) String() string {
str := ""
if l.ref.ID() != 0 {