allow file metadata digests to be optional + add link destination

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2021-03-29 12:07:42 -04:00
parent 8551168702
commit db35186c7d
8 changed files with 523 additions and 51 deletions
+11 -9
View File
@@ -74,16 +74,18 @@ func (i *DigestsCataloger) catalogLocation(resolver source.FileResolver, locatio
return nil, fmt.Errorf("unable to observe contents of %+v: %+v", location.RealPath, err)
}
if size == 0 {
return make([]Digest, 0), nil
}
result := make([]Digest, len(i.hashes))
if size > 0 {
// only capture digests when there is content. It is important to do this based on SIZE and not
// FILE TYPE. The reasoning is that it is possible for a tar to be crafted with a header-only
// file type but a body is still allowed.
for idx, hasher := range hashers {
result[idx] = Digest{
Algorithm: cleanAlgorithmName(i.hashes[idx].String()),
Value: fmt.Sprintf("%+x", hasher.Sum(nil)),
}
// only capture digests when there is content. It is important to do this based on SIZE and not
// FILE TYPE. The reasoning is that it is possible for a tar to be crafted with a header-only
// file type but a body is still allowed.
for idx, hasher := range hashers {
result[idx] = Digest{
Algorithm: cleanAlgorithmName(i.hashes[idx].String()),
Value: fmt.Sprintf("%+x", hasher.Sum(nil)),
}
}
+98 -6
View File
@@ -7,6 +7,10 @@ import (
"os"
"testing"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/stretchr/testify/assert"
"github.com/anchore/syft/syft/source"
@@ -38,12 +42,13 @@ func testDigests(t testing.TB, files []string, hashes ...crypto.Hash) map[source
return digests
}
func TestDigestsCataloger(t *testing.T) {
files := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
func TestDigestsCataloger_SimpleContents(t *testing.T) {
regularFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
tests := []struct {
name string
algorithms []string
files []string
expected map[source.Location][]Digest
constructorErr bool
catalogErr bool
@@ -51,22 +56,32 @@ func TestDigestsCataloger(t *testing.T) {
{
name: "bad algorithm",
algorithms: []string{"sha-nothing"},
files: regularFiles,
constructorErr: true,
},
{
name: "unsupported algorithm",
algorithms: []string{"sha512"},
files: regularFiles,
constructorErr: true,
},
{
name: "md5-sha1-sha256",
name: "md5",
algorithms: []string{"md5"},
expected: testDigests(t, files, crypto.MD5),
files: regularFiles,
expected: testDigests(t, regularFiles, crypto.MD5),
},
{
name: "md5-sha1-sha256",
algorithms: []string{"md5", "sha1", "sha256"},
expected: testDigests(t, files, crypto.MD5, crypto.SHA1, crypto.SHA256),
files: regularFiles,
expected: testDigests(t, regularFiles, crypto.MD5, crypto.SHA1, crypto.SHA256),
},
{
name: "directory returns error",
algorithms: []string{"md5"},
files: []string{"test-fixtures/last"},
catalogErr: true,
},
}
@@ -81,7 +96,7 @@ func TestDigestsCataloger(t *testing.T) {
return
}
resolver := source.NewMockResolverForPaths(files...)
resolver := source.NewMockResolverForPaths(test.files...)
actual, err := c.Catalog(resolver)
if err != nil && !test.catalogErr {
t.Fatalf("could not catalog (but should have been able to): %+v", err)
@@ -96,3 +111,80 @@ func TestDigestsCataloger(t *testing.T) {
})
}
}
func TestDigestsCataloger_MixFileTypes(t *testing.T) {
testImage := "image-file-type-mix"
if *updateImageGoldenFiles {
imagetest.UpdateGoldenFixtureImage(t, testImage)
}
img := imagetest.GetGoldenFixtureImage(t, testImage)
src, err := source.NewFromImage(img, "---")
if err != nil {
t.Fatalf("could not create source: %+v", err)
}
resolver, err := src.FileResolver(source.SquashedScope)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
tests := []struct {
path string
expected string
}{
{
path: "/file-1.txt",
expected: "888c139e550867814eb7c33b84d76e4d",
},
{
path: "/hardlink-1",
},
{
path: "/symlink-1",
},
{
path: "/char-device-1",
},
{
path: "/block-device-1",
},
{
path: "/fifo-1",
},
{
path: "/bin",
},
}
for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
c, err := NewDigestsCataloger([]string{"md5"})
if err != nil {
t.Fatalf("unable to get cataloger: %+v", err)
}
actual, err := c.Catalog(resolver)
if err != nil {
t.Fatalf("could not catalog: %+v", err)
}
_, ref, err := img.SquashedTree().File(file.Path(test.path))
if err != nil {
t.Fatalf("unable to get file=%q : %+v", test.path, err)
}
l := source.NewLocationFromImage(test.path, *ref, img)
if len(actual[l]) == 0 {
if test.expected != "" {
t.Fatalf("no digest found, but expected one")
}
} else {
assert.Equal(t, actual[l][0].Value, test.expected, "mismatched digests")
}
})
}
}
+15 -13
View File
@@ -50,7 +50,7 @@ func TestFileMetadataCataloger(t *testing.T) {
exists: true,
expected: source.FileMetadata{
Mode: 0644,
Type: "regularFile",
Type: "RegularFile",
UserID: 1,
GroupID: 2,
},
@@ -59,20 +59,22 @@ func TestFileMetadataCataloger(t *testing.T) {
path: "/hardlink-1",
exists: true,
expected: source.FileMetadata{
Mode: 0644,
Type: "hardLink",
UserID: 1,
GroupID: 2,
Mode: 0644,
Type: "HardLink",
LinkDestination: "file-1.txt",
UserID: 1,
GroupID: 2,
},
},
{
path: "/symlink-1",
exists: true,
expected: source.FileMetadata{
Mode: 0777 | os.ModeSymlink,
Type: "symbolicLink",
UserID: 0,
GroupID: 0,
Mode: 0777 | os.ModeSymlink,
Type: "SymbolicLink",
LinkDestination: "file-1.txt",
UserID: 0,
GroupID: 0,
},
},
{
@@ -80,7 +82,7 @@ func TestFileMetadataCataloger(t *testing.T) {
exists: true,
expected: source.FileMetadata{
Mode: 0644 | os.ModeDevice | os.ModeCharDevice,
Type: "characterDevice",
Type: "CharacterDevice",
UserID: 0,
GroupID: 0,
},
@@ -90,7 +92,7 @@ func TestFileMetadataCataloger(t *testing.T) {
exists: true,
expected: source.FileMetadata{
Mode: 0644 | os.ModeDevice,
Type: "blockDevice",
Type: "BlockDevice",
UserID: 0,
GroupID: 0,
},
@@ -100,7 +102,7 @@ func TestFileMetadataCataloger(t *testing.T) {
exists: true,
expected: source.FileMetadata{
Mode: 0644 | os.ModeNamedPipe,
Type: "fifoNode",
Type: "FIFONode",
UserID: 0,
GroupID: 0,
},
@@ -110,7 +112,7 @@ func TestFileMetadataCataloger(t *testing.T) {
exists: true,
expected: source.FileMetadata{
Mode: 0755 | os.ModeDir,
Type: "directory",
Type: "Directory",
UserID: 0,
GroupID: 0,
},
+10 -8
View File
@@ -7,10 +7,11 @@ import (
)
type FileMetadata struct {
Mode os.FileMode
Type FileType
UserID int
GroupID int
Mode os.FileMode
Type FileType
UserID int
GroupID int
LinkDestination string
}
func fileMetadataByLocation(img *image.Image, location Location) (FileMetadata, error) {
@@ -20,9 +21,10 @@ func fileMetadataByLocation(img *image.Image, location Location) (FileMetadata,
}
return FileMetadata{
Mode: entry.Metadata.Mode,
Type: newFileTypeFromTarHeaderTypeFlag(entry.Metadata.TypeFlag),
UserID: entry.Metadata.UserID,
GroupID: entry.Metadata.GroupID,
Mode: entry.Metadata.Mode,
Type: newFileTypeFromTarHeaderTypeFlag(entry.Metadata.TypeFlag),
UserID: entry.Metadata.UserID,
GroupID: entry.Metadata.GroupID,
LinkDestination: entry.Metadata.Linkname,
}, nil
}