-
Notifications
You must be signed in to change notification settings - Fork 887
Add support for programmatically listing catalogers #5046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adityasaky
wants to merge
1
commit into
anchore:main
Choose a base branch
from
adityasaky:api-expose-catalogers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package exp | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| "github.com/anchore/syft/internal/capabilities" | ||
| "github.com/anchore/syft/internal/task" | ||
| "github.com/anchore/syft/syft/cataloging" | ||
| "github.com/anchore/syft/syft/cataloging/pkgcataloging" | ||
| ) | ||
|
|
||
| // Cataloger describes an available cataloger and its selection metadata. | ||
| type Cataloger struct { | ||
| Name string `json:"name"` | ||
| Tags []string `json:"tags"` | ||
| } | ||
|
|
||
| // AllCatalogers returns information about all registered catalogers | ||
| // (both package and file catalogers), sorted by name. Any additional | ||
| // user-provided cataloger references are included in the result. | ||
| func AllCatalogers(additional ...pkgcataloging.CatalogerReference) ([]Cataloger, error) { | ||
| return SelectCatalogers(cataloging.SelectionRequest{ | ||
| DefaultNamesOrTags: []string{"all"}, | ||
| }, additional...) | ||
| } | ||
|
|
||
| // SelectCatalogers returns information about catalogers matching the given | ||
| // selection request. It applies the same selection logic used by CreateSBOM | ||
| // and the "syft cataloger list" CLI command. | ||
| // | ||
| // Any additional user-provided cataloger references are merged into the | ||
| // selectable set before selection is applied. References with AlwaysEnabled | ||
| // set to true are always included in the result regardless of the selection. | ||
| // | ||
| // If the selection request is empty (zero value), no catalogers are returned; | ||
| // use AllCatalogers() or pass a selection with DefaultNamesOrTags: []string{"all"}. | ||
| func SelectCatalogers(selection cataloging.SelectionRequest, additional ...pkgcataloging.CatalogerReference) ([]Cataloger, error) { | ||
| cfg := task.DefaultCatalogingFactoryConfig() | ||
|
|
||
| pkgTasks, err := task.DefaultPackageTaskFactories().Tasks(cfg) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to create package cataloger tasks: %w", err) | ||
| } | ||
|
|
||
| fileTasks, err := task.DefaultFileTaskFactories().Tasks(cfg) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to create file cataloger tasks: %w", err) | ||
| } | ||
|
|
||
| var persistentTasks []task.Task | ||
| for _, ref := range additional { | ||
| if ref.Cataloger == nil { | ||
| continue | ||
| } | ||
| t := task.NewPackageTask(cfg, ref.Cataloger, ref.Tags...) | ||
| if ref.AlwaysEnabled { | ||
| persistentTasks = append(persistentTasks, t) | ||
| } else { | ||
| pkgTasks = append(pkgTasks, t) | ||
| } | ||
| } | ||
|
|
||
| if selection.IsEmpty() { | ||
| if len(persistentTasks) > 0 { | ||
| return extractCatalogers(persistentTasks), nil | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| taskGroups := [][]task.Task{pkgTasks, fileTasks} | ||
| selectedGroups, _, err := task.SelectInGroups(taskGroups, selection) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to select catalogers: %w", err) | ||
| } | ||
|
|
||
| var selected []task.Task | ||
| for _, group := range selectedGroups { | ||
| selected = append(selected, group...) | ||
| } | ||
| selected = append(selected, persistentTasks...) | ||
|
|
||
| return extractCatalogers(selected), nil | ||
| } | ||
|
|
||
| func extractCatalogers(tasks []task.Task) []Cataloger { | ||
| infos := capabilities.ExtractCatalogerInfo(tasks) | ||
|
|
||
| catalogers := make([]Cataloger, 0, len(infos)) | ||
| for _, info := range infos { | ||
| tags := info.Selectors | ||
| if tags == nil { | ||
| tags = []string{} | ||
| } | ||
| catalogers = append(catalogers, Cataloger{ | ||
| Name: info.Name, | ||
| Tags: tags, | ||
| }) | ||
| } | ||
|
|
||
| sort.Slice(catalogers, func(i, j int) bool { | ||
| return catalogers[i].Name < catalogers[j].Name | ||
| }) | ||
|
|
||
| return catalogers | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package exp | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/anchore/syft/syft/artifact" | ||
| "github.com/anchore/syft/syft/cataloging" | ||
| "github.com/anchore/syft/syft/cataloging/pkgcataloging" | ||
| "github.com/anchore/syft/syft/file" | ||
| "github.com/anchore/syft/syft/pkg" | ||
| ) | ||
|
|
||
| type dummyCataloger struct { | ||
| name string | ||
| } | ||
|
|
||
| func (d dummyCataloger) Name() string { return d.name } | ||
| func (d dummyCataloger) Catalog(_ context.Context, _ file.Resolver) ([]pkg.Package, []artifact.Relationship, error) { | ||
| return nil, nil, nil | ||
| } | ||
|
|
||
| func TestAllCatalogers(t *testing.T) { | ||
| t.Run("happy path", func(t *testing.T) { | ||
| catalogers, err := AllCatalogers() | ||
| require.NoError(t, err) | ||
| assert.Greater(t, len(catalogers), 10) | ||
|
|
||
| for i := 1; i < len(catalogers); i++ { | ||
| assert.LessOrEqual(t, catalogers[i-1].Name, catalogers[i].Name, "results should be sorted by name") | ||
| } | ||
|
|
||
| names := make(map[string]bool) | ||
| for _, c := range catalogers { | ||
| assert.NotEmpty(t, c.Name) | ||
| names[c.Name] = true | ||
| } | ||
|
|
||
| assert.True(t, names["python-installed-package-cataloger"], "should contain python-installed-package-cataloger") | ||
| assert.True(t, names["go-module-binary-cataloger"], "should contain go-module-binary-cataloger") | ||
| }) | ||
|
|
||
| t.Run("contains file catalogers", func(t *testing.T) { | ||
| catalogers, err := AllCatalogers() | ||
| require.NoError(t, err) | ||
|
|
||
| hasFileCataloger := slices.ContainsFunc(catalogers, func(c Cataloger) bool { | ||
| return slices.Contains(c.Tags, "file") | ||
| }) | ||
| assert.True(t, hasFileCataloger, "should include file catalogers") | ||
| }) | ||
| } | ||
|
|
||
| func TestSelectCatalogers(t *testing.T) { | ||
| t.Run("with filter", func(t *testing.T) { | ||
| selection := cataloging.NewSelectionRequest(). | ||
| WithDefaults("all"). | ||
| WithSubSelections("python") | ||
|
|
||
| catalogers, err := SelectCatalogers(selection) | ||
| require.NoError(t, err) | ||
| assert.Greater(t, len(catalogers), 0) | ||
|
|
||
| for _, c := range catalogers { | ||
| // file catalogers are always included by the selection logic; skip them | ||
| if slices.Contains(c.Tags, "file") { | ||
| continue | ||
| } | ||
| assert.True(t, slices.Contains(c.Tags, "python"), "cataloger %q should have python tag", c.Name) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("empty selection", func(t *testing.T) { | ||
| catalogers, err := SelectCatalogers(cataloging.SelectionRequest{}) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, catalogers) | ||
| }) | ||
|
|
||
| t.Run("with additional always-enabled cataloger", func(t *testing.T) { | ||
| ref := pkgcataloging.CatalogerReference{ | ||
| Cataloger: dummyCataloger{name: "my-custom-cataloger"}, | ||
| AlwaysEnabled: true, | ||
| Tags: []string{"custom"}, | ||
| } | ||
|
|
||
| catalogers, err := SelectCatalogers( | ||
| cataloging.NewSelectionRequest().WithDefaults("all"), | ||
| ref, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| found := slices.ContainsFunc(catalogers, func(c Cataloger) bool { | ||
| return c.Name == "my-custom-cataloger" | ||
| }) | ||
| assert.True(t, found, "should include the always-enabled custom cataloger") | ||
| }) | ||
|
|
||
| t.Run("with additional selectable cataloger matching selection", func(t *testing.T) { | ||
| ref := pkgcataloging.CatalogerReference{ | ||
| Cataloger: dummyCataloger{name: "my-python-cataloger"}, | ||
| Tags: []string{"python", "custom"}, | ||
| } | ||
|
|
||
| catalogers, err := SelectCatalogers( | ||
| cataloging.NewSelectionRequest().WithDefaults("all").WithSubSelections("custom"), | ||
| ref, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| found := slices.ContainsFunc(catalogers, func(c Cataloger) bool { | ||
| return c.Name == "my-python-cataloger" | ||
| }) | ||
| assert.True(t, found, "should include custom cataloger that matches selection") | ||
| }) | ||
|
|
||
| t.Run("with additional selectable cataloger not matching selection", func(t *testing.T) { | ||
| ref := pkgcataloging.CatalogerReference{ | ||
| Cataloger: dummyCataloger{name: "my-ruby-cataloger"}, | ||
| Tags: []string{"ruby", "custom"}, | ||
| } | ||
|
|
||
| catalogers, err := SelectCatalogers( | ||
| cataloging.NewSelectionRequest().WithDefaults("all").WithSubSelections("python"), | ||
| ref, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| found := slices.ContainsFunc(catalogers, func(c Cataloger) bool { | ||
| return c.Name == "my-ruby-cataloger" | ||
| }) | ||
| assert.False(t, found, "should not include custom cataloger that does not match selection") | ||
| }) | ||
|
|
||
| t.Run("always-enabled cataloger included even with empty selection", func(t *testing.T) { | ||
| ref := pkgcataloging.CatalogerReference{ | ||
| Cataloger: dummyCataloger{name: "my-persistent-cataloger"}, | ||
| AlwaysEnabled: true, | ||
| Tags: []string{"custom"}, | ||
| } | ||
|
|
||
| catalogers, err := SelectCatalogers(cataloging.SelectionRequest{}, ref) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Len(t, catalogers, 1) | ||
| assert.Equal(t, "my-persistent-cataloger", catalogers[0].Name) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| // Package exp provides experimental APIs for the syft library. | ||
| // | ||
| // APIs in this package have NO stability guarantees. They may change or be | ||
| // removed in any release without notice. Do not depend on their behavior | ||
| // in production systems. | ||
| // | ||
| // Once an API has proven stable and useful, it may be promoted to the main | ||
| // syft package with proper stability guarantees. | ||
| package exp |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently using this approach to provide any user defined catalogers that are configured via
CreateSBOMConfig.WithCatalogers(). I'm not sure this is the best way to go about this though.