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

@ -23,4 +23,4 @@ func defaultDebianConfig() debianConfig {
return debianConfig{
IncludeDeInstalled: def.IncludeDeInstalled,
}
}
}

View File

@ -14,27 +14,27 @@ func NewDBCataloger() pkg.Cataloger {
return generic.NewCataloger("dpkg-db-cataloger").
// 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/*}"
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))
}
// 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 {
return generic.NewCataloger("dpkg-db-cataloger").
// 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/*}"
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))
}
// NewArchiveCataloger returns a new Debian package cataloger object capable of parsing .deb archive files
func NewArchiveCataloger() pkg.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 {
return generic.NewCataloger("deb-archive-cataloger").
WithParserByGlobs(newDebArchiveParser(cfg), "**/*.deb")
WithParserByGlobs(parseDebArchive(cfg), "**/*.deb")
}

View File

@ -13,4 +13,4 @@ func DefaultCatalogerConfig() CatalogerConfig {
func (c CatalogerConfig) WithIncludeDeInstalled(include bool) CatalogerConfig {
c.IncludeDeInstalled = include
return c
}
}

View File

@ -21,12 +21,6 @@ import (
"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.
// A .deb file is an ar archive containing three main files:
// - 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)
//
// 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) {
return parseDebArchiveWithConfig(ctx, nil, nil, reader, DefaultCatalogerConfig())
func parseDebArchive(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)
}
}
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)
var metadata *pkg.DpkgArchiveEntry

View File

@ -33,7 +33,7 @@ var (
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 parseDpkgDBWithConfig(ctx, resolver, env, reader, cfg)
}
@ -82,13 +82,17 @@ func findDpkgInfoFiles(name string, resolver file.Resolver, dbLocation file.Loca
return locations
}
func parseDpkgStatus(reader io.Reader) ([]pkg.DpkgDBEntry, error) {
return parseDpkgStatusWithConfig(reader, DefaultCatalogerConfig())
}
func parseDpkgStatusWithConfig(reader io.Reader, cfg CatalogerConfig) ([]pkg.DpkgDBEntry, error) {
buffedReader := bufio.NewReader(reader)
var metadata []pkg.DpkgDBEntry
continueProcessing := true
for continueProcessing {
entry, err := parseDpkgStatusEntryWithConfig(buffedReader, cfg)
entry, err := parseDpkgStatusEntry(buffedReader, cfg)
if err != nil {
if errors.Is(err, errEndOfPackages) {
continueProcessing = false
@ -123,12 +127,7 @@ type dpkgExtractedMetadata struct {
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) (*pkg.DpkgDBEntry, error) {
return parseDpkgStatusEntryWithConfig(reader, DefaultCatalogerConfig())
}
func parseDpkgStatusEntryWithConfig(reader *bufio.Reader, cfg CatalogerConfig) (*pkg.DpkgDBEntry, error) {
func parseDpkgStatusEntry(reader *bufio.Reader, cfg CatalogerConfig) (*pkg.DpkgDBEntry, error) {
var retErr error
dpkgFields, err := extractAllFields(reader)
if err != nil {

View File

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