Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
7 changes: 6 additions & 1 deletion cmd/syft/internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,18 @@ func runScan(ctx context.Context, id clio.Identification, opts *scanOptions, use
}

func getSource(ctx context.Context, opts *options.Catalog, userInput string, sources ...string) (source.Source, error) {
authors, err := options.ParseAuthors(opts.Source.Authors)
if err != nil {
return nil, fmt.Errorf("invalid authors: %w", err)
}

cfg := syft.DefaultGetSourceConfig().
WithRegistryOptions(opts.Registry.ToOptions()).
WithAlias(source.Alias{
Name: opts.Source.Name,
Version: opts.Source.Version,
Supplier: opts.Source.Supplier,
Authors: authors,
}).
WithExcludeConfig(source.ExcludeConfig{
Paths: opts.Exclusions,
Expand All @@ -228,7 +234,6 @@ func getSource(ctx context.Context, opts *options.Catalog, userInput string, sou
WithSources(sources...).
WithDefaultImagePullSource(opts.Source.Image.DefaultPullSource)

var err error
var platform *image.Platform

if opts.Platform != "" {
Expand Down
3 changes: 3 additions & 0 deletions cmd/syft/internal/options/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ func (cfg *Catalog) AddFlags(flags clio.FlagSet) {

flags.StringVarP(&cfg.Source.Supplier, "source-supplier", "",
"the organization that supplied the component, which often may be the manufacturer, distributor, or repackager")

flags.StringArrayVarP(&cfg.Source.Authors, "authors", "",
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for the delay getting back to you with some feedback, due to holidays and time off it took a while to work out some of the details with the team.

I, personally, think this is part of a larger feature: specifying arbitrary SBOM data that Syft doesn't produce. But we've decided that we should probably kick that can down the road until Syft 2.0 if we decide to make changes to the way any of the current --source-name and these additional flags work, but for now adding flags for these purposeful new 2 features makes the most sense.

As for this PR, there are a few things I think we should change, assuming I understand what's being implemented correctly. Am I correct in saying the author is intended to specify the author of the SBOM, rather than author of the source (the source being "thing that was scanned" -- container, directory, etc.)? I'm assuming so, in which case I think Henry's PR is closer to the end result and we should get them aligned by adding the Authors to the SBOM, rather than the source, and similarly use an appropriate flag: --sbom-author.

For both of these PRs I think we should add exported properties directly on the SBOM for Authors and the arbitrary Properties. I don't see any other spot where it makes sense to put them. And because these aren't really involved in cataloging, I don't think it's useful to add cataloging configuration to set these -- it's just unnecessary and possibly confusing indirection. Instead, they should be set directly on the SBOM during decoding by the format decoders, or by the CLI scan command. If we determine it's important to have this in the configuration that gets passed to CreateSBOM, it can be added later, but we can't remove it after we add it.

As far as the CLI goes, instead of piggybacking on the source config, let's introduce a new top-level sbomConfig (similar to the sourceConfig) where the authors and properties will be set to more accurately reflect how this data is used, and it should live alongside the cataloging config here (with the json/yaml/mapstructure key sbom, so syft config makes sense under sbom:) like:

type actor struct {
  Type string `json:"type" yaml:"type" mapstructure:"type"`
  Name string `json:"name" yaml:"name" mapstructure:"name"`
  Email string `json:"email" yaml:"email" mapstructure:"email"`
}

type sbomConfig struct {
  Authors []actor `json:"authors" yaml:"authors" mapstructure:"authors"`
  authors []string // used for CLI input
  Properties map[string]string `json:"properties" yaml:"properties" mapstructure:"properties"`
  properties []string // used for CLI input
}

func (c *sbomConfig) PostLoad() error {
  // to support env vars e.g. SYFT_SBOM_AUTHOR_NAME, we need custom env var lookups see: https://github.com/anchore/syft/blob/main/cmd/syft/internal/options/registry.go#L35
  // or maybe SYFT_SBOM_AUTHORS using a flattened name=value list as described below
  // do the custom CLI parsing of strings here, we could accept JSON in addition to any more bespoke format
}

By having this configuration structure, the yaml configuration will work as expected, so the last remaining bit is exactly how this gets specified for the flags. I've given a suggestion above, keeping the bespoke name=value that we've adopted elsewhere and for the properties. I think we should be careful about introducing bespoke parsing, like the <type>:<name>:<value> -- the only other spot that I'm aware we use : is in a URI-like spot, where the preceding value is the scheme, whereas this is just a way to split values. Maybe think about adopting a name=value like we use in other spots and the properties also should probably use, e.g. --sbom-author type=person,name=TheName,email=e@mail (though using a comma is another pattern we have that is equivalent to multiple flags, e.g. --from docker,registry == --from docker --from registry, and using the Flatten function, it could also make properties easier e.g. --sbom-property name1=value1,name2=value2, so consider a different separator character. Sorry I don't have a great answer here, but we can finalize this detail fairly quickly with the whole team towards the end; I would lean towards &, but it needs to be escaped in shells, so maybe +?, definitely open to suggestions here).

Again, apologies for the delay and I hope this is understandable enough!

"the authors who created this SBOM, format: 'type:name:email' where type is Person, Organization, or Tool (can be repeated)")
}

func (cfg *Catalog) DescribeFields(descriptions fangs.FieldDescriptionSet) {
Expand Down
35 changes: 35 additions & 0 deletions cmd/syft/internal/options/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (

"github.com/anchore/clio"
stereoscopeFile "github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/syft/syft/source"
"github.com/anchore/syft/syft/source/sourceproviders"
)

type sourceConfig struct {
Name string `json:"name" yaml:"name" mapstructure:"name"`
Version string `json:"version" yaml:"version" mapstructure:"version"`
Supplier string `json:"supplier" yaml:"supplier" mapstructure:"supplier"`
Authors []string `json:"authors" yaml:"authors" mapstructure:"authors"`
Source string `json:"source" yaml:"source" mapstructure:"source"`
BasePath string `yaml:"base-path" json:"base-path" mapstructure:"base-path"` // specify base path for all file paths
File fileSource `json:"file" yaml:"file" mapstructure:"file"`
Expand Down Expand Up @@ -85,3 +87,36 @@ func checkDefaultSourceValues(source string) error {

return nil
}

// ParseAuthors parses author strings in the format "type:name:email" into source.Author structs
func ParseAuthors(authorStrings []string) ([]source.Author, error) {
var authors []source.Author
for _, authorStr := range authorStrings {
parts := strings.Split(authorStr, ":")
if len(parts) < 2 {
return nil, fmt.Errorf("invalid author format '%s', expected 'type:name' or 'type:name:email'", authorStr)
}

authorType := parts[0]
if authorType != "Person" && authorType != "Organization" && authorType != "Tool" {
return nil, fmt.Errorf("invalid author type '%s', must be Person, Organization, or Tool", authorType)
}

name := parts[1]
if name == "" {
return nil, fmt.Errorf("author name cannot be empty")
}

email := ""
if len(parts) >= 3 {
email = parts[2]
}

authors = append(authors, source.Author{
Name: name,
Email: email,
Type: authorType,
})
}
return authors, nil
}
13 changes: 13 additions & 0 deletions cmd/syft/internal/test/integration/encode_decode_cycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) {
// supplier is not available as part of the SBOM Config API since the flag
// is used in conjunction with the SourceConfig which is injected into generateSBOM during scan
originalSBOM.Source.Supplier = "anchore"

originalSBOM.Source.Authors = []source.Author{
{
Name: "Test Author",
Email: "[email protected]",
Type: "Person",
},
{
Name: "Test Organization",
Email: "[email protected]",
Type: "Organization",
},
}
f := encoders.GetByString(test.name)
require.NotNil(t, f)

Expand Down
3 changes: 2 additions & 1 deletion internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package internal
const (
// JSONSchemaVersion is the current schema version output by the JSON encoder
// This is roughly following the "SchemaVer" guidelines for versioning the JSON schema. Please see schema/json/README.md for details on how to increment.
JSONSchemaVersion = "16.1.0"
JSONSchemaVersion = "16.1.1"

// Changelog
// 16.1.1 - added "authors" field to the source object to support SBOM author information.
// 16.1.0 - reformulated the python pdm fields (added "URL" and removed the unused "path" field).

)
Loading
Loading