mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 22:12:28 +01:00
with nested cataloging on, java leaves root-level archives to the archive task, so a root `app.jar` package only exists once that task has run. With the default `FilesOwnedByPackageSelection` the root file catalogers ran in an earlier group, never saw the package, and `app.jar` got no file metadata or digests. It did get them with the feature off. - in owned-by-package mode the groups are now packages, then the archive task, then the root file tasks. Other modes are unchanged - the file tasks merge their results into the SBOM instead of replacing the maps, since the archive task has already written nested files into them Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
200 lines
5.9 KiB
Go
200 lines
5.9 KiB
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
|
|
"github.com/anchore/syft/internal/archive"
|
|
"github.com/anchore/syft/internal/sbomsync"
|
|
"github.com/anchore/syft/syft/artifact"
|
|
"github.com/anchore/syft/syft/cataloging/filecataloging"
|
|
"github.com/anchore/syft/syft/file"
|
|
"github.com/anchore/syft/syft/file/cataloger/executable"
|
|
"github.com/anchore/syft/syft/file/cataloger/filecontent"
|
|
"github.com/anchore/syft/syft/file/cataloger/filedigest"
|
|
"github.com/anchore/syft/syft/file/cataloger/filemetadata"
|
|
"github.com/anchore/syft/syft/pkg"
|
|
"github.com/anchore/syft/syft/sbom"
|
|
)
|
|
|
|
func DefaultFileTaskFactories() Factories {
|
|
return Factories{
|
|
newFileDigestCatalogerTaskFactory("digest"),
|
|
newFileMetadataCatalogerTaskFactory("file-metadata"),
|
|
newFileContentCatalogerTaskFactory("content"),
|
|
newExecutableCatalogerTaskFactory("binary-metadata"),
|
|
}
|
|
}
|
|
|
|
func newFileDigestCatalogerTaskFactory(tags ...string) factory {
|
|
return func(cfg CatalogingFactoryConfig) Task {
|
|
return newFileDigestCatalogerTask(cfg.FilesConfig.Selection, cfg.FilesConfig.Hashers, tags...)
|
|
}
|
|
}
|
|
|
|
func newFileDigestCatalogerTask(selection file.Selection, hashers []crypto.Hash, tags ...string) Task {
|
|
fn := func(ctx context.Context, resolver file.Resolver, builder sbomsync.Builder) error {
|
|
if selection == file.NoFilesSelection || len(hashers) == 0 {
|
|
return nil
|
|
}
|
|
|
|
accessor := builder.(sbomsync.Accessor)
|
|
|
|
coordinates, ok := coordinatesForSelection(ctx, selection, builder.(sbomsync.Accessor))
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
result, err := filedigest.NewCataloger(hashers).Catalog(ctx, resolver, coordinates...)
|
|
|
|
accessor.WriteToSBOM(func(sbom *sbom.SBOM) {
|
|
mergeInto(&sbom.Artifacts.FileDigests, result)
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
return NewTask("file-digest-cataloger", fn, commonFileTags(tags)...)
|
|
}
|
|
|
|
func newFileMetadataCatalogerTaskFactory(tags ...string) factory {
|
|
return func(cfg CatalogingFactoryConfig) Task {
|
|
return newFileMetadataCatalogerTask(cfg.FilesConfig.Selection, tags...)
|
|
}
|
|
}
|
|
|
|
func newFileMetadataCatalogerTask(selection file.Selection, tags ...string) Task {
|
|
fn := func(ctx context.Context, resolver file.Resolver, builder sbomsync.Builder) error {
|
|
if selection == file.NoFilesSelection {
|
|
return nil
|
|
}
|
|
|
|
accessor := builder.(sbomsync.Accessor)
|
|
|
|
coordinates, ok := coordinatesForSelection(ctx, selection, builder.(sbomsync.Accessor))
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
result, err := filemetadata.NewCataloger().Catalog(ctx, resolver, coordinates...)
|
|
|
|
accessor.WriteToSBOM(func(sbom *sbom.SBOM) {
|
|
mergeInto(&sbom.Artifacts.FileMetadata, result)
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
return NewTask("file-metadata-cataloger", fn, commonFileTags(tags)...)
|
|
}
|
|
|
|
func newFileContentCatalogerTaskFactory(tags ...string) factory {
|
|
return func(cfg CatalogingFactoryConfig) Task {
|
|
return newFileContentCatalogerTask(cfg.FilesConfig.Content, tags...)
|
|
}
|
|
}
|
|
|
|
func newFileContentCatalogerTask(cfg filecontent.Config, tags ...string) Task {
|
|
fn := func(ctx context.Context, resolver file.Resolver, builder sbomsync.Builder) error {
|
|
if len(cfg.Globs) == 0 {
|
|
return nil
|
|
}
|
|
|
|
accessor := builder.(sbomsync.Accessor)
|
|
|
|
result, err := filecontent.NewCataloger(cfg).Catalog(ctx, resolver)
|
|
|
|
accessor.WriteToSBOM(func(sbom *sbom.SBOM) {
|
|
mergeInto(&sbom.Artifacts.FileContents, result)
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
return NewTask("file-content-cataloger", fn, commonFileTags(tags)...)
|
|
}
|
|
|
|
func newExecutableCatalogerTaskFactory(tags ...string) factory {
|
|
return func(cfg CatalogingFactoryConfig) Task {
|
|
return newExecutableCatalogerTask(cfg.FilesConfig.Selection, cfg.FilesConfig.Executable, tags...)
|
|
}
|
|
}
|
|
|
|
func newExecutableCatalogerTask(selection file.Selection, cfg executable.Config, tags ...string) Task {
|
|
fn := func(ctx context.Context, resolver file.Resolver, builder sbomsync.Builder) error {
|
|
if selection == file.NoFilesSelection {
|
|
return nil
|
|
}
|
|
|
|
accessor := builder.(sbomsync.Accessor)
|
|
|
|
result, err := executable.NewCataloger(cfg).CatalogCtx(ctx, resolver)
|
|
|
|
accessor.WriteToSBOM(func(sbom *sbom.SBOM) {
|
|
mergeInto(&sbom.Artifacts.Executables, result)
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
return NewTask("file-executable-cataloger", fn, commonFileTags(tags)...)
|
|
}
|
|
|
|
// TODO: this should be replaced with a fix that allows passing a coordinate or location iterator to the cataloger
|
|
// Today internal to both cataloger this functions differently: a slice of coordinates vs a channel of locations
|
|
//
|
|
// Only coordinates in the resolver's own frame are returned: inside an archive, the files of that archive,
|
|
// and at the scan root, files outside any archive. A package's location can be in another frame (a nested
|
|
// java package is located at the archive in its parent), and looking that path up here would miss or,
|
|
// worse, hit an unrelated file of the same name.
|
|
func coordinatesForSelection(ctx context.Context, selection file.Selection, accessor sbomsync.Accessor) ([]file.Coordinates, bool) {
|
|
if selection == file.AllFilesSelection {
|
|
return nil, true
|
|
}
|
|
|
|
if selection == file.FilesOwnedByPackageSelection {
|
|
var coordinates file.CoordinateSet
|
|
|
|
accessor.ReadFromSBOM(func(sbom *sbom.SBOM) {
|
|
// get any file coordinates that are owned by a package
|
|
for _, r := range sbom.Relationships {
|
|
if r.Type != artifact.ContainsRelationship {
|
|
continue
|
|
}
|
|
if _, ok := r.From.(pkg.Package); !ok {
|
|
continue
|
|
}
|
|
if c, ok := r.To.(file.Coordinates); ok {
|
|
coordinates.Add(c)
|
|
}
|
|
}
|
|
|
|
// get any file coordinates referenced by a package directly
|
|
for p := range sbom.Artifacts.Packages.Enumerate() {
|
|
coordinates.Add(p.Locations.CoordinateSet().ToSlice()...)
|
|
}
|
|
})
|
|
|
|
frame := archive.TraversalFromContext(ctx).ContentsArchivePath()
|
|
var coords []file.Coordinates
|
|
for _, c := range coordinates.ToSlice() {
|
|
if c.ArchivePath == frame {
|
|
coords = append(coords, c)
|
|
}
|
|
}
|
|
|
|
if len(coords) == 0 {
|
|
return nil, false
|
|
}
|
|
|
|
return coords, true
|
|
}
|
|
|
|
return nil, false
|
|
}
|
|
|
|
func commonFileTags(tags []string) []string {
|
|
tags = append(tags, filecataloging.FileTag)
|
|
return tags
|
|
}
|