Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/syft/internal/commands/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ func generateSBOMForAttestation(ctx context.Context, id clio.Identification, opt
}

src, err := getSource(ctx, opts, userInput, stereoscope.RegistryTag)

if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion cmd/syft/internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ func runScan(ctx context.Context, id clio.Identification, opts *scanOptions, use
}

src, err := getSource(ctx, &opts.Catalog, userInput, sources...)

if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func BenchmarkImagePackageCatalogers(b *testing.B) {
// get the source object for the image
theSource, err := syft.GetSource(context.Background(), tarPath, syft.DefaultGetSourceConfig().WithSources("docker-archive"))
require.NoError(b, err)

b.Cleanup(func() {
require.NoError(b, theSource.Close())
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/syft/internal/test/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func catalogFixtureImageWithConfig(t *testing.T, fixtureImageName string, cfg *s
// get the source to build an SBOM against
theSource, err := syft.GetSource(context.Background(), tarPath, syft.DefaultGetSourceConfig().WithSources("docker-archive"))
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, theSource.Close())
})

// build the SBOM
s, err := syft.CreateSBOM(context.Background(), theSource, cfg)

require.NoError(t, err)
Expand All @@ -66,7 +66,7 @@ func catalogDirectory(t *testing.T, dir string, catalogerSelection ...string) (s
func catalogDirectoryWithConfig(t *testing.T, dir string, cfg *syft.CreateSBOMConfig) (sbom.SBOM, source.Source) {
cfg.CatalogerSelection = cfg.CatalogerSelection.WithDefaults(pkgcataloging.DirectoryTag)

// get the source to build an sbom against
// get the source to build an SBOM against
theSource, err := syft.GetSource(context.Background(), dir, syft.DefaultGetSourceConfig().WithSources("dir"))
require.NoError(t, err)
t.Cleanup(func() {
Expand Down
2 changes: 1 addition & 1 deletion examples/create_custom_sbom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const defaultImage = "alpine:3.19"
func main() {
// automagically get a source.Source for arbitrary string input
src := getSource(imageReference())
defer src.Close()

// will catalog the given source and return a SBOM keeping in mind several configurable options
sbom := getSBOM(src)
Expand All @@ -46,7 +47,6 @@ func getSource(input string) source.Source {
fmt.Println("detecting source type for input:", input, "...")

src, err := syft.GetSource(context.Background(), input, nil)

if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/create_simple_sbom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultImage = "alpine:3.19"
func main() {
// automagically get a source.Source for arbitrary string input
src := getSource(imageReference())
defer src.Close()

// catalog the given source and return a SBOM
sbom := getSBOM(src)
Expand All @@ -40,7 +41,6 @@ func imageReference() string {

func getSource(input string) source.Source {
src, err := syft.GetSource(context.Background(), input, nil)

if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/select_catalogers/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const defaultImage = "alpine:3.19"
func main() {
// automagically get a source.Source for arbitrary string input
src := getSource(imageReference())
defer src.Close()

// catalog the given source and return a SBOM
// let's explicitly use catalogers that are:
Expand All @@ -44,7 +45,6 @@ func imageReference() string {

func getSource(input string) source.Source {
src, err := syft.GetSource(context.Background(), input, nil)

if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions examples/source_from_registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
image := "alpine:3.19"

src, _ := syft.GetSource(context.Background(), image, syft.DefaultGetSourceConfig().WithSources("registry"))
defer src.Close()

sbom, _ := syft.CreateSBOM(context.Background(), src, syft.DefaultCreateSBOMConfig())

Expand Down
4 changes: 4 additions & 0 deletions internal/os/feature_detection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func Test_EnvironmentTask(t *testing.T) {
// get the source
theSource, err := syft.GetSource(context.Background(), tarPath, syft.DefaultGetSourceConfig().WithSources("docker-archive"))
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, theSource.Close())
})

resolver, err := theSource.FileResolver(source.SquashedScope)
require.NoError(t, err)

Expand Down
10 changes: 5 additions & 5 deletions syft/internal/fileresolver/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ type Directory struct {
indexer *directoryIndexer
}

func NewFromDirectory(root string, base string, pathFilters ...PathIndexVisitor) (*Directory, error) {
r, err := newFromDirectoryWithoutIndex(root, base, pathFilters...)
func NewFromDirectory(root, base string, pathFilters ...PathIndexVisitor) (*Directory, error) {
resolver, err := newFromDirectoryWithoutIndex(root, base, pathFilters...)
if err != nil {
return nil, err
}

return r, r.buildIndex()
return resolver, resolver.buildIndex()
}

func newFromDirectoryWithoutIndex(root string, base string, pathFilters ...PathIndexVisitor) (*Directory, error) {
func newFromDirectoryWithoutIndex(root, base string, pathFilters ...PathIndexVisitor) (*Directory, error) {
chroot, err := NewChrootContextFromCWD(root, base)
if err != nil {
return nil, fmt.Errorf("unable to interpret chroot context: %w", err)
Expand Down Expand Up @@ -66,6 +66,6 @@ func (r *Directory) buildIndex() error {
}

// Stringer to represent a directory path data source
func (r Directory) String() string {
func (r *Directory) String() string {
return fmt.Sprintf("dir:%s", r.path)
}
28 changes: 20 additions & 8 deletions syft/internal/fileresolver/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,31 @@ type File struct {
indexer *fileIndexer
}

// parent should be the symlink free absolute path to the parent directory
// NewFromFile single file analyser
// path is the filepath of the file we're creating content access for
func NewFromFile(parent, path string, pathFilters ...PathIndexVisitor) (*File, error) {
chroot, err := NewChrootContextFromCWD(parent, parent)
func NewFromFile(path string, pathFilters ...PathIndexVisitor) (*File, error) {
resolver, err := newFromFileWithoutIndex(path, pathFilters...)
if err != nil {
return nil, err
}

return resolver, resolver.buildIndex()
}

func newFromFileWithoutIndex(path string, pathFilters ...PathIndexVisitor) (*File, error) {
absParentDir, err := absoluteSymlinkFreePathToParent(path)
if err != nil {
return nil, err
}

chroot, err := NewChrootContextFromCWD(absParentDir, absParentDir)
if err != nil {
return nil, fmt.Errorf("unable to interpret chroot context: %w", err)
}

cleanBase := chroot.Base()

file := &File{
return &File{
path: path,
FiletreeResolver: FiletreeResolver{
Chroot: *chroot,
Expand All @@ -36,9 +50,7 @@ func NewFromFile(parent, path string, pathFilters ...PathIndexVisitor) (*File, e
Opener: nativeOSFileOpener,
},
indexer: newFileIndexer(path, cleanBase, pathFilters...),
}

return file, file.buildIndex()
}, nil
}

func (r *File) buildIndex() error {
Expand All @@ -58,6 +70,6 @@ func (r *File) buildIndex() error {
}

// Stringer to represent a file path data source
func (r File) String() string {
func (r *File) String() string {
return fmt.Sprintf("file:%s", r.path)
}
41 changes: 31 additions & 10 deletions syft/internal/fileresolver/filetree_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1384,9 +1384,10 @@ func TestFileResolver_FilesByPath(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, parentPath)

resolver, err := NewFromFile(parentPath, tt.filePath)
resolver, err := NewFromFile(tt.filePath)
require.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

refs, err := resolver.FilesByPath(tt.fileByPathInput)
require.NoError(t, err)
Expand Down Expand Up @@ -1431,8 +1432,11 @@ func TestFileResolver_MultipleFilesByPath(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, parentPath)

resolver, err := NewFromFile(parentPath, filePath)
resolver, err := NewFromFile(filePath)
assert.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

refs, err := resolver.FilesByPath(tt.input...)
assert.NoError(t, err)

Expand All @@ -1449,8 +1453,11 @@ func TestFileResolver_FilesByGlob(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, parentPath)

resolver, err := NewFromFile(parentPath, filePath)
resolver, err := NewFromFile(filePath)
assert.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

refs, err := resolver.FilesByGlob("**/*.txt")
assert.NoError(t, err)

Expand All @@ -1476,8 +1483,11 @@ func Test_fileResolver_FilesByMIMEType(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, parentPath)

resolver, err := NewFromFile(parentPath, filePath)
resolver, err := NewFromFile(filePath)
assert.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

locations, err := resolver.FilesByMIMEType(test.mimeType)
assert.NoError(t, err)
assert.Equal(t, test.expectedPaths.Size(), len(locations))
Expand All @@ -1497,10 +1507,12 @@ func Test_fileResolver_FileContentsByLocation(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, parentPath)

r, err := NewFromFile(parentPath, filePath)
resolver, err := NewFromFile(filePath)
require.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

exists, existingPath, err := r.Tree.File(stereoscopeFile.Path(filepath.Join(cwd, "test-fixtures/image-simple/file-1.txt")))
exists, existingPath, err := resolver.Tree.File(stereoscopeFile.Path(filepath.Join(cwd, "test-fixtures/image-simple/file-1.txt")))
require.True(t, exists)
require.NoError(t, err)
require.True(t, existingPath.HasReference())
Expand All @@ -1525,7 +1537,7 @@ func Test_fileResolver_FileContentsByLocation(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {

actual, err := r.FileContentsByLocation(test.location)
actual, err := resolver.FileContentsByLocation(test.location)
if test.err {
require.Error(t, err)
return
Expand All @@ -1546,8 +1558,11 @@ func TestFileResolver_AllLocations_errorOnDirRequest(t *testing.T) {
parentPath, err := absoluteSymlinkFreePathToParent(filePath)
require.NoError(t, err)
require.NotNil(t, parentPath)
resolver, err := NewFromFile(parentPath, filePath)

resolver, err := NewFromFile(filePath)
require.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

var dirLoc *file.Location
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -1575,8 +1590,11 @@ func TestFileResolver_AllLocations(t *testing.T) {
parentPath, err := absoluteSymlinkFreePathToParent(filePath)
require.NoError(t, err)
require.NotNil(t, parentPath)
resolver, err := NewFromFile(parentPath, filePath)

resolver, err := NewFromFile(filePath)
require.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

paths := strset.New()
for loc := range resolver.AllLocations(context.Background()) {
Expand All @@ -1600,8 +1618,11 @@ func Test_FileResolver_AllLocationsDoesNotLeakGoRoutine(t *testing.T) {
parentPath, err := absoluteSymlinkFreePathToParent(filePath)
require.NoError(t, err)
require.NotNil(t, parentPath)
resolver, err := NewFromFile(parentPath, filePath)

resolver, err := NewFromFile(filePath)
require.NoError(t, err)
require.NotNil(t, resolver)
assert.Equal(t, resolver.Chroot.Base(), parentPath)

require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func Test_noSQLiteDriverError(t *testing.T) {
// this test package does must not import the sqlite library
file := "../test-fixtures/Packages"
resolver, err := fileresolver.NewFromFile(file, file)
resolver, err := fileresolver.NewFromFile(file)
require.NoError(t, err)

cataloger := redhat.NewDBCataloger()
Expand Down
Loading
Loading