Skip to content

Commit

Permalink
port deb/dpkg cataloger to new generic cataloger pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Oct 25, 2022
1 parent bd5adbc commit 0f73ff8
Show file tree
Hide file tree
Showing 23 changed files with 702 additions and 756 deletions.
9 changes: 9 additions & 0 deletions internal/string_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,12 @@ func TruncateMiddleEllipsis(input string, maxLen int) string {
}
return input[:maxLen/2] + "..." + input[len(input)-(maxLen/2):]
}

func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion syft/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func CatalogPackages(src *source.Source, cfg cataloger.Config) (*pkg.Catalog, []
}

// if the catalogers have been configured, use them regardless of input type
var catalogers []cataloger.Cataloger
var catalogers []pkg.Cataloger
if len(cfg.Catalogers) > 0 {
catalogers = cataloger.AllCatalogers(cfg)
} else {
Expand Down
16 changes: 16 additions & 0 deletions syft/pkg/cataloger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pkg

import (
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/source"
)

// Cataloger describes behavior for an object to participate in parsing container image or file system
// contents for the purpose of discovering Packages. Each concrete implementation should focus on discovering Packages
// for a specific Package Type or ecosystem.
type Cataloger interface {
// Name returns a string that uniquely describes a cataloger
Name() string
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing the catalog source.
Catalog(resolver source.FileResolver) ([]Package, []artifact.Relationship, error)
}
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/apkdb/parse_apk_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func TestMultiplePackages(t *testing.T) {
VersionID: "3.12",
}}

pkgtest.TestGenericParserWithEnv(t, fixture, parseApkDB, &env, expected, expectedRelationships)
pkgtest.TestFileParserWithEnv(t, fixture, parseApkDB, &env, expected, expectedRelationships)

}

Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newMonitor() (*progress.Manual, *progress.Manual) {
// In order to efficiently retrieve contents from a underlying container image the content fetch requests are
// done in bulk. Specifically, all files of interest are collected from each catalogers and accumulated into a single
// request.
func Catalog(resolver source.FileResolver, release *linux.Release, catalogers ...Cataloger) (*pkg.Catalog, []artifact.Relationship, error) {
func Catalog(resolver source.FileResolver, release *linux.Release, catalogers ...pkg.Cataloger) (*pkg.Catalog, []artifact.Relationship, error) {
catalog := pkg.NewCatalog()
var allRelationships []artifact.Relationship

Expand Down
28 changes: 8 additions & 20 deletions syft/pkg/cataloger/cataloger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger/alpm"
"github.com/anchore/syft/syft/pkg/cataloger/apkdb"
Expand All @@ -28,24 +27,13 @@ import (
"github.com/anchore/syft/syft/pkg/cataloger/ruby"
"github.com/anchore/syft/syft/pkg/cataloger/rust"
"github.com/anchore/syft/syft/pkg/cataloger/swift"
"github.com/anchore/syft/syft/source"
)

const AllCatalogersPattern = "all"

// Cataloger describes behavior for an object to participate in parsing container image or file system
// contents for the purpose of discovering Packages. Each concrete implementation should focus on discovering Packages
// for a specific Package Type or ecosystem.
type Cataloger interface {
// Name returns a string that uniquely describes a cataloger
Name() string
// Catalog is given an object to resolve file references and content, this function returns any discovered Packages after analyzing the catalog source.
Catalog(resolver source.FileResolver) ([]pkg.Package, []artifact.Relationship, error)
}

// ImageCatalogers returns a slice of locally implemented catalogers that are fit for detecting installations of packages.
func ImageCatalogers(cfg Config) []Cataloger {
return filterCatalogers([]Cataloger{
func ImageCatalogers(cfg Config) []pkg.Cataloger {
return filterCatalogers([]pkg.Cataloger{
alpm.NewAlpmdbCataloger(),
ruby.NewGemSpecCataloger(),
python.NewPythonPackageCataloger(),
Expand All @@ -62,8 +50,8 @@ func ImageCatalogers(cfg Config) []Cataloger {
}

// DirectoryCatalogers returns a slice of locally implemented catalogers that are fit for detecting packages from index files (and select installations)
func DirectoryCatalogers(cfg Config) []Cataloger {
return filterCatalogers([]Cataloger{
func DirectoryCatalogers(cfg Config) []pkg.Cataloger {
return filterCatalogers([]pkg.Cataloger{
alpm.NewAlpmdbCataloger(),
ruby.NewGemFileLockCataloger(),
python.NewPythonIndexCataloger(),
Expand All @@ -89,8 +77,8 @@ func DirectoryCatalogers(cfg Config) []Cataloger {
}

// AllCatalogers returns all implemented catalogers
func AllCatalogers(cfg Config) []Cataloger {
return filterCatalogers([]Cataloger{
func AllCatalogers(cfg Config) []pkg.Cataloger {
return filterCatalogers([]pkg.Cataloger{
alpm.NewAlpmdbCataloger(),
ruby.NewGemFileLockCataloger(),
ruby.NewGemSpecCataloger(),
Expand Down Expand Up @@ -128,7 +116,7 @@ func RequestedAllCatalogers(cfg Config) bool {
return false
}

func filterCatalogers(catalogers []Cataloger, enabledCatalogerPatterns []string) []Cataloger {
func filterCatalogers(catalogers []pkg.Cataloger, enabledCatalogerPatterns []string) []pkg.Cataloger {
// if cataloger is not set, all applicable catalogers are enabled by default
if len(enabledCatalogerPatterns) == 0 {
return catalogers
Expand All @@ -138,7 +126,7 @@ func filterCatalogers(catalogers []Cataloger, enabledCatalogerPatterns []string)
return catalogers
}
}
var keepCatalogers []Cataloger
var keepCatalogers []pkg.Cataloger
for _, cataloger := range catalogers {
if contains(enabledCatalogerPatterns, cataloger.Name()) {
keepCatalogers = append(keepCatalogers, cataloger)
Expand Down
4 changes: 2 additions & 2 deletions syft/pkg/cataloger/cataloger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/anchore/syft/syft/source"
)

var _ Cataloger = (*dummy)(nil)
var _ pkg.Cataloger = (*dummy)(nil)

type dummy struct {
name string
Expand Down Expand Up @@ -147,7 +147,7 @@ func Test_filterCatalogers(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var catalogers []Cataloger
var catalogers []pkg.Cataloger
for _, n := range tt.catalogers {
catalogers = append(catalogers, dummy{name: n})
}
Expand Down
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/cpp/parse_conanfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ func TestParseConanfile(t *testing.T) {
// TODO: relationships are not under test
var expectedRelationships []artifact.Relationship

pkgtest.TestGenericParser(t, fixture, parseConanfile, expected, expectedRelationships)
pkgtest.TestFileParser(t, fixture, parseConanfile, expected, expectedRelationships)
}
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/cpp/parse_conanlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ func TestParseConanlock(t *testing.T) {
// TODO: relationships are not under test
var expectedRelationships []artifact.Relationship

pkgtest.TestGenericParser(t, fixture, parseConanlock, expected, expectedRelationships)
pkgtest.TestFileParser(t, fixture, parseConanlock, expected, expectedRelationships)
}
2 changes: 1 addition & 1 deletion syft/pkg/cataloger/dart/parse_pubspec_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ func TestParsePubspecLock(t *testing.T) {
// TODO: relationships are not under test
var expectedRelationships []artifact.Relationship

pkgtest.TestGenericParser(t, fixture, parsePubspecLock, expected, expectedRelationships)
pkgtest.TestFileParser(t, fixture, parsePubspecLock, expected, expectedRelationships)
}
Loading

0 comments on commit 0f73ff8

Please sign in to comment.