Skip to content
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

Add configuration to disable capturing any file info in SBOM #3132

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/syft/internal/options/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 6 additions & 3 deletions cmd/syft/internal/options/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -33,6 +34,7 @@ type fileExecutable struct {

func defaultFileConfig() fileConfig {
return fileConfig{
Enabled: true,
Metadata: fileMetadata{
Selection: file.FilesOwnedByPackageSelection,
Digests: []string{"sha1", "sha256"},
Expand Down Expand Up @@ -64,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)`)
Expand Down
1 change: 1 addition & 0 deletions cmd/syft/internal/test/integration/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions syft/cataloging/filecataloging/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ 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"`
Executable executable.Config `yaml:"executable" json:"executable" mapstructure:"executable"`
}

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"`
Expand All @@ -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(),
Expand All @@ -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),
}
Expand Down
3 changes: 2 additions & 1 deletion syft/cataloging/filecataloging/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 13 additions & 12 deletions syft/create_sbom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions syft/file/coordinate_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions syft/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 set.Size() > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the downside with this is that we are adding relationships already from pkg-to-file or file-to-file and here we're silently dropping only the nodes, but not changing the relationships. I think this part needs a little more thought -- that is, if there is an edge in the SBOM that describes the file directly, then we either need to include that file or drop the relationship.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi! sounds right.. however I am not sure how to solve it.
please let me know how to proceed further so this PR can be merged in the future :)

for _, relationship := range s.Relationships {
for _, coordinates := range extractCoordinates(relationship) {
set.Add(coordinates)
}
}
}
return set.ToSlice()
Expand Down
Loading