chore: update config injection

Signed-off-by: Christopher Phillips <32073428+spiffcs@users.noreply.github.com>
This commit is contained in:
Christopher Phillips 2025-09-26 14:51:29 -04:00
parent 6fa1831484
commit 7a131ff462
No known key found for this signature in database
6 changed files with 21 additions and 26 deletions

View File

@ -14,27 +14,27 @@ func NewDBCataloger() pkg.Cataloger {
return generic.NewCataloger("dpkg-db-cataloger"). return generic.NewCataloger("dpkg-db-cataloger").
// note: these globs have been intentionally split up in order to improve search performance, // note: these globs have been intentionally split up in order to improve search performance,
// please do NOT combine into: "**/var/lib/dpkg/{status,status.d/*}" // please do NOT combine into: "**/var/lib/dpkg/{status,status.d/*}"
WithParserByGlobs(newDpkgDBParser(CatalogerConfig{IncludeDeInstalled: true}), "**/lib/dpkg/status", "**/lib/dpkg/status.d/*", "**/lib/opkg/info/*.control", "**/lib/opkg/status"). WithParserByGlobs(parseDpkgDB(CatalogerConfig{IncludeDeInstalled: true}), "**/lib/dpkg/status", "**/lib/dpkg/status.d/*", "**/lib/opkg/info/*.control", "**/lib/opkg/status").
WithProcessors(dependency.Processor(dbEntryDependencySpecifier)) WithProcessors(dependency.Processor(dbEntryDependencySpecifier))
} }
// NewDBCatalogerWithOpts returns a new Deb package cataloger capable of parsing DPKG status DB flat-file stores with custom configuration. // NewDBCatalogerWithOpts returns a new Deb package cataloger capable of parsing DPKG status DB flat-file stores with syft configuration.
func NewDBCatalogerWithOpts(cfg CatalogerConfig) pkg.Cataloger { func NewDBCatalogerWithOpts(cfg CatalogerConfig) pkg.Cataloger {
return generic.NewCataloger("dpkg-db-cataloger"). return generic.NewCataloger("dpkg-db-cataloger").
// note: these globs have been intentionally split up in order to improve search performance, // note: these globs have been intentionally split up in order to improve search performance,
// please do NOT combine into: "**/var/lib/dpkg/{status,status.d/*}" // please do NOT combine into: "**/var/lib/dpkg/{status,status.d/*}"
WithParserByGlobs(newDpkgDBParser(cfg), "**/lib/dpkg/status", "**/lib/dpkg/status.d/*", "**/lib/opkg/info/*.control", "**/lib/opkg/status"). WithParserByGlobs(parseDpkgDB(cfg), "**/lib/dpkg/status", "**/lib/dpkg/status.d/*", "**/lib/opkg/info/*.control", "**/lib/opkg/status").
WithProcessors(dependency.Processor(dbEntryDependencySpecifier)) WithProcessors(dependency.Processor(dbEntryDependencySpecifier))
} }
// NewArchiveCataloger returns a new Debian package cataloger object capable of parsing .deb archive files // NewArchiveCataloger returns a new Debian package cataloger object capable of parsing .deb archive files
func NewArchiveCataloger() pkg.Cataloger { func NewArchiveCataloger() pkg.Cataloger {
return generic.NewCataloger("deb-archive-cataloger"). return generic.NewCataloger("deb-archive-cataloger").
WithParserByGlobs(newDebArchiveParser(CatalogerConfig{IncludeDeInstalled: true}), "**/*.deb") WithParserByGlobs(parseDebArchive(CatalogerConfig{IncludeDeInstalled: true}), "**/*.deb")
} }
// NewArchiveCatalogerWithOpts returns a new Debian package cataloger object capable of parsing .deb archive files with custom configuration. // NewArchiveCatalogerWithOpts returns a new Debian package cataloger object capable of parsing .deb archive files with syft configuration.
func NewArchiveCatalogerWithOpts(cfg CatalogerConfig) pkg.Cataloger { func NewArchiveCatalogerWithOpts(cfg CatalogerConfig) pkg.Cataloger {
return generic.NewCataloger("deb-archive-cataloger"). return generic.NewCataloger("deb-archive-cataloger").
WithParserByGlobs(newDebArchiveParser(cfg), "**/*.deb") WithParserByGlobs(parseDebArchive(cfg), "**/*.deb")
} }

View File

@ -21,12 +21,6 @@ import (
"github.com/anchore/syft/syft/pkg/cataloger/generic" "github.com/anchore/syft/syft/pkg/cataloger/generic"
) )
func newDebArchiveParser(cfg CatalogerConfig) generic.Parser {
return func(ctx context.Context, resolver file.Resolver, env *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
return parseDebArchiveWithConfig(ctx, resolver, env, reader, cfg)
}
}
// parseDebArchive parses a Debian package archive (.deb) file and returns the packages it contains. // parseDebArchive parses a Debian package archive (.deb) file and returns the packages it contains.
// A .deb file is an ar archive containing three main files: // A .deb file is an ar archive containing three main files:
// - debian-binary: Version of the .deb format (usually "2.0") // - debian-binary: Version of the .deb format (usually "2.0")
@ -34,11 +28,13 @@ func newDebArchiveParser(cfg CatalogerConfig) generic.Parser {
// - data.tar.gz/xz/zst: Contains the actual files to be installed (not processed by this cataloger) // - data.tar.gz/xz/zst: Contains the actual files to be installed (not processed by this cataloger)
// //
// This function extracts and processes the control information to create package metadata. // This function extracts and processes the control information to create package metadata.
func parseDebArchive(ctx context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { func parseDebArchive(cfg CatalogerConfig) generic.Parser {
return parseDebArchiveWithConfig(ctx, nil, nil, reader, DefaultCatalogerConfig()) return func(ctx context.Context, resolver file.Resolver, env *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
return parseDebArchiveWithConfig(ctx, resolver, env, reader, cfg)
}
} }
func parseDebArchiveWithConfig(ctx context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser, cfg CatalogerConfig) ([]pkg.Package, []artifact.Relationship, error) { func parseDebArchiveWithConfig(ctx context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser, _ CatalogerConfig) ([]pkg.Package, []artifact.Relationship, error) {
arReader := ar.NewReader(reader) arReader := ar.NewReader(reader)
var metadata *pkg.DpkgArchiveEntry var metadata *pkg.DpkgArchiveEntry

View File

@ -33,7 +33,7 @@ var (
sourceRegexp = regexp.MustCompile(`(?P<name>\S+)( \((?P<version>.*)\))?`) sourceRegexp = regexp.MustCompile(`(?P<name>\S+)( \((?P<version>.*)\))?`)
) )
func newDpkgDBParser(cfg CatalogerConfig) generic.Parser { func parseDpkgDB(cfg CatalogerConfig) generic.Parser {
return func(ctx context.Context, resolver file.Resolver, env *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { return func(ctx context.Context, resolver file.Resolver, env *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
return parseDpkgDBWithConfig(ctx, resolver, env, reader, cfg) return parseDpkgDBWithConfig(ctx, resolver, env, reader, cfg)
} }
@ -82,13 +82,17 @@ func findDpkgInfoFiles(name string, resolver file.Resolver, dbLocation file.Loca
return locations return locations
} }
func parseDpkgStatus(reader io.Reader) ([]pkg.DpkgDBEntry, error) {
return parseDpkgStatusWithConfig(reader, DefaultCatalogerConfig())
}
func parseDpkgStatusWithConfig(reader io.Reader, cfg CatalogerConfig) ([]pkg.DpkgDBEntry, error) { func parseDpkgStatusWithConfig(reader io.Reader, cfg CatalogerConfig) ([]pkg.DpkgDBEntry, error) {
buffedReader := bufio.NewReader(reader) buffedReader := bufio.NewReader(reader)
var metadata []pkg.DpkgDBEntry var metadata []pkg.DpkgDBEntry
continueProcessing := true continueProcessing := true
for continueProcessing { for continueProcessing {
entry, err := parseDpkgStatusEntryWithConfig(buffedReader, cfg) entry, err := parseDpkgStatusEntry(buffedReader, cfg)
if err != nil { if err != nil {
if errors.Is(err, errEndOfPackages) { if errors.Is(err, errEndOfPackages) {
continueProcessing = false continueProcessing = false
@ -123,12 +127,7 @@ type dpkgExtractedMetadata struct {
Status string `mapstructure:"Status"` Status string `mapstructure:"Status"`
} }
// parseDpkgStatusEntry returns an individual Dpkg entry, or returns errEndOfPackages if there are no more packages to parse from the reader. func parseDpkgStatusEntry(reader *bufio.Reader, cfg CatalogerConfig) (*pkg.DpkgDBEntry, error) {
func parseDpkgStatusEntry(reader *bufio.Reader) (*pkg.DpkgDBEntry, error) {
return parseDpkgStatusEntryWithConfig(reader, DefaultCatalogerConfig())
}
func parseDpkgStatusEntryWithConfig(reader *bufio.Reader, cfg CatalogerConfig) (*pkg.DpkgDBEntry, error) {
var retErr error var retErr error
dpkgFields, err := extractAllFields(reader) dpkgFields, err := extractAllFields(reader)
if err != nil { if err != nil {

View File

@ -403,7 +403,7 @@ Installed-Size: 10kib
WithErrorAssertion(tt.wantErr). WithErrorAssertion(tt.wantErr).
WithLinuxRelease(linux.Release{ID: "debian", VersionID: "10"}). WithLinuxRelease(linux.Release{ID: "debian", VersionID: "10"}).
Expects(tt.want, nil). Expects(tt.want, nil).
TestParser(t, parseDpkgDB) TestParser(t, parseDpkgDB(DefaultCatalogerConfig()))
}) })
} }
} }