From 2f378d806efdf95a09463ea99cde62565d44c2ae Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Fri, 15 Dec 2023 14:43:09 -0500 Subject: [PATCH] fix file digest cataloger when passed coordinates (#2436) Signed-off-by: Alex Goodman --- syft/file/cataloger/filedigest/cataloger.go | 6 ++- .../cataloger/filedigest/cataloger_test.go | 44 +++++++++++++++++++ .../cataloger/filemetadata/cataloger_test.go | 1 - 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/syft/file/cataloger/filedigest/cataloger.go b/syft/file/cataloger/filedigest/cataloger.go index 91f719c01..00f193b27 100644 --- a/syft/file/cataloger/filedigest/cataloger.go +++ b/syft/file/cataloger/filedigest/cataloger.go @@ -37,7 +37,11 @@ func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordina locations = intCataloger.AllRegularFiles(resolver) } else { for _, c := range coordinates { - locations = append(locations, file.NewLocationFromCoordinates(c)) + locs, err := resolver.FilesByPath(c.RealPath) + if err != nil { + return nil, fmt.Errorf("unable to get file locations for path %q: %w", c.RealPath, err) + } + locations = append(locations, locs...) } } diff --git a/syft/file/cataloger/filedigest/cataloger_test.go b/syft/file/cataloger/filedigest/cataloger_test.go index 9ebaceed0..417e4b739 100644 --- a/syft/file/cataloger/filedigest/cataloger_test.go +++ b/syft/file/cataloger/filedigest/cataloger_test.go @@ -160,3 +160,47 @@ func TestDigestsCataloger_MixFileTypes(t *testing.T) { }) } } + +func TestFileDigestCataloger_GivenCoordinates(t *testing.T) { + testImage := "image-file-type-mix" + + img := imagetest.GetFixtureImage(t, "docker-archive", testImage) + + c := NewCataloger([]crypto.Hash{crypto.SHA256}) + + src, err := source.NewFromStereoscopeImageObject(img, testImage, nil) + require.NoError(t, err) + + resolver, err := src.FileResolver(source.SquashedScope) + require.NoError(t, err) + + tests := []struct { + path string + exists bool + expected string + }{ + { + path: "/file-1.txt", + exists: true, + expected: "b089629781f05ef805b4511e93717f2ffa4c9d991771d5cbfa4b7242b4ef5fff", + }, + } + + for _, test := range tests { + t.Run(test.path, func(t *testing.T) { + _, ref, err := img.SquashedTree().File(stereoscopeFile.Path(test.path)) + require.NoError(t, err) + + l := file.NewLocationFromImage(test.path, *ref.Reference, img) + + // note: an important difference between this test and the previous is that this test is using a list + // of specific coordinates to catalog + actual, err := c.Catalog(resolver, l.Coordinates) + require.NoError(t, err) + require.Len(t, actual, 1) + + assert.Equal(t, test.expected, actual[l.Coordinates][0].Value, "mismatched digests") + }) + } + +} diff --git a/syft/file/cataloger/filemetadata/cataloger_test.go b/syft/file/cataloger/filemetadata/cataloger_test.go index 8c646e570..48f6b6c5f 100644 --- a/syft/file/cataloger/filemetadata/cataloger_test.go +++ b/syft/file/cataloger/filemetadata/cataloger_test.go @@ -168,7 +168,6 @@ func TestFileMetadataCataloger_GivenCoordinates(t *testing.T) { path string exists bool expected file.Metadata - err bool }{ { path: "/file-1.txt",