Skip to content

Commit

Permalink
change dir resolver to include virtual path (anchore#2259)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Nov 2, 2023
1 parent d97baba commit 82a8a24
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 35 deletions.
2 changes: 1 addition & 1 deletion syft/file/cataloger/internal/all_regular_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Test_allRegularFiles(t *testing.T) {
return r
},
wantRealPaths: strset.New("file1.txt", "nested/file2.txt"),
wantVirtualPaths: strset.New("nested/linked-file1.txt"),
wantVirtualPaths: strset.New("file1.txt", "nested/file2.txt", "nested/linked-file1.txt"),
},
}
for _, tt := range tests {
Expand Down
8 changes: 4 additions & 4 deletions syft/file/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func NewLocation(realPath string) Location {
Coordinates: Coordinates{
RealPath: realPath,
},
VirtualPath: realPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -94,6 +95,7 @@ func NewLocationFromCoordinates(coordinates Coordinates) Location {
return Location{
LocationData: LocationData{
Coordinates: coordinates,
VirtualPath: coordinates.RealPath,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand Down Expand Up @@ -137,7 +139,8 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location
Coordinates: Coordinates{
RealPath: responsePath,
},
ref: ref,
VirtualPath: responsePath,
ref: ref,
},
LocationMetadata: LocationMetadata{
Annotations: map[string]string{},
Expand All @@ -147,9 +150,6 @@ func NewLocationFromDirectory(responsePath string, ref file.Reference) Location

// NewVirtualLocationFromDirectory creates a new Location representing the given path (extracted from the Reference) relative to the given directory with a separate virtual access path.
func NewVirtualLocationFromDirectory(responsePath, virtualResponsePath string, ref file.Reference) Location {
if responsePath == virtualResponsePath {
return NewLocationFromDirectory(responsePath, ref)
}
return Location{
LocationData: LocationData{
Coordinates: Coordinates{
Expand Down
16 changes: 4 additions & 12 deletions syft/internal/fileresolver/container_image_all_layers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ func TestAllLayersResolver_FilesByPath(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")

resolver, err := NewFromContainerImageAllLayers(img)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
require.NoError(t, err)

hasPath := resolver.HasPath(c.linkPath)
if !c.forcePositiveHasPath {
Expand All @@ -109,9 +107,7 @@ func TestAllLayersResolver_FilesByPath(t *testing.T) {
}

refs, err := resolver.FilesByPath(c.linkPath)
if err != nil {
t.Fatalf("could not use resolver: %+v", err)
}
require.NoError(t, err)

if len(refs) != len(c.resolutions) {
t.Fatalf("unexpected number of resolutions: %d", len(refs))
Expand Down Expand Up @@ -207,14 +203,10 @@ func TestAllLayersResolver_FilesByGlob(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")

resolver, err := NewFromContainerImageAllLayers(img)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
require.NoError(t, err)

refs, err := resolver.FilesByGlob(c.glob)
if err != nil {
t.Fatalf("could not use resolver: %+v", err)
}
require.NoError(t, err)

if len(refs) != len(c.resolutions) {
t.Fatalf("unexpected number of resolutions: %d", len(refs))
Expand Down
16 changes: 4 additions & 12 deletions syft/internal/fileresolver/container_image_squash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ func TestImageSquashResolver_FilesByPath(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")

resolver, err := NewFromContainerImageSquash(img)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
require.NoError(t, err)

hasPath := resolver.HasPath(c.linkPath)
if !c.forcePositiveHasPath {
Expand All @@ -90,9 +88,7 @@ func TestImageSquashResolver_FilesByPath(t *testing.T) {
}

refs, err := resolver.FilesByPath(c.linkPath)
if err != nil {
t.Fatalf("could not use resolver: %+v", err)
}
require.NoError(t, err)

expectedRefs := 1
if c.resolvePath == "" {
Expand Down Expand Up @@ -187,14 +183,10 @@ func TestImageSquashResolver_FilesByGlob(t *testing.T) {
img := imagetest.GetFixtureImage(t, "docker-archive", "image-symlinks")

resolver, err := NewFromContainerImageSquash(img)
if err != nil {
t.Fatalf("could not create resolver: %+v", err)
}
require.NoError(t, err)

refs, err := resolver.FilesByGlob(c.glob)
if err != nil {
t.Fatalf("could not use resolver: %+v", err)
}
require.NoError(t, err)

expectedRefs := 1
if c.resolvePath == "" {
Expand Down
3 changes: 2 additions & 1 deletion syft/internal/fileresolver/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ func (r *Directory) FilesByMIMEType(types ...string) ([]file.Location, error) {
if uniqueFileIDs.Contains(*refVia.Reference) {
continue
}
location := file.NewLocationFromDirectory(
location := file.NewVirtualLocationFromDirectory(
r.responsePath(string(refVia.Reference.RealPath)),
r.responsePath(string(refVia.RequestPath)),
*refVia.Reference,
)
uniqueFileIDs.Add(*refVia.Reference)
Expand Down
5 changes: 4 additions & 1 deletion syft/internal/fileresolver/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestDirectoryResolver_FilesByPath_request_response(t *testing.T) {
base string
input string
expectedRealPath string
expectedVirtualPath string
expectedVirtualPath string // note: if empty it will be assumed to match the expectedRealPath
}{
{
name: "relative root, relative request, direct",
Expand Down Expand Up @@ -489,6 +489,9 @@ func TestDirectoryResolver_FilesByPath_request_response(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.expectedVirtualPath == "" {
c.expectedVirtualPath = c.expectedRealPath
}

// we need to mimic a shell, otherwise we won't get a path within a symlink
targetPath := filepath.Join(testDir, c.cwd)
Expand Down
3 changes: 2 additions & 1 deletion syft/internal/fileresolver/unindexed_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,14 @@ func (u UnindexedDirectory) Write(location file.Location, reader io.Reader) erro
func (u UnindexedDirectory) newLocation(filePath string, resolveLinks bool) *file.Location {
filePath = path.Clean(filePath)

virtualPath := ""
virtualPath := filePath
realPath := filePath

if resolveLinks {
paths := u.resolveLinks(filePath)
if len(paths) > 1 {
realPath = paths[len(paths)-1]
// TODO: this is not quite correct, as the equivalent of os.EvalSymlinks needs to be done (in the context of afero)
if realPath != path.Clean(filePath) {
virtualPath = paths[0]
}
Expand Down
9 changes: 6 additions & 3 deletions syft/internal/fileresolver/unindexed_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Test_UnindexDirectoryResolver_FilesByPath_request_response(t *testing.T) {
base string
input string
expectedRealPath string
expectedVirtualPath string
expectedVirtualPath string // if empty, the virtual path should be the same as the real path
}{
{
name: "relative root, relative request, direct",
Expand Down Expand Up @@ -498,6 +498,9 @@ func Test_UnindexDirectoryResolver_FilesByPath_request_response(t *testing.T) {
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.expectedVirtualPath == "" {
c.expectedVirtualPath = c.expectedRealPath
}

// we need to mimic a shell, otherwise we won't get a path within a symlink
targetPath := filepath.Join(testDir, c.cwd)
Expand Down Expand Up @@ -1090,7 +1093,7 @@ func Test_UnindexedDirectoryResolver_resolvesLinks(t *testing.T) {
file.NewLocation("file-1.txt"),
file.NewLocation("file-2.txt"),
file.NewLocation("file-3.txt"),
file.NewLocation("parent/file-4.txt"),
file.NewVirtualLocation("parent/file-4.txt", "parent-link/file-4.txt"),
},
},
{
Expand Down Expand Up @@ -1120,7 +1123,7 @@ func Test_UnindexedDirectoryResolver_resolvesLinks(t *testing.T) {
file.NewLocation("file-1.txt"),
file.NewLocation("file-2.txt"),
file.NewLocation("file-3.txt"),
file.NewLocation("parent/file-4.txt"),
file.NewVirtualLocation("parent/file-4.txt", "parent-link/file-4.txt"),
},
},
{
Expand Down

0 comments on commit 82a8a24

Please sign in to comment.