From 703edff8767d47dd2fd169fadc7ba29ecd51f090 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 10 Dec 2025 13:22:45 -0500 Subject: [PATCH] call file config validate in cli post load Signed-off-by: Alex Goodman --- cmd/syft/internal/options/catalog.go | 5 +++++ .../package_catalogers_represented_test.go | 3 +-- internal/task/factory.go | 15 +++++-------- internal/task/file_tasks.go | 22 ++++++++----------- internal/task/package_task_factory.go | 8 +++---- syft/file/cataloger/executable/cataloger.go | 2 +- 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/cmd/syft/internal/options/catalog.go b/cmd/syft/internal/options/catalog.go index bd606c09b..592252b79 100644 --- a/cmd/syft/internal/options/catalog.go +++ b/cmd/syft/internal/options/catalog.go @@ -303,6 +303,11 @@ func (cfg *Catalog) PostLoad() error { return fmt.Errorf("cannot enable exclude-binary-overlap-by-ownership without enabling package-file-ownership-overlap") } + // validate file executable options + if err := cfg.ToFilesConfig().Executable.Validate(); err != nil { + return fmt.Errorf("invalid file executable configuration: %w", err) + } + return nil } diff --git a/cmd/syft/internal/test/integration/package_catalogers_represented_test.go b/cmd/syft/internal/test/integration/package_catalogers_represented_test.go index 329236644..ec7452eb8 100644 --- a/cmd/syft/internal/test/integration/package_catalogers_represented_test.go +++ b/cmd/syft/internal/test/integration/package_catalogers_represented_test.go @@ -31,8 +31,7 @@ func TestAllPackageCatalogersReachableInTasks(t *testing.T) { taskFactories := task.DefaultPackageTaskFactories() taskTagsByName := make(map[string][]string) for _, factory := range taskFactories { - tsk, err := factory(task.DefaultCatalogingFactoryConfig()) - require.NoError(t, err) + tsk := factory(task.DefaultCatalogingFactoryConfig()) if taskTagsByName[tsk.Name()] != nil { t.Fatalf("duplicate task name: %q", tsk.Name()) } diff --git a/internal/task/factory.go b/internal/task/factory.go index ffae1605a..c51c1975b 100644 --- a/internal/task/factory.go +++ b/internal/task/factory.go @@ -1,7 +1,6 @@ package task import ( - "errors" "fmt" "sort" "strings" @@ -9,7 +8,7 @@ import ( "github.com/scylladb/go-set/strset" ) -type factory func(cfg CatalogingFactoryConfig) (Task, error) +type factory func(cfg CatalogingFactoryConfig) Task type Factories []factory @@ -17,13 +16,9 @@ func (f Factories) Tasks(cfg CatalogingFactoryConfig) ([]Task, error) { var allTasks []Task taskNames := strset.New() duplicateTaskNames := strset.New() - var errs []error + var err error for _, fact := range f { - tsk, err := fact(cfg) - if err != nil { - errs = append(errs, err) - continue - } + tsk := fact(cfg) if tsk == nil { continue } @@ -38,8 +33,8 @@ func (f Factories) Tasks(cfg CatalogingFactoryConfig) ([]Task, error) { if duplicateTaskNames.Size() > 0 { names := duplicateTaskNames.List() sort.Strings(names) - errs = append(errs, fmt.Errorf("duplicate cataloger task names: %v", strings.Join(names, ", "))) + err = fmt.Errorf("duplicate cataloger task names: %v", strings.Join(names, ", ")) } - return allTasks, errors.Join(errs...) + return allTasks, err } diff --git a/internal/task/file_tasks.go b/internal/task/file_tasks.go index c904e97cb..7af11fcd1 100644 --- a/internal/task/file_tasks.go +++ b/internal/task/file_tasks.go @@ -26,8 +26,8 @@ func DefaultFileTaskFactories() Factories { } func newFileDigestCatalogerTaskFactory(tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { - return newFileDigestCatalogerTask(cfg.FilesConfig.Selection, cfg.FilesConfig.Hashers, tags...), nil + return func(cfg CatalogingFactoryConfig) Task { + return newFileDigestCatalogerTask(cfg.FilesConfig.Selection, cfg.FilesConfig.Hashers, tags...) } } @@ -57,8 +57,8 @@ func newFileDigestCatalogerTask(selection file.Selection, hashers []crypto.Hash, } func newFileMetadataCatalogerTaskFactory(tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { - return newFileMetadataCatalogerTask(cfg.FilesConfig.Selection, tags...), nil + return func(cfg CatalogingFactoryConfig) Task { + return newFileMetadataCatalogerTask(cfg.FilesConfig.Selection, tags...) } } @@ -88,8 +88,8 @@ func newFileMetadataCatalogerTask(selection file.Selection, tags ...string) Task } func newFileContentCatalogerTaskFactory(tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { - return newFileContentCatalogerTask(cfg.FilesConfig.Content, tags...), nil + return func(cfg CatalogingFactoryConfig) Task { + return newFileContentCatalogerTask(cfg.FilesConfig.Content, tags...) } } @@ -114,16 +114,12 @@ func newFileContentCatalogerTask(cfg filecontent.Config, tags ...string) Task { } func newExecutableCatalogerTaskFactory(tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { + 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, error) { - if err := cfg.Validate(); err != nil { - return nil, err - } - +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 @@ -140,7 +136,7 @@ func newExecutableCatalogerTask(selection file.Selection, cfg executable.Config, return err } - return NewTask("file-executable-cataloger", fn, commonFileTags(tags)...), nil + 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 diff --git a/internal/task/package_task_factory.go b/internal/task/package_task_factory.go index 0f3f3218b..0e83bf2ca 100644 --- a/internal/task/package_task_factory.go +++ b/internal/task/package_task_factory.go @@ -21,14 +21,14 @@ import ( ) func newPackageTaskFactory(catalogerFactory func(CatalogingFactoryConfig) pkg.Cataloger, tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { - return NewPackageTask(cfg, catalogerFactory(cfg), tags...), nil + return func(cfg CatalogingFactoryConfig) Task { + return NewPackageTask(cfg, catalogerFactory(cfg), tags...) } } func newSimplePackageTaskFactory(catalogerFactory func() pkg.Cataloger, tags ...string) factory { - return func(cfg CatalogingFactoryConfig) (Task, error) { - return NewPackageTask(cfg, catalogerFactory(), tags...), nil + return func(cfg CatalogingFactoryConfig) Task { + return NewPackageTask(cfg, catalogerFactory(), tags...) } } diff --git a/syft/file/cataloger/executable/cataloger.go b/syft/file/cataloger/executable/cataloger.go index 8802eeb1b..b323a5efe 100644 --- a/syft/file/cataloger/executable/cataloger.go +++ b/syft/file/cataloger/executable/cataloger.go @@ -48,7 +48,7 @@ type SymbolConfig struct { // Types are the types of Go symbols to capture, relative to `go tool nm` output (e.g. T, t, R, r, D, d, B, b, C, U, etc). // If empty, all symbol types are captured. - Types []string + Types []string `json:"types" yaml:"types" mapstructure:"types"` // Go configures Go-specific symbol capturing settings. Go GoSymbolConfig `json:"go" yaml:"go" mapstructure:"go"`