From 6ee3a17021ad02a99ca3a8d1ba7fa7140eab7b0f Mon Sep 17 00:00:00 2001 From: tomersein Date: Sat, 17 Aug 2024 12:49:42 +0300 Subject: [PATCH 1/4] fix none in files Signed-off-by: tomersein --- cmd/syft/internal/options/catalog.go | 1 + cmd/syft/internal/options/file.go | 2 ++ syft/cataloging/filecataloging/config.go | 4 +++ syft/cataloging/filecataloging/config_test.go | 3 ++- syft/create_sbom_config.go | 25 ++++++++++--------- syft/sbom/sbom.go | 8 +++--- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/cmd/syft/internal/options/catalog.go b/cmd/syft/internal/options/catalog.go index 22cb286a445..f11ec92d3bb 100644 --- a/cmd/syft/internal/options/catalog.go +++ b/cmd/syft/internal/options/catalog.go @@ -133,6 +133,7 @@ func (cfg Catalog) ToFilesConfig() filecataloging.Config { } return filecataloging.Config{ + Enabled: cfg.File.Enabled, Selection: cfg.File.Metadata.Selection, Hashers: hashers, Content: filecontent.Config{ diff --git a/cmd/syft/internal/options/file.go b/cmd/syft/internal/options/file.go index 6ac9c8d2ba3..18fa7d86d2d 100644 --- a/cmd/syft/internal/options/file.go +++ b/cmd/syft/internal/options/file.go @@ -12,6 +12,7 @@ import ( ) type fileConfig struct { + Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` Metadata fileMetadata `yaml:"metadata" json:"metadata" mapstructure:"metadata"` Content fileContent `yaml:"content" json:"content" mapstructure:"content"` Executable fileExecutable `yaml:"executable" json:"executable" mapstructure:"executable"` @@ -33,6 +34,7 @@ type fileExecutable struct { func defaultFileConfig() fileConfig { return fileConfig{ + Enabled: true, Metadata: fileMetadata{ Selection: file.FilesOwnedByPackageSelection, Digests: []string{"sha1", "sha256"}, diff --git a/syft/cataloging/filecataloging/config.go b/syft/cataloging/filecataloging/config.go index cc639c2f17b..2dd37e84c11 100644 --- a/syft/cataloging/filecataloging/config.go +++ b/syft/cataloging/filecataloging/config.go @@ -14,6 +14,7 @@ import ( ) type Config struct { + Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` Selection file.Selection `yaml:"selection" json:"selection" mapstructure:"selection"` Hashers []crypto.Hash `yaml:"hashers" json:"hashers" mapstructure:"hashers"` Content filecontent.Config `yaml:"content" json:"content" mapstructure:"content"` @@ -21,6 +22,7 @@ type Config struct { } type configMarshaledForm struct { + Enabled bool `yaml:"enabled" json:"enabled" mapstructure:"enabled"` Selection file.Selection `yaml:"selection" json:"selection" mapstructure:"selection"` Hashers []string `yaml:"hashers" json:"hashers" mapstructure:"hashers"` Content filecontent.Config `yaml:"content" json:"content" mapstructure:"content"` @@ -32,6 +34,7 @@ func DefaultConfig() Config { log.WithFields("error", err).Warn("unable to create file hashers") } return Config{ + Enabled: true, Selection: file.FilesOwnedByPackageSelection, Hashers: hashers, Content: filecontent.DefaultConfig(), @@ -41,6 +44,7 @@ func DefaultConfig() Config { func (cfg Config) MarshalJSON() ([]byte, error) { marshaled := configMarshaledForm{ + Enabled: cfg.Enabled, Selection: cfg.Selection, Hashers: hashersToString(cfg.Hashers), } diff --git a/syft/cataloging/filecataloging/config_test.go b/syft/cataloging/filecataloging/config_test.go index ca62dccb1a1..0116320fc47 100644 --- a/syft/cataloging/filecataloging/config_test.go +++ b/syft/cataloging/filecataloging/config_test.go @@ -21,10 +21,11 @@ func TestConfig_MarshalJSON(t *testing.T) { { name: "converts hashers to strings", cfg: Config{ + Enabled: true, Selection: file.FilesOwnedByPackageSelection, Hashers: []crypto.Hash{crypto.SHA256}, }, - want: []byte(`{"selection":"owned-by-package","hashers":["sha-256"],"content":{"globs":null,"skip-files-above-size":0}}`), + want: []byte(`{"enabled":true,"selection":"owned-by-package","hashers":["sha-256"],"content":{"globs":null,"skip-files-above-size":0}}`), }, } for _, tt := range tests { diff --git a/syft/create_sbom_config.go b/syft/create_sbom_config.go index 8e9e76b5c3b..43bf501479c 100644 --- a/syft/create_sbom_config.go +++ b/syft/create_sbom_config.go @@ -222,18 +222,19 @@ func (c *CreateSBOMConfig) makeTaskGroups(src source.Description) ([][]task.Task // fileTasks returns the set of tasks that should be run to catalog files. func (c *CreateSBOMConfig) fileTasks() []task.Task { var tsks []task.Task - - if t := task.NewFileDigestCatalogerTask(c.Files.Selection, c.Files.Hashers...); t != nil { - tsks = append(tsks, t) - } - if t := task.NewFileMetadataCatalogerTask(c.Files.Selection); t != nil { - tsks = append(tsks, t) - } - if t := task.NewFileContentCatalogerTask(c.Files.Content); t != nil { - tsks = append(tsks, t) - } - if t := task.NewExecutableCatalogerTask(c.Files.Selection, c.Files.Executable); t != nil { - tsks = append(tsks, t) + if c.Files.Enabled { + if t := task.NewFileDigestCatalogerTask(c.Files.Selection, c.Files.Hashers...); t != nil { + tsks = append(tsks, t) + } + if t := task.NewFileMetadataCatalogerTask(c.Files.Selection); t != nil { + tsks = append(tsks, t) + } + if t := task.NewFileContentCatalogerTask(c.Files.Content); t != nil { + tsks = append(tsks, t) + } + if t := task.NewExecutableCatalogerTask(c.Files.Selection, c.Files.Executable); t != nil { + tsks = append(tsks, t) + } } return tsks diff --git a/syft/sbom/sbom.go b/syft/sbom/sbom.go index 1c8cbc60ff2..b5fc9c42392 100644 --- a/syft/sbom/sbom.go +++ b/syft/sbom/sbom.go @@ -66,9 +66,11 @@ func (s SBOM) AllCoordinates() []file.Coordinates { for coordinates := range s.Artifacts.Unknowns { set.Add(coordinates) } - for _, relationship := range s.Relationships { - for _, coordinates := range extractCoordinates(relationship) { - set.Add(coordinates) + if len(set.ToSlice()) > 0 { + for _, relationship := range s.Relationships { + for _, coordinates := range extractCoordinates(relationship) { + set.Add(coordinates) + } } } return set.ToSlice() From 6b1e606d90e21678a5400cbe2405952a15382322 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 12 Nov 2024 10:00:19 -0500 Subject: [PATCH 2/4] check coord set size more efficiently Signed-off-by: Alex Goodman --- syft/file/coordinate_set.go | 4 ++++ syft/sbom/sbom.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/syft/file/coordinate_set.go b/syft/file/coordinate_set.go index ba56005d302..4790fe41640 100644 --- a/syft/file/coordinate_set.go +++ b/syft/file/coordinate_set.go @@ -78,6 +78,10 @@ func (s CoordinateSet) ToSlice() []Coordinates { return coordinates } +func (s CoordinateSet) Size() int { + return len(s.set) +} + func (s CoordinateSet) Hash() (uint64, error) { return hashstructure.Hash(s.ToSlice(), hashstructure.FormatV2, &hashstructure.HashOptions{ ZeroNil: true, diff --git a/syft/sbom/sbom.go b/syft/sbom/sbom.go index b5fc9c42392..2974be7be82 100644 --- a/syft/sbom/sbom.go +++ b/syft/sbom/sbom.go @@ -66,7 +66,7 @@ func (s SBOM) AllCoordinates() []file.Coordinates { for coordinates := range s.Artifacts.Unknowns { set.Add(coordinates) } - if len(set.ToSlice()) > 0 { + if set.Size() > 0 { for _, relationship := range s.Relationships { for _, coordinates := range extractCoordinates(relationship) { set.Add(coordinates) From f33bdb5e79ff0a919ec71d1ef14db53a85936733 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 12 Nov 2024 10:06:35 -0500 Subject: [PATCH 3/4] fix integration test Signed-off-by: Alex Goodman --- cmd/syft/internal/test/integration/files_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/syft/internal/test/integration/files_test.go b/cmd/syft/internal/test/integration/files_test.go index 6794eed08eb..39750ee087f 100644 --- a/cmd/syft/internal/test/integration/files_test.go +++ b/cmd/syft/internal/test/integration/files_test.go @@ -36,6 +36,7 @@ func TestFileCataloging_Default(t *testing.T) { func TestFileCataloging_AllFiles(t *testing.T) { cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{}) cfg = cfg.WithFilesConfig(filecataloging.Config{ + Enabled: true, Selection: file.AllFilesSelection, Hashers: []crypto.Hash{ crypto.SHA256, From 427a8934687ef9c2905a07977eec8827a7164e66 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 12 Nov 2024 10:09:26 -0500 Subject: [PATCH 4/4] update help text Signed-off-by: Alex Goodman --- cmd/syft/internal/options/file.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/syft/internal/options/file.go b/cmd/syft/internal/options/file.go index 18fa7d86d2d..b4830f86712 100644 --- a/cmd/syft/internal/options/file.go +++ b/cmd/syft/internal/options/file.go @@ -66,11 +66,12 @@ func (c *fileConfig) PostLoad() error { } func (c *fileConfig) DescribeFields(descriptions clio.FieldDescriptionSet) { + descriptions.Add(&c.Enabled, `enable or disable file cataloging entirely`) descriptions.Add(&c.Metadata.Selection, `select which files should be captured by the file-metadata cataloger and included in the SBOM. Options include: - - "all": capture all files from the search space - - "owned-by-package": capture only files owned by packages - - "none", "": do not capture any files`) + - "all": capture metadata for all files from the search space + - "owned-by-package": capture metadata only for files owned by packages + - "none", "": do not capture metadata for any files`) descriptions.Add(&c.Metadata.Digests, `the file digest algorithms to use when cataloging files (options: "md5", "sha1", "sha224", "sha256", "sha384", "sha512")`) descriptions.Add(&c.Content.SkipFilesAboveSize, `skip searching a file entirely if it is above the given size (default = 1MB; unit = bytes)`)