diff --git a/cmd/syft/cli/commands/attest.go b/cmd/syft/cli/commands/attest.go index 97ade2437c0..030b8352ba0 100644 --- a/cmd/syft/cli/commands/attest.go +++ b/cmd/syft/cli/commands/attest.go @@ -19,12 +19,10 @@ import ( "github.com/anchore/syft/internal/log" "github.com/anchore/syft/syft/event" "github.com/anchore/syft/syft/event/monitor" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/github" - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" - "github.com/anchore/syft/syft/formats/text" + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/spdxjson" + "github.com/anchore/syft/syft/format/spdxtagvalue" + "github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) @@ -37,30 +35,33 @@ const ( ) type attestOptions struct { - options.Config `yaml:",inline" mapstructure:",squash"` - options.SingleOutput `yaml:",inline" mapstructure:",squash"` - options.UpdateCheck `yaml:",inline" mapstructure:",squash"` - options.Catalog `yaml:",inline" mapstructure:",squash"` - options.Attest `yaml:",inline" mapstructure:",squash"` + options.Config `yaml:",inline" mapstructure:",squash"` + options.Output `yaml:",inline" mapstructure:",squash"` + options.UpdateCheck `yaml:",inline" mapstructure:",squash"` + options.Catalog `yaml:",inline" mapstructure:",squash"` + options.Attest `yaml:",inline" mapstructure:",squash"` } func Attest(app clio.Application) *cobra.Command { id := app.ID() - var allowableOutputs []string - for _, f := range formats.AllIDs() { - switch f { - case table.ID, text.ID, github.ID, template.ID: - continue - } - allowableOutputs = append(allowableOutputs, f.String()) - } - opts := &attestOptions{ UpdateCheck: options.DefaultUpdateCheck(), - SingleOutput: options.SingleOutput{ - AllowableOptions: allowableOutputs, - Output: syftjson.ID.String(), + Output: options.Output{ + AllowMultipleOutputs: false, + AllowableOptions: []string{ + string(syftjson.ID), + string(cyclonedxjson.ID), + string(spdxjson.ID), + string(spdxtagvalue.ID), + }, + Outputs: []string{syftjson.ID.String()}, + OutputFile: options.OutputFile{ // nolint:staticcheck + Enabled: false, // explicitly not allowed + }, + OutputTemplate: options.OutputTemplate{ + Enabled: false, // explicitly not allowed + }, }, Catalog: options.DefaultCatalog(), } @@ -95,15 +96,13 @@ func runAttest(id clio.Identification, opts *attestOptions, userInput string) er return fmt.Errorf("unable to build SBOM: %w", err) } - o := opts.Output - - f, err := os.CreateTemp("", o) + f, err := os.CreateTemp("", "syft-attest-") if err != nil { return fmt.Errorf("unable to create temp file: %w", err) } defer os.Remove(f.Name()) - writer, err := opts.SBOMWriter(f.Name()) + writer, err := opts.SBOMWriter() if err != nil { return fmt.Errorf("unable to create SBOM writer: %w", err) } @@ -118,10 +117,21 @@ func runAttest(id clio.Identification, opts *attestOptions, userInput string) er return fmt.Errorf("unable to find cosign in PATH; make sure you have it installed") } + outputNames := opts.OutputNameSet() + var outputName string + switch outputNames.Size() { + case 0: + return fmt.Errorf("no output format specified") + case 1: + outputName = outputNames.List()[0] + default: + return fmt.Errorf("multiple output formats specified: %s", strings.Join(outputNames.List(), ", ")) + } + // Select Cosign predicate type based on defined output type // As orientation, check: https://github.com/sigstore/cosign/blob/main/pkg/cosign/attestation/attestation.go var predicateType string - switch strings.ToLower(o) { + switch strings.ToLower(outputName) { case "cyclonedx-json": predicateType = "cyclonedx" case "spdx-tag-value", "spdx-tv": diff --git a/cmd/syft/cli/commands/convert.go b/cmd/syft/cli/commands/convert.go index f42b7da629e..604c408e537 100644 --- a/cmd/syft/cli/commands/convert.go +++ b/cmd/syft/cli/commands/convert.go @@ -11,7 +11,7 @@ import ( "github.com/anchore/syft/cmd/syft/cli/options" "github.com/anchore/syft/internal" "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/syft/formats" + "github.com/anchore/syft/syft/format" ) const ( @@ -23,7 +23,7 @@ const ( type ConvertOptions struct { options.Config `yaml:",inline" mapstructure:",squash"` - options.MultiOutput `yaml:",inline" mapstructure:",squash"` + options.Output `yaml:",inline" mapstructure:",squash"` options.UpdateCheck `yaml:",inline" mapstructure:",squash"` } @@ -33,6 +33,7 @@ func Convert(app clio.Application) *cobra.Command { opts := &ConvertOptions{ UpdateCheck: options.DefaultUpdateCheck(), + Output: options.DefaultOutput(), } return app.SetupCommand(&cobra.Command{ @@ -63,10 +64,13 @@ func RunConvert(opts *ConvertOptions, userInput string) error { return err } - var reader io.ReadCloser + var reader io.ReadSeekCloser if userInput == "-" { - reader = os.Stdin + // though os.Stdin is an os.File, it does not support seeking + // you will get errors such as "seek /dev/stdin: illegal seek". + // We need to buffer what we read. + reader = internal.NewBufferedSeeker(os.Stdin) } else { f, err := os.Open(userInput) if err != nil { @@ -78,7 +82,7 @@ func RunConvert(opts *ConvertOptions, userInput string) error { reader = f } - s, _, err := formats.Decode(reader) + s, _, _, err := format.Decode(reader) if err != nil { return fmt.Errorf("failed to decode SBOM: %w", err) } diff --git a/cmd/syft/cli/commands/packages.go b/cmd/syft/cli/commands/packages.go index c3a67992f7f..45e7eef1499 100644 --- a/cmd/syft/cli/commands/packages.go +++ b/cmd/syft/cli/commands/packages.go @@ -14,7 +14,6 @@ import ( "github.com/anchore/syft/internal/file" "github.com/anchore/syft/internal/log" "github.com/anchore/syft/syft/artifact" - "github.com/anchore/syft/syft/formats/template" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) @@ -55,14 +54,14 @@ const ( type packagesOptions struct { options.Config `yaml:",inline" mapstructure:",squash"` - options.MultiOutput `yaml:",inline" mapstructure:",squash"` + options.Output `yaml:",inline" mapstructure:",squash"` options.UpdateCheck `yaml:",inline" mapstructure:",squash"` options.Catalog `yaml:",inline" mapstructure:",squash"` } func defaultPackagesOptions() *packagesOptions { return &packagesOptions{ - MultiOutput: options.DefaultOutput(), + Output: options.DefaultOutput(), UpdateCheck: options.DefaultUpdateCheck(), Catalog: options.DefaultCatalog(), } @@ -108,11 +107,6 @@ func validateArgs(cmd *cobra.Command, args []string, error string) error { // nolint:funlen func runPackages(id clio.Identification, opts *packagesOptions, userInput string) error { - err := validatePackageOutputOptions(&opts.MultiOutput) - if err != nil { - return err - } - writer, err := opts.SBOMWriter() if err != nil { return err @@ -235,19 +229,3 @@ func mergeRelationships(cs ...<-chan artifact.Relationship) (relationships []art return relationships } - -func validatePackageOutputOptions(cfg *options.MultiOutput) error { - var usesTemplateOutput bool - for _, o := range cfg.Outputs { - if o == template.ID.String() { - usesTemplateOutput = true - break - } - } - - if usesTemplateOutput && cfg.OutputTemplatePath == "" { - return fmt.Errorf(`must specify path to template file when using "template" output format`) - } - - return nil -} diff --git a/cmd/syft/cli/commands/poweruser.go b/cmd/syft/cli/commands/poweruser.go index b8776e56cc9..fe8496b01ca 100644 --- a/cmd/syft/cli/commands/poweruser.go +++ b/cmd/syft/cli/commands/poweruser.go @@ -14,7 +14,7 @@ import ( "github.com/anchore/syft/cmd/syft/cli/options" "github.com/anchore/syft/internal" "github.com/anchore/syft/syft/artifact" - "github.com/anchore/syft/syft/formats/syftjson" + "github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) @@ -42,6 +42,9 @@ func PowerUser(app clio.Application) *cobra.Command { pkgs.FileClassification.Cataloger.Enabled = true opts := &powerUserOptions{ Catalog: pkgs, + OutputFile: options.OutputFile{ // nolint:staticcheck + Enabled: true, + }, } return app.SetupCommand(&cobra.Command{ @@ -62,7 +65,7 @@ func PowerUser(app clio.Application) *cobra.Command { //nolint:funlen func runPowerUser(id clio.Identification, opts *powerUserOptions, userInput string) error { - writer, err := opts.SBOMWriter(syftjson.Format()) + writer, err := opts.SBOMWriter(syftjson.NewFormatEncoder()) if err != nil { return err } diff --git a/cmd/syft/cli/options/output.go b/cmd/syft/cli/options/output.go index 09c226f3eaa..f6ee7573e4b 100644 --- a/cmd/syft/cli/options/output.go +++ b/cmd/syft/cli/options/output.go @@ -2,99 +2,232 @@ package options import ( "fmt" - "slices" + "sort" + "strings" + + "github.com/hashicorp/go-multierror" + "github.com/scylladb/go-set/strset" "github.com/anchore/clio" - "github.com/anchore/fangs" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/cyclonedxxml" + "github.com/anchore/syft/syft/format/github" + "github.com/anchore/syft/syft/format/spdxjson" + "github.com/anchore/syft/syft/format/spdxtagvalue" + "github.com/anchore/syft/syft/format/syftjson" + "github.com/anchore/syft/syft/format/table" + "github.com/anchore/syft/syft/format/template" + "github.com/anchore/syft/syft/format/text" "github.com/anchore/syft/syft/sbom" ) -// MultiOutput has the standard output options syft accepts: multiple -o, --file, --template -type MultiOutput struct { - Outputs []string `yaml:"output" json:"output" mapstructure:"output"` // -o, the format to use for output - OutputFile `yaml:",inline" json:"" mapstructure:",squash"` - OutputTemplatePath string `yaml:"output-template-path" json:"output-template-path" mapstructure:"output-template-path"` // -t template file to use for output -} - var _ interface { clio.FlagAdder -} = (*MultiOutput)(nil) + clio.PostLoader +} = (*Output)(nil) + +// Output has the standard output options syft accepts: multiple -o, --file, --template +type Output struct { + AllowableOptions []string `yaml:"-" json:"-" mapstructure:"-"` + AllowMultipleOutputs bool `yaml:"-" json:"-" mapstructure:"-"` + Outputs []string `yaml:"output" json:"output" mapstructure:"output"` // -o, the format to use for output + OutputFile `yaml:",inline" json:"" mapstructure:",squash"` + OutputTemplate `yaml:"template" json:"template" mapstructure:"template"` +} -func DefaultOutput() MultiOutput { - return MultiOutput{ - Outputs: []string{string(table.ID)}, +func DefaultOutput() Output { + return Output{ + AllowMultipleOutputs: true, + Outputs: []string{string(table.ID)}, + OutputFile: OutputFile{ + Enabled: true, + }, + OutputTemplate: OutputTemplate{ + Enabled: true, + }, } } -func (o *MultiOutput) AddFlags(flags clio.FlagSet) { - flags.StringArrayVarP(&o.Outputs, "output", "o", - fmt.Sprintf("report output format (= to output to a file), formats=%v", formats.AllIDs())) +func (o *Output) AddFlags(flags clio.FlagSet) { + var names []string + for _, id := range supportedIDs() { + names = append(names, id.String()) + } + sort.Strings(names) - flags.StringVarP(&o.OutputTemplatePath, "template", "t", - "specify the path to a Go template file") + flags.StringArrayVarP(&o.Outputs, "output", "o", + fmt.Sprintf("report output format (= to output to a file), formats=%v", names)) } -func (o *MultiOutput) SBOMWriter() (sbom.Writer, error) { - return makeSBOMWriter(o.Outputs, o.File, o.OutputTemplatePath) +func (o Output) SBOMWriter() (sbom.Writer, error) { + names := o.OutputNameSet() + + if len(o.Outputs) > 1 && !o.AllowMultipleOutputs { + return nil, fmt.Errorf("only one output format is allowed (given %d: %s)", len(o.Outputs), names) + } + + usesTemplateOutput := names.Has(string(template.ID)) + + if usesTemplateOutput && o.OutputTemplate.Path == "" { + return nil, fmt.Errorf(`must specify path to template file when using "template" output format`) + } + + encoders, err := o.Encoders() + if err != nil { + return nil, err + } + + return makeSBOMWriter(o.Outputs, o.File, encoders) } -// SingleOutput allows only 1 output to be specified, with a user able to set what options are allowed by setting AllowableOptions -type SingleOutput struct { - AllowableOptions []string `yaml:"-" json:"-" mapstructure:"-"` - Output string `yaml:"output" json:"output" mapstructure:"output"` - OutputTemplatePath string `yaml:"output-template-path" json:"output-template-path" mapstructure:"output-template-path"` // -t template file to use for output +func (o *Output) Encoders() ([]sbom.FormatEncoder, error) { + // setup all encoders based on the configuration + var list encoderList + + // in the future there will be application configuration options that can be used to set the default output format + list.addWithErr(template.ID)(o.OutputTemplate.formatEncoders()) + list.add(syftjson.ID)(syftjson.NewFormatEncoder()) + list.add(table.ID)(table.NewFormatEncoder()) + list.add(text.ID)(text.NewFormatEncoder()) + list.add(github.ID)(github.NewFormatEncoder()) + list.addWithErr(cyclonedxxml.ID)(cycloneDxXMLEncoders()) + list.addWithErr(cyclonedxjson.ID)(cycloneDxJSONEncoders()) + list.addWithErr(spdxjson.ID)(spdxJSONEncoders()) + list.addWithErr(spdxtagvalue.ID)(spdxTagValueEncoders()) + + return list.encoders, list.err } -var _ clio.FlagAdder = (*SingleOutput)(nil) +func (o Output) OutputNameSet() *strset.Set { + names := strset.New() + for _, output := range o.Outputs { + fields := strings.Split(output, "=") + names.Add(fields[0]) + } -func (o *SingleOutput) AddFlags(flags clio.FlagSet) { - flags.StringVarP(&o.Output, "output", "o", - fmt.Sprintf("report output format, options=%v", o.AllowableOptions)) + return names +} - if slices.Contains(o.AllowableOptions, template.ID.String()) { - flags.StringVarP(&o.OutputTemplatePath, "template", "t", - "specify the path to a Go template file") - } +type encoderList struct { + encoders []sbom.FormatEncoder + err error } -func (o *SingleOutput) SBOMWriter(file string) (sbom.Writer, error) { - return makeSBOMWriter([]string{o.Output}, file, o.OutputTemplatePath) +func (l *encoderList) addWithErr(name sbom.FormatID) func([]sbom.FormatEncoder, error) { + return func(encs []sbom.FormatEncoder, err error) { + if err != nil { + l.err = multierror.Append(l.err, fmt.Errorf("unable to configure %q format encoder: %w", name, err)) + return + } + for _, enc := range encs { + if enc == nil { + l.err = multierror.Append(l.err, fmt.Errorf("unable to configure %q format encoder: nil encoder returned", name)) + continue + } + l.encoders = append(l.encoders, enc) + } + } } -// Deprecated: OutputFile is only the --file argument -type OutputFile struct { - File string `yaml:"file" json:"file" mapstructure:"file"` // --file, the file to write report output to +func (l *encoderList) add(name sbom.FormatID) func(...sbom.FormatEncoder) { + return func(encs ...sbom.FormatEncoder) { + for _, enc := range encs { + if enc == nil { + l.err = multierror.Append(l.err, fmt.Errorf("unable to configure %q format encoder: nil encoder returned", name)) + continue + } + l.encoders = append(l.encoders, enc) + } + } } -var _ interface { - clio.FlagAdder - clio.PostLoader -} = (*OutputFile)(nil) +// TODO: when application configuration is made for this format then this should be ported to the options object +// that is created for that configuration (as done with the template output option) +func cycloneDxXMLEncoders() ([]sbom.FormatEncoder, error) { + var ( + encs []sbom.FormatEncoder + errs error + ) + for _, v := range cyclonedxxml.SupportedVersions() { + enc, err := cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.EncoderConfig{Version: v}) + if err != nil { + errs = multierror.Append(errs, err) + } else { + encs = append(encs, enc) + } + } + return encs, errs +} -func (o *OutputFile) AddFlags(flags clio.FlagSet) { - flags.StringVarP(&o.File, "file", "", - "file to write the default report output to (default is STDOUT)") +// TODO: when application configuration is made for this format then this should be ported to the options object +// that is created for that configuration (as done with the template output option) +func cycloneDxJSONEncoders() ([]sbom.FormatEncoder, error) { + var ( + encs []sbom.FormatEncoder + errs error + ) + for _, v := range cyclonedxjson.SupportedVersions() { + enc, err := cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.EncoderConfig{Version: v}) + if err != nil { + errs = multierror.Append(errs, err) + } else { + encs = append(encs, enc) + } + } + return encs, errs +} - if pfp, ok := flags.(fangs.PFlagSetProvider); ok { - flagSet := pfp.PFlagSet() - flagSet.Lookup("file").Deprecated = "use: output" +// TODO: when application configuration is made for this format then this should be ported to the options object +// that is created for that configuration (as done with the template output option) +func spdxJSONEncoders() ([]sbom.FormatEncoder, error) { + var ( + encs []sbom.FormatEncoder + errs error + ) + for _, v := range spdxjson.SupportedVersions() { + enc, err := spdxjson.NewFormatEncoderWithConfig(spdxjson.EncoderConfig{Version: v}) + if err != nil { + errs = multierror.Append(errs, err) + } else { + encs = append(encs, enc) + } } + return encs, errs } -func (o *OutputFile) PostLoad() error { - if o.File != "" { - file, err := expandFilePath(o.File) +// TODO: when application configuration is made for this format then this should be ported to the options object +// that is created for that configuration (as done with the template output option) +func spdxTagValueEncoders() ([]sbom.FormatEncoder, error) { + var ( + encs []sbom.FormatEncoder + errs error + ) + for _, v := range spdxtagvalue.SupportedVersions() { + enc, err := spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.EncoderConfig{Version: v}) if err != nil { - return err + errs = multierror.Append(errs, err) + } else { + encs = append(encs, enc) } - o.File = file } - return nil + return encs, errs } -func (o *OutputFile) SBOMWriter(f sbom.Format) (sbom.Writer, error) { - return makeSBOMWriterForFormat(f, o.File) +func supportedIDs() []sbom.FormatID { + encs := []sbom.FormatID{ + // encoders that support a single version + syftjson.ID, + github.ID, + table.ID, + text.ID, + template.ID, + + // encoders that support multiple versions + cyclonedxxml.ID, + cyclonedxjson.ID, + spdxtagvalue.ID, + spdxjson.ID, + } + + return encs } diff --git a/cmd/syft/cli/options/output_file.go b/cmd/syft/cli/options/output_file.go new file mode 100644 index 00000000000..b23fb91e13a --- /dev/null +++ b/cmd/syft/cli/options/output_file.go @@ -0,0 +1,56 @@ +package options + +import ( + "github.com/anchore/clio" + "github.com/anchore/fangs" + "github.com/anchore/syft/syft/sbom" +) + +var _ interface { + clio.FlagAdder + clio.PostLoader +} = (*OutputFile)(nil) + +// Deprecated: OutputFile supports the --file to write the SBOM output to +type OutputFile struct { + Enabled bool `yaml:"-" json:"-" mapstructure:"-"` + File string `yaml:"file" json:"file" mapstructure:"file"` +} + +func (o *OutputFile) AddFlags(flags clio.FlagSet) { + if o.Enabled { + flags.StringVarP(&o.File, "file", "", + "file to write the default report output to (default is STDOUT)") + + if pfp, ok := flags.(fangs.PFlagSetProvider); ok { + flagSet := pfp.PFlagSet() + flagSet.Lookup("file").Deprecated = "use: output" + } + } +} + +func (o *OutputFile) PostLoad() error { + if !o.Enabled { + return nil + } + if o.File != "" { + file, err := expandFilePath(o.File) + if err != nil { + return err + } + o.File = file + } + return nil +} + +func (o *OutputFile) SBOMWriter(f sbom.FormatEncoder) (sbom.Writer, error) { + if !o.Enabled { + return nil, nil + } + writer, err := newSBOMMultiWriter(newSBOMWriterDescription(f, o.File)) + if err != nil { + return nil, err + } + + return writer, nil +} diff --git a/cmd/syft/cli/options/output_template.go b/cmd/syft/cli/options/output_template.go new file mode 100644 index 00000000000..0adac8d66f8 --- /dev/null +++ b/cmd/syft/cli/options/output_template.go @@ -0,0 +1,31 @@ +package options + +import ( + "github.com/anchore/clio" + "github.com/anchore/syft/syft/format/template" + "github.com/anchore/syft/syft/sbom" +) + +var _ clio.FlagAdder = (*OutputTemplate)(nil) + +type OutputTemplate struct { + Enabled bool `yaml:"-" json:"-" mapstructure:"-"` + Path string `yaml:"path" json:"path" mapstructure:"path"` // -t template file to use for output +} + +func (o *OutputTemplate) AddFlags(flags clio.FlagSet) { + if o.Enabled { + flags.StringVarP(&o.Path, "template", "t", + "specify the path to a Go template file") + } +} + +func (o OutputTemplate) formatEncoders() ([]sbom.FormatEncoder, error) { + if !o.Enabled { + return nil, nil + } + enc, err := template.NewFormatEncoder(template.EncoderConfig{ + TemplatePath: o.Path, + }) + return []sbom.FormatEncoder{enc}, err +} diff --git a/cmd/syft/cli/options/output_test.go b/cmd/syft/cli/options/output_test.go new file mode 100644 index 00000000000..5715ff08427 --- /dev/null +++ b/cmd/syft/cli/options/output_test.go @@ -0,0 +1,179 @@ +package options + +import ( + "testing" + + "github.com/scylladb/go-set/strset" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format" + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/cyclonedxxml" + "github.com/anchore/syft/syft/format/github" + "github.com/anchore/syft/syft/format/spdxjson" + "github.com/anchore/syft/syft/format/spdxtagvalue" + "github.com/anchore/syft/syft/format/syftjson" + "github.com/anchore/syft/syft/format/table" + "github.com/anchore/syft/syft/format/template" + "github.com/anchore/syft/syft/format/text" + "github.com/anchore/syft/syft/sbom" +) + +func Test_getEncoders(t *testing.T) { + allDefaultEncoderNames := strset.New() + for _, id := range supportedIDs() { + allDefaultEncoderNames.Add(id.String()) + } + + opts := DefaultOutput() + opts.OutputTemplate.Path = "somewhere" + + encoders, err := opts.Encoders() + require.NoError(t, err) + require.NotEmpty(t, encoders) + + encoderNames := strset.New() + for _, e := range encoders { + encoderNames.Add(e.ID().String()) + } + + assert.ElementsMatch(t, allDefaultEncoderNames.List(), encoderNames.List(), "not all encoders are expressed") +} + +func Test_EncoderCollection_ByString_IDOnly_Defaults(t *testing.T) { + tests := []struct { + name string + want sbom.FormatID + }{ + // SPDX Tag-Value + { + name: "spdx", + want: spdxtagvalue.ID, + }, + { + name: "spdx-tag-value", + want: spdxtagvalue.ID, + }, + { + name: "spdx-tv", + want: spdxtagvalue.ID, + }, + { + name: "spdxtv", // clean variant + want: spdxtagvalue.ID, + }, + + // SPDX JSON + { + name: "spdx-json", + want: spdxjson.ID, + }, + { + name: "spdxjson", // clean variant + want: spdxjson.ID, + }, + + // Cyclonedx JSON + { + name: "cyclonedx-json", + want: cyclonedxjson.ID, + }, + { + name: "cyclonedxjson", // clean variant + want: cyclonedxjson.ID, + }, + + // Cyclonedx XML + { + name: "cdx", + want: cyclonedxxml.ID, + }, + { + name: "cyclone", + want: cyclonedxxml.ID, + }, + { + name: "cyclonedx", + want: cyclonedxxml.ID, + }, + { + name: "cyclonedx-xml", + want: cyclonedxxml.ID, + }, + { + name: "cyclonedxxml", // clean variant + want: cyclonedxxml.ID, + }, + + // Syft Table + { + name: "table", + want: table.ID, + }, + { + name: "syft-table", + want: table.ID, + }, + + // Syft Text + { + name: "text", + want: text.ID, + }, + { + name: "syft-text", + want: text.ID, + }, + + // Syft JSON + { + name: "json", + want: syftjson.ID, + }, + { + name: "syft-json", + want: syftjson.ID, + }, + { + name: "syftjson", // clean variant + want: syftjson.ID, + }, + + // GitHub JSON + { + name: "github", + want: github.ID, + }, + { + name: "github-json", + want: github.ID, + }, + + // Syft template + { + name: "template", + want: template.ID, + }, + } + + opts := DefaultOutput() + opts.OutputTemplate.Path = "somewhere" + + defaultEncoders, err := opts.Encoders() + require.NoError(t, err) + + encoders := format.NewEncoderCollection(defaultEncoders...) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + f := encoders.GetByString(tt.name) + if tt.want == "" { + require.Nil(t, f) + return + } + require.NotNil(t, f) + assert.Equal(t, tt.want, f.ID()) + }) + } +} diff --git a/cmd/syft/cli/options/writer.go b/cmd/syft/cli/options/writer.go index 0472451691b..77feda56245 100644 --- a/cmd/syft/cli/options/writer.go +++ b/cmd/syft/cli/options/writer.go @@ -6,16 +6,17 @@ import ( "io" "os" "path" + "sort" "strings" "github.com/hashicorp/go-multierror" "github.com/mitchellh/go-homedir" + "github.com/scylladb/go-set/strset" "github.com/anchore/syft/internal/bus" "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" + "github.com/anchore/syft/syft/format" + "github.com/anchore/syft/syft/format/table" "github.com/anchore/syft/syft/sbom" ) @@ -28,8 +29,8 @@ var _ interface { // makeSBOMWriter creates a sbom.Writer for output or returns an error. this will either return a valid writer // or an error but neither both and if there is no error, sbom.Writer.Close() should be called -func makeSBOMWriter(outputs []string, defaultFile, templateFilePath string) (sbom.Writer, error) { - outputOptions, err := parseSBOMOutputFlags(outputs, defaultFile, templateFilePath) +func makeSBOMWriter(outputs []string, defaultFile string, encoders []sbom.FormatEncoder) (sbom.Writer, error) { + outputOptions, err := parseSBOMOutputFlags(outputs, defaultFile, encoders) if err != nil { return nil, err } @@ -42,18 +43,10 @@ func makeSBOMWriter(outputs []string, defaultFile, templateFilePath string) (sbo return writer, nil } -// makeSBOMWriterForFormat creates a sbom.Writer for for the given format or returns an error. -func makeSBOMWriterForFormat(format sbom.Format, path string) (sbom.Writer, error) { - writer, err := newSBOMMultiWriter(newSBOMWriterDescription(format, path)) - if err != nil { - return nil, err - } - - return writer, nil -} - // parseSBOMOutputFlags utility to parse command-line option strings and retain the existing behavior of default format and file -func parseSBOMOutputFlags(outputs []string, defaultFile, templateFilePath string) (out []sbomWriterDescription, errs error) { +func parseSBOMOutputFlags(outputs []string, defaultFile string, encoders []sbom.FormatEncoder) (out []sbomWriterDescription, errs error) { + encoderCollection := format.NewEncoderCollection(encoders...) + // always should have one option -- we generally get the default of "table", but just make sure if len(outputs) == 0 { outputs = append(outputs, table.ID.String()) @@ -76,29 +69,77 @@ func parseSBOMOutputFlags(outputs []string, defaultFile, templateFilePath string file = parts[1] } - format := formats.ByName(name) - if format == nil { - errs = multierror.Append(errs, fmt.Errorf(`unsupported output format "%s", supported formats are: %+v`, name, formats.AllIDs())) + enc := encoderCollection.GetByString(name) + if enc == nil { + errs = multierror.Append(errs, fmt.Errorf(`unsupported output format "%s", supported formats are: %+v`, name, formatVersionOptions(encoderCollection.NameVersions()))) continue } - if tmpl, ok := format.(template.OutputFormat); ok { - tmpl.SetTemplatePath(templateFilePath) - format = tmpl + out = append(out, newSBOMWriterDescription(enc, file)) + } + return out, errs +} + +// formatVersionOptions takes a list like ["github-json", "syft-json@11.0.0", "cyclonedx-xml@1.0", "cyclondx-xml@1.1"...] +// and formats it into a human-readable string like: +// +// Available formats: +// - cyclonedx-json @ 1.2, 1.3, 1.4, 1.5 +// - cyclonedx-xml @ 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 +// - github-json +// - spdx-json @ 2.2, 2.3 +// - spdx-tag-value @ 2.1, 2.2, 2.3 +// - syft-json +// - syft-table +// - syft-text +// - template +func formatVersionOptions(nameVersionPairs []string) string { + availableVersions := make(map[string][]string) + availableFormats := strset.New() + for _, nameVersion := range nameVersionPairs { + fields := strings.SplitN(nameVersion, "@", 2) + if len(fields) == 2 { + availableVersions[fields[0]] = append(availableVersions[fields[0]], fields[1]) + } + availableFormats.Add(fields[0]) + } + + // find any formats with exactly one version -- remove them from the version map + for name, versions := range availableVersions { + if len(versions) == 1 { + delete(availableVersions, name) } + } + + sortedAvailableFormats := availableFormats.List() + sort.Strings(sortedAvailableFormats) + + var s strings.Builder - out = append(out, newSBOMWriterDescription(format, file)) + s.WriteString("\n") + s.WriteString("Available formats:") + + for _, name := range sortedAvailableFormats { + s.WriteString("\n") + + s.WriteString(fmt.Sprintf(" - %s", name)) + + if len(availableVersions[name]) > 0 { + s.WriteString(" @ ") + s.WriteString(strings.Join(availableVersions[name], ", ")) + } } - return out, errs + + return s.String() } // sbomWriterDescription Format and path strings used to create sbom.Writer type sbomWriterDescription struct { - Format sbom.Format + Format sbom.FormatEncoder Path string } -func newSBOMWriterDescription(f sbom.Format, p string) sbomWriterDescription { +func newSBOMWriterDescription(f sbom.FormatEncoder, p string) sbomWriterDescription { expandedPath, err := homedir.Expand(p) if err != nil { log.Warnf("could not expand given writer output path=%q: %w", p, err) @@ -171,7 +212,7 @@ func (m *sbomMultiWriter) Write(s sbom.SBOM) (errs error) { // sbomStreamWriter implements sbom.Writer for a given format and io.Writer, also providing a close function for cleanup type sbomStreamWriter struct { - format sbom.Format + format sbom.FormatEncoder out io.Writer } @@ -191,7 +232,7 @@ func (w *sbomStreamWriter) Close() error { // sbomPublisher implements sbom.Writer that publishes results to the event bus type sbomPublisher struct { - format sbom.Format + format sbom.FormatEncoder } // Write the provided SBOM to the data stream diff --git a/cmd/syft/cli/options/writer_test.go b/cmd/syft/cli/options/writer_test.go index dfdc60a394b..165fd6d52ff 100644 --- a/cmd/syft/cli/options/writer_test.go +++ b/cmd/syft/cli/options/writer_test.go @@ -8,43 +8,71 @@ import ( "github.com/docker/docker/pkg/homedir" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/anchore/syft/syft/sbom" ) func Test_MakeSBOMWriter(t *testing.T) { tests := []struct { + name string outputs []string wantErr assert.ErrorAssertionFunc }{ { + name: "go case", outputs: []string{"json"}, wantErr: assert.NoError, }, { + name: "multiple", outputs: []string{"table", "json"}, wantErr: assert.NoError, }, { + name: "unknown format", outputs: []string{"unknown"}, wantErr: func(t assert.TestingT, err error, bla ...interface{}) bool { - return assert.ErrorContains(t, err, `unsupported output format "unknown", supported formats are: [`) + return assert.ErrorContains(t, err, `unsupported output format "unknown", supported formats are:`) }, }, } for _, tt := range tests { - _, err := makeSBOMWriter(tt.outputs, "", "") - tt.wantErr(t, err) + t.Run(tt.name, func(t *testing.T) { + opt := DefaultOutput() + encoders, err := opt.Encoders() + require.NoError(t, err) + _, err = makeSBOMWriter(tt.outputs, "", encoders) + tt.wantErr(t, err) + }) } } -func dummyEncoder(io.Writer, sbom.SBOM) error { +func dummyFormat(name string) sbom.FormatEncoder { + return dummyEncoder{name: name} +} + +var _ sbom.FormatEncoder = (*dummyEncoder)(nil) + +type dummyEncoder struct { + name string +} + +func (d dummyEncoder) ID() sbom.FormatID { + return sbom.FormatID(d.name) +} + +func (d dummyEncoder) Aliases() []string { return nil } -func dummyFormat(name string) sbom.Format { - return sbom.NewFormat(sbom.AnyVersion, dummyEncoder, nil, nil, sbom.FormatID(name)) +func (d dummyEncoder) Version() string { + return sbom.AnyVersion +} + +func (d dummyEncoder) Encode(writer io.Writer, s sbom.SBOM) error { + return nil } func Test_newSBOMMultiWriter(t *testing.T) { @@ -227,3 +255,39 @@ func Test_newSBOMWriterDescription(t *testing.T) { }) } } + +func Test_formatVersionOptions(t *testing.T) { + + tests := []struct { + name string + nameVersionPairs []string + want string + }{ + { + name: "gocase", + nameVersionPairs: []string{ + "cyclonedx-json@1.2", "cyclonedx-json@1.3", "cyclonedx-json@1.4", "cyclonedx-json@1.5", + "cyclonedx-xml@1.0", "cyclonedx-xml@1.1", "cyclonedx-xml@1.2", "cyclonedx-xml@1.3", + "cyclonedx-xml@1.4", "cyclonedx-xml@1.5", "github-json", "spdx-json@2.2", "spdx-json@2.3", + "spdx-tag-value@2.1", "spdx-tag-value@2.2", "spdx-tag-value@2.3", "syft-json@11.0.0", + "syft-table", "syft-text", "template", + }, + want: ` +Available formats: + - cyclonedx-json @ 1.2, 1.3, 1.4, 1.5 + - cyclonedx-xml @ 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 + - github-json + - spdx-json @ 2.2, 2.3 + - spdx-tag-value @ 2.1, 2.2, 2.3 + - syft-json + - syft-table + - syft-text + - template`, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, formatVersionOptions(tt.nameVersionPairs)) + }) + } +} diff --git a/go.mod b/go.mod index 519656c017f..007457d93ad 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/charmbracelet/lipgloss v0.9.1 github.com/dave/jennifer v1.7.0 github.com/deitch/magic v0.0.0-20230404182410-1ff89d7342da - github.com/docker/distribution v2.8.3+incompatible // indirect + github.com/distribution/reference v0.5.0 github.com/docker/docker v24.0.6+incompatible github.com/dustin/go-humanize v1.0.1 github.com/facebookincubator/nvdtools v0.1.5 @@ -55,6 +55,7 @@ require ( github.com/pelletier/go-toml v1.9.5 github.com/saferwall/pe v1.4.7 github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d + github.com/sanity-io/litter v1.5.5 github.com/sassoftware/go-rpmutils v0.2.0 // pinned to pull in 386 arch fix: https://github.com/scylladb/go-set/commit/cc7b2070d91ebf40d233207b633e28f5bd8f03a5 github.com/scylladb/go-set v1.0.3-0.20200225121959-cc7b2070d91e @@ -71,16 +72,10 @@ require ( github.com/zyedidia/generic v1.2.2-0.20230320175451-4410d2372cb1 golang.org/x/mod v0.13.0 golang.org/x/net v0.17.0 - golang.org/x/term v0.13.0 // indirect gopkg.in/yaml.v3 v3.0.1 modernc.org/sqlite v1.26.0 ) -require ( - github.com/distribution/reference v0.5.0 - github.com/sanity-io/litter v1.5.5 -) - require ( dario.cat/mergo v1.0.0 // indirect github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 // indirect @@ -113,6 +108,7 @@ require ( github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/cli v24.0.0+incompatible // indirect + github.com/docker/distribution v2.8.3+incompatible // indirect github.com/docker/docker-credential-helpers v0.7.0 // indirect github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect @@ -209,6 +205,7 @@ require ( golang.org/x/crypto v0.14.0 // indirect golang.org/x/sync v0.3.0 // indirect golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect golang.org/x/tools v0.13.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect diff --git a/internal/buffered_seek_reader.go b/internal/buffered_seek_reader.go new file mode 100644 index 00000000000..93e7e981b1b --- /dev/null +++ b/internal/buffered_seek_reader.go @@ -0,0 +1,84 @@ +package internal + +import ( + "bytes" + "errors" + "io" + + "github.com/anchore/syft/internal/log" +) + +var _ io.ReadSeekCloser = (*bufferedSeekReader)(nil) + +// bufferedSeekReader wraps an io.ReadCloser to provide io.Seeker functionality. +// It only supports seeking from the start and cannot seek past what has already been read. +type bufferedSeekReader struct { + r io.ReadCloser + buf *bytes.Reader + data []byte + pos int64 + closed bool +} + +func NewBufferedSeeker(rc io.ReadCloser) io.ReadSeekCloser { + return &bufferedSeekReader{ + r: rc, + } +} + +func (bs *bufferedSeekReader) Read(p []byte) (int, error) { + if bs.closed { + return 0, errors.New("cannot read from closed reader") + } + if bs.pos == int64(len(bs.data)) { + // if we're at the end of our buffer, read more data into it + tmp := make([]byte, len(p)) + + n, err := bs.r.Read(tmp) + if err != nil && err != io.EOF { + return 0, err + } else if err == io.EOF { + bs.closed = true + } + bs.data = append(bs.data, tmp[:n]...) + bs.buf = bytes.NewReader(bs.data) + } + + n, err := bs.buf.ReadAt(p, bs.pos) + if err != nil && err != io.EOF { + log.WithFields("error", err).Trace("buffered seek reader failed to read from underlying reader") + } + bs.pos += int64(n) + + return n, nil +} + +func (bs *bufferedSeekReader) Seek(offset int64, whence int) (int64, error) { + var abs int64 + switch whence { + case io.SeekStart: + abs = offset + case io.SeekCurrent: + abs = bs.pos + offset + case io.SeekEnd: + return 0, errors.New("'SeekEnd' not supported") + default: + return 0, errors.New("invalid seek option") + } + + if abs < 0 { + return 0, errors.New("unable to seek before start") + } + + if abs > int64(len(bs.data)) { + return 0, errors.New("unable to seek past read data") + } + + bs.pos = abs + return bs.pos, nil +} + +func (bs *bufferedSeekReader) Close() error { + bs.closed = true + return bs.r.Close() +} diff --git a/internal/buffered_seek_reader_test.go b/internal/buffered_seek_reader_test.go new file mode 100644 index 00000000000..c343a3a5e13 --- /dev/null +++ b/internal/buffered_seek_reader_test.go @@ -0,0 +1,141 @@ +package internal + +import ( + "bytes" + "io" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBufferedSeeker_Read(t *testing.T) { + tests := []struct { + name string + initialData string + readLengths []int + expectedReads []string + expectError bool + }{ + { + name: "go case (read)", + initialData: "Hello, World!", + readLengths: []int{5}, + expectedReads: []string{"Hello"}, + }, + { + name: "multiple reads", + initialData: "Hello, World!", + readLengths: []int{5, 8}, + expectedReads: []string{"Hello", ", World!"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString(tt.initialData))) + + for i, length := range tt.readLengths { + buf := make([]byte, length) + n, err := bs.Read(buf) + + if !tt.expectError { + assert.NoError(t, err) + assert.Equalf(t, tt.expectedReads[i], string(buf[:n]), "read index %d", i) + } else { + assert.Error(t, err) + } + } + }) + } +} + +func TestBufferedSeeker_Seek(t *testing.T) { + tests := []struct { + name string + initialData string + readLengths []int + seekOffsets []int64 + seekWhence []int + expectedReads []string + seekError require.ErrorAssertionFunc + readError require.ErrorAssertionFunc + }{ + { + name: "seek start 0 without read first", + initialData: "Hello, World!", + readLengths: []int{5}, + seekOffsets: []int64{0}, + seekWhence: []int{io.SeekStart}, + expectedReads: []string{"Hello"}, + }, + { + name: "read + seek back", + initialData: "Hello, World!", + readLengths: []int{5, 8, 8}, + seekOffsets: []int64{-1, -1, 2}, + seekWhence: []int{io.SeekStart, io.SeekStart, io.SeekStart}, + expectedReads: []string{"Hello", ", World!", "llo, Wor"}, + }, + { + name: "seek past read data", + initialData: "Hello, World!", + readLengths: []int{5}, + seekOffsets: []int64{20}, + seekWhence: []int{io.SeekStart}, + expectedReads: []string{""}, + seekError: require.Error, + }, + { + name: "seek to end", + initialData: "Hello, World!", + readLengths: []int{-1}, + seekOffsets: []int64{20}, + seekWhence: []int{io.SeekEnd}, + expectedReads: []string{""}, + seekError: require.Error, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.seekError == nil { + tt.seekError = require.NoError + } + if tt.readError == nil { + tt.readError = require.NoError + } + + bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString(tt.initialData))) + + for i, length := range tt.readLengths { + if len(tt.seekOffsets) > i && tt.seekOffsets[i] >= 0 { + _, err := bs.Seek(tt.seekOffsets[i], tt.seekWhence[i]) + tt.seekError(t, err) + if err != nil { + continue + } + } + + if length >= 0 { + buf := make([]byte, length) + n, err := bs.Read(buf) + tt.readError(t, err) + if err != nil { + continue + } + assert.Equalf(t, tt.expectedReads[i], string(buf[:n]), "read index %d", i) + } + } + }) + } +} + +func TestBufferedSeeker_Close(t *testing.T) { + bs := NewBufferedSeeker(io.NopCloser(bytes.NewBufferString("Hello, World!"))) + err := bs.Close() + assert.NoError(t, err) + n, err := bs.Read(make([]byte, 1)) + assert.Equal(t, 0, n) + assert.Error(t, err) +} diff --git a/schema/cyclonedx/Makefile b/schema/cyclonedx/Makefile index 70fbd9c3350..277cbbfae8e 100644 --- a/schema/cyclonedx/Makefile +++ b/schema/cyclonedx/Makefile @@ -1,7 +1,11 @@ .DEFAULT_GOAL := validate-schema .PHONY: validate-schema validate-schema: - go run ../../cmd/syft/main.go ubuntu:latest -vv -o cyclonedx > bom.xml + @echo "Generating CycloneDX SBOMs..." + go run ../../cmd/syft/main.go ubuntu:latest -v -o cyclonedx-xml=bom.xml -o cyclonedx-json=bom.json + + @echo "\nValidating CycloneDX XML..." xmllint --noout --schema ./cyclonedx.xsd bom.xml - go run ../../cmd/syft/main.go ubuntu:latest -vv -o cyclonedx-json > bom.json + + @echo "\nValidating CycloneDX JSON..." ../../.tool/yajsv -s cyclonedx.json bom.json diff --git a/schema/cyclonedx/README.md b/schema/cyclonedx/README.md index b9278db66c4..64ae62a52a1 100644 --- a/schema/cyclonedx/README.md +++ b/schema/cyclonedx/README.md @@ -5,3 +5,5 @@ however, this tool does not know how to deal with references from HTTP, only the local filesystem. For this reason we've included a copy of all schemas needed to validate `syft` output, modified to reference local copies of dependent schemas. + +You can get the latest schemas from the [CycloneDX specifications repo](https://github.com/CycloneDX/specification/tree/master/schema). diff --git a/schema/cyclonedx/cyclonedx.json b/schema/cyclonedx/cyclonedx.json index dafd884524c..14ba745f023 100644 --- a/schema/cyclonedx/cyclonedx.json +++ b/schema/cyclonedx/cyclonedx.json @@ -1,20 +1,19 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "$id": "http://cyclonedx.org/schema/bom-1.5.schema.json", "type": "object", "title": "CycloneDX Software Bill of Materials Standard", "$comment" : "CycloneDX JSON schema is published under the terms of the Apache License 2.0.", "required": [ "bomFormat", - "specVersion", - "version" + "specVersion" ], "additionalProperties": false, "properties": { "$schema": { "type": "string", "enum": [ - "http://cyclonedx.org/schema/bom-1.4.schema.json" + "http://cyclonedx.org/schema/bom-1.5.schema.json" ] }, "bomFormat": { @@ -29,7 +28,7 @@ "type": "string", "title": "CycloneDX Specification Version", "description": "The version of the CycloneDX specification a BOM conforms to (starting at version 1.2).", - "examples": ["1.4"] + "examples": ["1.5"] }, "serialNumber": { "type": "string", @@ -42,6 +41,7 @@ "type": "integer", "title": "BOM Version", "description": "Whenever an existing BOM is modified, either manually or through automated processes, the version of the BOM SHOULD be incremented by 1. When a system is presented with multiple BOMs with identical serial numbers, the system SHOULD use the most recent version of the BOM. The default version is '1'.", + "minimum": 1, "default": 1, "examples": [1] }, @@ -52,7 +52,6 @@ }, "components": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/component"}, "uniqueItems": true, "title": "Components", @@ -60,7 +59,6 @@ }, "services": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/service"}, "uniqueItems": true, "title": "Services", @@ -68,14 +66,12 @@ }, "externalReferences": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/externalReference"}, "title": "External References", - "description": "External references provide a way to document systems, sites, and information that may be relevant but which are not included with the BOM." + "description": "External references provide a way to document systems, sites, and information that may be relevant, but are not included with the BOM. They may also establish specific relationships within or external to the BOM." }, "dependencies": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/dependency"}, "uniqueItems": true, "title": "Dependencies", @@ -83,20 +79,40 @@ }, "compositions": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/compositions"}, "uniqueItems": true, "title": "Compositions", - "description": "Compositions describe constituent parts (including components, services, and dependency relationships) and their completeness." + "description": "Compositions describe constituent parts (including components, services, and dependency relationships) and their completeness. The completeness of vulnerabilities expressed in a BOM may also be described." }, "vulnerabilities": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/vulnerability"}, "uniqueItems": true, "title": "Vulnerabilities", "description": "Vulnerabilities identified in components or services." }, + "annotations": { + "type": "array", + "items": {"$ref": "#/definitions/annotations"}, + "uniqueItems": true, + "title": "Annotations", + "description": "Comments made by people, organizations, or tools about any object with a bom-ref, such as components, services, vulnerabilities, or the BOM itself. Unlike inventory information, annotations may contain opinion or commentary from various stakeholders. Annotations may be inline (with inventory) or externalized via BOM-Link, and may optionally be signed." + }, + "formulation": { + "type": "array", + "items": {"$ref": "#/definitions/formula"}, + "uniqueItems": true, + "title": "Formulation", + "description": "Describes how a component or service was manufactured or deployed. This is achieved through the use of formulas, workflows, tasks, and steps, which declare the precise steps to reproduce along with the observed formulas describing the steps which transpired in the manufacturing process." + }, + "properties": { + "type": "array", + "title": "Properties", + "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", + "items": { + "$ref": "#/definitions/property" + } + }, "signature": { "$ref": "#/definitions/signature", "title": "Signature", @@ -105,8 +121,42 @@ }, "definitions": { "refType": { - "$comment": "Identifier-DataType for interlinked elements.", - "type": "string" + "description": "Identifier for referable and therefore interlink-able elements.", + "type": "string", + "minLength": 1, + "$comment": "value SHOULD not start with the BOM-Link intro 'urn:cdx:'" + }, + "refLinkType": { + "description": "Descriptor for an element identified by the attribute 'bom-ref' in the same BOM document.\nIn contrast to `bomLinkElementType`.", + "allOf": [{"$ref": "#/definitions/refType"}] + }, + "bomLinkDocumentType": { + "title": "BOM-Link Document", + "description": "Descriptor for another BOM document. See https://cyclonedx.org/capabilities/bomlink/", + "type": "string", + "format": "iri-reference", + "pattern": "^urn:cdx:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/[1-9][0-9]*$", + "$comment": "part of the pattern is based on `bom.serialNumber`'s pattern" + }, + "bomLinkElementType": { + "title": "BOM-Link Element", + "description": "Descriptor for an element in a BOM document. See https://cyclonedx.org/capabilities/bomlink/", + "type": "string", + "format": "iri-reference", + "pattern": "^urn:cdx:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/[1-9][0-9]*#.+$", + "$comment": "part of the pattern is based on `bom.serialNumber`'s pattern" + }, + "bomLink": { + "anyOf": [ + { + "title": "BOM-Link Document", + "$ref": "#/definitions/bomLinkDocumentType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] }, "metadata": { "type": "object", @@ -119,18 +169,90 @@ "title": "Timestamp", "description": "The date and time (timestamp) when the BOM was created." }, - "tools": { + "lifecycles": { "type": "array", - "title": "Creation Tools", - "description": "The tool(s) used in the creation of the BOM.", - "additionalItems": false, - "items": {"$ref": "#/definitions/tool"} + "title": "Lifecycles", + "description": "", + "items": { + "type": "object", + "title": "Lifecycle", + "description": "The product lifecycle(s) that this BOM represents.", + "oneOf": [ + { + "required": ["phase"], + "additionalProperties": false, + "properties": { + "phase": { + "type": "string", + "title": "Phase", + "description": "A pre-defined phase in the product lifecycle.\n\n* __design__ = BOM produced early in the development lifecycle containing inventory of components and services that are proposed or planned to be used. The inventory may need to be procured, retrieved, or resourced prior to use.\n* __pre-build__ = BOM consisting of information obtained prior to a build process and may contain source files and development artifacts and manifests. The inventory may need to be resolved and retrieved prior to use.\n* __build__ = BOM consisting of information obtained during a build process where component inventory is available for use. The precise versions of resolved components are usually available at this time as well as the provenance of where the components were retrieved from.\n* __post-build__ = BOM consisting of information obtained after a build process has completed and the resulting components(s) are available for further analysis. Built components may exist as the result of a CI/CD process, may have been installed or deployed to a system or device, and may need to be retrieved or extracted from the system or device.\n* __operations__ = BOM produced that represents inventory that is running and operational. This may include staging or production environments and will generally encompass multiple SBOMs describing the applications and operating system, along with HBOMs describing the hardware that makes up the system. Operations Bill of Materials (OBOM) can provide full-stack inventory of runtime environments, configurations, and additional dependencies.\n* __discovery__ = BOM consisting of information observed through network discovery providing point-in-time enumeration of embedded, on-premise, and cloud-native services such as server applications, connected devices, microservices, and serverless functions.\n* __decommission__ = BOM containing inventory that will be, or has been retired from operations.", + "enum": [ + "design", + "pre-build", + "build", + "post-build", + "operations", + "discovery", + "decommission" + ] + } + } + }, + { + "required": ["name"], + "additionalProperties": false, + "properties": { + "name": { + "type": "string", + "title": "Name", + "description": "The name of the lifecycle phase" + }, + "description": { + "type": "string", + "title": "Description", + "description": "The description of the lifecycle phase" + } + } + } + ] + } + }, + "tools": { + "oneOf": [ + { + "type": "object", + "title": "Creation Tools", + "description": "The tool(s) used in the creation of the BOM.", + "additionalProperties": false, + "properties": { + "components": { + "type": "array", + "items": {"$ref": "#/definitions/component"}, + "uniqueItems": true, + "title": "Components", + "description": "A list of software and hardware components used as tools" + }, + "services": { + "type": "array", + "items": {"$ref": "#/definitions/service"}, + "uniqueItems": true, + "title": "Services", + "description": "A list of services used as tools. This may include microservices, function-as-a-service, and other types of network or intra-process services." + } + } + }, + { + "type": "array", + "title": "Creation Tools (legacy)", + "description": "[Deprecated] The tool(s) used in the creation of the BOM.", + "items": {"$ref": "#/definitions/tool"} + } + ] }, "authors" :{ "type": "array", "title": "Authors", "description": "The person(s) who created the BOM. Authors are common in BOMs created through manual processes. BOMs created through automated means may not have authors.", - "additionalItems": false, "items": {"$ref": "#/definitions/organizationalContact"} }, "component": { @@ -149,16 +271,13 @@ "$ref": "#/definitions/organizationalEntity" }, "licenses": { - "type": "array", "title": "BOM License(s)", - "additionalItems": false, - "items": {"$ref": "#/definitions/licenseChoice"} + "$ref": "#/definitions/licenseChoice" }, "properties": { "type": "array", "title": "Properties", "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", - "additionalItems": false, "items": {"$ref": "#/definitions/property"} } } @@ -166,7 +285,7 @@ "tool": { "type": "object", "title": "Tool", - "description": "Information about the automated or manual tool used", + "description": "[Deprecated] - DO NOT USE. This will be removed in a future version. This will be removed in a future version. Use component or service instead. Information about the automated or manual tool used", "additionalProperties": false, "properties": { "vendor": { @@ -186,17 +305,15 @@ }, "hashes": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/hash"}, "title": "Hashes", "description": "The hashes of the tool (if applicable)." }, "externalReferences": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/externalReference"}, "title": "External References", - "description": "External references provide a way to document systems, sites, and information that may be relevant but which are not included with the BOM." + "description": "External references provide a way to document systems, sites, and information that may be relevant, but are not included with the BOM. They may also establish specific relationships within or external to the BOM." } } }, @@ -206,6 +323,11 @@ "description": "", "additionalProperties": false, "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the object elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, "name": { "type": "string", "title": "Name", @@ -228,7 +350,6 @@ "type": "array", "title": "Contact", "description": "A contact at the organization. Multiple contacts are allowed.", - "additionalItems": false, "items": {"$ref": "#/definitions/organizationalContact"} } } @@ -239,6 +360,11 @@ "description": "", "additionalProperties": false, "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the object elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, "name": { "type": "string", "title": "Name", @@ -276,13 +402,17 @@ "framework", "library", "container", + "platform", "operating-system", "device", + "device-driver", "firmware", - "file" + "file", + "machine-learning-model", + "data" ], "title": "Component Type", - "description": "Specifies the type of component. For software components, classify as application if no more specific appropriate classification is available or cannot be determined for the component. Types include:\n\n* __application__ = A software application. Refer to [https://en.wikipedia.org/wiki/Application_software](https://en.wikipedia.org/wiki/Application_software) for information about applications.\n* __framework__ = A software framework. Refer to [https://en.wikipedia.org/wiki/Software_framework](https://en.wikipedia.org/wiki/Software_framework) for information on how frameworks vary slightly from libraries.\n* __library__ = A software library. Refer to [https://en.wikipedia.org/wiki/Library_(computing)](https://en.wikipedia.org/wiki/Library_(computing))\n for information about libraries. All third-party and open source reusable components will likely be a library. If the library also has key features of a framework, then it should be classified as a framework. If not, or is unknown, then specifying library is RECOMMENDED.\n* __container__ = A packaging and/or runtime format, not specific to any particular technology, which isolates software inside the container from software outside of a container through virtualization technology. Refer to [https://en.wikipedia.org/wiki/OS-level_virtualization](https://en.wikipedia.org/wiki/OS-level_virtualization)\n* __operating-system__ = A software operating system without regard to deployment model (i.e. installed on physical hardware, virtual machine, image, etc) Refer to [https://en.wikipedia.org/wiki/Operating_system](https://en.wikipedia.org/wiki/Operating_system)\n* __device__ = A hardware device such as a processor, or chip-set. A hardware device containing firmware SHOULD include a component for the physical hardware itself, and another component of type 'firmware' or 'operating-system' (whichever is relevant), describing information about the software running on the device.\n* __firmware__ = A special type of software that provides low-level control over a devices hardware. Refer to [https://en.wikipedia.org/wiki/Firmware](https://en.wikipedia.org/wiki/Firmware)\n* __file__ = A computer file. Refer to [https://en.wikipedia.org/wiki/Computer_file](https://en.wikipedia.org/wiki/Computer_file) for information about files.", + "description": "Specifies the type of component. For software components, classify as application if no more specific appropriate classification is available or cannot be determined for the component. Types include:\n\n* __application__ = A software application. Refer to [https://en.wikipedia.org/wiki/Application_software](https://en.wikipedia.org/wiki/Application_software) for information about applications.\n* __framework__ = A software framework. Refer to [https://en.wikipedia.org/wiki/Software_framework](https://en.wikipedia.org/wiki/Software_framework) for information on how frameworks vary slightly from libraries.\n* __library__ = A software library. Refer to [https://en.wikipedia.org/wiki/Library_(computing)](https://en.wikipedia.org/wiki/Library_(computing))\n for information about libraries. All third-party and open source reusable components will likely be a library. If the library also has key features of a framework, then it should be classified as a framework. If not, or is unknown, then specifying library is RECOMMENDED.\n* __container__ = A packaging and/or runtime format, not specific to any particular technology, which isolates software inside the container from software outside of a container through virtualization technology. Refer to [https://en.wikipedia.org/wiki/OS-level_virtualization](https://en.wikipedia.org/wiki/OS-level_virtualization)\n* __platform__ = A runtime environment which interprets or executes software. This may include runtimes such as those that execute bytecode or low-code/no-code application platforms.\n* __operating-system__ = A software operating system without regard to deployment model (i.e. installed on physical hardware, virtual machine, image, etc) Refer to [https://en.wikipedia.org/wiki/Operating_system](https://en.wikipedia.org/wiki/Operating_system)\n* __device__ = A hardware device such as a processor, or chip-set. A hardware device containing firmware SHOULD include a component for the physical hardware itself, and another component of type 'firmware' or 'operating-system' (whichever is relevant), describing information about the software running on the device.\n See also the list of [known device properties](https://github.com/CycloneDX/cyclonedx-property-taxonomy/blob/main/cdx/device.md).\n* __device-driver__ = A special type of software that operates or controls a particular type of device. Refer to [https://en.wikipedia.org/wiki/Device_driver](https://en.wikipedia.org/wiki/Device_driver)\n* __firmware__ = A special type of software that provides low-level control over a devices hardware. Refer to [https://en.wikipedia.org/wiki/Firmware](https://en.wikipedia.org/wiki/Firmware)\n* __file__ = A computer file. Refer to [https://en.wikipedia.org/wiki/Computer_file](https://en.wikipedia.org/wiki/Computer_file) for information about files.\n* __machine-learning-model__ = A model based on training data that can make predictions or decisions without being explicitly programmed to do so.\n* __data__ = A collection of discrete values that convey information.", "examples": ["library"] }, "mime-type": { @@ -351,13 +481,10 @@ "hashes": { "type": "array", "title": "Component Hashes", - "additionalItems": false, "items": {"$ref": "#/definitions/hash"} }, "licenses": { - "type": "array", - "additionalItems": false, - "items": {"$ref": "#/definitions/licenseChoice"}, + "$ref": "#/definitions/licenseChoice", "title": "Component License(s)" }, "copyright": { @@ -398,35 +525,30 @@ "type": "array", "title": "Ancestors", "description": "Describes zero or more components in which a component is derived from. This is commonly used to describe forks from existing projects where the forked version contains a ancestor node containing the original component it was forked from. For example, Component A is the original component. Component B is the component being used and documented in the BOM. However, Component B contains a pedigree node with a single ancestor documenting Component A - the original component from which Component B is derived from.", - "additionalItems": false, "items": {"$ref": "#/definitions/component"} }, "descendants": { "type": "array", "title": "Descendants", "description": "Descendants are the exact opposite of ancestors. This provides a way to document all forks (and their forks) of an original or root component.", - "additionalItems": false, "items": {"$ref": "#/definitions/component"} }, "variants": { "type": "array", "title": "Variants", "description": "Variants describe relations where the relationship between the components are not known. For example, if Component A contains nearly identical code to Component B. They are both related, but it is unclear if one is derived from the other, or if they share a common ancestor.", - "additionalItems": false, "items": {"$ref": "#/definitions/component"} }, "commits": { "type": "array", "title": "Commits", "description": "A list of zero or more commits which provide a trail describing how the component deviates from an ancestor, descendant, or variant.", - "additionalItems": false, "items": {"$ref": "#/definitions/commit"} }, "patches": { "type": "array", "title": "Patches", "description": ">A list of zero or more patches describing how the component deviates from an ancestor, descendant, or variant. Patches may be complimentary to commits or may be used in place of commits.", - "additionalItems": false, "items": {"$ref": "#/definitions/patch"} }, "notes": { @@ -438,14 +560,12 @@ }, "externalReferences": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/externalReference"}, "title": "External References", - "description": "External references provide a way to document systems, sites, and information that may be relevant but which are not included with the BOM." + "description": "External references provide a way to document systems, sites, and information that may be relevant, but are not included with the BOM. They may also establish specific relationships within or external to the BOM." }, "components": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/component"}, "uniqueItems": true, "title": "Components", @@ -461,11 +581,20 @@ "title": "Release notes", "description": "Specifies optional release notes." }, + "modelCard": { + "$ref": "#/definitions/modelCard", + "title": "Machine Learning Model Card" + }, + "data": { + "type": "array", + "items": {"$ref": "#/definitions/componentData"}, + "title": "Data", + "description": "This object SHOULD be specified for any component of type `data` and MUST NOT be specified for other component types." + }, "properties": { "type": "array", "title": "Properties", "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", - "additionalItems": false, "items": {"$ref": "#/definitions/property"} }, "signature": { @@ -610,6 +739,11 @@ ], "additionalProperties": false, "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the license elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, "id": { "$ref": "spdx.schema.json", "title": "License ID (SPDX)", @@ -633,32 +767,197 @@ "description": "The URL to the license file. If specified, a 'license' externalReference should also be specified for completeness", "examples": ["https://www.apache.org/licenses/LICENSE-2.0.txt"], "format": "iri-reference" + }, + "licensing": { + "type": "object", + "title": "Licensing information", + "description": "Licensing details describing the licensor/licensee, license type, renewal and expiration dates, and other important metadata", + "additionalProperties": false, + "properties": { + "altIds": { + "type": "array", + "title": "Alternate License Identifiers", + "description": "License identifiers that may be used to manage licenses and their lifecycle", + "items": { + "type": "string" + } + }, + "licensor": { + "title": "Licensor", + "description": "The individual or organization that grants a license to another individual or organization", + "type": "object", + "additionalProperties": false, + "properties": { + "organization": { + "title": "Licensor (Organization)", + "description": "The organization that granted the license", + "$ref": "#/definitions/organizationalEntity" + }, + "individual": { + "title": "Licensor (Individual)", + "description": "The individual, not associated with an organization, that granted the license", + "$ref": "#/definitions/organizationalContact" + } + }, + "oneOf":[ + { + "required": ["organization"] + }, + { + "required": ["individual"] + } + ] + }, + "licensee": { + "title": "Licensee", + "description": "The individual or organization for which a license was granted to", + "type": "object", + "additionalProperties": false, + "properties": { + "organization": { + "title": "Licensee (Organization)", + "description": "The organization that was granted the license", + "$ref": "#/definitions/organizationalEntity" + }, + "individual": { + "title": "Licensee (Individual)", + "description": "The individual, not associated with an organization, that was granted the license", + "$ref": "#/definitions/organizationalContact" + } + }, + "oneOf":[ + { + "required": ["organization"] + }, + { + "required": ["individual"] + } + ] + }, + "purchaser": { + "title": "Purchaser", + "description": "The individual or organization that purchased the license", + "type": "object", + "additionalProperties": false, + "properties": { + "organization": { + "title": "Purchaser (Organization)", + "description": "The organization that purchased the license", + "$ref": "#/definitions/organizationalEntity" + }, + "individual": { + "title": "Purchaser (Individual)", + "description": "The individual, not associated with an organization, that purchased the license", + "$ref": "#/definitions/organizationalContact" + } + }, + "oneOf":[ + { + "required": ["organization"] + }, + { + "required": ["individual"] + } + ] + }, + "purchaseOrder": { + "type": "string", + "title": "Purchase Order", + "description": "The purchase order identifier the purchaser sent to a supplier or vendor to authorize a purchase" + }, + "licenseTypes": { + "type": "array", + "title": "License Type", + "description": "The type of license(s) that was granted to the licensee\n\n* __academic__ = A license that grants use of software solely for the purpose of education or research.\n* __appliance__ = A license covering use of software embedded in a specific piece of hardware.\n* __client-access__ = A Client Access License (CAL) allows client computers to access services provided by server software.\n* __concurrent-user__ = A Concurrent User license (aka floating license) limits the number of licenses for a software application and licenses are shared among a larger number of users.\n* __core-points__ = A license where the core of a computer's processor is assigned a specific number of points.\n* __custom-metric__ = A license for which consumption is measured by non-standard metrics.\n* __device__ = A license that covers a defined number of installations on computers and other types of devices.\n* __evaluation__ = A license that grants permission to install and use software for trial purposes.\n* __named-user__ = A license that grants access to the software to one or more pre-defined users.\n* __node-locked__ = A license that grants access to the software on one or more pre-defined computers or devices.\n* __oem__ = An Original Equipment Manufacturer license that is delivered with hardware, cannot be transferred to other hardware, and is valid for the life of the hardware.\n* __perpetual__ = A license where the software is sold on a one-time basis and the licensee can use a copy of the software indefinitely.\n* __processor-points__ = A license where each installation consumes points per processor.\n* __subscription__ = A license where the licensee pays a fee to use the software or service.\n* __user__ = A license that grants access to the software or service by a specified number of users.\n* __other__ = Another license type.\n", + "items": { + "type": "string", + "enum": [ + "academic", + "appliance", + "client-access", + "concurrent-user", + "core-points", + "custom-metric", + "device", + "evaluation", + "named-user", + "node-locked", + "oem", + "perpetual", + "processor-points", + "subscription", + "user", + "other" + ] + } + }, + "lastRenewal": { + "type": "string", + "format": "date-time", + "title": "Last Renewal", + "description": "The timestamp indicating when the license was last renewed. For new purchases, this is often the purchase or acquisition date. For non-perpetual licenses or subscriptions, this is the timestamp of when the license was last renewed." + }, + "expiration": { + "type": "string", + "format": "date-time", + "title": "Expiration", + "description": "The timestamp indicating when the current license expires (if applicable)." + } + } + }, + "properties": { + "type": "array", + "title": "Properties", + "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", + "items": {"$ref": "#/definitions/property"} } } }, "licenseChoice": { - "type": "object", - "title": "License(s)", - "additionalProperties": false, - "properties": { - "license": { - "$ref": "#/definitions/license" - }, - "expression": { - "type": "string", - "title": "SPDX License Expression", - "examples": [ - "Apache-2.0 AND (MIT OR GPL-2.0-only)", - "GPL-3.0-only WITH Classpath-exception-2.0" - ] - } - }, - "oneOf":[ + "title": "License Choice", + "description": "EITHER (list of SPDX licenses and/or named licenses) OR (tuple of one SPDX License Expression)", + "type": "array", + "oneOf": [ { - "required": ["license"] + "title": "Multiple licenses", + "description": "A list of SPDX licenses and/or named licenses.", + "type": "array", + "items": { + "type": "object", + "required": ["license"], + "additionalProperties": false, + "properties": { + "license": {"$ref": "#/definitions/license"} + } + } }, { - "required": ["expression"] + "title": "SPDX License Expression", + "description": "A tuple of exactly one SPDX License Expression.", + "type": "array", + "additionalItems": false, + "minItems": 1, + "maxItems": 1, + "items": [{ + "type": "object", + "additionalProperties": false, + "required": ["expression"], + "properties": { + "expression": { + "type": "string", + "title": "SPDX License Expression", + "examples": [ + "Apache-2.0 AND (MIT OR GPL-2.0-only)", + "GPL-3.0-only WITH Classpath-exception-2.0" + ] + }, + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the license elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + } + } + }] } ] }, @@ -723,7 +1022,6 @@ }, "resolves": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/issue"}, "title": "Resolves", "description": "A collection of issues the patch resolves" @@ -842,7 +1140,7 @@ "externalReference": { "type": "object", "title": "External Reference", - "description": "Specifies an individual external reference", + "description": "External references provide a way to document systems, sites, and information that may be relevant, but are not included with the BOM. They may also establish specific relationships within or external to the BOM.", "required": [ "url", "type" @@ -850,10 +1148,19 @@ "additionalProperties": false, "properties": { "url": { - "type": "string", + "anyOf": [ + { + "title": "URL", + "type": "string", + "format": "iri-reference" + }, + { + "title": "BOM-Link", + "$ref": "#/definitions/bomLink" + } + ], "title": "URL", - "description": "The URL to the external reference", - "format": "iri-reference" + "description": "The URI (URL or URN) to the external reference. External references are URIs and therefore can accept any URL scheme including https ([RFC-7230](https://www.ietf.org/rfc/rfc7230.txt)), mailto ([RFC-2368](https://www.ietf.org/rfc/rfc2368.txt)), tel ([RFC-3966](https://www.ietf.org/rfc/rfc3966.txt)), and dns ([RFC-4501](https://www.ietf.org/rfc/rfc4501.txt)). External references may also include formally registered URNs such as [CycloneDX BOM-Link](https://cyclonedx.org/capabilities/bomlink/) to reference CycloneDX BOMs or any object within a BOM. BOM-Link transforms applicable external references into relationships that can be expressed in a BOM or across BOMs." }, "comment": { "type": "string", @@ -863,7 +1170,7 @@ "type": { "type": "string", "title": "Type", - "description": "Specifies the type of external reference. There are built-in types to describe common references. If a type does not exist for the reference being referred to, use the \"other\" type.", + "description": "Specifies the type of external reference.\n\n* __vcs__ = Version Control System\n* __issue-tracker__ = Issue or defect tracking system, or an Application Lifecycle Management (ALM) system\n* __website__ = Website\n* __advisories__ = Security advisories\n* __bom__ = Bill of Materials (SBOM, OBOM, HBOM, SaaSBOM, etc)\n* __mailing-list__ = Mailing list or discussion group\n* __social__ = Social media account\n* __chat__ = Real-time chat platform\n* __documentation__ = Documentation, guides, or how-to instructions\n* __support__ = Community or commercial support\n* __distribution__ = Direct or repository download location\n* __distribution-intake__ = The location where a component was published to. This is often the same as \"distribution\" but may also include specialized publishing processes that act as an intermediary\n* __license__ = The URL to the license file. If a license URL has been defined in the license node, it should also be defined as an external reference for completeness\n* __build-meta__ = Build-system specific meta file (i.e. pom.xml, package.json, .nuspec, etc)\n* __build-system__ = URL to an automated build system\n* __release-notes__ = URL to release notes\n* __security-contact__ = Specifies a way to contact the maintainer, supplier, or provider in the event of a security incident. Common URIs include links to a disclosure procedure, a mailto (RFC-2368) that specifies an email address, a tel (RFC-3966) that specifies a phone number, or dns (RFC-4501) that specifies the records containing DNS Security TXT\n* __model-card__ = A model card describes the intended uses of a machine learning model, potential limitations, biases, ethical considerations, training parameters, datasets used to train the model, performance metrics, and other relevant data useful for ML transparency\n* __log__ = A record of events that occurred in a computer system or application, such as problems, errors, or information on current operations\n* __configuration__ = Parameters or settings that may be used by other components or services\n* __evidence__ = Information used to substantiate a claim\n* __formulation__ = Describes how a component or service was manufactured or deployed\n* __attestation__ = Human or machine-readable statements containing facts, evidence, or testimony\n* __threat-model__ = An enumeration of identified weaknesses, threats, and countermeasures, dataflow diagram (DFD), attack tree, and other supporting documentation in human-readable or machine-readable format\n* __adversary-model__ = The defined assumptions, goals, and capabilities of an adversary.\n* __risk-assessment__ = Identifies and analyzes the potential of future events that may negatively impact individuals, assets, and/or the environment. Risk assessments may also include judgments on the tolerability of each risk.\n* __vulnerability-assertion__ = A Vulnerability Disclosure Report (VDR) which asserts the known and previously unknown vulnerabilities that affect a component, service, or product including the analysis and findings describing the impact (or lack of impact) that the reported vulnerability has on a component, service, or product.\n* __exploitability-statement__ = A Vulnerability Exploitability eXchange (VEX) which asserts the known vulnerabilities that do not affect a product, product family, or organization, and optionally the ones that do. The VEX should include the analysis and findings describing the impact (or lack of impact) that the reported vulnerability has on the product, product family, or organization.\n* __pentest-report__ = Results from an authorized simulated cyberattack on a component or service, otherwise known as a penetration test\n* __static-analysis-report__ = SARIF or proprietary machine or human-readable report for which static analysis has identified code quality, security, and other potential issues with the source code\n* __dynamic-analysis-report__ = Dynamic analysis report that has identified issues such as vulnerabilities and misconfigurations\n* __runtime-analysis-report__ = Report generated by analyzing the call stack of a running application\n* __component-analysis-report__ = Report generated by Software Composition Analysis (SCA), container analysis, or other forms of component analysis\n* __maturity-report__ = Report containing a formal assessment of an organization, business unit, or team against a maturity model\n* __certification-report__ = Industry, regulatory, or other certification from an accredited (if applicable) certification body\n* __quality-metrics__ = Report or system in which quality metrics can be obtained\n* __codified-infrastructure__ = Code or configuration that defines and provisions virtualized infrastructure, commonly referred to as Infrastructure as Code (IaC)\n* __poam__ = Plans of Action and Milestones (POAM) compliment an \"attestation\" external reference. POAM is defined by NIST as a \"document that identifies tasks needing to be accomplished. It details resources required to accomplish the elements of the plan, any milestones in meeting the tasks and scheduled completion dates for the milestones\".\n* __other__ = Use this if no other types accurately describe the purpose of the external reference", "enum": [ "vcs", "issue-tracker", @@ -876,16 +1183,38 @@ "documentation", "support", "distribution", + "distribution-intake", "license", "build-meta", "build-system", "release-notes", + "security-contact", + "model-card", + "log", + "configuration", + "evidence", + "formulation", + "attestation", + "threat-model", + "adversary-model", + "risk-assessment", + "vulnerability-assertion", + "exploitability-statement", + "pentest-report", + "static-analysis-report", + "dynamic-analysis-report", + "runtime-analysis-report", + "component-analysis-report", + "maturity-report", + "certification-report", + "codified-infrastructure", + "quality-metrics", + "poam", "other" ] }, "hashes": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/hash"}, "title": "Hashes", "description": "The hashes of the external reference (if applicable)." @@ -895,26 +1224,25 @@ "dependency": { "type": "object", "title": "Dependency", - "description": "Defines the direct dependencies of a component. Components that do not have their own dependencies MUST be declared as empty elements within the graph. Components that are not represented in the dependency graph MAY have unknown dependencies. It is RECOMMENDED that implementations assume this to be opaque and not an indicator of a component being dependency-free.", + "description": "Defines the direct dependencies of a component or service. Components or services that do not have their own dependencies MUST be declared as empty elements within the graph. Components or services that are not represented in the dependency graph MAY have unknown dependencies. It is RECOMMENDED that implementations assume this to be opaque and not an indicator of a object being dependency-free. It is RECOMMENDED to leverage compositions to indicate unknown dependency graphs.", "required": [ "ref" ], "additionalProperties": false, "properties": { "ref": { - "$ref": "#/definitions/refType", + "$ref": "#/definitions/refLinkType", "title": "Reference", - "description": "References a component by the components bom-ref attribute" + "description": "References a component or service by its bom-ref attribute" }, "dependsOn": { "type": "array", "uniqueItems": true, - "additionalItems": false, "items": { - "$ref": "#/definitions/refType" + "$ref": "#/definitions/refLinkType" }, "title": "Depends On", - "description": "The bom-ref identifiers of the components that are dependencies of this dependency object." + "description": "The bom-ref identifiers of the components or services that are dependencies of this dependency object." } } }, @@ -979,29 +1307,29 @@ "title": "Crosses Trust Boundary", "description": "A boolean value indicating if use of the service crosses a trust zone or boundary. A value of true indicates that by using the service, a trust boundary is crossed. A value of false indicates that by using the service, a trust boundary is not crossed." }, + "trustZone": { + "type": "string", + "title": "Trust Zone", + "description": "The name of the trust zone the service resides in." + }, "data": { "type": "array", - "additionalItems": false, - "items": {"$ref": "#/definitions/dataClassification"}, - "title": "Data Classification", - "description": "Specifies the data classification." + "items": {"$ref": "#/definitions/serviceData"}, + "title": "Data", + "description": "Specifies information about the data including the directional flow of data and the data classification." }, "licenses": { - "type": "array", - "additionalItems": false, - "items": {"$ref": "#/definitions/licenseChoice"}, + "$ref": "#/definitions/licenseChoice", "title": "Component License(s)" }, "externalReferences": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/externalReference"}, "title": "External References", - "description": "External references provide a way to document systems, sites, and information that may be relevant but which are not included with the BOM." + "description": "External references provide a way to document systems, sites, and information that may be relevant, but are not included with the BOM. They may also establish specific relationships within or external to the BOM." }, "services": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/service"}, "uniqueItems": true, "title": "Services", @@ -1016,7 +1344,6 @@ "type": "array", "title": "Properties", "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", - "additionalItems": false, "items": {"$ref": "#/definitions/property"} }, "signature": { @@ -1026,7 +1353,7 @@ } } }, - "dataClassification": { + "serviceData": { "type": "object", "title": "Hash Objects", "required": [ @@ -1036,18 +1363,73 @@ "additionalProperties": false, "properties": { "flow": { - "$ref": "#/definitions/dataFlow", + "$ref": "#/definitions/dataFlowDirection", "title": "Directional Flow", "description": "Specifies the flow direction of the data. Direction is relative to the service. Inbound flow states that data enters the service. Outbound flow states that data leaves the service. Bi-directional states that data flows both ways, and unknown states that the direction is not known." }, "classification": { + "$ref": "#/definitions/dataClassification" + }, + "name": { + "type": "string", + "title": "Name", + "description": "Name for the defined data", + "examples": [ + "Credit card reporting" + ] + }, + "description": { "type": "string", - "title": "Classification", - "description": "Data classification tags data according to its type, sensitivity, and value if altered, stolen, or destroyed." + "title": "Description", + "description": "Short description of the data content and usage", + "examples": [ + "Credit card information being exchanged in between the web app and the database" + ] + }, + "governance": { + "type": "object", + "title": "Data Governance", + "$ref": "#/definitions/dataGovernance" + }, + "source": { + "type": "array", + "items": { + "anyOf": [ + { + "title": "URL", + "type": "string", + "format": "iri-reference" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] + }, + "title": "Source", + "description": "The URI, URL, or BOM-Link of the components or services the data came in from" + }, + "destination": { + "type": "array", + "items": { + "anyOf": [ + { + "title": "URL", + "type": "string", + "format": "iri-reference" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] + }, + "title": "Destination", + "description": "The URI, URL, or BOM-Link of the components or services the data is sent to" } } }, - "dataFlow": { + "dataFlowDirection": { "type": "string", "enum": [ "inbound", @@ -1073,22 +1455,183 @@ } } }, - "componentEvidence": { "type": "object", "title": "Evidence", "description": "Provides the ability to document evidence collected through various forms of extraction or analysis.", "additionalProperties": false, "properties": { - "licenses": { + "identity": { + "type": "object", + "description": "Evidence that substantiates the identity of a component.", + "required": [ "field" ], + "additionalProperties": false, + "properties": { + "field": { + "type": "string", + "enum": [ + "group", "name", "version", "purl", "cpe", "swid", "hash" + ], + "title": "Field", + "description": "The identity field of the component which the evidence describes." + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1, + "title": "Confidence", + "description": "The overall confidence of the evidence from 0 - 1, where 1 is 100% confidence." + }, + "methods": { + "type": "array", + "title": "Methods", + "description": "The methods used to extract and/or analyze the evidence.", + "items": { + "type": "object", + "required": [ + "technique" , + "confidence" + ], + "additionalProperties": false, + "properties": { + "technique": { + "title": "Technique", + "description": "The technique used in this method of analysis.", + "type": "string", + "enum": [ + "source-code-analysis", + "binary-analysis", + "manifest-analysis", + "ast-fingerprint", + "hash-comparison", + "instrumentation", + "dynamic-analysis", + "filename", + "attestation", + "other" + ] + }, + "confidence": { + "type": "number", + "minimum": 0, + "maximum": 1, + "title": "Confidence", + "description": "The confidence of the evidence from 0 - 1, where 1 is 100% confidence. Confidence is specific to the technique used. Each technique of analysis can have independent confidence." + }, + "value": { + "type": "string", + "title": "Value", + "description": "The value or contents of the evidence." + } + } + } + }, + "tools": { + "type": "array", + "uniqueItems": true, + "items": { + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] + }, + "title": "BOM References", + "description": "The object in the BOM identified by its bom-ref. This is often a component or service, but may be any object type supporting bom-refs. Tools used for analysis should already be defined in the BOM, either in the metadata/tools, components, or formulation." + } + } + }, + "occurrences": { "type": "array", - "additionalItems": false, - "items": {"$ref": "#/definitions/licenseChoice"}, + "title": "Occurrences", + "description": "Evidence of individual instances of a component spread across multiple locations.", + "items": { + "type": "object", + "required": [ "location" ], + "additionalProperties": false, + "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the occurrence elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, + "location": { + "type": "string", + "title": "Location", + "description": "The location or path to where the component was found." + } + } + } + }, + "callstack": { + "type": "object", + "description": "Evidence of the components use through the callstack.", + "additionalProperties": false, + "properties": { + "frames": { + "type": "array", + "title": "Methods", + "items": { + "type": "object", + "required": [ + "module" + ], + "additionalProperties": false, + "properties": { + "package": { + "title": "Package", + "description": "A package organizes modules into namespaces, providing a unique namespace for each type it contains.", + "type": "string" + }, + "module": { + "title": "Module", + "description": "A module or class that encloses functions/methods and other code.", + "type": "string" + }, + "function": { + "title": "Function", + "description": "A block of code designed to perform a particular task.", + "type": "string" + }, + "parameters": { + "title": "Parameters", + "description": "Optional arguments that are passed to the module or function.", + "type": "array", + "items": { + "type": "string" + } + }, + "line": { + "title": "Line", + "description": "The line number the code that is called resides on.", + "type": "integer" + }, + "column": { + "title": "Column", + "description": "The column the code that is called resides.", + "type": "integer" + }, + "fullFilename": { + "title": "Full Filename", + "description": "The full path and filename of the module.", + "type": "string" + } + } + } + } + } + }, + "licenses": { + "$ref": "#/definitions/licenseChoice", "title": "Component License(s)" }, "copyright": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/copyright"}, "title": "Copyright" } @@ -1102,16 +1645,30 @@ ], "additionalProperties": false, "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the composition elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, "aggregate": { "$ref": "#/definitions/aggregateType", "title": "Aggregate", - "description": "Specifies an aggregate type that describe how complete a relationship is." + "description": "Specifies an aggregate type that describe how complete a relationship is.\n\n* __complete__ = The relationship is complete. No further relationships including constituent components, services, or dependencies are known to exist.\n* __incomplete__ = The relationship is incomplete. Additional relationships exist and may include constituent components, services, or dependencies.\n* __incomplete_first_party_only__ = The relationship is incomplete. Only relationships for first-party components, services, or their dependencies are represented.\n* __incomplete_first_party_proprietary_only__ = The relationship is incomplete. Only relationships for first-party components, services, or their dependencies are represented, limited specifically to those that are proprietary.\n* __incomplete_first_party_opensource_only__ = The relationship is incomplete. Only relationships for first-party components, services, or their dependencies are represented, limited specifically to those that are opensource.\n* __incomplete_third_party_only__ = The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented.\n* __incomplete_third_party_proprietary_only__ = The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are proprietary.\n* __incomplete_third_party_opensource_only__ = The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are opensource.\n* __unknown__ = The relationship may be complete or incomplete. This usually signifies a 'best-effort' to obtain constituent components, services, or dependencies but the completeness is inconclusive.\n* __not_specified__ = The relationship completeness is not specified.\n" }, "assemblies": { "type": "array", "uniqueItems": true, "items": { - "type": "string" + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] }, "title": "BOM references", "description": "The bom-ref identifiers of the components or services being described. Assemblies refer to nested relationships whereby a constituent part may include other constituent parts. References do not cascade to child parts. References are explicit for the specified constituent part only." @@ -1125,6 +1682,15 @@ "title": "BOM references", "description": "The bom-ref identifiers of the components or services being described. Dependencies refer to a relationship whereby an independent constituent part requires another independent constituent part. References do not cascade to transitive dependencies. References are explicit for the specified dependency only." }, + "vulnerabilities": { + "type": "array", + "uniqueItems": true, + "items": { + "type": "string" + }, + "title": "BOM references", + "description": "The bom-ref identifiers of the vulnerabilities being described." + }, "signature": { "$ref": "#/definitions/signature", "title": "Signature", @@ -1139,7 +1705,11 @@ "complete", "incomplete", "incomplete_first_party_only", + "incomplete_first_party_proprietary_only", + "incomplete_first_party_opensource_only", "incomplete_third_party_only", + "incomplete_third_party_proprietary_only", + "incomplete_third_party_opensource_only", "unknown", "not_specified" ] @@ -1147,6 +1717,7 @@ "property": { "type": "object", "title": "Lightweight name-value pair", + "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", "properties": { "name": { "type": "string", @@ -1257,14 +1828,12 @@ }, "resolves": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/issue"}, "title": "Resolves", "description": "A collection of issues that have been resolved." }, "notes": { "type": "array", - "additionalItems": false, "items": {"$ref": "#/definitions/note"}, "title": "Notes", "description": "Zero or more release notes containing the locale and content. Multiple note objects may be specified to support release notes in a wide variety of languages." @@ -1273,7 +1842,6 @@ "type": "array", "title": "Properties", "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", - "additionalItems": false, "items": {"$ref": "#/definitions/property"} } } @@ -1321,12 +1889,14 @@ "scoreMethod": { "type": "string", "title": "Method", - "description": "Specifies the severity or risk scoring methodology or standard used.\n\n* CVSSv2 - [Common Vulnerability Scoring System v2](https://www.first.org/cvss/v2/)\n* CVSSv3 - [Common Vulnerability Scoring System v3](https://www.first.org/cvss/v3-0/)\n* CVSSv31 - [Common Vulnerability Scoring System v3.1](https://www.first.org/cvss/v3-1/)\n* OWASP - [OWASP Risk Rating Methodology](https://owasp.org/www-community/OWASP_Risk_Rating_Methodology)", + "description": "Specifies the severity or risk scoring methodology or standard used.\n\n* CVSSv2 - [Common Vulnerability Scoring System v2](https://www.first.org/cvss/v2/)\n* CVSSv3 - [Common Vulnerability Scoring System v3](https://www.first.org/cvss/v3-0/)\n* CVSSv31 - [Common Vulnerability Scoring System v3.1](https://www.first.org/cvss/v3-1/)\n* CVSSv4 - [Common Vulnerability Scoring System v4](https://www.first.org/cvss/v4-0/)\n* OWASP - [OWASP Risk Rating Methodology](https://owasp.org/www-community/OWASP_Risk_Rating_Methodology)\n* SSVC - [Stakeholder Specific Vulnerability Categorization](https://github.com/CERTCC/SSVC) (all versions)", "enum": [ "CVSSv2", "CVSSv3", "CVSSv31", + "CVSSv4", "OWASP", + "SSVC", "other" ] }, @@ -1424,7 +1994,7 @@ "vulnerability": { "type": "object", "title": "Vulnerability", - "description": "Defines a weakness in an component or service that could be exploited or triggered by a threat source.", + "description": "Defines a weakness in a component or service that could be exploited or triggered by a threat source.", "additionalProperties": false, "properties": { "bom-ref": { @@ -1450,8 +2020,8 @@ "type": "array", "title": "References", "description": "Zero or more pointers to vulnerabilities that are the equivalent of the vulnerability specified. Often times, the same vulnerability may exist in multiple sources of vulnerability intelligence, but have different identifiers. References provide a way to correlate vulnerabilities across multiple sources of vulnerability intelligence.", - "additionalItems": false, "items": { + "type": "object", "required": [ "id", "source" @@ -1479,7 +2049,6 @@ "type": "array", "title": "Ratings", "description": "List of vulnerability ratings", - "additionalItems": false, "items": { "$ref": "#/definitions/rating" } @@ -1488,8 +2057,7 @@ "type": "array", "title": "CWEs", "description": "List of Common Weaknesses Enumerations (CWEs) codes that describes this vulnerability. For example 399 (of https://cwe.mitre.org/data/definitions/399.html)", - "examples": ["399"], - "additionalItems": false, + "examples": [399], "items": { "$ref": "#/definitions/cwe" } @@ -1502,18 +2070,45 @@ "detail": { "type": "string", "title": "Details", - "description": "If available, an in-depth description of the vulnerability as provided by the source organization. Details often include examples, proof-of-concepts, and other information useful in understanding root cause." + "description": "If available, an in-depth description of the vulnerability as provided by the source organization. Details often include information useful in understanding root cause." }, "recommendation": { "type": "string", - "title": "Details", + "title": "Recommendation", "description": "Recommendations of how the vulnerability can be remediated or mitigated." }, + "workaround": { + "type": "string", + "title": "Workarounds", + "description": "A bypass, usually temporary, of the vulnerability that reduces its likelihood and/or impact. Workarounds often involve changes to configuration or deployments." + }, + "proofOfConcept": { + "type": "object", + "title": "Proof of Concept", + "description": "Evidence used to reproduce the vulnerability.", + "properties": { + "reproductionSteps": { + "type": "string", + "title": "Steps to Reproduce", + "description": "Precise steps to reproduce the vulnerability." + }, + "environment": { + "type": "string", + "title": "Environment", + "description": "A description of the environment in which reproduction was possible." + }, + "supportingMaterial": { + "type": "array", + "title": "Supporting Material", + "description": "Supporting material that helps in reproducing or understanding how reproduction is possible. This may include screenshots, payloads, and PoC exploit code.", + "items": { "$ref": "#/definitions/attachment" } + } + } + }, "advisories": { "type": "array", "title": "Advisories", "description": "Published advisories of the vulnerability if provided.", - "additionalItems": false, "items": { "$ref": "#/definitions/advisory" } @@ -1536,6 +2131,12 @@ "title": "Updated", "description": "The date and time (timestamp) when the vulnerability record was last updated." }, + "rejected": { + "type": "string", + "format": "date-time", + "title": "Rejected", + "description": "The date and time (timestamp) when the vulnerability record was rejected (if applicable)." + }, "credits": { "type": "object", "title": "Credits", @@ -1546,7 +2147,6 @@ "type": "array", "title": "Organizations", "description": "The organizations credited with vulnerability discovery.", - "additionalItems": false, "items": { "$ref": "#/definitions/organizationalEntity" } @@ -1555,7 +2155,6 @@ "type": "array", "title": "Individuals", "description": "The individuals, not associated with organizations, that are credited with vulnerability discovery.", - "additionalItems": false, "items": { "$ref": "#/definitions/organizationalContact" } @@ -1563,11 +2162,36 @@ } }, "tools": { - "type": "array", - "title": "Creation Tools", - "description": "The tool(s) used to identify, confirm, or score the vulnerability.", - "additionalItems": false, - "items": {"$ref": "#/definitions/tool"} + "oneOf": [ + { + "type": "object", + "title": "Tools", + "description": "The tool(s) used to identify, confirm, or score the vulnerability.", + "additionalProperties": false, + "properties": { + "components": { + "type": "array", + "items": {"$ref": "#/definitions/component"}, + "uniqueItems": true, + "title": "Components", + "description": "A list of software and hardware components used as tools" + }, + "services": { + "type": "array", + "items": {"$ref": "#/definitions/service"}, + "uniqueItems": true, + "title": "Services", + "description": "A list of services used as tools. This may include microservices, function-as-a-service, and other types of network or intra-process services." + } + } + }, + { + "type": "array", + "title": "Tools (legacy)", + "description": "[Deprecated] The tool(s) used to identify, confirm, or score the vulnerability.", + "items": {"$ref": "#/definitions/tool"} + } + ] }, "analysis": { "type": "object", @@ -1585,7 +2209,6 @@ "type": "array", "title": "Response", "description": "A response to the vulnerability by the manufacturer, supplier, or project responsible for the affected component or service. More than one response is allowed. Responses are strongly encouraged for vulnerabilities where the analysis state is exploitable.", - "additionalItems": false, "items": { "type": "string", "enum": [ @@ -1601,21 +2224,42 @@ "type": "string", "title": "Detail", "description": "Detailed description of the impact including methods used during assessment. If a vulnerability is not exploitable, this field should include specific details on why the component or service is not impacted by this vulnerability." + }, + "firstIssued": { + "type": "string", + "format": "date-time", + "title": "First Issued", + "description": "The date and time (timestamp) when the analysis was first issued." + }, + "lastUpdated": { + "type": "string", + "format": "date-time", + "title": "Last Updated", + "description": "The date and time (timestamp) when the analysis was last updated." } } }, "affects": { "type": "array", "uniqueItems": true, - "additionalItems": false, "items": { + "type": "object", "required": [ "ref" ], "additionalProperties": false, "properties": { "ref": { - "$ref": "#/definitions/refType", + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ], "title": "Reference", "description": "References a component or service by the objects bom-ref" }, @@ -1623,8 +2267,8 @@ "type": "array", "title": "Versions", "description": "Zero or more individual versions or range of versions.", - "additionalItems": false, "items": { + "type": "object", "oneOf": [ { "required": ["version"] @@ -1641,7 +2285,7 @@ }, "range": { "description": "A version range specified in Package URL Version Range syntax (vers) which is defined at https://github.com/package-url/purl-spec/VERSION-RANGE-SPEC.rst", - "$ref": "#/definitions/version" + "$ref": "#/definitions/range" }, "status": { "description": "The vulnerability status for the version or range of versions.", @@ -1660,7 +2304,6 @@ "type": "array", "title": "Properties", "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", - "additionalItems": false, "items": { "$ref": "#/definitions/property" } @@ -1688,6 +2331,1465 @@ "minLength": 1, "maxLength": 1024 }, + "annotations": { + "type": "object", + "title": "Annotations", + "description": "A comment, note, explanation, or similar textual content which provides additional context to the object(s) being annotated.", + "required": [ + "subjects", + "annotator", + "timestamp", + "text" + ], + "additionalProperties": false, + "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the annotation elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, + "subjects": { + "type": "array", + "uniqueItems": true, + "items": { + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] + }, + "title": "BOM References", + "description": "The object in the BOM identified by its bom-ref. This is often a component or service, but may be any object type supporting bom-refs." + }, + "annotator": { + "type": "object", + "title": "Annotator", + "description": "The organization, person, component, or service which created the textual content of the annotation.", + "oneOf": [ + { + "required": [ + "organization" + ] + }, + { + "required": [ + "individual" + ] + }, + { + "required": [ + "component" + ] + }, + { + "required": [ + "service" + ] + } + ], + "additionalProperties": false, + "properties": { + "organization": { + "description": "The organization that created the annotation", + "$ref": "#/definitions/organizationalEntity" + }, + "individual": { + "description": "The person that created the annotation", + "$ref": "#/definitions/organizationalContact" + }, + "component": { + "description": "The tool or component that created the annotation", + "$ref": "#/definitions/component" + }, + "service": { + "description": "The service that created the annotation", + "$ref": "#/definitions/service" + } + } + }, + "timestamp": { + "type": "string", + "format": "date-time", + "title": "Timestamp", + "description": "The date and time (timestamp) when the annotation was created." + }, + "text": { + "type": "string", + "title": "Text", + "description": "The textual content of the annotation." + }, + "signature": { + "$ref": "#/definitions/signature", + "title": "Signature", + "description": "Enveloped signature in [JSON Signature Format (JSF)](https://cyberphone.github.io/doc/security/jsf.html)." + } + } + }, + "modelCard": { + "$comment": "Model card support in CycloneDX is derived from TensorFlow Model Card Toolkit released under the Apache 2.0 license and available from https://github.com/tensorflow/model-card-toolkit/blob/main/model_card_toolkit/schema/v0.0.2/model_card.schema.json. In addition, CycloneDX model card support includes portions of VerifyML, also released under the Apache 2.0 license and available from https://github.com/cylynx/verifyml/blob/main/verifyml/model_card_toolkit/schema/v0.0.4/model_card.schema.json.", + "type": "object", + "title": "Model Card", + "description": "A model card describes the intended uses of a machine learning model and potential limitations, including biases and ethical considerations. Model cards typically contain the training parameters, which datasets were used to train the model, performance metrics, and other relevant data useful for ML transparency. This object SHOULD be specified for any component of type `machine-learning-model` and MUST NOT be specified for other component types.", + "additionalProperties": false, + "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the model card elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, + "modelParameters": { + "type": "object", + "title": "Model Parameters", + "description": "Hyper-parameters for construction of the model.", + "additionalProperties": false, + "properties": { + "approach": { + "type": "object", + "title": "Approach", + "description": "The overall approach to learning used by the model for problem solving.", + "additionalProperties": false, + "properties": { + "type": { + "type": "string", + "title": "Learning Type", + "description": "Learning types describing the learning problem or hybrid learning problem.", + "enum": [ + "supervised", + "unsupervised", + "reinforcement-learning", + "semi-supervised", + "self-supervised" + ] + } + } + }, + "task": { + "type": "string", + "title": "Task", + "description": "Directly influences the input and/or output. Examples include classification, regression, clustering, etc." + }, + "architectureFamily": { + "type": "string", + "title": "Architecture Family", + "description": "The model architecture family such as transformer network, convolutional neural network, residual neural network, LSTM neural network, etc." + }, + "modelArchitecture": { + "type": "string", + "title": "Model Architecture", + "description": "The specific architecture of the model such as GPT-1, ResNet-50, YOLOv3, etc." + }, + "datasets": { + "type": "array", + "title": "Datasets", + "description": "The datasets used to train and evaluate the model.", + "items" : { + "oneOf" : [ + { + "title": "Inline Component Data", + "$ref": "#/definitions/componentData" + }, + { + "type": "object", + "title": "Data Component Reference", + "additionalProperties": false, + "properties": { + "ref": { + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ], + "title": "Reference", + "description": "References a data component by the components bom-ref attribute" + } + } + } + ] + } + }, + "inputs": { + "type": "array", + "title": "Inputs", + "description": "The input format(s) of the model", + "items": { "$ref": "#/definitions/inputOutputMLParameters" } + }, + "outputs": { + "type": "array", + "title": "Outputs", + "description": "The output format(s) from the model", + "items": { "$ref": "#/definitions/inputOutputMLParameters" } + } + } + }, + "quantitativeAnalysis": { + "type": "object", + "title": "Quantitative Analysis", + "description": "A quantitative analysis of the model", + "additionalProperties": false, + "properties": { + "performanceMetrics": { + "type": "array", + "title": "Performance Metrics", + "description": "The model performance metrics being reported. Examples may include accuracy, F1 score, precision, top-3 error rates, MSC, etc.", + "items": { "$ref": "#/definitions/performanceMetric" } + }, + "graphics": { "$ref": "#/definitions/graphicsCollection" } + } + }, + "considerations": { + "type": "object", + "title": "Considerations", + "description": "What considerations should be taken into account regarding the model's construction, training, and application?", + "additionalProperties": false, + "properties": { + "users": { + "type": "array", + "title": "Users", + "description": "Who are the intended users of the model?", + "items": { + "type": "string" + } + }, + "useCases": { + "type": "array", + "title": "Use Cases", + "description": "What are the intended use cases of the model?", + "items": { + "type": "string" + } + }, + "technicalLimitations": { + "type": "array", + "title": "Technical Limitations", + "description": "What are the known technical limitations of the model? E.g. What kind(s) of data should the model be expected not to perform well on? What are the factors that might degrade model performance?", + "items": { + "type": "string" + } + }, + "performanceTradeoffs": { + "type": "array", + "title": "Performance Tradeoffs", + "description": "What are the known tradeoffs in accuracy/performance of the model?", + "items": { + "type": "string" + } + }, + "ethicalConsiderations": { + "type": "array", + "title": "Ethical Considerations", + "description": "What are the ethical (or environmental) risks involved in the application of this model?", + "items": { "$ref": "#/definitions/risk" } + }, + "fairnessAssessments": { + "type": "array", + "title": "Fairness Assessments", + "description": "How does the model affect groups at risk of being systematically disadvantaged? What are the harms and benefits to the various affected groups?", + "items": { + "$ref": "#/definitions/fairnessAssessment" + } + } + } + }, + "properties": { + "type": "array", + "title": "Properties", + "description": "Provides the ability to document properties in a name-value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Unlike key-value stores, properties support duplicate names, each potentially having different values. Property names of interest to the general public are encouraged to be registered in the [CycloneDX Property Taxonomy](https://github.com/CycloneDX/cyclonedx-property-taxonomy). Formal registration is OPTIONAL.", + "items": {"$ref": "#/definitions/property"} + } + } + }, + "inputOutputMLParameters": { + "type": "object", + "title": "Input and Output Parameters", + "additionalProperties": false, + "properties": { + "format": { + "description": "The data format for input/output to the model. Example formats include string, image, time-series", + "type": "string" + } + } + }, + "componentData": { + "type": "object", + "additionalProperties": false, + "required": [ + "type" + ], + "properties": { + "bom-ref": { + "$ref": "#/definitions/refType", + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the dataset elsewhere in the BOM. Every bom-ref MUST be unique within the BOM." + }, + "type": { + "type": "string", + "title": "Type of Data", + "description": "The general theme or subject matter of the data being specified.\n\n* __source-code__ = Any type of code, code snippet, or data-as-code.\n* __configuration__ = Parameters or settings that may be used by other components.\n* __dataset__ = A collection of data.\n* __definition__ = Data that can be used to create new instances of what the definition defines.\n* __other__ = Any other type of data that does not fit into existing definitions.", + "enum": [ + "source-code", + "configuration", + "dataset", + "definition", + "other" + ] + }, + "name": { + "description": "The name of the dataset.", + "type": "string" + }, + "contents": { + "type": "object", + "title": "Data Contents", + "description": "The contents or references to the contents of the data being described.", + "additionalProperties": false, + "properties": { + "attachment": { + "title": "Data Attachment", + "description": "An optional way to include textual or encoded data.", + "$ref": "#/definitions/attachment" + }, + "url": { + "type": "string", + "title": "Data URL", + "description": "The URL to where the data can be retrieved.", + "format": "iri-reference" + }, + "properties": { + "type": "array", + "title": "Configuration Properties", + "description": "Provides the ability to document name-value parameters used for configuration.", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "classification": { + "$ref": "#/definitions/dataClassification" + }, + "sensitiveData": { + "type": "array", + "description": "A description of any sensitive data in a dataset.", + "items": { + "type": "string" + } + }, + "graphics": { "$ref": "#/definitions/graphicsCollection" }, + "description": { + "description": "A description of the dataset. Can describe size of dataset, whether it's used for source code, training, testing, or validation, etc.", + "type": "string" + }, + "governance": { + "type": "object", + "title": "Data Governance", + "$ref": "#/definitions/dataGovernance" + } + } + }, + "dataGovernance": { + "type": "object", + "title": "Data Governance", + "additionalProperties": false, + "properties": { + "custodians": { + "type": "array", + "title": "Data Custodians", + "description": "Data custodians are responsible for the safe custody, transport, and storage of data.", + "items": { "$ref": "#/definitions/dataGovernanceResponsibleParty" } + }, + "stewards": { + "type": "array", + "title": "Data Stewards", + "description": "Data stewards are responsible for data content, context, and associated business rules.", + "items": { "$ref": "#/definitions/dataGovernanceResponsibleParty" } + }, + "owners": { + "type": "array", + "title": "Data Owners", + "description": "Data owners are concerned with risk and appropriate access to data.", + "items": { "$ref": "#/definitions/dataGovernanceResponsibleParty" } + } + } + }, + "dataGovernanceResponsibleParty": { + "type": "object", + "additionalProperties": false, + "properties": { + "organization": { + "title": "Organization", + "$ref": "#/definitions/organizationalEntity" + }, + "contact": { + "title": "Individual", + "$ref": "#/definitions/organizationalContact" + } + }, + "oneOf":[ + { + "required": ["organization"] + }, + { + "required": ["contact"] + } + ] + }, + "graphicsCollection": { + "type": "object", + "title": "Graphics Collection", + "description": "A collection of graphics that represent various measurements.", + "additionalProperties": false, + "properties": { + "description": { + "description": "A description of this collection of graphics.", + "type": "string" + }, + "collection": { + "description": "A collection of graphics.", + "type": "array", + "items": { "$ref": "#/definitions/graphic" } + } + } + }, + "graphic": { + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "description": "The name of the graphic.", + "type": "string" + }, + "image": { + "title": "Graphic Image", + "description": "The graphic (vector or raster). Base64 encoding MUST be specified for binary images.", + "$ref": "#/definitions/attachment" + } + } + }, + "performanceMetric": { + "type": "object", + "additionalProperties": false, + "properties": { + "type": { + "description": "The type of performance metric.", + "type": "string" + }, + "value": { + "description": "The value of the performance metric.", + "type": "string" + }, + "slice": { + "description": "The name of the slice this metric was computed on. By default, assume this metric is not sliced.", + "type": "string" + }, + "confidenceInterval": { + "description": "The confidence interval of the metric.", + "type": "object", + "additionalProperties": false, + "properties": { + "lowerBound": { + "description": "The lower bound of the confidence interval.", + "type": "string" + }, + "upperBound": { + "description": "The upper bound of the confidence interval.", + "type": "string" + } + } + } + } + }, + "risk": { + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "description": "The name of the risk.", + "type": "string" + }, + "mitigationStrategy": { + "description": "Strategy used to address this risk.", + "type": "string" + } + } + }, + "fairnessAssessment": { + "type": "object", + "title": "Fairness Assessment", + "description": "Information about the benefits and harms of the model to an identified at risk group.", + "additionalProperties": false, + "properties": { + "groupAtRisk": { + "type": "string", + "description": "The groups or individuals at risk of being systematically disadvantaged by the model." + }, + "benefits": { + "type": "string", + "description": "Expected benefits to the identified groups." + }, + "harms": { + "type": "string", + "description": "Expected harms to the identified groups." + }, + "mitigationStrategy": { + "type": "string", + "description": "With respect to the benefits and harms outlined, please describe any mitigation strategy implemented." + } + } + }, + "dataClassification": { + "type": "string", + "title": "Data Classification", + "description": "Data classification tags data according to its type, sensitivity, and value if altered, stolen, or destroyed." + }, + "formula": { + "title": "Formula", + "description": "Describes workflows and resources that captures rules and other aspects of how the associated BOM component or service was formed.", + "type": "object", + "additionalProperties": false, + "properties": { + "bom-ref": { + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the formula elsewhere in the BOM. Every bom-ref MUST be unique within the BOM.", + "$ref": "#/definitions/refType" + }, + "components": { + "title": "Components", + "description": "Transient components that are used in tasks that constitute one or more of this formula's workflows", + "type": "array", + "items": { + "$ref": "#/definitions/component" + }, + "uniqueItems": true + }, + "services": { + "title": "Services", + "description": "Transient services that are used in tasks that constitute one or more of this formula's workflows", + "type": "array", + "items": { + "$ref": "#/definitions/service" + }, + "uniqueItems": true + }, + "workflows": { + "title": "Workflows", + "description": "List of workflows that can be declared to accomplish specific orchestrated goals and independently triggered.", + "$comment": "Different workflows can be designed to work together to perform end-to-end CI/CD builds and deployments.", + "type": "array", + "items": { + "$ref": "#/definitions/workflow" + }, + "uniqueItems": true + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "workflow": { + "title": "Workflow", + "description": "A specialized orchestration task.", + "$comment": "Workflow are as task themselves and can trigger other workflow tasks. These relationships can be modeled in the taskDependencies graph.", + "type": "object", + "required": [ + "bom-ref", + "uid", + "taskTypes" + ], + "additionalProperties": false, + "properties": { + "bom-ref": { + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the workflow elsewhere in the BOM. Every bom-ref MUST be unique within the BOM.", + "$ref": "#/definitions/refType" + }, + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier for the resource instance within its deployment context.", + "type": "string" + }, + "name": { + "title": "Name", + "description": "The name of the resource instance.", + "type": "string" + }, + "description": { + "title": "Description", + "description": "A description of the resource instance.", + "type": "string" + }, + "resourceReferences": { + "title": "Resource references", + "description": "References to component or service resources that are used to realize the resource instance.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/resourceReferenceChoice" + } + }, + "tasks": { + "title": "Tasks", + "description": "The tasks that comprise the workflow.", + "$comment": "Note that tasks can appear more than once as different instances (by name or UID).", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/task" + } + }, + "taskDependencies": { + "title": "Task dependency graph", + "description": "The graph of dependencies between tasks within the workflow.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/dependency" + } + }, + "taskTypes": { + "title": "Task types", + "description": "Indicates the types of activities performed by the set of workflow tasks.", + "$comment": "Currently, these types reflect common CI/CD actions.", + "type": "array", + "items": { + "$ref": "#/definitions/taskType" + } + }, + "trigger": { + "title": "Trigger", + "description": "The trigger that initiated the task.", + "$ref": "#/definitions/trigger" + }, + "steps": { + "title": "Steps", + "description": "The sequence of steps for the task.", + "type": "array", + "items": { + "$ref": "#/definitions/step" + }, + "uniqueItems": true + }, + "inputs": { + "title": "Inputs", + "description": "Represents resources and data brought into a task at runtime by executor or task commands", + "examples": ["a `configuration` file which was declared as a local `component` or `externalReference`"], + "type": "array", + "items": { + "$ref": "#/definitions/inputType" + }, + "uniqueItems": true + }, + "outputs": { + "title": "Outputs", + "description": "Represents resources and data output from a task at runtime by executor or task commands", + "examples": ["a log file or metrics data produced by the task"], + "type": "array", + "items": { + "$ref": "#/definitions/outputType" + }, + "uniqueItems": true + }, + "timeStart": { + "title": "Time start", + "description": "The date and time (timestamp) when the task started.", + "type": "string", + "format": "date-time" + }, + "timeEnd": { + "title": "Time end", + "description": "The date and time (timestamp) when the task ended.", + "type": "string", + "format": "date-time" + }, + "workspaces": { + "title": "Workspaces", + "description": "A set of named filesystem or data resource shareable by workflow tasks.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/workspace" + } + }, + "runtimeTopology": { + "title": "Runtime topology", + "description": "A graph of the component runtime topology for workflow's instance.", + "$comment": "A description of the runtime component and service topology. This can describe a partial or complete topology used to host and execute the task (e.g., hardware, operating systems, configurations, etc.),", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/dependency" + } + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "task": { + "title": "Task", + "description": "Describes the inputs, sequence of steps and resources used to accomplish a task and its output.", + "$comment": "Tasks are building blocks for constructing assemble CI/CD workflows or pipelines.", + "type": "object", + "required": [ + "bom-ref", + "uid", + "taskTypes" + ], + "additionalProperties": false, + "properties": { + "bom-ref": { + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the task elsewhere in the BOM. Every bom-ref MUST be unique within the BOM.", + "$ref": "#/definitions/refType" + }, + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier for the resource instance within its deployment context.", + "type": "string" + }, + "name": { + "title": "Name", + "description": "The name of the resource instance.", + "type": "string" + }, + "description": { + "title": "Description", + "description": "A description of the resource instance.", + "type": "string" + }, + "resourceReferences": { + "title": "Resource references", + "description": "References to component or service resources that are used to realize the resource instance.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/resourceReferenceChoice" + } + }, + "taskTypes": { + "title": "Task types", + "description": "Indicates the types of activities performed by the set of workflow tasks.", + "$comment": "Currently, these types reflect common CI/CD actions.", + "type": "array", + "items": { + "$ref": "#/definitions/taskType" + } + }, + "trigger": { + "title": "Trigger", + "description": "The trigger that initiated the task.", + "$ref": "#/definitions/trigger" + }, + "steps": { + "title": "Steps", + "description": "The sequence of steps for the task.", + "type": "array", + "items": { + "$ref": "#/definitions/step" + }, + "uniqueItems": true + }, + "inputs": { + "title": "Inputs", + "description": "Represents resources and data brought into a task at runtime by executor or task commands", + "examples": ["a `configuration` file which was declared as a local `component` or `externalReference`"], + "type": "array", + "items": { + "$ref": "#/definitions/inputType" + }, + "uniqueItems": true + }, + "outputs": { + "title": "Outputs", + "description": "Represents resources and data output from a task at runtime by executor or task commands", + "examples": ["a log file or metrics data produced by the task"], + "type": "array", + "items": { + "$ref": "#/definitions/outputType" + }, + "uniqueItems": true + }, + "timeStart": { + "title": "Time start", + "description": "The date and time (timestamp) when the task started.", + "type": "string", + "format": "date-time" + }, + "timeEnd": { + "title": "Time end", + "description": "The date and time (timestamp) when the task ended.", + "type": "string", + "format": "date-time" + }, + "workspaces": { + "title": "Workspaces", + "description": "A set of named filesystem or data resource shareable by workflow tasks.", + "type": "array", + "items": { + "$ref": "#/definitions/workspace" + }, + "uniqueItems": true + }, + "runtimeTopology": { + "title": "Runtime topology", + "description": "A graph of the component runtime topology for task's instance.", + "$comment": "A description of the runtime component and service topology. This can describe a partial or complete topology used to host and execute the task (e.g., hardware, operating systems, configurations, etc.),", + "type": "array", + "items": { + "$ref": "#/definitions/dependency" + }, + "uniqueItems": true + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "step": { + "type": "object", + "description": "Executes specific commands or tools in order to accomplish its owning task as part of a sequence.", + "additionalProperties": false, + "properties": { + "name": { + "title": "Name", + "description": "A name for the step.", + "type": "string" + }, + "description": { + "title": "Description", + "description": "A description of the step.", + "type": "string" + }, + "commands": { + "title": "Commands", + "description": "Ordered list of commands or directives for the step", + "type": "array", + "items": { + "$ref": "#/definitions/command" + } + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "command": { + "type": "object", + "additionalProperties": false, + "properties": { + "executed": { + "title": "Executed", + "description": "A text representation of the executed command.", + "type": "string" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "workspace": { + "title": "Workspace", + "description": "A named filesystem or data resource shareable by workflow tasks.", + "type": "object", + "required": [ + "bom-ref", + "uid" + ], + "additionalProperties": false, + "properties": { + "bom-ref": { + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the workspace elsewhere in the BOM. Every bom-ref MUST be unique within the BOM.", + "$ref": "#/definitions/refType" + }, + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier for the resource instance within its deployment context.", + "type": "string" + }, + "name": { + "title": "Name", + "description": "The name of the resource instance.", + "type": "string" + }, + "aliases": { + "title": "Aliases", + "description": "The names for the workspace as referenced by other workflow tasks. Effectively, a name mapping so other tasks can use their own local name in their steps.", + "type": "array", + "items": {"type": "string"} + }, + "description": { + "title": "Description", + "description": "A description of the resource instance.", + "type": "string" + }, + "resourceReferences": { + "title": "Resource references", + "description": "References to component or service resources that are used to realize the resource instance.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/resourceReferenceChoice" + } + }, + "accessMode": { + "title": "Access mode", + "description": "Describes the read-write access control for the workspace relative to the owning resource instance.", + "type": "string", + "enum": [ + "read-only", + "read-write", + "read-write-once", + "write-once", + "write-only" + ] + }, + "mountPath": { + "title": "Mount path", + "description": "A path to a location on disk where the workspace will be available to the associated task's steps.", + "type": "string" + }, + "managedDataType": { + "title": "Managed data type", + "description": "The name of a domain-specific data type the workspace represents.", + "$comment": "This property is for CI/CD frameworks that are able to provide access to structured, managed data at a more granular level than a filesystem.", + "examples": ["ConfigMap","Secret"], + "type": "string" + }, + "volumeRequest": { + "title": "Volume request", + "description": "Identifies the reference to the request for a specific volume type and parameters.", + "examples": ["a kubernetes Persistent Volume Claim (PVC) name"], + "type": "string" + }, + "volume": { + "title": "Volume", + "description": "Information about the actual volume instance allocated to the workspace.", + "$comment": "The actual volume allocated may be different than the request.", + "examples": ["see https://kubernetes.io/docs/concepts/storage/persistent-volumes/"], + "$ref": "#/definitions/volume" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "volume": { + "title": "Volume", + "description": "An identifiable, logical unit of data storage tied to a physical device.", + "type": "object", + "additionalProperties": false, + "properties": { + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier for the volume instance within its deployment context.", + "type": "string" + }, + "name": { + "title": "Name", + "description": "The name of the volume instance", + "type": "string" + }, + "mode": { + "title": "Mode", + "description": "The mode for the volume instance.", + "type": "string", + "enum": [ + "filesystem", "block" + ], + "default": "filesystem" + }, + "path": { + "title": "Path", + "description": "The underlying path created from the actual volume.", + "type": "string" + }, + "sizeAllocated": { + "title": "Size allocated", + "description": "The allocated size of the volume accessible to the associated workspace. This should include the scalar size as well as IEC standard unit in either decimal or binary form.", + "examples": ["10GB", "2Ti", "1Pi"], + "type": "string" + }, + "persistent": { + "title": "Persistent", + "description": "Indicates if the volume persists beyond the life of the resource it is associated with.", + "type": "boolean" + }, + "remote": { + "title": "Remote", + "description": "Indicates if the volume is remotely (i.e., network) attached.", + "type": "boolean" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "trigger": { + "title": "Trigger", + "description": "Represents a resource that can conditionally activate (or fire) tasks based upon associated events and their data.", + "type": "object", + "additionalProperties": false, + "required": [ + "type", + "bom-ref", + "uid" + ], + "properties": { + "bom-ref": { + "title": "BOM Reference", + "description": "An optional identifier which can be used to reference the trigger elsewhere in the BOM. Every bom-ref MUST be unique within the BOM.", + "$ref": "#/definitions/refType" + }, + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier for the resource instance within its deployment context.", + "type": "string" + }, + "name": { + "title": "Name", + "description": "The name of the resource instance.", + "type": "string" + }, + "description": { + "title": "Description", + "description": "A description of the resource instance.", + "type": "string" + }, + "resourceReferences": { + "title": "Resource references", + "description": "References to component or service resources that are used to realize the resource instance.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/resourceReferenceChoice" + } + }, + "type": { + "title": "Type", + "description": "The source type of event which caused the trigger to fire.", + "type": "string", + "enum": [ + "manual", + "api", + "webhook", + "scheduled" + ] + }, + "event": { + "title": "Event", + "description": "The event data that caused the associated trigger to activate.", + "$ref": "#/definitions/event" + }, + "conditions": { + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/condition" + } + }, + "timeActivated": { + "title": "Time activated", + "description": "The date and time (timestamp) when the trigger was activated.", + "type": "string", + "format": "date-time" + }, + "inputs": { + "title": "Inputs", + "description": "Represents resources and data brought into a task at runtime by executor or task commands", + "examples": ["a `configuration` file which was declared as a local `component` or `externalReference`"], + "type": "array", + "items": { + "$ref": "#/definitions/inputType" + }, + "uniqueItems": true + }, + "outputs": { + "title": "Outputs", + "description": "Represents resources and data output from a task at runtime by executor or task commands", + "examples": ["a log file or metrics data produced by the task"], + "type": "array", + "items": { + "$ref": "#/definitions/outputType" + }, + "uniqueItems": true + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "event": { + "title": "Event", + "description": "Represents something that happened that may trigger a response.", + "type": "object", + "additionalProperties": false, + "properties": { + "uid": { + "title": "Unique Identifier (UID)", + "description": "The unique identifier of the event.", + "type": "string" + }, + "description": { + "title": "Description", + "description": "A description of the event.", + "type": "string" + }, + "timeReceived": { + "title": "Time Received", + "description": "The date and time (timestamp) when the event was received.", + "type": "string", + "format": "date-time" + }, + "data": { + "title": "Data", + "description": "Encoding of the raw event data.", + "$ref": "#/definitions/attachment" + }, + "source": { + "title": "Source", + "description": "References the component or service that was the source of the event", + "$ref": "#/definitions/resourceReferenceChoice" + }, + "target": { + "title": "Target", + "description": "References the component or service that was the target of the event", + "$ref": "#/definitions/resourceReferenceChoice" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "inputType": { + "title": "Input type", + "description": "Type that represents various input data types and formats.", + "type": "object", + "oneOf": [ + { + "required": [ + "resource" + ] + }, + { + "required": [ + "parameters" + ] + }, + { + "required": [ + "environmentVars" + ] + }, + { + "required": [ + "data" + ] + } + ], + "additionalProperties": false, + "properties": { + "source": { + "title": "Source", + "description": "A references to the component or service that provided the input to the task (e.g., reference to a service with data flow value of `inbound`)", + "examples": [ + "source code repository", + "database" + ], + "$ref": "#/definitions/resourceReferenceChoice" + }, + "target": { + "title": "Target", + "description": "A reference to the component or service that received or stored the input if not the task itself (e.g., a local, named storage workspace)", + "examples": [ + "workspace", + "directory" + ], + "$ref": "#/definitions/resourceReferenceChoice" + }, + "resource": { + "title": "Resource", + "description": "A reference to an independent resource provided as an input to a task by the workflow runtime.", + "examples": [ + "reference to a configuration file in a repository (i.e., a bom-ref)", + "reference to a scanning service used in a task (i.e., a bom-ref)" + ], + "$ref": "#/definitions/resourceReferenceChoice" + }, + "parameters": { + "title": "Parameters", + "description": "Inputs that have the form of parameters with names and values.", + "type": "array", + "uniqueItems": true, + "items": { + "$ref": "#/definitions/parameter" + } + }, + "environmentVars": { + "title": "Environment variables", + "description": "Inputs that have the form of parameters with names and values.", + "type": "array", + "uniqueItems": true, + "items": { + "oneOf": [ + { + "$ref": "#/definitions/property" + }, + { + "type": "string" + } + ] + } + }, + "data": { + "title": "Data", + "description": "Inputs that have the form of data.", + "$ref": "#/definitions/attachment" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "outputType": { + "type": "object", + "oneOf": [ + { + "required": [ + "resource" + ] + }, + { + "required": [ + "environmentVars" + ] + }, + { + "required": [ + "data" + ] + } + ], + "additionalProperties": false, + "properties": { + "type": { + "title": "Type", + "description": "Describes the type of data output.", + "type": "string", + "enum": [ + "artifact", + "attestation", + "log", + "evidence", + "metrics", + "other" + ] + }, + "source": { + "title": "Source", + "description": "Component or service that generated or provided the output from the task (e.g., a build tool)", + "$ref": "#/definitions/resourceReferenceChoice" + }, + "target": { + "title": "Target", + "description": "Component or service that received the output from the task (e.g., reference to an artifactory service with data flow value of `outbound`)", + "examples": ["a log file described as an `externalReference` within its target domain."], + "$ref": "#/definitions/resourceReferenceChoice" + }, + "resource": { + "title": "Resource", + "description": "A reference to an independent resource generated as output by the task.", + "examples": [ + "configuration file", + "source code", + "scanning service" + ], + "$ref": "#/definitions/resourceReferenceChoice" + }, + "data": { + "title": "Data", + "description": "Outputs that have the form of data.", + "$ref": "#/definitions/attachment" + }, + "environmentVars": { + "title": "Environment variables", + "description": "Outputs that have the form of environment variables.", + "type": "array", + "items": { + "oneOf": [ + { + "$ref": "#/definitions/property" + }, + { + "type": "string" + } + ] + }, + "uniqueItems": true + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "resourceReferenceChoice": { + "title": "Resource reference choice", + "description": "A reference to a locally defined resource (e.g., a bom-ref) or an externally accessible resource.", + "$comment": "Enables reference to a resource that participates in a workflow; using either internal (bom-ref) or external (externalReference) types.", + "type": "object", + "additionalProperties": false, + "properties": { + "ref": { + "title": "BOM Reference", + "description": "References an object by its bom-ref attribute", + "anyOf": [ + { + "title": "Ref", + "$ref": "#/definitions/refLinkType" + }, + { + "title": "BOM-Link Element", + "$ref": "#/definitions/bomLinkElementType" + } + ] + }, + "externalReference": { + "title": "External reference", + "description": "Reference to an externally accessible resource.", + "$ref": "#/definitions/externalReference" + } + }, + "oneOf": [ + { + "required": [ + "ref" + ] + }, + { + "required": [ + "externalReference" + ] + } + ] + }, + "condition": { + "title": "Condition", + "description": "A condition that was used to determine a trigger should be activated.", + "type": "object", + "additionalProperties": false, + "properties": { + "description": { + "title": "Description", + "description": "Describes the set of conditions which cause the trigger to activate.", + "type": "string" + }, + "expression": { + "title": "Expression", + "description": "The logical expression that was evaluated that determined the trigger should be fired.", + "type": "string" + }, + "properties": { + "type": "array", + "title": "Properties", + "items": { + "$ref": "#/definitions/property" + } + } + } + }, + "taskType": { + "type": "string", + "enum": [ + "copy", + "clone", + "lint", + "scan", + "merge", + "build", + "test", + "deliver", + "deploy", + "release", + "clean", + "other" + ] + }, + "parameter": { + "title": "Parameter", + "description": "A representation of a functional parameter.", + "type": "object", + "additionalProperties": false, + "properties": { + "name": { + "title": "Name", + "description": "The name of the parameter.", + "type": "string" + }, + "value": { + "title": "Value", + "description": "The value of the parameter.", + "type": "string" + }, + "dataType": { + "title": "Data type", + "description": "The data type of the parameter.", + "type": "string" + } + } + }, "signature": { "$ref": "jsf-0.82.schema.json#/definitions/signature", "title": "Signature", diff --git a/schema/cyclonedx/cyclonedx.xsd b/schema/cyclonedx/cyclonedx.xsd index f9859c78910..eeb64a6aca2 100644 --- a/schema/cyclonedx/cyclonedx.xsd +++ b/schema/cyclonedx/cyclonedx.xsd @@ -16,13 +16,13 @@ limitations under the License. --> + version="1.5.0"> @@ -37,9 +37,49 @@ limitations under the License. - Identifier-DataType for interlinked elements. + Identifier for referable and therefore interlink-able elements. - + + + + + + + + + Descriptor for an element identified by the attribute "bom-ref" in the same BOM document. + In contrast to `bomLinkElementType`. + + + + + + + + + Descriptor for another BOM document. + See https://cyclonedx.org/capabilities/bomlink/ + + + + + + + + + + + Descriptor for an element in another BOM document. + See https://cyclonedx.org/capabilities/bomlink/ + + + + + + + + + @@ -49,14 +89,74 @@ limitations under the License. The date and time (timestamp) when the BOM was created. + + + + The product lifecycle(s) that this BOM represents. + + + + + + + + + + + + A pre-defined phase in the product lifecycle. + + + + + + + + + The name of the lifecycle phase + + + + + + + The description of the lifecycle phase + + + + + + + + + + The tool(s) used in the creation of the BOM. - - - + + + + + DEPRECATED. Use tools\components or tools\services instead. + + + + + + + A list of software and hardware components used as tools. + + + + + A list of services used as tools. + + + + @@ -89,7 +189,7 @@ limitations under the License. - Provides the ability to document properties in a key/value store. + Provides the ability to document properties in a name/value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Property names of interest to the general public are encouraged to be registered in the @@ -113,6 +213,75 @@ limitations under the License. + + + + + + BOM produced early in the development lifecycle containing inventory of components and services + that are proposed or planned to be used. The inventory may need to be procured, retrieved, + or resourced prior to use. + + + + + + + BOM consisting of information obtained prior to a build process and may contain source files + and development artifacts and manifests. The inventory may need to be resolved and retrieved + prior to use. + + + + + + + BOM consisting of information obtained during a build process where component inventory is + available for use. The precise versions of resolved components are usually available at this + time as well as the provenance of where the components were retrieved from. + + + + + + + BOM consisting of information obtained after a build process has completed and the resulting + components(s) are available for further analysis. Built components may exist as the result of a + CI/CD process, may have been installed or deployed to a system or device, and may need to be + retrieved or extracted from the system or device. + + + + + + + BOM produced that represents inventory that is running and operational. This may include staging + or production environments and will generally encompass multiple SBOMs describing the applications + and operating system, along with HBOMs describing the hardware that makes up the system. Operations + Bill of Materials (OBOM) can provide full-stack inventory of runtime environments, configurations, + and additional dependencies. + + + + + + + BOM consisting of information observed through network discovery providing point-in-time + enumeration of embedded, on-premise, and cloud-native services such as server applications, + connected devices, microservices, and serverless functions. + + + + + + + BOM containing inventory that will be, or has been retired from operations. + + + + + + @@ -138,6 +307,14 @@ limitations under the License. + + + + An optional identifier which can be used to reference the object elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + User-defined attributes may be used on this element as long as they @@ -219,6 +396,14 @@ limitations under the License. + + + + An optional identifier which can be used to reference the object elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + User-defined attributes may be used on this element as long as they @@ -289,7 +474,7 @@ limitations under the License. Specifies a description for the component - + Specifies the scope of the component. If scope is not specified, 'required' scope SHOULD be assumed by the consumer of the BOM. @@ -358,7 +543,7 @@ limitations under the License. - Provides the ability to document properties in a key/value store. + Provides the ability to document properties in a name/value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Property names of interest to the general public are encouraged to be registered in the @@ -397,6 +582,21 @@ limitations under the License. Specifies optional release notes. + + + A model card describes the intended uses of a machine learning model and potential + limitations, including biases and ethical considerations. Model cards typically contain the + training parameters, which datasets were used to train the model, performance metrics, and other + relevant data useful for ML transparency. This object SHOULD be specified for any component of + type `machine-learning-model` and MUST NOT be specified for other component types. + + + + + This object SHOULD be specified for any component of type `data` and MUST NOT be + specified for other component types. + + @@ -463,6 +663,141 @@ limitations under the License. an externalReference should also be specified for completeness. + + + Licensing details describing the licensor/licensee, license type, renewal and + expiration dates, and other important metadata + + + + + + License identifiers that may be used to manage licenses and + their lifecycle + + + + + + + + + + The individual or organization that grants a license to another + individual or organization + + + + + + + The organization that granted the license + + + + + The individual, not associated with an organization, + that granted the license + + + + + + + + + The individual or organization for which a license was granted to + + + + + + + The organization that was granted the license + + + + + The individual, not associated with an organization, + that was granted the license + + + + + + + + + The individual or organization that purchased the license + + + + + + + The organization that purchased the license + + + + + The individual, not associated with an organization, + that purchased the license + + + + + + + + + The purchase order identifier the purchaser sent to a supplier or + vendor to authorize a purchase + + + + + The type of license(s) that was granted to the licensee + + + + + + + + + + The timestamp indicating when the license was last + renewed. For new purchases, this is often the purchase or acquisition date. + For non-perpetual licenses or subscriptions, this is the timestamp of when the + license was last renewed. + + + + + The timestamp indicating when the current license + expires (if applicable). + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + @@ -471,6 +806,14 @@ limitations under the License. + + + + An optional identifier which can be used to reference the license elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + @@ -565,6 +908,12 @@ limitations under the License. virtualization technology. Refer to https://en.wikipedia.org/wiki/OS-level_virtualization + + + A runtime environment which interprets or executes software. This may include + runtimes such as those that execute bytecode or low-code/no-code application platforms. + + A software operating system without regard to deployment model @@ -577,7 +926,15 @@ limitations under the License. A hardware device such as a processor, or chip-set. A hardware device containing firmware SHOULD include a component for the physical hardware itself, and another component of type 'firmware' or 'operating-system' (whichever is relevant), describing - information about the software running on the device. + information about the software running on the device. + See also the list of known device properties: https://github.com/CycloneDX/cyclonedx-property-taxonomy/blob/main/cdx/device.md + + + + + + A special type of software that operates or controls a particular type of device. + Refer to https://en.wikipedia.org/wiki/Device_driver @@ -592,6 +949,17 @@ limitations under the License. for information about files. + + + A model based on training data that can make predictions or decisions without + being explicitly programmed to do so. + + + + + A collection of discrete values that convey information. + + @@ -612,6 +980,108 @@ limitations under the License. + + + + + A license that grants use of software solely for the purpose + of education or research. + + + + + A license covering use of software embedded in a specific + piece of hardware. + + + + + A Client Access License (CAL) allows client computers to access + services provided by server software. + + + + + A Concurrent User license (aka floating license) limits the + number of licenses for a software application and licenses are shared among + a larger number of users. + + + + + A license where the core of a computer's processor is assigned + a specific number of points. + + + + + A license for which consumption is measured by non-standard + metrics. + + + + + A license that covers a defined number of installations on + computers and other types of devices. + + + + + A license that grants permission to install and use software + for trial purposes. + + + + + A license that grants access to the software to one or more + pre-defined users. + + + + + A license that grants access to the software on one or more + pre-defined computers or devices. + + + + + An Original Equipment Manufacturer license that is delivered + with hardware, cannot be transferred to other hardware, and is valid for the + life of the hardware. + + + + + A license where the software is sold on a one-time basis and + the licensee can use a copy of the software indefinitely. + + + + + A license where each installation consumes points per + processor. + + + + + A license where the licensee pays a fee to use the software + or service. + + + + + A license that grants access to the software or service by a + specified number of users. + + + + + Another license type. + + + + + @@ -724,7 +1194,7 @@ limitations under the License. - Bill-of-material document (CycloneDX, SPDX, SWID, etc) + Bill-of-materials (SBOM, OBOM, HBOM, SaaSBOM, etc) @@ -757,6 +1227,11 @@ limitations under the License. Direct or repository download location + + + The location where a component was published to. This is often the same as "distribution" but may also include specialized publishing processes that act as an intermediary + + The URL to the license file. If a license URL has been defined in the license @@ -778,38 +1253,159 @@ limitations under the License. URL to release notes - + - Use this if no other types accurately describe the purpose of the external reference + Specifies a way to contact the maintainer, supplier, or provider in the event of a security incident. Common URIs include links to a disclosure procedure, a mailto (RFC-2368) that specifies an email address, a tel (RFC-3966) that specifies a phone number, or dns (RFC-4501]) that specifies the records containing DNS Security TXT. - - - - - - - External references provide a way to document systems, sites, and information that may be relevant - but which are not included with the BOM. - - - - + - Zero or more external references can be defined + A model card describes the intended uses of a machine learning model, potential + limitations, biases, ethical considerations, training parameters, datasets used to train the + model, performance metrics, and other relevant data useful for ML transparency. - - - - - - - + + - The URL to the external reference + A record of events that occurred in a computer system or application, such as problems, errors, or information on current operations. - - + + + + Parameters or settings that may be used by other components or services. + + + + + Information used to substantiate a claim. + + + + + Describes how a component or service was manufactured or deployed. + + + + + Human or machine-readable statements containing facts, evidence, or testimony + + + + + An enumeration of identified weaknesses, threats, and countermeasures, dataflow diagram (DFD), attack tree, and other supporting documentation in human-readable or machine-readable format + + + + + The defined assumptions, goals, and capabilities of an adversary. + + + + + Identifies and analyzes the potential of future events that may negatively impact individuals, assets, and/or the environment. Risk assessments may also include judgments on the tolerability of each risk. + + + + + A Vulnerability Disclosure Report (VDR) which asserts the known and previously unknown vulnerabilities that affect a component, service, or product including the analysis and findings describing the impact (or lack of impact) that the reported vulnerability has on a component, service, or product. + + + + + A Vulnerability Exploitability eXchange (VEX) which asserts the known vulnerabilities that do not affect a product, product family, or organization, and optionally the ones that do. The VEX should include the analysis and findings describing the impact (or lack of impact) that the reported vulnerability has on the product, product family, or organization. + + + + + Results from an authorized simulated cyberattack on a component or service, otherwise known as a penetration test + + + + + SARIF or proprietary machine or human-readable report for which static analysis has identified code quality, security, and other potential issues with the source code + + + + + Dynamic analysis report that has identified issues such as vulnerabilities and misconfigurations + + + + + Report generated by analyzing the call stack of a running application + + + + + Report generated by Software Composition Analysis (SCA), container analysis, or other forms of component analysis + + + + + Report containing a formal assessment of an organization, business unit, or team against a maturity model + + + + + Industry, regulatory, or other certification from an accredited (if applicable) certification body + + + + + Report or system in which quality metrics can be obtained + + + + + Code or configuration that defines and provisions virtualized infrastructure, commonly referred to as Infrastructure as Code (IaC) + + + + + Plans of Action and Milestones (POAM) compliment an "attestation" external reference. POAM is defined by NIST as a "document that identifies tasks needing to be accomplished. It details resources required to accomplish the elements of the plan, any milestones in meeting the tasks and scheduled completion dates for the milestones". + + + + + Use this if no other types accurately describe the purpose of the external reference + + + + + + + + + External references provide a way to document systems, sites, and information that may be + relevant, but are not included with the BOM. They may also establish specific relationships + within or external to the BOM. + + + + + + Zero or more external references can be defined + + + + + + + + + + The URI (URL or URN) to the external reference. External references + are URIs and therefore can accept any URL scheme including https, mailto, tel, and dns. + External references may also include formally registered URNs such as CycloneDX BOM-Link to + reference CycloneDX BOMs or any object within a BOM. BOM-Link transforms applicable external + references into relationships that can be expressed in a BOM or across BOMs. Refer to: + https://cyclonedx.org/capabilities/bomlink/ + + + + + + An optional comment describing the external reference @@ -1188,9 +1784,9 @@ limitations under the License. - + - References a component or service by the its bom-ref attribute + References a component or service by its bom-ref attribute @@ -1205,10 +1801,12 @@ limitations under the License. - Components that do not have their own dependencies MUST be declared as empty - elements within the graph. Components that are not represented in the dependency graph MAY - have unknown dependencies. It is RECOMMENDED that implementations assume this to be opaque - and not an indicator of a component being dependency-free. + Defines the direct dependencies of a component or service. Components or services + that do not have their own dependencies MUST be declared as empty elements within the graph. + Components or services that are not represented in the dependency graph MAY have unknown + dependencies. It is RECOMMENDED that implementations assume this to be opaque and not an + indicator of a object being dependency-free. It is RECOMMENDED to leverage compositions to + indicate unknown dependency graphs. @@ -1288,15 +1886,85 @@ limitations under the License. A value of false indicates that by using the service, a trust boundary is not crossed. + + + The name of the trust zone the service resides in. + + - - + + + + + DEPRECATED: Specifies the data classification. THIS FIELD IS DEPRECATED AS OF v1.5. Use dataflow\classification instead + + + + Specifies the data classification. + + + + + Specifies the data classification. + + + + + + The URI, URL, or BOM-Link of the components or services the data came in from. + + + + + + + + + + + + + + The URI, URL, or BOM-Link of the components or services the data is sent to. + + + + + + + + + + + + + + + + Name for the defined data. + + + + + + + Short description of the data content and usage. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + - + @@ -1307,7 +1975,7 @@ limitations under the License. - Provides the ability to document properties in a key/value store. + Provides the ability to document properties in a name/value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Property names of interest to the general public are encouraged to be registered in the @@ -1398,11 +2066,25 @@ limitations under the License. - + A valid SPDX license expression. Refer to https://spdx.org/specifications for syntax requirements + + + + + + + An optional identifier which can be used to reference the license elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + @@ -1413,8 +2095,208 @@ limitations under the License. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Evidence that substantiates the identity of a component. + + + + + + The identity field of the component which the evidence describes. + + + + + The overall confidence of the evidence from 0 - 1, where 1 is 100% confidence. + + + + + The methods used to extract and/or analyze the evidence. + + + + + + + + + The technique used in this method of analysis. + + + + + The confidence of the evidence from 0 - 1, where 1 is 100% confidence. Confidence is specific to the technique used. Each technique of analysis can have independent confidence. + + + + + The value or contents of the evidence. + + + + + + + + + + + + The object in the BOM identified by its bom-ref. This is often a component or service, + but may be any object type supporting bom-refs. Tools used for analysis should already + be defined in the BOM, either in the metadata/tools, components, or formulation. + + + + + + + + + + + + + + Evidence of individual instances of a component spread across multiple locations. + + + + + + + + + The location or path to where the component was found. + + + + + + + An optional identifier which can be used to reference the occurrence elsewhere + in the BOM. Every bom-ref MUST be unique within the BOM. + + + + + + + + + + + Evidence of the components use through the callstack. + + + + + + + + + + + + A package organizes modules into namespaces, providing a unique namespace for each type it contains. + + + + + A module or class that encloses functions/methods and other code. + + + + + A block of code designed to perform a particular task. + + + + + Optional arguments that are passed to the module or function. + + + + + + + + + + The line number the code that is called resides on. + + + + + The column the code that is called resides. + + + + + The full path and filename of the module. + + + + + + + + + + + + The object in the BOM identified by its bom-ref. This is often a component or service, + but may be any object type supporting bom-refs. Tools used for analysis should already + be defined in the BOM, either in the metadata/tools, components, or formulation. + + + + + + + + + + + @@ -1502,14 +2384,41 @@ limitations under the License. + + + + The bom-ref identifiers of the vulnerabilities being described. + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + + + An optional identifier which can be used to reference the composition elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + - The relationship is complete. No further relationships including constituent components, services, or dependencies exist. + The relationship is complete. No further relationships including constituent components, services, or dependencies are known to exist. @@ -1522,11 +2431,31 @@ limitations under the License. The relationship is incomplete. Only relationships for first-party components, services, or their dependencies are represented. + + + The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are proprietary. + + + + + The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are opensource. + + The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented. + + + The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are proprietary. + + + + + The relationship is incomplete. Only relationships for third-party components, services, or their dependencies are represented, limited specifically to those that are opensource. + + The relationship may be complete or incomplete. This usually signifies a 'best-effort' to obtain constituent components, services, or dependencies but the completeness is inconclusive. @@ -1566,9 +2495,9 @@ limitations under the License. * minor = A minor release, also known as an update, may contain a smaller number of changes than major releases. * patch = Patch releases are typically unplanned and may resolve defects or important security issues. * pre-release = A pre-release may include alpha, beta, or release candidates and typically have - limited support. They provide the ability to preview a release prior to its general availability. + limited support. They provide the ability to preview a release prior to its general availability. * internal = Internal releases are not for public consumption and are intended to be used exclusively - by the project or manufacturer that produced it. + by the project or manufacturer that produced it. @@ -1603,7 +2532,7 @@ limitations under the License. One or more alternate names the release may be referred to. This may - include unofficial terms used by development and marketing teams (e.g. code names). + include unofficial terms used by development and marketing teams (e.g. code names). @@ -1636,7 +2565,7 @@ limitations under the License. Zero or more release notes containing the locale and content. Multiple - note elements may be specified to support release notes in a wide variety of languages. + note elements may be specified to support release notes in a wide variety of languages. @@ -1659,7 +2588,7 @@ limitations under the License. - Provides the ability to document properties in a key/value store. + Provides the ability to document properties in a name/value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Property names of interest to the general public are encouraged to be registered in the @@ -1683,31 +2612,1736 @@ limitations under the License. - - - - References a component or service by the its bom-ref attribute - - - - - User-defined attributes may be used on this element as long as they - do not have the same name as an existing attribute used by the schema. - - - - - - - - - + + + + + A model card describes the intended uses of a machine learning model and potential limitations, including + biases and ethical considerations. Model cards typically contain the training parameters, which datasets + were used to train the model, performance metrics, and other relevant data useful for ML transparency. + This object SHOULD be specified for any component of type `machine-learning-model` and MUST NOT be specified + for other component types. + + + + + + + Hyper-parameters for construction of the model. + + + + + + + + The overall approach to learning used by the model for problem solving. + + + + + + + + Learning types describing the learning problem or hybrid learning problem. + + + + + + + + + + Directly influences the input and/or output. Examples include classification, + regression, clustering, etc. + + + + + + + The model architecture family such as transformer network, convolutional neural + network, residual neural network, LSTM neural network, etc. + + + + + + + The specific architecture of the model such as GPT-1, ResNet-50, YOLOv3, etc. + + + + + + + The datasets used to train and evaluate the model. + + + + + + + References a data component by the components bom-ref attribute + + + + + + + + + + + + + The input format(s) of the model + + + + + + + + + + + The data format for input to the model. Example formats include string, image, time-series + + + + + + + + + + + + + The output format(s) from the model + + + + + + + + + + + The data format for output from the model. Example formats include string, image, time-series + + + + + + + + + + + + + + + + A quantitative analysis of the model + + + + + + + + + + + + + + The type of performance metric. + + + + + + + The value of the performance metric. + + + + + + + The name of the slice this metric was computed on. By default, assume + this metric is not sliced. + + + + + + + The confidence interval of the metric. + + + + + + + + The lower bound of the confidence interval. + + + + + + + The upper bound of the confidence interval. + + + + + + + + + + + + + + + + A collection of graphics that represent various measurements + + + + + + + + A description of this collection of graphics. + + + + + + + A collection of graphics. + + + + + + + + + + + The name of the graphic. + + + + + + + The graphic (vector or raster). Base64 encoding MUST be specified for binary images. + + + + + + + + + + + + + + + + + + + What considerations should be taken into account regarding the model's construction, training, + and application? + + + + + + + + Who are the intended users of the model? + + + + + + + + + + + + What are the intended use cases of the model? + + + + + + + + + + + + What are the known technical limitations of the model? E.g. What kind(s) of data + should the model be expected not to perform well on? What are the factors that might + degrade model performance? + + + + + + + + + + + + What are the known tradeoffs in accuracy/performance of the model? + + + + + + + + + + + + What are the ethical (or environmental) risks involved in the application of this model? + + + + + + + + + + + The name of the risk + + + + + + + Strategy used to address this risk + + + + + + + + + + + + + How does the model affect groups at risk of being systematically disadvantaged? + What are the harms and benefits to the various affected groups? + + + + + + + + + + + The groups or individuals at risk of being systematically disadvantaged by the model. + + + + + + + Expected benefits to the identified groups. + + + + + + + Expected harms to the identified groups. + + + + + + + With respect to the benefits and harms outlined, please + describe any mitigation strategy implemented. + + + + + + + + + + + + + + + + + An optional identifier which can be used to reference the model card elsewhere in the BOM. + Every bom-ref MUST be unique within the BOM. + + + + + + + + + + TODO + + + + + TODO + + + + + TODO + + + + + TODO + + + + + TODO + + + + + + + + + + + The general theme or subject matter of the data being specified. + + + + + + + The name of the dataset. + + + + + + + The contents or references to the contents of the data being described. + + + + + + + An optional way to include textual or encoded data. + + + + + The URL to where the data can be retrieved. + + + + + Provides the ability to document name-value parameters used for configuration. + + + + + + + + + Data classification tags data according to its type, sensitivity, and value if altered, stolen, or destroyed. + + + + + + + A description of any sensitive data in a dataset. + + + + + + + A collection of graphics that represent various measurements. + + + + + + + A description of the dataset. Can describe size of dataset, whether it's used for source code, + training, testing, or validation, etc. + + + + + + + + + An optional identifier which can be used to reference the dataset elsewhere in the BOM. + Every bom-ref MUST be unique within the BOM. + + + + + + + + + + + Data custodians are responsible for the safe custody, transport, and storage of data. + + + + + + + + + + + + Data stewards are responsible for data content, context, and associated business rules. + + + + + + + + + + + + Data owners are concerned with risk and appropriate access to data. + + + + + + + + + + + + + + + + + + + + + + A collection of graphics that represent various measurements. + + + + + + + A description of this collection of graphics. + + + + + + + A collection of graphics. + + + + + + + + + + + The name of the graphic. + + + + + + + The graphic (vector or raster). Base64 encoding MUST be specified for binary images. + + + + + + + + + + + + + + + + + Any type of code, code snippet, or data-as-code. + + + + + Parameters or settings that may be used by other components. + + + + + A collection of data. + + + + + Data that can be used to create new instances of what the definition defines. + + + + + Any other type of data that does not fit into existing definitions. + + + + + + + + + References a component or service by its bom-ref attribute + + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + Specifies an individual property with a name and value. + + + + + + The name of the property. Duplicate names are allowed, each potentially having a different value. + + + + + + + + + + + Defines a weakness in a component or service that could be exploited or triggered by a threat source. + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + The identifier that uniquely identifies the vulnerability. For example: + CVE-2021-39182, GHSA-35m5-8cvj-8783, and SNYK-PYTHON-ENROCRYPT-1912876. + + + + + The source that published the vulnerability. + + + + + Zero or more pointers to vulnerabilities that are the equivalent of the + vulnerability specified. Often times, the same vulnerability may exist in multiple sources of + vulnerability intelligence, but have different identifiers. References provide a way to + correlate vulnerabilities across multiple sources of vulnerability intelligence. + + + + + + A pointer to a vulnerability that is the equivalent of the + vulnerability specified. + + + + + + The identifier that uniquely identifies the vulnerability. For example: + CVE-2021-39182, GHSA-35m5-8cvj-8783, and SNYK-PYTHON-ENROCRYPT-1912876. + + + + + The source that published the vulnerability. + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + + List of vulnerability ratings. + + + + + + + + + + + + List of Common Weaknesses Enumerations (CWEs) codes that describes this vulnerability. + For example 399 (of https://cwe.mitre.org/data/definitions/399.html) + + + + + + + + + + A description of the vulnerability as provided by the source. + + + + + If available, an in-depth description of the vulnerability as provided by the + source organization. Details often include information useful in understanding root cause. + + + + + Recommendations of how the vulnerability can be remediated or mitigated. + + + + + A bypass, usually temporary, of the vulnerability that reduces its likelihood and/or impact. Workarounds often involve changes to configuration or deployments. + + + + + + + Evidence used to reproduce the vulnerability. + + + + + + Precise steps to reproduce the vulnerability. + + + + + A description of the environment in which reproduction was possible. + + + + + Supporting material that helps in reproducing or understanding how reproduction is possible. This may include screenshots, payloads, and PoC exploit code. + + + + + + + + + + + + + + + Published advisories of the vulnerability if provided. + + + + + + + + + + The date and time (timestamp) when the vulnerability record was created in the vulnerability database. + + + + + The date and time (timestamp) when the vulnerability record was first published. + + + + + The date and time (timestamp) when the vulnerability record was last updated. + + + + + The date and time (timestamp) when the vulnerability record was rejected (if applicable). + + + + + Individuals or organizations credited with the discovery of the vulnerability. + + + + + + The organizations credited with vulnerability discovery. + + + + + + + + + + The individuals, not associated with organizations, that are credited with vulnerability discovery. + + + + + + + + + + + + + The tool(s) used to identify, confirm, or score the vulnerability. + + + + + + + DEPRECATED. Use tools\components or tools\services instead. + + + + + + + A list of software and hardware components used as tools. + + + + + A list of services used as tools. + + + + + + + + + + + An assessment of the impact and exploitability of the vulnerability. + + + + + + + Declares the current state of an occurrence of a vulnerability, after automated or manual analysis. + + + + + + + The rationale of why the impact analysis state was asserted. + + + + + + A response to the vulnerability by the manufacturer, supplier, or + project responsible for the affected component or service. More than one response + is allowed. Responses are strongly encouraged for vulnerabilities where the analysis + state is exploitable. + + + + + + + + + + + Detailed description of the impact including methods used during assessment. + If a vulnerability is not exploitable, this field should include specific details + on why the component or service is not impacted by this vulnerability. + + + + + + + The date and time (timestamp) when the analysis was first issued. + + + + + + + The date and time (timestamp) when the analysis was last updated. + + + + + + + + + The components or services that are affected by the vulnerability. + + + + + + + + + References a component or service by the objects bom-ref. + + + + + + + + Zero or more individual versions or range of versions. + + + + + + + + + + A single version of a component or service. + + + + + A version range specified in Package URL Version Range syntax (vers) which is defined at https://github.com/package-url/purl-spec/VERSION-RANGE-SPEC.rst + + + + + + + The vulnerability status for the version or range of versions. + + + + + + + + + + + + + + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + + An optional identifier which can be used to reference the vulnerability elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + + + + + The name of the source. + For example: NVD, National Vulnerability Database, OSS Index, VulnDB, and GitHub Advisories + + + + + + The url of the vulnerability documentation as provided by the source. + For example: https://nvd.nist.gov/vuln/detail/CVE-2021-39182 + + + + + + + + + + The source that calculated the severity or risk rating of the vulnerability. + + + + + The numerical score of the rating. + + + + + Textual representation of the severity that corresponds to the numerical score of the rating. + + + + + The risk scoring methodology/standard used. + + + + + Textual representation of the metric values used to score the vulnerability. + + + + + An optional reason for rating the vulnerability as it was. + + + + + + + + + + An optional name of the advisory. + + + + + Location where the advisory can be obtained. + + + + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + The organization that created the annotation + + + + + The person that created the annotation + + + + + The tool or component that created the annotation + + + + + The service that created the annotation + + + + + + + + + + + The objects in the BOM identified by their bom-ref's. This is often components or services, but may be any object type supporting bom-refs. + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + + The organization, individual, component, or service which created the textual content + of the annotation. + + + + + The date and time (timestamp) when the annotation was created. + + + + + The textual content of the annotation. + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + An optional identifier which can be used to reference the annotation elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + Textual representation of the severity of the vulnerability adopted by the analysis method. If the + analysis method uses values other than what is provided, the user is expected to translate appropriately. + + + + + + + + + + + + + + + + + Declares the current state of an occurrence of a vulnerability, after automated or manual analysis. + + + + + + + The vulnerability has been remediated. + + + + + + + The vulnerability has been remediated and evidence of the changes are provided in the affected + components pedigree containing verifiable commit history and/or diff(s). + + + + + + + The vulnerability may be directly or indirectly exploitable. + + + + + + + The vulnerability is being investigated. + + + + + + + The vulnerability is not specific to the component or service and was falsely identified or associated. + + + + + + + The component or service is not affected by the vulnerability. Justification should be specified + for all not_affected cases. + + + + + + + + + + The rationale of why the impact analysis state was asserted. + + + + + + + The code has been removed or tree-shaked. + + + + + + + The vulnerable code is not invoked at runtime. + + + + + + + Exploitability requires a configurable option to be set/unset. + + + + + + + Exploitability requires a dependency that is not present. + + + + + + + Exploitability requires a certain environment which is not present. + + + + + + + Exploitability requires a compiler flag to be set/unset. + + + + + + + Exploits are prevented at runtime. + + + + + + + Attacks are blocked at physical, logical, or network perimeter. + + + + + + + Preventative measures have been implemented that reduce the likelihood and/or impact of the vulnerability. + + + + + + + + + + Specifies the severity or risk scoring methodology or standard used. + + + + + + + The rating is based on CVSS v2 standard + https://www.first.org/cvss/v2/ + + + + + + + The rating is based on CVSS v3.0 standard + https://www.first.org/cvss/v3-0/ + + + + + + + The rating is based on CVSS v3.1 standard + https://www.first.org/cvss/v3-1/ + + + + + + + The rating is based on CVSS v4.0 standard + https://www.first.org/cvss/v4-0/ + + + + + + + The rating is based on OWASP Risk Rating + https://owasp.org/www-community/OWASP_Risk_Rating_Methodology + + + + + + + The rating is based on Stakeholder Specific Vulnerability Categorization (all versions) + https://github.com/CERTCC/SSVC + + + + + + + Use this if the risk scoring methodology is not based on any of the options above + + + + + + + + + + The rationale of why the impact analysis state was asserted. + + + + + + + + + + + + + + + The vulnerability status of a given version or range of versions of a product. The statuses + 'affected' and 'unaffected' indicate that the version is affected or unaffected by the vulnerability. + The status 'unknown' indicates that it is unknown or unspecified whether the given version is affected. + There can be many reasons for an 'unknown' status, including that an investigation has not been + undertaken or that a vendor has not disclosed the status. + + + + + + + + + + + + + Describes how a component or service was manufactured or deployed. This is achieved through the use + of formulas, workflows, tasks, and steps, which declare the precise steps to reproduce along with the + observed formulas describing the steps which transpired in the manufacturing process. + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + Describes workflows and resources that captures rules and other aspects of how the associated + BOM component or service was formed. + + + + + + Transient components that are used in tasks that constitute one or more of + this formula's workflows + + + + + Transient services that are used in tasks that constitute one or more of + this formula's workflows + + + + + List of workflows that can be declared to accomplish specific orchestrated goals + and independently triggered. + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + + An optional identifier which can be used to reference the formula elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + The unique identifier for the resource instance within its deployment context. + + + + + + + The name of the resource instance. + + + + + + + The description of the resource instance. + + + + + + References to component or service resources that are used to realize + the resource instance. + + + + + The tasks that comprise the workflow. + + + + + The graph of dependencies between tasks within the workflow. + + + + + Indicates the types of activities performed by the set of workflow tasks. + + + + + + + + + + The trigger that initiated the task. + + + + + + The sequence of steps for the task. + + + + + + + + + + + Represents resources and data brought into a task at runtime by executor + or task commands + + + + + + + + + + Represents resources and data output from a task at runtime by executor + or task commands + + + + + + + + + + + The date and time (timestamp) when the task started. + + + + + + + The date and time (timestamp) when the task ended. + + + + + + A set of named filesystem or data resource shareable by workflow tasks. + + + + + A graph of the component runtime topology for workflow's instance. + A description of the runtime component and service topology. This can describe a partial or + complete topology used to host and execute the task (e.g., hardware, operating systems, + configurations, etc.) + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + An optional identifier which can be used to reference the workflow elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + User-defined attributes may be used on this element as long as they @@ -1716,28 +4350,248 @@ limitations under the License. - - - Specifies an individual property with a name and value. - - - - + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + - The name of the property. Duplicate names are allowed, each potentially having a different value. + + References an object by its bom-ref attribute + - - - + + + + + + + + Reference to an externally accessible resource. + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + The unique identifier for the resource instance within its deployment context. + + + + + + + The name of the resource instance. + + + + + + + The description of the resource instance. + + + + + + + References to component or service resources that are used to realize the resource instance. + + + + + + + Indicates the types of activities performed by the set of workflow tasks. + + + + + + + + + + + + The trigger that initiated the task. + + + + + + + The sequence of steps for the task. + + + + + + + + + + + + Represents resources and data brought into a task at runtime by executor or task commands. + + + + + + + + + + + + Represents resources and data output from a task at runtime by executor or task commands + + + + + + + + + + + + The date and time (timestamp) when the task started. + + + + + + + The date and time (timestamp) when the task ended. + + + + + + + A set of named filesystem or data resource shareable by workflow tasks. + + + + + + + A graph of the component runtime topology for task's instance. + + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + An optional identifier which can be used to reference the task elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + - - - - - Defines a weakness in an component or service that could be exploited or triggered by a threat source. - - + + + + + + + + + + + + + + + + + + + + @@ -1754,259 +4608,260 @@ limitations under the License. - - - + + + + A named filesystem or data resource shareable by workflow tasks. + + + + - The identifier that uniquely identifies the vulnerability. For example: - CVE-2021-39182, GHSA-35m5-8cvj-8783, and SNYK-PYTHON-ENROCRYPT-1912876. + + The unique identifier for the resource instance within its deployment context. + - + - The source that published the vulnerability. + + The name of the resource instance. + - + - Zero or more pointers to vulnerabilities that are the equivalent of the - vulnerability specified. Often times, the same vulnerability may exist in multiple sources of - vulnerability intelligence, but have different identifiers. References provide a way to - correlate vulnerabilities across multiple sources of vulnerability intelligence. + + The names for the workspace as referenced by other workflow tasks. Effectively, a name mapping + so other tasks can use their own local name in their steps. + - - - - A pointer to a vulnerability that is the equivalent of the - vulnerability specified. - - - - - - The identifier that uniquely identifies the vulnerability. For example: - CVE-2021-39182, GHSA-35m5-8cvj-8783, and SNYK-PYTHON-ENROCRYPT-1912876. - - - - - The source that published the vulnerability. - - - - - - - - - Allows any undeclared elements as long as the elements are placed in a different namespace. - - - + + - + - List of vulnerability ratings. + + The description of the resource instance. + - - - - - - - - - - List of Common Weaknesses Enumerations (CWEs) codes that describes this vulnerability. - For example 399 (of https://cwe.mitre.org/data/definitions/399.html) - - - - - - + + + + References to component or service resources that are used to realize the resource instance. + + - + - A description of the vulnerability as provided by the source. + + Describes the read-write access control for the workspace relative to the owning resource instance. + - + - If available, an in-depth description of the vulnerability as provided by the - source organization. Details often include examples, proof-of-concepts, and other information - useful in understanding root cause. + + A path to a location on disk where the workspace will be available to the associated task's steps. + - + - Recommendations of how the vulnerability can be remediated or mitigated. + + The name of a domain-specific data type the workspace represents. This property is for CI/CD + frameworks that are able to provide access to structured, managed data at a more granular level + than a filesystem. + - - - - - Published advisories of the vulnerability if provided. - - - - - - + + + + Identifies the reference to the request for a specific volume type and parameters. + + - + - The date and time (timestamp) when the vulnerability record was created in the vulnerability database. + + Information about the actual volume instance allocated to the workspace. + - + - The date and time (timestamp) when the vulnerability record was first published. + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. - + - The date and time (timestamp) when the vulnerability record was last updated. + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + + An optional identifier which can be used to reference the workflow elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + + + + + + + + + An identifiable, logical unit of data storage tied to a physical device. + + + + + + + The unique identifier for the volume instance within its deployment context. + - + - Individuals or organizations credited with the discovery of the vulnerability. + + The name of the volume instance + - - - - - The organizations credited with vulnerability discovery. - - - - - - - - - - The individuals, not associated with organizations, that are credited with vulnerability discovery. - - - - - - - - - - + - The tool(s) used to identify, confirm, or score the vulnerability. + + The mode for the volume instance. + - - - - - - - - - - An assessment of the impact and exploitability of the vulnerability. - - - - - - - Declares the current state of an occurrence of a vulnerability, after automated or manual analysis. - - - - - - - The rationale of why the impact analysis state was asserted. - - - - - - A response to the vulnerability by the manufacturer, supplier, or - project responsible for the affected component or service. More than one response - is allowed. Responses are strongly encouraged for vulnerabilities where the analysis - state is exploitable. - - - - - - - - - - - Detailed description of the impact including methods used during assessment. - If a vulnerability is not exploitable, this field should include specific details - on why the component or service is not impacted by this vulnerability. - - - - - + + + + The underlying path created from the actual volume. + + + + + + + The allocated size of the volume accessible to the associated workspace. This should include + the scalar size as well as IEC standard unit in either decimal or binary form. + + - + - The components or services that are affected by the vulnerability. + + Indicates if the volume persists beyond the life of the resource it is associated with. + + + + + + + Indicates if the volume is remotely (i.e., network) attached. + + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + + + + + + + + + + + Executes specific commands or tools in order to accomplish its owning task as part of a sequence. + + + + + + + A name for the step. + + + + + + + A description of the step. + + + + + + + Ordered list of commands or directives for the step + - - + + - - + + - References a component or service by the objects bom-ref. + + A text representation of the executed command. + - + - Zero or more individual versions or range of versions. + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. - - - - - - - - - A single version of a component or service. - - - - - A version range specified in Package URL Version Range syntax (vers) which is defined at https://github.com/package-url/purl-spec/VERSION-RANGE-SPEC.rst - - - - - - - The vulnerability status for the version or range of versions. - - - - - - - - @@ -2014,311 +4869,496 @@ limitations under the License. + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + - + - - An optional identifier which can be used to reference the vulnerability elsewhere in the BOM. - Uniqueness is enforced within all elements and children of the root-level bom element. - + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. - + - - - + + + - The name of the source. - For example: NVD, National Vulnerability Database, OSS Index, VulnDB, and GitHub Advisories + + The unique identifier for the resource instance within its deployment context. - + - The url of the vulnerability documentation as provided by the source. - For example: https://nvd.nist.gov/vuln/detail/CVE-2021-39182 + + The name of the resource instance. + - - - - - - + - The source that calculated the severity or risk rating of the vulnerability. + + The description of the resource instance. + - + - The numerical score of the rating. + + References to component or service resources that are used to realize the resource instance. + - + - Textual representation of the severity that corresponds to the numerical score of the rating. + + The source type of event which caused the trigger to fire. + - + - The risk scoring methodology/standard used. + + The event data that caused the associated trigger to activate. + - + + + + + + + A condition that was used to determine a trigger should be activated. + + + + + + + + Describes the set of conditions which cause the trigger to activate. + + + + + + + The logical expression that was evaluated that determined the trigger should be fired. + + + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + + + + + + - Textual representation of the metric values used to score the vulnerability. + + The date and time (timestamp) when the trigger was activated. + - + - An optional reason for rating the vulnerability as it was. + + Represents resources and data brought into a task at runtime by executor or task commands + + + + + + - - - - - - + - An optional name of the advisory. + + Represents resources and data output from a task at runtime by executor or task commands + + + + + + - + - Location where the advisory can be obtained. + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + + Allows any undeclared elements as long as the elements are placed in a different namespace. + + + + + + + An optional identifier which can be used to reference the trigger elsewhere in the BOM. + Uniqueness is enforced within all elements and children of the root-level bom element. + + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + - - - - Textual representation of the severity of the vulnerability adopted by the analysis method. If the - analysis method uses values other than what is provided, the user is expected to translate appropriately. - - + - - - - - - - + + + + - - - - Declares the current state of an occurrence of a vulnerability, after automated or manual analysis. - - - - + + + - The vulnerability has been remediated. + The unique identifier of the event. - - + + - The vulnerability has been remediated and evidence of the changes are provided in the affected - components pedigree containing verifiable commit history and/or diff(s). + A description of the event. - - + + - The vulnerability may be directly or indirectly exploitable. + The date and time (timestamp) when the event was received. - - + + + + + Encoding of the raw event data. + + + + - The vulnerability is being investigated. + References the component or service that was the source of the event - - + + - The vulnerability is not specific to the component or service and was falsely identified or associated. + References the component or service that was the target of the event - - + + + + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. + + + - The component or service is not affected by the vulnerability. Justification should be specified - for all not_affected cases. + Allows any undeclared elements as long as the elements are placed in a different namespace. - - - + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + - + - - The rationale of why the impact analysis state was asserted. + + Type that represents various input data types and formats. - - + + + + + + A reference to an independent resource provided as an input to a task by the workflow runtime. + + + + + + + Inputs that have the form of parameters with names and values. + + + + + + + Inputs that have the form of parameters with names and values. + + + + + + + + + + + + + + + + Inputs that have the form of data. + + + + + - The code has been removed or tree-shaked. + A references to the component or service that provided the input to the task + (e.g., reference to a service with data flow value of inbound) - - + + - The vulnerable code is not invoked at runtime. + A reference to the component or service that received or stored the input if not the task + itself (e.g., a local, named storage workspace) - - + + - - Exploitability requires a configurable option to be set/unset. - + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. - - + + - Exploitability requires a dependency that is not present. + Allows any undeclared elements as long as the elements are placed in a different namespace. - - + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + Represents resources and data output from a task at runtime by executor or task commands + + + + + + + + A reference to an independent resource generated as output by the task. + + + + + + + Outputs that have the form of environment variables. + + + + + + + + + + + + + + + + Outputs that have the form of data. + + + + + - Exploitability requires a certain environment which is not present. + Describes the type of data output. - - + + - Exploitability requires a compiler flag to be set/unset. + Component or service that generated or provided the output from the task (e.g., a build tool) - - + + - Exploits are prevented at runtime. + Component or service that received the output from the task + (e.g., reference to an artifactory service with data flow value of outbound) - - + + - - Attacks are blocked at physical, logical, or network perimeter. - + Provides the ability to document properties in a name/value store. + This provides flexibility to include data not officially supported in the standard + without having to use additional namespaces or create extensions. Property names + of interest to the general public are encouraged to be registered in the + CycloneDX Property Taxonomy - https://github.com/CycloneDX/cyclonedx-property-taxonomy. + Formal registration is OPTIONAL. - - + + - Preventative measures have been implemented that reduce the likelihood and/or impact of the vulnerability. + Allows any undeclared elements as long as the elements are placed in a different namespace. - + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + + + + + + + + + + - + + + + + + + - - Specifies the severity or risk scoring methodology or standard used. + + A representation of a functional parameter. - - - - - The rating is based on CVSS v2 standard - https://www.first.org/cvss/v2/ - - - - + + - - The rating is based on CVSS v3.0 standard - https://www.first.org/cvss/v3-0/ + + The name of the parameter. - - + + - - The rating is based on CVSS v3.1 standard - https://www.first.org/cvss/v3-1/ + + The value of the parameter. - - + + - - The rating is based on OWASP Risk Rating - https://owasp.org/www-community/OWASP_Risk_Rating_Methodology + + The data type of the parameter. - - + + - - Use this if the risk scoring methodology is not based on any of the options above + + Allows any undeclared elements as long as the elements are placed in a different namespace. - - - - - - - - The rationale of why the impact analysis state was asserted. - - - - - - - - - - - - - - - The vulnerability status of a given version or range of versions of a product. The statuses - 'affected' and 'unaffected' indicate that the version is affected or unaffected by the vulnerability. - The status 'unknown' indicates that it is unknown or unspecified whether the given version is affected. - There can be many reasons for an 'unknown' status, including that an investigation has not been - undertaken or that a vendor has not disclosed the status. - - - - - - - - - + + + + + User-defined attributes may be used on this element as long as they + do not have the same name as an existing attribute used by the schema. + + + @@ -2351,12 +5391,12 @@ limitations under the License. - Compositions describe constituent parts (including components, services, and dependency relationships) and their completeness. + Compositions describe constituent parts (including components, services, and dependency relationships) and their completeness. The completeness of vulnerabilities expressed in a BOM may also be described. - Provides the ability to document properties in a key/value store. + Provides the ability to document properties in a name/value store. This provides flexibility to include data not officially supported in the standard without having to use additional namespaces or create extensions. Property names of interest to the general public are encouraged to be registered in the @@ -2369,6 +5409,23 @@ limitations under the License. Vulnerabilities identified in components or services. + + + Comments made by people, organizations, or tools about any object with + a bom-ref, such as components, services, vulnerabilities, or the BOM itself. Unlike + inventory information, annotations may contain opinion or commentary from various + stakeholders. Annotations may be inline (with inventory) or externalized via BOM-Link, + and may optionally be signed. + + + + + Describes how a component or service was manufactured or deployed. This is + achieved through the use of formulas, workflows, tasks, and steps, which declare the precise + steps to reproduce along with the observed formulas describing the steps which transpired + in the manufacturing process. + + @@ -2377,7 +5434,7 @@ limitations under the License. - + Whenever an existing BOM is modified, either manually or through automated processes, the version of the BOM SHOULD be incremented by 1. When a system is presented with @@ -2404,4 +5461,4 @@ limitations under the License. - + \ No newline at end of file diff --git a/syft/encode_decode.go b/syft/encode_decode.go deleted file mode 100644 index b06fb801eb5..00000000000 --- a/syft/encode_decode.go +++ /dev/null @@ -1,18 +0,0 @@ -package syft - -import ( - "io" - - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/sbom" -) - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func Encode(s sbom.SBOM, f sbom.Format) ([]byte, error) { - return formats.Encode(s, f) -} - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func Decode(reader io.Reader) (*sbom.SBOM, sbom.Format, error) { - return formats.Decode(reader) -} diff --git a/syft/formats/common/cyclonedxhelpers/author.go b/syft/format/common/cyclonedxhelpers/author.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/author.go rename to syft/format/common/cyclonedxhelpers/author.go diff --git a/syft/formats/common/cyclonedxhelpers/author_test.go b/syft/format/common/cyclonedxhelpers/author_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/author_test.go rename to syft/format/common/cyclonedxhelpers/author_test.go diff --git a/syft/formats/common/cyclonedxhelpers/component.go b/syft/format/common/cyclonedxhelpers/component.go similarity index 98% rename from syft/formats/common/cyclonedxhelpers/component.go rename to syft/format/common/cyclonedxhelpers/component.go index d7c79875025..89348d926d0 100644 --- a/syft/formats/common/cyclonedxhelpers/component.go +++ b/syft/format/common/cyclonedxhelpers/component.go @@ -7,7 +7,7 @@ import ( "github.com/anchore/packageurl-go" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/common" + "github.com/anchore/syft/syft/format/common" "github.com/anchore/syft/syft/pkg" ) diff --git a/syft/formats/common/cyclonedxhelpers/component_test.go b/syft/format/common/cyclonedxhelpers/component_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/component_test.go rename to syft/format/common/cyclonedxhelpers/component_test.go diff --git a/syft/formats/common/cyclonedxhelpers/cpe.go b/syft/format/common/cyclonedxhelpers/cpe.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/cpe.go rename to syft/format/common/cyclonedxhelpers/cpe.go diff --git a/syft/formats/common/cyclonedxhelpers/cpe_test.go b/syft/format/common/cyclonedxhelpers/cpe_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/cpe_test.go rename to syft/format/common/cyclonedxhelpers/cpe_test.go diff --git a/syft/formats/common/cyclonedxhelpers/decoder.go b/syft/format/common/cyclonedxhelpers/decoder.go similarity index 84% rename from syft/formats/common/cyclonedxhelpers/decoder.go rename to syft/format/common/cyclonedxhelpers/decoder.go index 3400cf9efbc..13a297e8056 100644 --- a/syft/formats/common/cyclonedxhelpers/decoder.go +++ b/syft/format/common/cyclonedxhelpers/decoder.go @@ -2,56 +2,18 @@ package cyclonedxhelpers import ( "fmt" - "io" - "strings" "github.com/CycloneDX/cyclonedx-go" "github.com/anchore/packageurl-go" "github.com/anchore/syft/syft/artifact" - "github.com/anchore/syft/syft/formats/common" + "github.com/anchore/syft/syft/format/common" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) -const cycloneDXXmlSchema = "http://cyclonedx.org/schema/bom" - -func GetValidator(format cyclonedx.BOMFileFormat) sbom.Validator { - return func(reader io.Reader) error { - bom := &cyclonedx.BOM{} - err := cyclonedx.NewBOMDecoder(reader, format).Decode(bom) - if err != nil { - return err - } - - xmlWithoutNS := format == cyclonedx.BOMFileFormatXML && !strings.Contains(bom.XMLNS, cycloneDXXmlSchema) - xmlWithoutComponents := format == cyclonedx.BOMFileFormatXML && bom.Components == nil - if (cyclonedx.BOM{} == *bom || xmlWithoutComponents || xmlWithoutNS) { - return fmt.Errorf("not a valid CycloneDX document") - } - return nil - } -} - -func GetDecoder(format cyclonedx.BOMFileFormat) sbom.Decoder { - return func(reader io.Reader) (*sbom.SBOM, error) { - bom := &cyclonedx.BOM{ - Components: &[]cyclonedx.Component{}, - } - err := cyclonedx.NewBOMDecoder(reader, format).Decode(bom) - if err != nil { - return nil, err - } - s, err := ToSyftModel(bom) - if err != nil { - return nil, err - } - return s, nil - } -} - func ToSyftModel(bom *cyclonedx.BOM) (*sbom.SBOM, error) { if bom == nil { return nil, fmt.Errorf("no content defined in CycloneDX BOM") diff --git a/syft/formats/common/cyclonedxhelpers/decoder_test.go b/syft/format/common/cyclonedxhelpers/decoder_test.go similarity index 96% rename from syft/formats/common/cyclonedxhelpers/decoder_test.go rename to syft/format/common/cyclonedxhelpers/decoder_test.go index 5d335d70026..cbdebb777d5 100644 --- a/syft/formats/common/cyclonedxhelpers/decoder_test.go +++ b/syft/format/common/cyclonedxhelpers/decoder_test.go @@ -1,8 +1,6 @@ package cyclonedxhelpers import ( - "bytes" - "encoding/json" "fmt" "testing" @@ -327,18 +325,6 @@ func Test_missingDataDecode(t *testing.T) { assert.Equal(t, pkg.Licenses.Empty(), true) } -func Test_missingComponentsDecode(t *testing.T) { - bom := &cyclonedx.BOM{ - SpecVersion: cyclonedx.SpecVersion1_4, - } - bomBytes, _ := json.Marshal(&bom) - decode := GetDecoder(cyclonedx.BOMFileFormatJSON) - - _, err := decode(bytes.NewReader(bomBytes)) - - assert.NoError(t, err) -} - func Test_decodeDependencies(t *testing.T) { c1 := cyclonedx.Component{ Name: "c1", diff --git a/syft/formats/common/cyclonedxhelpers/description.go b/syft/format/common/cyclonedxhelpers/description.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/description.go rename to syft/format/common/cyclonedxhelpers/description.go diff --git a/syft/formats/common/cyclonedxhelpers/description_test.go b/syft/format/common/cyclonedxhelpers/description_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/description_test.go rename to syft/format/common/cyclonedxhelpers/description_test.go diff --git a/syft/formats/common/cyclonedxhelpers/external_references.go b/syft/format/common/cyclonedxhelpers/external_references.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/external_references.go rename to syft/format/common/cyclonedxhelpers/external_references.go diff --git a/syft/formats/common/cyclonedxhelpers/external_references_test.go b/syft/format/common/cyclonedxhelpers/external_references_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/external_references_test.go rename to syft/format/common/cyclonedxhelpers/external_references_test.go diff --git a/syft/formats/common/cyclonedxhelpers/format.go b/syft/format/common/cyclonedxhelpers/format.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/format.go rename to syft/format/common/cyclonedxhelpers/format.go diff --git a/syft/formats/common/cyclonedxhelpers/format_test.go b/syft/format/common/cyclonedxhelpers/format_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/format_test.go rename to syft/format/common/cyclonedxhelpers/format_test.go diff --git a/syft/formats/common/cyclonedxhelpers/group.go b/syft/format/common/cyclonedxhelpers/group.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/group.go rename to syft/format/common/cyclonedxhelpers/group.go diff --git a/syft/formats/common/cyclonedxhelpers/group_test.go b/syft/format/common/cyclonedxhelpers/group_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/group_test.go rename to syft/format/common/cyclonedxhelpers/group_test.go diff --git a/syft/formats/common/cyclonedxhelpers/licenses.go b/syft/format/common/cyclonedxhelpers/licenses.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/licenses.go rename to syft/format/common/cyclonedxhelpers/licenses.go diff --git a/syft/formats/common/cyclonedxhelpers/licenses_test.go b/syft/format/common/cyclonedxhelpers/licenses_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/licenses_test.go rename to syft/format/common/cyclonedxhelpers/licenses_test.go diff --git a/syft/formats/common/cyclonedxhelpers/properties.go b/syft/format/common/cyclonedxhelpers/properties.go similarity index 89% rename from syft/formats/common/cyclonedxhelpers/properties.go rename to syft/format/common/cyclonedxhelpers/properties.go index ba630daeb21..243bd539734 100644 --- a/syft/formats/common/cyclonedxhelpers/properties.go +++ b/syft/format/common/cyclonedxhelpers/properties.go @@ -3,7 +3,7 @@ package cyclonedxhelpers import ( "github.com/CycloneDX/cyclonedx-go" - "github.com/anchore/syft/syft/formats/common" + "github.com/anchore/syft/syft/format/common" ) var ( diff --git a/syft/formats/common/cyclonedxhelpers/publisher.go b/syft/format/common/cyclonedxhelpers/publisher.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/publisher.go rename to syft/format/common/cyclonedxhelpers/publisher.go diff --git a/syft/formats/common/cyclonedxhelpers/publisher_test.go b/syft/format/common/cyclonedxhelpers/publisher_test.go similarity index 100% rename from syft/formats/common/cyclonedxhelpers/publisher_test.go rename to syft/format/common/cyclonedxhelpers/publisher_test.go diff --git a/syft/formats/common/property_encoder.go b/syft/format/common/property_encoder.go similarity index 100% rename from syft/formats/common/property_encoder.go rename to syft/format/common/property_encoder.go diff --git a/syft/formats/common/property_encoder_test.go b/syft/format/common/property_encoder_test.go similarity index 100% rename from syft/formats/common/property_encoder_test.go rename to syft/format/common/property_encoder_test.go diff --git a/syft/formats/common/spdxhelpers/description.go b/syft/format/common/spdxhelpers/description.go similarity index 100% rename from syft/formats/common/spdxhelpers/description.go rename to syft/format/common/spdxhelpers/description.go diff --git a/syft/formats/common/spdxhelpers/description_test.go b/syft/format/common/spdxhelpers/description_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/description_test.go rename to syft/format/common/spdxhelpers/description_test.go diff --git a/syft/formats/common/spdxhelpers/document_name.go b/syft/format/common/spdxhelpers/document_name.go similarity index 100% rename from syft/formats/common/spdxhelpers/document_name.go rename to syft/format/common/spdxhelpers/document_name.go diff --git a/syft/formats/common/spdxhelpers/document_name_test.go b/syft/format/common/spdxhelpers/document_name_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/document_name_test.go rename to syft/format/common/spdxhelpers/document_name_test.go diff --git a/syft/formats/common/spdxhelpers/document_namespace.go b/syft/format/common/spdxhelpers/document_namespace.go similarity index 100% rename from syft/formats/common/spdxhelpers/document_namespace.go rename to syft/format/common/spdxhelpers/document_namespace.go diff --git a/syft/formats/common/spdxhelpers/document_namespace_test.go b/syft/format/common/spdxhelpers/document_namespace_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/document_namespace_test.go rename to syft/format/common/spdxhelpers/document_namespace_test.go diff --git a/syft/formats/common/spdxhelpers/download_location.go b/syft/format/common/spdxhelpers/download_location.go similarity index 100% rename from syft/formats/common/spdxhelpers/download_location.go rename to syft/format/common/spdxhelpers/download_location.go diff --git a/syft/formats/common/spdxhelpers/download_location_test.go b/syft/format/common/spdxhelpers/download_location_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/download_location_test.go rename to syft/format/common/spdxhelpers/download_location_test.go diff --git a/syft/formats/common/spdxhelpers/external_ref.go b/syft/format/common/spdxhelpers/external_ref.go similarity index 100% rename from syft/formats/common/spdxhelpers/external_ref.go rename to syft/format/common/spdxhelpers/external_ref.go diff --git a/syft/formats/common/spdxhelpers/external_refs.go b/syft/format/common/spdxhelpers/external_refs.go similarity index 100% rename from syft/formats/common/spdxhelpers/external_refs.go rename to syft/format/common/spdxhelpers/external_refs.go diff --git a/syft/formats/common/spdxhelpers/external_refs_test.go b/syft/format/common/spdxhelpers/external_refs_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/external_refs_test.go rename to syft/format/common/spdxhelpers/external_refs_test.go diff --git a/syft/formats/common/spdxhelpers/file_type.go b/syft/format/common/spdxhelpers/file_type.go similarity index 100% rename from syft/formats/common/spdxhelpers/file_type.go rename to syft/format/common/spdxhelpers/file_type.go diff --git a/syft/formats/common/spdxhelpers/homepage.go b/syft/format/common/spdxhelpers/homepage.go similarity index 100% rename from syft/formats/common/spdxhelpers/homepage.go rename to syft/format/common/spdxhelpers/homepage.go diff --git a/syft/formats/common/spdxhelpers/homepage_test.go b/syft/format/common/spdxhelpers/homepage_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/homepage_test.go rename to syft/format/common/spdxhelpers/homepage_test.go diff --git a/syft/formats/common/spdxhelpers/license.go b/syft/format/common/spdxhelpers/license.go similarity index 100% rename from syft/formats/common/spdxhelpers/license.go rename to syft/format/common/spdxhelpers/license.go diff --git a/syft/formats/common/spdxhelpers/license_test.go b/syft/format/common/spdxhelpers/license_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/license_test.go rename to syft/format/common/spdxhelpers/license_test.go diff --git a/syft/formats/common/spdxhelpers/none_if_empty.go b/syft/format/common/spdxhelpers/none_if_empty.go similarity index 100% rename from syft/formats/common/spdxhelpers/none_if_empty.go rename to syft/format/common/spdxhelpers/none_if_empty.go diff --git a/syft/formats/common/spdxhelpers/none_if_empty_test.go b/syft/format/common/spdxhelpers/none_if_empty_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/none_if_empty_test.go rename to syft/format/common/spdxhelpers/none_if_empty_test.go diff --git a/syft/formats/common/spdxhelpers/originator_test.go b/syft/format/common/spdxhelpers/originator_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/originator_test.go rename to syft/format/common/spdxhelpers/originator_test.go diff --git a/syft/formats/common/spdxhelpers/origintor.go b/syft/format/common/spdxhelpers/origintor.go similarity index 100% rename from syft/formats/common/spdxhelpers/origintor.go rename to syft/format/common/spdxhelpers/origintor.go diff --git a/syft/formats/common/spdxhelpers/relationship_type.go b/syft/format/common/spdxhelpers/relationship_type.go similarity index 100% rename from syft/formats/common/spdxhelpers/relationship_type.go rename to syft/format/common/spdxhelpers/relationship_type.go diff --git a/syft/formats/common/spdxhelpers/source_info.go b/syft/format/common/spdxhelpers/source_info.go similarity index 100% rename from syft/formats/common/spdxhelpers/source_info.go rename to syft/format/common/spdxhelpers/source_info.go diff --git a/syft/formats/common/spdxhelpers/source_info_test.go b/syft/format/common/spdxhelpers/source_info_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/source_info_test.go rename to syft/format/common/spdxhelpers/source_info_test.go diff --git a/syft/formats/common/spdxhelpers/spdxid.go b/syft/format/common/spdxhelpers/spdxid.go similarity index 100% rename from syft/formats/common/spdxhelpers/spdxid.go rename to syft/format/common/spdxhelpers/spdxid.go diff --git a/syft/formats/common/spdxhelpers/spdxid_test.go b/syft/format/common/spdxhelpers/spdxid_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/spdxid_test.go rename to syft/format/common/spdxhelpers/spdxid_test.go diff --git a/syft/formats/common/spdxhelpers/to_format_model.go b/syft/format/common/spdxhelpers/to_format_model.go similarity index 99% rename from syft/formats/common/spdxhelpers/to_format_model.go rename to syft/format/common/spdxhelpers/to_format_model.go index 62f809644c7..d61eccef02d 100644 --- a/syft/formats/common/spdxhelpers/to_format_model.go +++ b/syft/format/common/spdxhelpers/to_format_model.go @@ -19,7 +19,7 @@ import ( "github.com/anchore/syft/internal/spdxlicense" "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/common/util" + "github.com/anchore/syft/syft/format/common/util" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" diff --git a/syft/formats/common/spdxhelpers/to_format_model_test.go b/syft/format/common/spdxhelpers/to_format_model_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/to_format_model_test.go rename to syft/format/common/spdxhelpers/to_format_model_test.go diff --git a/syft/formats/common/spdxhelpers/to_syft_model.go b/syft/format/common/spdxhelpers/to_syft_model.go similarity index 99% rename from syft/formats/common/spdxhelpers/to_syft_model.go rename to syft/format/common/spdxhelpers/to_syft_model.go index f61f723c8db..ae999f9b22f 100644 --- a/syft/formats/common/spdxhelpers/to_syft_model.go +++ b/syft/format/common/spdxhelpers/to_syft_model.go @@ -18,7 +18,7 @@ import ( "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/common/util" + "github.com/anchore/syft/syft/format/common/util" "github.com/anchore/syft/syft/license" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" diff --git a/syft/formats/common/spdxhelpers/to_syft_model_test.go b/syft/format/common/spdxhelpers/to_syft_model_test.go similarity index 100% rename from syft/formats/common/spdxhelpers/to_syft_model_test.go rename to syft/format/common/spdxhelpers/to_syft_model_test.go diff --git a/syft/formats/common/util/h_digest.go b/syft/format/common/util/h_digest.go similarity index 100% rename from syft/formats/common/util/h_digest.go rename to syft/format/common/util/h_digest.go diff --git a/syft/formats/common/util/h_digest_test.go b/syft/format/common/util/h_digest_test.go similarity index 100% rename from syft/formats/common/util/h_digest_test.go rename to syft/format/common/util/h_digest_test.go diff --git a/syft/format/cyclonedxjson/decoder.go b/syft/format/cyclonedxjson/decoder.go new file mode 100644 index 00000000000..67584cbc5ed --- /dev/null +++ b/syft/format/cyclonedxjson/decoder.go @@ -0,0 +1,131 @@ +package cyclonedxjson + +import ( + "encoding/json" + "fmt" + "io" + "strings" + + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/format/common/cyclonedxhelpers" + "github.com/anchore/syft/syft/format/internal/cyclonedxutil" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatDecoder = (*decoder)(nil) + +type decoder struct { + decoder cyclonedxutil.Decoder +} + +func NewFormatDecoder() sbom.FormatDecoder { + return decoder{ + decoder: cyclonedxutil.NewDecoder(cyclonedx.BOMFileFormatJSON), + } +} + +func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + id, version := d.Identify(reader) + if id != ID { + return nil, "", "", fmt.Errorf("not a cyclonedx json document") + } + if version == "" { + return nil, "", "", fmt.Errorf("unsupported cyclonedx json document version") + } + + doc, err := d.decoder.Decode(reader) + if err != nil { + return nil, id, version, fmt.Errorf("unable to decode cyclonedx json document: %w", err) + } + + s, err := cyclonedxhelpers.ToSyftModel(doc) + if err != nil { + return nil, id, version, err + } + + return s, id, version, nil +} + +func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + if _, err := reader.Seek(0, io.SeekStart); err != nil { + log.Debugf("unable to seek to start of CycloneDX JSON SBOM: %+v", err) + return "", "" + } + + type Document struct { + JSONSchema string `json:"$schema"` + BOMFormat string `json:"bomFormat"` + SpecVersion string `json:"specVersion"` + } + + dec := json.NewDecoder(reader) + + var doc Document + err := dec.Decode(&doc) + if err != nil { + // maybe not json? maybe not valid? doesn't matter, we won't process it. + return "", "" + } + + id, version := getFormatInfo(doc.JSONSchema, doc.BOMFormat, doc.SpecVersion) + if version == "" || id != ID { + // not a cyclonedx json document that we support + return "", "" + } + + return id, version +} + +func getFormatInfo(schemaURI, bomFormat string, specVersion any) (sbom.FormatID, string) { + if !strings.Contains(schemaURI, "cyclonedx.org/schema/bom") { + // not a cyclonedx json document + return "", "" + } + + if bomFormat != "CycloneDX" { + // not a cyclonedx json document + return "", "" + } + + // by this point this looks to be valid cyclonedx json, but we need to know the version + + var ( + version string + spec cyclonedx.SpecVersion + err error + ) + switch s := specVersion.(type) { + case string: + version = s + spec, err = cyclonedxutil.SpecVersionFromString(version) + if err != nil { + // not a supported version, but is cyclonedx json + return ID, "" + } + case cyclonedx.SpecVersion: + spec = s + version = cyclonedxutil.VersionFromSpecVersion(spec) + if version == "" { + // not a supported version, but is cyclonedx json + return ID, "" + } + default: + // bad input provided for version info + return ID, "" + } + + if spec < 0 { + // not a supported version, but is cyclonedx json + return ID, "" + } + + return ID, version +} diff --git a/syft/format/cyclonedxjson/decoder_test.go b/syft/format/cyclonedxjson/decoder_test.go new file mode 100644 index 00000000000..1a949eb2b31 --- /dev/null +++ b/syft/format/cyclonedxjson/decoder_test.go @@ -0,0 +1,118 @@ +package cyclonedxjson + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/sbom" +) + +func TestDecoder_Decode(t *testing.T) { + tests := []struct { + name string + file string + err bool + distro string + packages []string + }{ + { + name: "dir-scan", + file: "snapshot/TestCycloneDxDirectoryEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "image-scan", + file: "snapshot/TestCycloneDxImageEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "not-an-sbom", + file: "bad-sbom", + err: true, + }, + } + for _, test := range tests { + t.Run(test.file, func(t *testing.T) { + reader, err := os.Open(filepath.Join("test-fixtures", test.file)) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + if test.err { + assert.Equal(t, sbom.FormatID(""), formatID) + assert.Equal(t, "", formatVersion) + + _, decodeID, decodeVersion, err := dec.Decode(reader) + require.Error(t, err) + assert.Equal(t, sbom.FormatID(""), decodeID) + assert.Equal(t, "", decodeVersion) + + return + } + assert.Equal(t, ID, formatID) + assert.NotEmpty(t, formatVersion) + + bom, decodeID, decodeVersion, err := dec.Decode(reader) + require.NotNil(t, bom) + require.NoError(t, err) + + assert.Equal(t, ID, decodeID) + assert.Equal(t, formatVersion, decodeVersion) + + split := strings.SplitN(test.distro, ":", 2) + distroName := split[0] + distroVersion := split[1] + assert.Equal(t, bom.Artifacts.LinuxDistribution.ID, distroName) + assert.Equal(t, bom.Artifacts.LinuxDistribution.Version, distroVersion) + + var pkgs []string + for p := range bom.Artifacts.Packages.Enumerate() { + pkgs = append(pkgs, fmt.Sprintf("%s:%s", p.Name, p.Version)) + } + + assert.ElementsMatch(t, test.packages, pkgs) + }) + } +} + +func TestDecoder_Identify(t *testing.T) { + type testCase struct { + name string + file string + id sbom.FormatID + version string + } + + var cases []testCase + + for _, version := range SupportedVersions() { + cases = append(cases, testCase{ + name: fmt.Sprintf("v%s schema", version), + file: fmt.Sprintf("test-fixtures/identify/%s.json", version), + id: ID, + version: version, + }) + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + reader, err := os.Open(test.file) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + }) + } +} diff --git a/syft/format/cyclonedxjson/encoder.go b/syft/format/cyclonedxjson/encoder.go new file mode 100644 index 00000000000..259dc9eba2a --- /dev/null +++ b/syft/format/cyclonedxjson/encoder.go @@ -0,0 +1,52 @@ +package cyclonedxjson + +import ( + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/syft/format/internal/cyclonedxutil" + "github.com/anchore/syft/syft/sbom" +) + +const ID = cyclonedxutil.JSONFormatID + +func SupportedVersions() []string { + return cyclonedxutil.SupportedVersions(ID) +} + +type EncoderConfig struct { + Version string +} + +type encoder struct { + cfg EncoderConfig + cyclonedxutil.Encoder +} + +func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) { + enc, err := cyclonedxutil.NewEncoder(cfg.Version, cyclonedx.BOMFileFormatJSON) + if err != nil { + return nil, err + } + return encoder{ + cfg: cfg, + Encoder: enc, + }, nil +} + +func DefaultEncoderConfig() EncoderConfig { + return EncoderConfig{ + Version: cyclonedxutil.DefaultVersion, + } +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{} +} + +func (e encoder) Version() string { + return e.cfg.Version +} diff --git a/syft/format/cyclonedxjson/encoder_test.go b/syft/format/cyclonedxjson/encoder_test.go new file mode 100644 index 00000000000..766d3f93f0d --- /dev/null +++ b/syft/format/cyclonedxjson/encoder_test.go @@ -0,0 +1,132 @@ +package cyclonedxjson + +import ( + "bytes" + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/internal/testutil" + "github.com/anchore/syft/syft/sbom" +) + +var updateSnapshot = flag.Bool("update-cyclonedx-json", false, "update the *.golden files for cyclone-dx JSON encoders") +var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") + +func getEncoder(t testing.TB) sbom.FormatEncoder { + enc, err := NewFormatEncoderWithConfig(DefaultEncoderConfig()) + require.NoError(t, err) + return enc +} + +func TestCycloneDxDirectoryEncoder(t *testing.T) { + dir := t.TempDir() + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: true, + Redactor: redactor(dir), + }, + ) +} + +func TestCycloneDxImageEncoder(t *testing.T) { + testImage := "image-simple" + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: true, + Redactor: redactor(), + }, + ) +} + +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). + WithValuesRedacted(values...). + WithPatternRedactors( + map[string]string{ + // UUIDs + `urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`: `urn:uuid:redacted`, + + // timestamps + `([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`: `timestamp:redacted`, + + // image hashes + `sha256:[A-Fa-f0-9]{64}`: `sha256:redacted`, + + // BOM refs + `"bom-ref":\s*"[^"]+"`: `"bom-ref":"redacted"`, + }, + ) +} + +func TestSupportedVersions(t *testing.T) { + encs := defaultFormatEncoders() + require.NotEmpty(t, encs) + + versions := SupportedVersions() + require.Equal(t, len(versions), len(encs)) + + subject := testutil.DirectoryInput(t, t.TempDir()) + dec := NewFormatDecoder() + + for _, enc := range encs { + t.Run(enc.Version(), func(t *testing.T) { + require.Contains(t, versions, enc.Version()) + + var buf bytes.Buffer + require.NoError(t, enc.Encode(&buf, subject)) + + id, version := dec.Identify(bytes.NewReader(buf.Bytes())) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + var s *sbom.SBOM + var err error + s, id, version, err = dec.Decode(bytes.NewReader(buf.Bytes())) + require.NoError(t, err) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + require.NotEmpty(t, s.Artifacts.Packages.PackageCount()) + + assert.Equal(t, len(subject.Relationships), len(s.Relationships), "mismatched relationship count") + + if !assert.Equal(t, subject.Artifacts.Packages.PackageCount(), s.Artifacts.Packages.PackageCount(), "mismatched package count") { + t.Logf("expected: %d", subject.Artifacts.Packages.PackageCount()) + for _, p := range subject.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + t.Logf("actual: %d", s.Artifacts.Packages.PackageCount()) + for _, p := range s.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + } + }) + } +} + +func defaultFormatEncoders() []sbom.FormatEncoder { + var encs []sbom.FormatEncoder + for _, version := range SupportedVersions() { + enc, err := NewFormatEncoderWithConfig(EncoderConfig{Version: version}) + if err != nil { + panic(err) + } + encs = append(encs, enc) + } + return encs +} diff --git a/syft/format/cyclonedxjson/test-fixtures/bad-sbom b/syft/format/cyclonedxjson/test-fixtures/bad-sbom new file mode 100644 index 00000000000..bb8a8152e0e --- /dev/null +++ b/syft/format/cyclonedxjson/test-fixtures/bad-sbom @@ -0,0 +1 @@ +not an sbom! \ No newline at end of file diff --git a/syft/format/cyclonedxjson/test-fixtures/identify/1.2.json b/syft/format/cyclonedxjson/test-fixtures/identify/1.2.json new file mode 100644 index 00000000000..2d898a2cec1 --- /dev/null +++ b/syft/format/cyclonedxjson/test-fixtures/identify/1.2.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://cyclonedx.org/schema/bom-1.2.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.2", + "serialNumber": "urn:uuid:8cfb48be-7ed8-4cea-8c23-8992e98d12be", + "version": 1, + "metadata": { + "timestamp": "2023-09-29T12:02:02-04:00", + "tools": [ + { + "vendor": "anchore", + "name": "syft", + "version": "[not provided]" + } + ], + "component": { + "bom-ref": "a0ff99a6af10f11f", + "type": "file", + "name": "go.mod", + "version": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + }, + "components": [ + { + "bom-ref": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651?package-id=2ff71a67fb024c86", + "type": "library", + "name": "github.com/wagoodman/go-partybus", + "version": "v0.0.0-20230516145632-8ccac152c651", + "cpe": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*", + "purl": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651" + } + ] +} diff --git a/syft/format/cyclonedxjson/test-fixtures/identify/1.3.json b/syft/format/cyclonedxjson/test-fixtures/identify/1.3.json new file mode 100644 index 00000000000..cf14029772b --- /dev/null +++ b/syft/format/cyclonedxjson/test-fixtures/identify/1.3.json @@ -0,0 +1,59 @@ +{ + "$schema": "http://cyclonedx.org/schema/bom-1.3.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.3", + "serialNumber": "urn:uuid:57ce2636-4ca6-46ba-b389-196af071d6fc", + "version": 1, + "metadata": { + "timestamp": "2023-09-29T12:02:02-04:00", + "tools": [ + { + "vendor": "anchore", + "name": "syft", + "version": "[not provided]" + } + ], + "component": { + "bom-ref": "a0ff99a6af10f11f", + "type": "file", + "name": "go.mod", + "version": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + }, + "components": [ + { + "bom-ref": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651?package-id=2ff71a67fb024c86", + "type": "library", + "name": "github.com/wagoodman/go-partybus", + "version": "v0.0.0-20230516145632-8ccac152c651", + "cpe": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*", + "purl": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651", + "properties": [ + { + "name": "syft:package:foundBy", + "value": "go-mod-file-cataloger" + }, + { + "name": "syft:package:language", + "value": "go" + }, + { + "name": "syft:package:metadataType", + "value": "GolangModMetadata" + }, + { + "name": "syft:package:type", + "value": "go-module" + }, + { + "name": "syft:cpe23", + "value": "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "name": "syft:location:0:path", + "value": "/go.mod" + } + ] + } + ] +} diff --git a/syft/format/cyclonedxjson/test-fixtures/identify/1.4.json b/syft/format/cyclonedxjson/test-fixtures/identify/1.4.json new file mode 100644 index 00000000000..a95bfc6bc99 --- /dev/null +++ b/syft/format/cyclonedxjson/test-fixtures/identify/1.4.json @@ -0,0 +1,59 @@ +{ + "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.4", + "serialNumber": "urn:uuid:6be7491f-6a9b-40f0-a60e-f74e2c0e23c3", + "version": 1, + "metadata": { + "timestamp": "2023-09-29T12:02:02-04:00", + "tools": [ + { + "vendor": "anchore", + "name": "syft", + "version": "[not provided]" + } + ], + "component": { + "bom-ref": "a0ff99a6af10f11f", + "type": "file", + "name": "go.mod", + "version": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + }, + "components": [ + { + "bom-ref": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651?package-id=2ff71a67fb024c86", + "type": "library", + "name": "github.com/wagoodman/go-partybus", + "version": "v0.0.0-20230516145632-8ccac152c651", + "cpe": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*", + "purl": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651", + "properties": [ + { + "name": "syft:package:foundBy", + "value": "go-mod-file-cataloger" + }, + { + "name": "syft:package:language", + "value": "go" + }, + { + "name": "syft:package:metadataType", + "value": "GolangModMetadata" + }, + { + "name": "syft:package:type", + "value": "go-module" + }, + { + "name": "syft:cpe23", + "value": "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "name": "syft:location:0:path", + "value": "/go.mod" + } + ] + } + ] +} diff --git a/syft/format/cyclonedxjson/test-fixtures/identify/1.5.json b/syft/format/cyclonedxjson/test-fixtures/identify/1.5.json new file mode 100644 index 00000000000..e63a6f069aa --- /dev/null +++ b/syft/format/cyclonedxjson/test-fixtures/identify/1.5.json @@ -0,0 +1,59 @@ +{ + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.5", + "serialNumber": "urn:uuid:22c456c5-c2a0-4285-a8ef-01b4382822c3", + "version": 1, + "metadata": { + "timestamp": "2023-09-29T12:02:02-04:00", + "tools": [ + { + "vendor": "anchore", + "name": "syft", + "version": "[not provided]" + } + ], + "component": { + "bom-ref": "a0ff99a6af10f11f", + "type": "file", + "name": "go.mod", + "version": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + }, + "components": [ + { + "bom-ref": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651?package-id=2ff71a67fb024c86", + "type": "library", + "name": "github.com/wagoodman/go-partybus", + "version": "v0.0.0-20230516145632-8ccac152c651", + "cpe": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*", + "purl": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651", + "properties": [ + { + "name": "syft:package:foundBy", + "value": "go-mod-file-cataloger" + }, + { + "name": "syft:package:language", + "value": "go" + }, + { + "name": "syft:package:metadataType", + "value": "GolangModMetadata" + }, + { + "name": "syft:package:type", + "value": "go-module" + }, + { + "name": "syft:cpe23", + "value": "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "name": "syft:location:0:path", + "value": "/go.mod" + } + ] + } + ] +} diff --git a/syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden b/syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden similarity index 96% rename from syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden rename to syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden index 77ff36dbaf7..4932b99f6d4 100644 --- a/syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden +++ b/syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden @@ -1,7 +1,7 @@ { - "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", "bomFormat": "CycloneDX", - "specVersion": "1.4", + "specVersion": "1.5", "serialNumber": "urn:uuid:redacted", "version": 1, "metadata": { diff --git a/syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden b/syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden similarity index 96% rename from syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden rename to syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden index ac4799123d1..dd91b9bf768 100644 --- a/syft/formats/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden +++ b/syft/format/cyclonedxjson/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden @@ -1,7 +1,7 @@ { - "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", "bomFormat": "CycloneDX", - "specVersion": "1.4", + "specVersion": "1.5", "serialNumber": "urn:uuid:redacted", "version": 1, "metadata": { diff --git a/syft/format/cyclonedxxml/decoder.go b/syft/format/cyclonedxxml/decoder.go new file mode 100644 index 00000000000..4e9df4a6526 --- /dev/null +++ b/syft/format/cyclonedxxml/decoder.go @@ -0,0 +1,106 @@ +package cyclonedxxml + +import ( + "encoding/xml" + "fmt" + "io" + "strings" + + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/format/common/cyclonedxhelpers" + "github.com/anchore/syft/syft/format/internal/cyclonedxutil" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatDecoder = (*decoder)(nil) + +type decoder struct { + decoder cyclonedxutil.Decoder +} + +func NewFormatDecoder() sbom.FormatDecoder { + return decoder{ + decoder: cyclonedxutil.NewDecoder(cyclonedx.BOMFileFormatXML), + } +} + +func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + + id, version := d.Identify(reader) + if id != ID { + return nil, "", "", fmt.Errorf("not a cyclonedx xml document") + } + if version == "" { + return nil, "", "", fmt.Errorf("unsupported cyclonedx xml document version") + } + + doc, err := d.decoder.Decode(reader) + if err != nil { + return nil, id, version, fmt.Errorf("unable to decode cyclonedx xml document: %w", err) + } + + s, err := cyclonedxhelpers.ToSyftModel(doc) + if err != nil { + return nil, id, version, err + } + + return s, id, version, nil +} + +func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + log.Debugf("unable to seek to start of CycloneDX XML SBOM: %+v", err) + return "", "" + } + + type Document struct { + XMLNS string `xml:"xmlns,attr"` + } + + dec := xml.NewDecoder(reader) + + var doc Document + err := dec.Decode(&doc) + if err != nil { + // maybe not xml? maybe not valid? doesn't matter, we won't process it. + return "", "" + } + + id, version := getFormatInfo(doc.XMLNS) + if version == "" || id != ID { + // not a cyclonedx xml document that we support + return "", "" + } + + return id, version +} + +func getFormatInfo(xmlns string) (sbom.FormatID, string) { + version := getVersionFromXMLNS(xmlns) + + if !strings.Contains(xmlns, "cyclonedx.org/schema/bom") { + // not a cyclonedx xml document + return "", "" + } + + spec, err := cyclonedxutil.SpecVersionFromString(version) + if spec < 0 || err != nil { + // not a supported version, but is cyclonedx xml + return ID, "" + } + return ID, version +} + +func getVersionFromXMLNS(xmlns string) string { + fields := strings.Split(xmlns, "/") + return fields[len(fields)-1] +} diff --git a/syft/format/cyclonedxxml/decoder_test.go b/syft/format/cyclonedxxml/decoder_test.go new file mode 100644 index 00000000000..b0d89514180 --- /dev/null +++ b/syft/format/cyclonedxxml/decoder_test.go @@ -0,0 +1,118 @@ +package cyclonedxxml + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/sbom" +) + +func TestDecoder_Decode(t *testing.T) { + tests := []struct { + name string + file string + err bool + distro string + packages []string + }{ + { + name: "dir-scan", + file: "snapshot/TestCycloneDxDirectoryEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "image-scan", + file: "snapshot/TestCycloneDxImageEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "not-an-sbom", + file: "bad-sbom", + err: true, + }, + } + for _, test := range tests { + t.Run(test.file, func(t *testing.T) { + reader, err := os.Open(filepath.Join("test-fixtures", test.file)) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + if test.err { + assert.Equal(t, sbom.FormatID(""), formatID) + assert.Equal(t, "", formatVersion) + + _, decodeID, decodeVersion, err := dec.Decode(reader) + require.Error(t, err) + assert.Equal(t, sbom.FormatID(""), decodeID) + assert.Equal(t, "", decodeVersion) + + return + } + assert.Equal(t, ID, formatID) + assert.NotEmpty(t, formatVersion) + + bom, decodeID, decodeVersion, err := dec.Decode(reader) + require.NotNil(t, bom) + require.NoError(t, err) + + assert.Equal(t, ID, decodeID) + assert.Equal(t, formatVersion, decodeVersion) + + split := strings.SplitN(test.distro, ":", 2) + distroName := split[0] + distroVersion := split[1] + assert.Equal(t, bom.Artifacts.LinuxDistribution.ID, distroName) + assert.Equal(t, bom.Artifacts.LinuxDistribution.Version, distroVersion) + + var pkgs []string + for p := range bom.Artifacts.Packages.Enumerate() { + pkgs = append(pkgs, fmt.Sprintf("%s:%s", p.Name, p.Version)) + } + + assert.ElementsMatch(t, test.packages, pkgs) + }) + } +} + +func TestDecoder_Identify(t *testing.T) { + type testCase struct { + name string + file string + id sbom.FormatID + version string + } + + var cases []testCase + + for _, version := range SupportedVersions() { + cases = append(cases, testCase{ + name: fmt.Sprintf("v%s schema", version), + file: fmt.Sprintf("test-fixtures/identify/%s.xml", version), + id: ID, + version: version, + }) + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + reader, err := os.Open(test.file) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + }) + } +} diff --git a/syft/format/cyclonedxxml/encoder.go b/syft/format/cyclonedxxml/encoder.go new file mode 100644 index 00000000000..a27c83a0cf7 --- /dev/null +++ b/syft/format/cyclonedxxml/encoder.go @@ -0,0 +1,58 @@ +package cyclonedxxml + +import ( + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/syft/format/internal/cyclonedxutil" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatEncoder = (*encoder)(nil) + +const ID = cyclonedxutil.XMLFormatID + +func SupportedVersions() []string { + return cyclonedxutil.SupportedVersions(ID) +} + +type EncoderConfig struct { + Version string +} + +type encoder struct { + cfg EncoderConfig + cyclonedxutil.Encoder +} + +func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) { + enc, err := cyclonedxutil.NewEncoder(cfg.Version, cyclonedx.BOMFileFormatXML) + if err != nil { + return nil, err + } + return encoder{ + cfg: cfg, + Encoder: enc, + }, nil +} + +func DefaultEncoderConfig() EncoderConfig { + return EncoderConfig{ + Version: cyclonedxutil.DefaultVersion, + } +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "cyclonedx", + "cyclone", + "cdx", + } +} + +func (e encoder) Version() string { + return e.cfg.Version +} diff --git a/syft/format/cyclonedxxml/encoder_test.go b/syft/format/cyclonedxxml/encoder_test.go new file mode 100644 index 00000000000..1f2d480283a --- /dev/null +++ b/syft/format/cyclonedxxml/encoder_test.go @@ -0,0 +1,129 @@ +package cyclonedxxml + +import ( + "bytes" + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/internal/testutil" + "github.com/anchore/syft/syft/sbom" +) + +var updateSnapshot = flag.Bool("update-cyclonedx-xml", false, "update the *.golden files for cyclone-dx XML encoders") +var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") + +func getEncoder(t testing.TB) sbom.FormatEncoder { + enc, err := NewFormatEncoderWithConfig(DefaultEncoderConfig()) + require.NoError(t, err) + return enc +} + +func TestCycloneDxDirectoryEncoder(t *testing.T) { + dir := t.TempDir() + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(dir), + }, + ) +} + +func TestCycloneDxImageEncoder(t *testing.T) { + testImage := "image-simple" + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(), + }, + ) +} + +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). + WithValuesRedacted(values...). + WithPatternRedactors( + map[string]string{ + // dates + `([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`: `redacted`, + + // image hashes and BOM refs + `sha256:[A-Za-z0-9]{64}`: `sha256:redacted`, + + // serial numbers and BOM refs + `(serialNumber|bom-ref)="[^"]+"`: `$1="redacted"`, + }, + ) +} + +func TestSupportedVersions(t *testing.T) { + encs := defaultFormatEncoders() + require.NotEmpty(t, encs) + + versions := SupportedVersions() + require.Equal(t, len(versions), len(encs)) + + subject := testutil.DirectoryInput(t, t.TempDir()) + dec := NewFormatDecoder() + + for _, enc := range encs { + t.Run(enc.Version(), func(t *testing.T) { + require.Contains(t, versions, enc.Version()) + + var buf bytes.Buffer + require.NoError(t, enc.Encode(&buf, subject)) + + id, version := dec.Identify(bytes.NewReader(buf.Bytes())) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + var s *sbom.SBOM + var err error + s, id, version, err = dec.Decode(bytes.NewReader(buf.Bytes())) + require.NoError(t, err) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + require.NotEmpty(t, s.Artifacts.Packages.PackageCount()) + + assert.Equal(t, len(subject.Relationships), len(s.Relationships), "mismatched relationship count") + + if !assert.Equal(t, subject.Artifacts.Packages.PackageCount(), s.Artifacts.Packages.PackageCount(), "mismatched package count") { + t.Logf("expected: %d", subject.Artifacts.Packages.PackageCount()) + for _, p := range subject.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + t.Logf("actual: %d", s.Artifacts.Packages.PackageCount()) + for _, p := range s.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + } + }) + } +} + +func defaultFormatEncoders() []sbom.FormatEncoder { + var encs []sbom.FormatEncoder + for _, version := range SupportedVersions() { + enc, err := NewFormatEncoderWithConfig(EncoderConfig{Version: version}) + if err != nil { + panic(err) + } + encs = append(encs, enc) + } + return encs +} diff --git a/syft/format/cyclonedxxml/test-fixtures/bad-sbom b/syft/format/cyclonedxxml/test-fixtures/bad-sbom new file mode 100644 index 00000000000..bb8a8152e0e --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/bad-sbom @@ -0,0 +1 @@ +not an sbom! \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.0.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.0.xml new file mode 100644 index 00000000000..e16ce415239 --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.0.xml @@ -0,0 +1,12 @@ + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + false + + + \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.1.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.1.xml new file mode 100644 index 00000000000..39942295f32 --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.1.xml @@ -0,0 +1,11 @@ + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + + + \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.2.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.2.xml new file mode 100644 index 00000000000..8085a7c88d7 --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.2.xml @@ -0,0 +1,25 @@ + + + + 2023-09-29T11:48:10-04:00 + + + anchore + syft + [not provided] + + + + go.mod + sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + + + \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.3.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.3.xml new file mode 100644 index 00000000000..089077bae7c --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.3.xml @@ -0,0 +1,33 @@ + + + + 2023-09-29T11:48:10-04:00 + + + anchore + syft + [not provided] + + + + go.mod + sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + + go-mod-file-cataloger + go + GolangModMetadata + go-module + cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + /go.mod + + + + \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.4.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.4.xml new file mode 100644 index 00000000000..f8cbc7e3001 --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.4.xml @@ -0,0 +1,33 @@ + + + + 2023-09-29T11:48:10-04:00 + + + anchore + syft + [not provided] + + + + go.mod + sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + + go-mod-file-cataloger + go + GolangModMetadata + go-module + cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + /go.mod + + + + \ No newline at end of file diff --git a/syft/format/cyclonedxxml/test-fixtures/identify/1.5.xml b/syft/format/cyclonedxxml/test-fixtures/identify/1.5.xml new file mode 100644 index 00000000000..97ae0242840 --- /dev/null +++ b/syft/format/cyclonedxxml/test-fixtures/identify/1.5.xml @@ -0,0 +1,33 @@ + + + + 2023-09-29T11:48:10-04:00 + + + anchore + syft + [not provided] + + + + go.mod + sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + + + + + github.com/wagoodman/go-partybus + v0.0.0-20230516145632-8ccac152c651 + cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + + go-mod-file-cataloger + go + GolangModMetadata + go-module + cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* + /go.mod + + + + \ No newline at end of file diff --git a/syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden b/syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden similarity index 97% rename from syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden rename to syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden index bbba1803408..1baaeb6f642 100644 --- a/syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden +++ b/syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxDirectoryEncoder.golden @@ -1,5 +1,5 @@ - + redacted diff --git a/syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden b/syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden similarity index 97% rename from syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden rename to syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden index 914602f17eb..8775b298fb8 100644 --- a/syft/formats/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden +++ b/syft/format/cyclonedxxml/test-fixtures/snapshot/TestCycloneDxImageEncoder.golden @@ -1,5 +1,5 @@ - + redacted diff --git a/syft/format/decoders.go b/syft/format/decoders.go new file mode 100644 index 00000000000..f7e0d6a2da4 --- /dev/null +++ b/syft/format/decoders.go @@ -0,0 +1,91 @@ +package format + +import ( + "fmt" + "io" + + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/cyclonedxxml" + "github.com/anchore/syft/syft/format/spdxjson" + "github.com/anchore/syft/syft/format/spdxtagvalue" + "github.com/anchore/syft/syft/format/syftjson" + "github.com/anchore/syft/syft/sbom" +) + +var ( + staticDecoders sbom.FormatDecoder + _ sbom.FormatDecoder = (*DecoderCollection)(nil) +) + +func init() { + staticDecoders = NewDecoderCollection(Decoders()...) +} + +func Decoders() []sbom.FormatDecoder { + return []sbom.FormatDecoder{ + syftjson.NewFormatDecoder(), + cyclonedxxml.NewFormatDecoder(), + cyclonedxjson.NewFormatDecoder(), + spdxtagvalue.NewFormatDecoder(), + spdxjson.NewFormatDecoder(), + } +} + +type DecoderCollection struct { + decoders []sbom.FormatDecoder +} + +func NewDecoderCollection(decoders ...sbom.FormatDecoder) sbom.FormatDecoder { + return &DecoderCollection{ + decoders: decoders, + } +} + +// Decode takes a set of bytes and attempts to decode it into an SBOM relative to the decoders in the collection. +func (c *DecoderCollection) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + var bestID sbom.FormatID + for _, d := range c.decoders { + id, version := d.Identify(reader) + if id == "" || version == "" { + if id != "" { + bestID = id + } + continue + } + + return d.Decode(reader) + } + + if bestID != "" { + return nil, bestID, "", fmt.Errorf("sbom format found to be %q but the version is not supported", bestID) + } + + return nil, "", "", fmt.Errorf("sbom format not recognized") +} + +// Identify takes a set of bytes and attempts to identify the format of the SBOM relative to the decoders in the collection. +func (c *DecoderCollection) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + for _, d := range c.decoders { + id, version := d.Identify(reader) + if id != "" && version != "" { + return id, version + } + } + return "", "" +} + +// Identify takes a set of bytes and attempts to identify the format of the SBOM. +func Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + return staticDecoders.Identify(reader) +} + +// Decode takes a set of bytes and attempts to decode it into an SBOM. +func Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + return staticDecoders.Decode(reader) +} diff --git a/syft/format/decoders_test.go b/syft/format/decoders_test.go new file mode 100644 index 00000000000..9064bb3b23a --- /dev/null +++ b/syft/format/decoders_test.go @@ -0,0 +1,62 @@ +package format + +import ( + "fmt" + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/anchore/syft/syft/format/syftjson" + "github.com/anchore/syft/syft/sbom" +) + +func TestIdentify(t *testing.T) { + tests := []struct { + fixture string + id sbom.FormatID + version string + }{ + { + fixture: "test-fixtures/alpine-syft.json", + id: syftjson.ID, + version: "1.1.0", + }, + } + for _, test := range tests { + t.Run(test.fixture, func(t *testing.T) { + reader, err := os.Open(test.fixture) + assert.NoError(t, err) + + id, version := Identify(reader) + assert.Equal(t, test.id, id) + assert.Equal(t, test.version, version) + + }) + } +} + +func TestFormats_EmptyInput(t *testing.T) { + for _, format := range Decoders() { + name := strings.Split(fmt.Sprintf("%#v", format), "{")[0] + + t.Run(name, func(t *testing.T) { + t.Run("Decode", func(t *testing.T) { + assert.NotPanics(t, func() { + decodedSBOM, _, _, err := format.Decode(nil) + assert.Error(t, err) + assert.Nil(t, decodedSBOM) + }) + }) + + t.Run("Identify", func(t *testing.T) { + assert.NotPanics(t, func() { + id, version := format.Identify(nil) + assert.Empty(t, id) + assert.Empty(t, version) + }) + }) + }) + } +} diff --git a/syft/format/encoders.go b/syft/format/encoders.go new file mode 100644 index 00000000000..ffe7c59cc5c --- /dev/null +++ b/syft/format/encoders.go @@ -0,0 +1,143 @@ +package format + +import ( + "bytes" + "fmt" + "regexp" + "sort" + "strings" + + "github.com/scylladb/go-set/strset" + + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/sbom" +) + +type EncoderCollection struct { + encoders []sbom.FormatEncoder +} + +func NewEncoderCollection(encoders ...sbom.FormatEncoder) *EncoderCollection { + return &EncoderCollection{ + encoders: encoders, + } +} + +// IDs returns all format IDs represented in the collection. +func (e EncoderCollection) IDs() []sbom.FormatID { + idSet := strset.New() + for _, f := range e.encoders { + idSet.Add(string(f.ID())) + } + + idList := idSet.List() + sort.Strings(idList) + + var ids []sbom.FormatID + for _, id := range idList { + ids = append(ids, sbom.FormatID(id)) + } + + return ids +} + +// NameVersions returns all formats that are supported by the collection as a list of "name@version" strings. +func (e EncoderCollection) NameVersions() []string { + set := strset.New() + for _, f := range e.encoders { + if f.Version() == sbom.AnyVersion { + set.Add(string(f.ID())) + } else { + set.Add(fmt.Sprintf("%s@%s", f.ID(), f.Version())) + } + } + + list := set.List() + sort.Strings(list) + + return list +} + +// Aliases returns all format aliases represented in the collection (where an ID would be "spdx-tag-value" the alias would be "spdx"). +func (e EncoderCollection) Aliases() []string { + aliases := strset.New() + for _, f := range e.encoders { + aliases.Add(f.Aliases()...) + } + lst := aliases.List() + sort.Strings(lst) + return lst +} + +// Get returns the contained encoder for a given format name and version. +func (e EncoderCollection) Get(name string, version string) sbom.FormatEncoder { + log.WithFields("name", name, "version", version).Trace("looking for matching encoder") + + name = cleanFormatName(name) + var mostRecentFormat sbom.FormatEncoder + + for _, f := range e.encoders { + log.WithFields("name", f.ID(), "version", f.Version(), "aliases", f.Aliases()).Trace("considering format") + names := []string{string(f.ID())} + names = append(names, f.Aliases()...) + for _, n := range names { + if cleanFormatName(n) == name && versionMatches(f.Version(), version) { + if mostRecentFormat == nil || f.Version() > mostRecentFormat.Version() { + mostRecentFormat = f + } + } + } + } + + if mostRecentFormat != nil { + log.WithFields("name", mostRecentFormat.ID(), "version", mostRecentFormat.Version()).Trace("found matching encoder") + } else { + log.WithFields("search-name", name, "search-version", version).Trace("no matching encoder found") + } + + return mostRecentFormat +} + +// GetByString accepts a name@version string, such as: +// - json +// - spdx-json@2.1 +// - cdx@1.5 +func (e EncoderCollection) GetByString(s string) sbom.FormatEncoder { + parts := strings.SplitN(s, "@", 2) + version := sbom.AnyVersion + if len(parts) > 1 { + version = parts[1] + } + return e.Get(parts[0], version) +} + +func versionMatches(version string, match string) bool { + if version == sbom.AnyVersion || match == sbom.AnyVersion { + return true + } + + match = strings.ReplaceAll(match, ".", "\\.") + match = strings.ReplaceAll(match, "*", ".*") + match = fmt.Sprintf("^%s(\\..*)*$", match) + matcher, err := regexp.Compile(match) + if err != nil { + return false + } + return matcher.MatchString(version) +} + +func cleanFormatName(name string) string { + r := strings.NewReplacer("-", "", "_", "") + return strings.ToLower(r.Replace(name)) +} + +// Encode takes all SBOM elements and a format option and encodes an SBOM document. +func Encode(s sbom.SBOM, f sbom.FormatEncoder) ([]byte, error) { + buff := bytes.Buffer{} + + if err := f.Encode(&buff, s); err != nil { + return nil, fmt.Errorf("unable to encode sbom: %w", err) + } + + return buff.Bytes(), nil +} diff --git a/syft/format/encoders_test.go b/syft/format/encoders_test.go new file mode 100644 index 00000000000..3e8128fb6af --- /dev/null +++ b/syft/format/encoders_test.go @@ -0,0 +1,111 @@ +package format + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/anchore/syft/syft/sbom" +) + +func Test_versionMatches(t *testing.T) { + tests := []struct { + name string + version string + match string + matches bool + }{ + { + name: "any version matches number", + version: string(sbom.AnyVersion), + match: "6", + matches: true, + }, + { + name: "number matches any version", + version: "6", + match: string(sbom.AnyVersion), + matches: true, + }, + { + name: "same number matches", + version: "3", + match: "3", + matches: true, + }, + { + name: "same major number matches", + version: "3.1", + match: "3", + matches: true, + }, + { + name: "same minor number matches", + version: "3.1", + match: "3.1", + matches: true, + }, + { + name: "wildcard-version matches minor", + version: "7.1.3", + match: "7.*", + matches: true, + }, + { + name: "wildcard-version matches patch", + version: "7.4.8", + match: "7.4.*", + matches: true, + }, + { + name: "sub-version matches major", + version: "7.19.11", + match: "7", + matches: true, + }, + { + name: "sub-version matches minor", + version: "7.55.2", + match: "7.55", + matches: true, + }, + { + name: "sub-version matches patch", + version: "7.32.6", + match: "7.32.6", + matches: true, + }, + // negative tests + { + name: "different number does not match", + version: "3", + match: "4", + matches: false, + }, + { + name: "sub-version doesn't match major", + version: "7.2.5", + match: "8.2.5", + matches: false, + }, + { + name: "sub-version doesn't match minor", + version: "7.2.9", + match: "7.1", + matches: false, + }, + { + name: "sub-version doesn't match patch", + version: "7.32.6", + match: "7.32.5", + matches: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + matches := versionMatches(test.version, test.match) + assert.Equal(t, test.matches, matches) + }) + } +} diff --git a/syft/format/github/encoder.go b/syft/format/github/encoder.go new file mode 100644 index 00000000000..a34db817f48 --- /dev/null +++ b/syft/format/github/encoder.go @@ -0,0 +1,42 @@ +package github + +import ( + "encoding/json" + "io" + + "github.com/anchore/syft/syft/format/github/internal/model" + "github.com/anchore/syft/syft/sbom" +) + +const ID sbom.FormatID = "github-json" + +type encoder struct { +} + +func NewFormatEncoder() sbom.FormatEncoder { + return encoder{} +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "github", + } +} + +func (e encoder) Version() string { + return sbom.AnyVersion +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { + bom := model.ToGithubModel(&s) + + enc := json.NewEncoder(writer) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + + return enc.Encode(bom) +} diff --git a/syft/format/github/encoder_test.go b/syft/format/github/encoder_test.go new file mode 100644 index 00000000000..865cce073d4 --- /dev/null +++ b/syft/format/github/encoder_test.go @@ -0,0 +1,57 @@ +package github + +import ( + "flag" + "testing" + + "github.com/anchore/syft/syft/format/internal/testutil" +) + +var updateSnapshot = flag.Bool("update-github", false, "update the *.golden files for github encoders") +var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") + +func TestGithubDirectoryEncoder(t *testing.T) { + dir := t.TempDir() + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: NewFormatEncoder(), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(dir), + }, + ) +} + +func TestGithubImageEncoder(t *testing.T) { + testImage := "image-simple" + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage), + Format: NewFormatEncoder(), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(), + }, + ) +} + +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). + WithValuesRedacted(values...). + WithPatternRedactors( + map[string]string{ + // dates + `"scanned":\s*"[^"]+"`: `"scanned":"redacted"`, + + // image metadata + `"syft:filesystem":\s*"[^"]+"`: `"syft:filesystem":"redacted"`, + }, + ) +} diff --git a/syft/formats/github/github_dependency_api.go b/syft/format/github/internal/model/github_dependency_api.go similarity index 99% rename from syft/formats/github/github_dependency_api.go rename to syft/format/github/internal/model/github_dependency_api.go index fe873d41809..56ab7657d97 100644 --- a/syft/formats/github/github_dependency_api.go +++ b/syft/format/github/internal/model/github_dependency_api.go @@ -1,4 +1,4 @@ -package github +package model // Derived from: https://gist.github.com/reiddraper/fdab2883db0f372c146d1a750fc1c43f diff --git a/syft/formats/github/encoder.go b/syft/format/github/internal/model/model.go similarity index 97% rename from syft/formats/github/encoder.go rename to syft/format/github/internal/model/model.go index 6f8ff5719e1..b5dc39ed113 100644 --- a/syft/formats/github/encoder.go +++ b/syft/format/github/internal/model/model.go @@ -1,4 +1,4 @@ -package github +package model import ( "fmt" @@ -14,8 +14,8 @@ import ( "github.com/anchore/syft/syft/source" ) -// toGithubModel converts the provided SBOM to a GitHub dependency model -func toGithubModel(s *sbom.SBOM) DependencySnapshot { +// ToGithubModel converts the provided SBOM to a GitHub dependency model +func ToGithubModel(s *sbom.SBOM) DependencySnapshot { scanTime := time.Now().Format(time.RFC3339) // TODO is there a record of this somewhere? v := s.Descriptor.Version if v == "[not provided]" || v == "" { diff --git a/syft/formats/github/encoder_test.go b/syft/format/github/internal/model/model_test.go similarity index 99% rename from syft/formats/github/encoder_test.go rename to syft/format/github/internal/model/model_test.go index e027c7936a3..df97f12f526 100644 --- a/syft/formats/github/encoder_test.go +++ b/syft/format/github/internal/model/model_test.go @@ -1,4 +1,4 @@ -package github +package model import ( "testing" @@ -182,7 +182,7 @@ func Test_toGithubModel(t *testing.T) { if test.metadata != nil { s.Source.Metadata = test.metadata } - actual := toGithubModel(&s) + actual := ToGithubModel(&s) if test.expected != nil { if d := cmp.Diff(*test.expected, actual, cmpopts.IgnoreFields(DependencySnapshot{}, "Scanned")); d != "" { diff --git a/syft/format/github/test-fixtures/snapshot/TestGithubDirectoryEncoder.golden b/syft/format/github/test-fixtures/snapshot/TestGithubDirectoryEncoder.golden new file mode 100644 index 00000000000..ef13eac7a15 --- /dev/null +++ b/syft/format/github/test-fixtures/snapshot/TestGithubDirectoryEncoder.golden @@ -0,0 +1,33 @@ +{ + "version": 0, + "job": {}, + "detector": { + "name": "syft", + "url": "https://github.com/anchore/syft", + "version": "v0.42.0-bogus" + }, + "metadata": { + "syft:distro": "pkg:generic/debian@1.2.3?like=like!" + }, + "manifests": { + "redacted/some/path/some/path/pkg1": { + "name": "redacted/some/path/some/path/pkg1", + "file": { + "source_location": "redacted/some/path/some/path/pkg1" + }, + "resolved": { + "": { + "package_url": "a-purl-2", + "relationship": "direct", + "scope": "runtime" + }, + "pkg:deb/debian/package-2@2.0.1": { + "package_url": "pkg:deb/debian/package-2@2.0.1", + "relationship": "direct", + "scope": "runtime" + } + } + } + }, + "scanned":"redacted" +} diff --git a/syft/format/github/test-fixtures/snapshot/TestGithubImageEncoder.golden b/syft/format/github/test-fixtures/snapshot/TestGithubImageEncoder.golden new file mode 100644 index 00000000000..1347d01956b --- /dev/null +++ b/syft/format/github/test-fixtures/snapshot/TestGithubImageEncoder.golden @@ -0,0 +1,47 @@ +{ + "version": 0, + "job": {}, + "detector": { + "name": "syft", + "url": "https://github.com/anchore/syft", + "version": "v0.42.0-bogus" + }, + "metadata": { + "syft:distro": "pkg:generic/debian@1.2.3?like=like!" + }, + "manifests": { + "user-image-input:/somefile-1.txt": { + "name": "user-image-input:/somefile-1.txt", + "file": { + "source_location": "user-image-input:/somefile-1.txt" + }, + "metadata": { + "syft:filesystem":"redacted" + }, + "resolved": { + "": { + "package_url": "a-purl-1", + "relationship": "direct", + "scope": "runtime" + } + } + }, + "user-image-input:/somefile-2.txt": { + "name": "user-image-input:/somefile-2.txt", + "file": { + "source_location": "user-image-input:/somefile-2.txt" + }, + "metadata": { + "syft:filesystem":"redacted" + }, + "resolved": { + "pkg:deb/debian/package-2@2.0.1": { + "package_url": "pkg:deb/debian/package-2@2.0.1", + "relationship": "direct", + "scope": "runtime" + } + } + } + }, + "scanned":"redacted" +} diff --git a/syft/format/internal/cyclonedxutil/decoder.go b/syft/format/internal/cyclonedxutil/decoder.go new file mode 100644 index 00000000000..735e8866d6d --- /dev/null +++ b/syft/format/internal/cyclonedxutil/decoder.go @@ -0,0 +1,33 @@ +package cyclonedxutil + +import ( + "fmt" + "io" + + "github.com/CycloneDX/cyclonedx-go" +) + +type Decoder struct { + format cyclonedx.BOMFileFormat +} + +func NewDecoder(format cyclonedx.BOMFileFormat) Decoder { + return Decoder{ + format: format, + } +} + +func (d Decoder) Decode(reader io.ReadSeeker) (*cyclonedx.BOM, error) { + doc := &cyclonedx.BOM{ + Components: &[]cyclonedx.Component{}, + } + if _, err := reader.Seek(0, io.SeekStart); err != nil { + return nil, fmt.Errorf("unable to seek to start of CycloneDX SBOM: %w", err) + } + err := cyclonedx.NewBOMDecoder(reader, d.format).Decode(doc) + if err != nil { + return nil, err + } + + return doc, nil +} diff --git a/syft/format/internal/cyclonedxutil/decoder_test.go b/syft/format/internal/cyclonedxutil/decoder_test.go new file mode 100644 index 00000000000..634bc472889 --- /dev/null +++ b/syft/format/internal/cyclonedxutil/decoder_test.go @@ -0,0 +1,21 @@ +package cyclonedxutil + +import ( + "bytes" + "encoding/json" + "testing" + + "github.com/CycloneDX/cyclonedx-go" + "github.com/stretchr/testify/assert" +) + +func Test_missingComponentsDecode(t *testing.T) { + bom := &cyclonedx.BOM{ + SpecVersion: cyclonedx.SpecVersion1_4, + } + bomBytes, _ := json.Marshal(&bom) + dec := NewDecoder(cyclonedx.BOMFileFormatJSON) + + _, err := dec.Decode(bytes.NewReader(bomBytes)) + assert.NoError(t, err) +} diff --git a/syft/format/internal/cyclonedxutil/encoder.go b/syft/format/internal/cyclonedxutil/encoder.go new file mode 100644 index 00000000000..ed209ef14ce --- /dev/null +++ b/syft/format/internal/cyclonedxutil/encoder.go @@ -0,0 +1,37 @@ +package cyclonedxutil + +import ( + "io" + + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/syft/format/common/cyclonedxhelpers" + "github.com/anchore/syft/syft/sbom" +) + +const DefaultVersion = "1.5" + +type Encoder struct { + version cyclonedx.SpecVersion + format cyclonedx.BOMFileFormat +} + +func NewEncoder(version string, format cyclonedx.BOMFileFormat) (Encoder, error) { + specVersion, err := SpecVersionFromString(version) + if err != nil { + return Encoder{}, err + } + return Encoder{ + version: specVersion, + format: format, + }, nil +} + +func (e Encoder) Encode(writer io.Writer, s sbom.SBOM) error { + bom := cyclonedxhelpers.ToFormatModel(s) + enc := cyclonedx.NewBOMEncoder(writer, e.format) + enc.SetPretty(true) + enc.SetEscapeHTML(false) + + return enc.EncodeVersion(bom, e.version) +} diff --git a/syft/format/internal/cyclonedxutil/versions.go b/syft/format/internal/cyclonedxutil/versions.go new file mode 100644 index 00000000000..426da2c65b2 --- /dev/null +++ b/syft/format/internal/cyclonedxutil/versions.go @@ -0,0 +1,66 @@ +package cyclonedxutil + +import ( + "fmt" + + "github.com/CycloneDX/cyclonedx-go" + + "github.com/anchore/syft/syft/sbom" +) + +const ( + XMLFormatID sbom.FormatID = "cyclonedx-xml" + JSONFormatID sbom.FormatID = "cyclonedx-json" +) + +func SupportedVersions(id sbom.FormatID) []string { + versions := []string{ + "1.2", + "1.3", + "1.4", + "1.5", + } + + if id != JSONFormatID { + // JSON format not supported for version < 1.2 + versions = append([]string{"1.0", "1.1"}, versions...) + } + + return versions +} + +func SpecVersionFromString(v string) (cyclonedx.SpecVersion, error) { + switch v { + case "1.0": + return cyclonedx.SpecVersion1_0, nil + case "1.1": + return cyclonedx.SpecVersion1_1, nil + case "1.2": + return cyclonedx.SpecVersion1_2, nil + case "1.3": + return cyclonedx.SpecVersion1_3, nil + case "1.4": + return cyclonedx.SpecVersion1_4, nil + case "1.5": + return cyclonedx.SpecVersion1_5, nil + } + return -1, fmt.Errorf("unsupported CycloneDX version %q", v) +} + +func VersionFromSpecVersion(spec cyclonedx.SpecVersion) string { + switch spec { + case cyclonedx.SpecVersion1_0: + return "1.0" + case cyclonedx.SpecVersion1_1: + return "1.1" + case cyclonedx.SpecVersion1_2: + return "1.2" + case cyclonedx.SpecVersion1_3: + return "1.3" + case cyclonedx.SpecVersion1_4: + return "1.4" + case cyclonedx.SpecVersion1_5: + return "1.5" + } + return "" +} diff --git a/syft/format/internal/spdxutil/versions.go b/syft/format/internal/spdxutil/versions.go new file mode 100644 index 00000000000..5718e5d2c7d --- /dev/null +++ b/syft/format/internal/spdxutil/versions.go @@ -0,0 +1,26 @@ +package spdxutil + +import ( + "github.com/anchore/syft/syft/sbom" +) + +const DefaultVersion = "2.3" + +const ( + JSONFormatID sbom.FormatID = "spdx-json" + TagValueFormatID sbom.FormatID = "spdx-tag-value" +) + +func SupportedVersions(id sbom.FormatID) []string { + versions := []string{ + "2.2", + "2.3", + } + + if id != JSONFormatID { + // JSON format is not supported in v2.1 + return append([]string{"2.1"}, versions...) + } + + return versions +} diff --git a/syft/formats/internal/testutils/directory_input.go b/syft/format/internal/testutil/directory_input.go similarity index 99% rename from syft/formats/internal/testutils/directory_input.go rename to syft/format/internal/testutil/directory_input.go index 232ac9c1275..9b3544293c5 100644 --- a/syft/formats/internal/testutils/directory_input.go +++ b/syft/format/internal/testutil/directory_input.go @@ -1,4 +1,4 @@ -package testutils +package testutil import ( "os" diff --git a/syft/formats/internal/testutils/file_relationships.go b/syft/format/internal/testutil/file_relationships.go similarity index 97% rename from syft/formats/internal/testutils/file_relationships.go rename to syft/format/internal/testutil/file_relationships.go index 39ab8211fec..5a522a17af6 100644 --- a/syft/formats/internal/testutils/file_relationships.go +++ b/syft/format/internal/testutil/file_relationships.go @@ -1,4 +1,4 @@ -package testutils +package testutil import ( "math/rand" diff --git a/syft/formats/internal/testutils/image_input.go b/syft/format/internal/testutil/image_input.go similarity index 79% rename from syft/formats/internal/testutils/image_input.go rename to syft/format/internal/testutil/image_input.go index e53bc5f7d10..d4bb15ddcdb 100644 --- a/syft/formats/internal/testutils/image_input.go +++ b/syft/format/internal/testutil/image_input.go @@ -1,9 +1,12 @@ -package testutils +package testutil import ( + "os" + "path/filepath" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/anchore/stereoscope/pkg/filetree" "github.com/anchore/stereoscope/pkg/image" @@ -25,6 +28,8 @@ func ImageInput(t testing.TB, testImage string, options ...ImageOption) sbom.SBO opt(&cfg) } + defer changeToDirectoryWithGoldenFixture(t, testImage)() + switch cfg.fromSnapshot { case true: img = imagetest.GetGoldenFixtureImage(t, testImage) @@ -65,6 +70,30 @@ func ImageInput(t testing.TB, testImage string, options ...ImageOption) sbom.SBO } } +func changeToDirectoryWithGoldenFixture(t testing.TB, testImage string) func() { + // check if test fixture exists... if not, check if there is a shared fixture relative to this dir + fn := func() {} + + path := filepath.Join("test-fixtures", testImage) + if _, err := os.Stat(path); err != nil { + // change dir, restore as defer + wd, err := os.Getwd() + require.NoError(t, err) + fn = func() { + require.NoError(t, os.Chdir(wd)) + } + + // change dir to the testutil dir + require.NoError(t, os.Chdir(filepath.Join(wd, "..", "internal", "testutil"))) + t.Cleanup(fn) + + if _, err := os.Stat(path); err != nil { + t.Fatalf("unable to find test fixture: %s", path) + } + } + return fn +} + func populateImageCatalog(catalog *pkg.Collection, img *image.Image) { _, ref1, _ := img.SquashedTree().File("/somefile-1.txt", filetree.FollowBasenameLinks) _, ref2, _ := img.SquashedTree().File("/somefile-2.txt", filetree.FollowBasenameLinks) diff --git a/syft/formats/internal/testutils/redactor.go b/syft/format/internal/testutil/redactor.go similarity index 99% rename from syft/formats/internal/testutils/redactor.go rename to syft/format/internal/testutil/redactor.go index 0c5505d1b00..7dab1935970 100644 --- a/syft/formats/internal/testutils/redactor.go +++ b/syft/format/internal/testutil/redactor.go @@ -1,4 +1,4 @@ -package testutils +package testutil import ( "bytes" diff --git a/syft/formats/internal/testutils/snapshot.go b/syft/format/internal/testutil/snapshot.go similarity index 90% rename from syft/formats/internal/testutils/snapshot.go rename to syft/format/internal/testutil/snapshot.go index f06850b1df2..10073b9cf1a 100644 --- a/syft/formats/internal/testutils/snapshot.go +++ b/syft/format/internal/testutil/snapshot.go @@ -1,4 +1,4 @@ -package testutils +package testutil import ( "bytes" @@ -27,7 +27,7 @@ func FromSnapshot() ImageOption { type EncoderSnapshotTestConfig struct { Subject sbom.SBOM - Format sbom.Format + Format sbom.FormatEncoder UpdateSnapshot bool PersistRedactionsInSnapshot bool IsJSON bool @@ -71,8 +71,9 @@ func AssertEncoderAgainstGoldenSnapshot(t *testing.T, cfg EncoderSnapshotTestCon func requireEqual(t *testing.T, expected any, actual any) { if diff := cmp.Diff(expected, actual); diff != "" { - t.Logf("expected: %s", expected) - t.Logf("actual: %s", actual) + // uncomment to debug + // t.Logf("expected: %s", expected) + // t.Logf("actual: %s", actual) t.Fatalf("mismatched output: %s", diff) } } @@ -84,6 +85,8 @@ type ImageSnapshotTestConfig struct { func AssertEncoderAgainstGoldenImageSnapshot(t *testing.T, imgCfg ImageSnapshotTestConfig, cfg EncoderSnapshotTestConfig) { if imgCfg.UpdateImageSnapshot { + defer changeToDirectoryWithGoldenFixture(t, imgCfg.Image)() + // grab the latest image contents and persist imagetest.UpdateGoldenFixtureImage(t, imgCfg.Image) } diff --git a/syft/formats/cyclonedxjson/test-fixtures/image-simple/Dockerfile b/syft/format/internal/testutil/test-fixtures/image-simple/Dockerfile similarity index 100% rename from syft/formats/cyclonedxjson/test-fixtures/image-simple/Dockerfile rename to syft/format/internal/testutil/test-fixtures/image-simple/Dockerfile diff --git a/syft/formats/cyclonedxjson/test-fixtures/image-simple/file-1.txt b/syft/format/internal/testutil/test-fixtures/image-simple/file-1.txt similarity index 100% rename from syft/formats/cyclonedxjson/test-fixtures/image-simple/file-1.txt rename to syft/format/internal/testutil/test-fixtures/image-simple/file-1.txt diff --git a/syft/formats/cyclonedxjson/test-fixtures/image-simple/file-2.txt b/syft/format/internal/testutil/test-fixtures/image-simple/file-2.txt similarity index 100% rename from syft/formats/cyclonedxjson/test-fixtures/image-simple/file-2.txt rename to syft/format/internal/testutil/test-fixtures/image-simple/file-2.txt diff --git a/syft/formats/spdxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/format/internal/testutil/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden similarity index 82% rename from syft/formats/spdxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden rename to syft/format/internal/testutil/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden index d7e3532665c..88814632962 100644 Binary files a/syft/formats/spdxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden and b/syft/format/internal/testutil/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden differ diff --git a/syft/format/spdxjson/decoder.go b/syft/format/spdxjson/decoder.go new file mode 100644 index 00000000000..f9484dcca98 --- /dev/null +++ b/syft/format/spdxjson/decoder.go @@ -0,0 +1,104 @@ +package spdxjson + +import ( + "encoding/json" + "fmt" + "io" + "strings" + + spdxJson "github.com/spdx/tools-golang/json" + + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/format/common/spdxhelpers" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatDecoder = (*decoder)(nil) + +type decoder struct { +} + +func NewFormatDecoder() sbom.FormatDecoder { + return decoder{} +} + +func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + + // since spdx lib will always return the latest version of the document, we need to identify the version + // first and then decode into the appropriate document object. Otherwise if we get the version info from the + // decoded object we will always get the latest version (instead of the version we decoded from). + id, version := d.Identify(reader) + if id != ID { + return nil, "", "", fmt.Errorf("not a spdx json document") + } + if version == "" { + return nil, "", "", fmt.Errorf("unsupported spdx json document version") + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + return nil, "", "", fmt.Errorf("unable to seek to start of SPDX JSON SBOM: %+v", err) + } + + doc, err := spdxJson.Read(reader) + if err != nil { + return nil, id, version, fmt.Errorf("unable to decode spdx json: %w", err) + } + + s, err := spdxhelpers.ToSyftModel(doc) + if err != nil { + return nil, id, version, err + } + return s, id, version, nil +} + +func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + log.Debugf("unable to seek to start of SPDX JSON SBOM: %+v", err) + return "", "" + } + + // Example JSON document + // { + // "spdxVersion": "SPDX-2.3", + // ... + type Document struct { + SPDXVersion string `json:"spdxVersion"` + } + + dec := json.NewDecoder(reader) + + var doc Document + err := dec.Decode(&doc) + if err != nil { + // maybe not json? maybe not valid? doesn't matter, we won't process it. + return "", "" + } + + id, version := getFormatInfo(doc.SPDXVersion) + if version == "" || id != ID { + // not a spdx json document that we support + return "", "" + } + + return id, version +} + +func getFormatInfo(spdxVersion string) (sbom.FormatID, string) { + // example input: SPDX-2.3 + if !strings.HasPrefix(strings.ToLower(spdxVersion), "spdx-") { + return "", "" + } + fields := strings.Split(spdxVersion, "-") + if len(fields) != 2 { + return ID, "" + } + + return ID, fields[1] +} diff --git a/syft/format/spdxjson/decoder_test.go b/syft/format/spdxjson/decoder_test.go new file mode 100644 index 00000000000..feee6bfb569 --- /dev/null +++ b/syft/format/spdxjson/decoder_test.go @@ -0,0 +1,180 @@ +package spdxjson + +import ( + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/pkg" + "github.com/anchore/syft/syft/sbom" +) + +func TestDecoder_Decode(t *testing.T) { + tests := []struct { + name string + fail bool + id sbom.FormatID + version string + packages []string + relationships []string + }{ + { + name: "alpine-3.10.syft.spdx.json", + id: ID, + version: "2.2", + packages: []string{"busybox", "libssl1.1", "ssl_client"}, + relationships: []string{"busybox", "busybox", "libssl1.1", "libssl1.1"}, + }, + { + name: "alpine-3.10.vendor.spdx.json", + id: ID, + version: "2.2", + packages: []string{"alpine", "busybox", "ssl_client"}, + relationships: []string{}, + }, + { + name: "example7-bin.spdx.json", + id: ID, + version: "2.2", + }, + { + name: "example7-go-module.spdx.json", + id: ID, + version: "2.2", + }, + { + name: "example7-golang.spdx.json", + id: ID, + version: "2.2", + }, + { + name: "example7-third-party-modules.spdx.json", + id: ID, + version: "2.2", + }, + { + name: "bad/example7-bin.spdx.json", + id: ID, + version: "2.2", + fail: true, + }, + { + name: "bad/example7-go-module.spdx.json", + id: ID, + version: "2.2", + fail: true, + }, + { + name: "bad/example7-golang.spdx.json", + id: ID, + version: "2.2", + fail: true, + }, + { + name: "bad/example7-third-party-modules.spdx.json", + id: ID, + version: "2.2", + fail: true, + }, + { + name: "bad/bad-sbom", + fail: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + reader, err := os.Open(filepath.Join("test-fixtures", "spdx", test.name)) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + if test.fail { + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + + _, decodeID, decodeVersion, err := dec.Decode(reader) + require.Error(t, err) + assert.Equal(t, test.id, decodeID) + assert.Equal(t, test.version, decodeVersion) + + return + } + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + + s, decodeID, decodeVersion, err := dec.Decode(reader) + + require.NoError(t, err) + assert.Equal(t, test.id, decodeID) + assert.Equal(t, test.version, decodeVersion) + + if test.packages != nil { + assert.Equal(t, s.Artifacts.Packages.PackageCount(), len(test.packages)) + + packages: + for _, pkgName := range test.packages { + for _, p := range s.Artifacts.Packages.Sorted() { + if p.Name == pkgName { + continue packages + } + } + assert.NoError(t, fmt.Errorf("Unable to find package: %s", pkgName)) + } + } + + if test.relationships != nil { + assert.Len(t, s.Relationships, len(test.relationships)) + + relationships: + for _, pkgName := range test.relationships { + for _, rel := range s.Relationships { + p, ok := rel.From.(pkg.Package) + if ok && p.Name == pkgName { + continue relationships + } + } + assert.NoError(t, fmt.Errorf("Unable to find relationship: %s", pkgName)) + } + } + }) + } +} + +func TestDecoder_Identify(t *testing.T) { + type testCase struct { + name string + file string + id sbom.FormatID + version string + } + + var cases []testCase + + for _, version := range SupportedVersions() { + cases = append(cases, testCase{ + name: fmt.Sprintf("v%s schema", version), + file: fmt.Sprintf("test-fixtures/identify/%s.json", version), + id: ID, + version: version, + }) + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + reader, err := os.Open(test.file) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + }) + } +} diff --git a/syft/format/spdxjson/encoder.go b/syft/format/spdxjson/encoder.go new file mode 100644 index 00000000000..e8bb68b9cf1 --- /dev/null +++ b/syft/format/spdxjson/encoder.go @@ -0,0 +1,92 @@ +package spdxjson + +import ( + "encoding/json" + "fmt" + "io" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_3" + + "github.com/anchore/syft/syft/format/common/spdxhelpers" + "github.com/anchore/syft/syft/format/internal/spdxutil" + "github.com/anchore/syft/syft/sbom" +) + +const ID = spdxutil.JSONFormatID + +func SupportedVersions() []string { + return spdxutil.SupportedVersions(ID) +} + +type EncoderConfig struct { + Version string +} + +type encoder struct { + cfg EncoderConfig +} + +func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) { + return encoder{ + cfg: cfg, + }, nil +} + +func DefaultEncoderConfig() EncoderConfig { + return EncoderConfig{ + Version: spdxutil.DefaultVersion, + } +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{} +} + +func (e encoder) Version() string { + return e.cfg.Version +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { + latestDoc := spdxhelpers.ToFormatModel(s) + if latestDoc == nil { + return fmt.Errorf("unable to convert SBOM to SPDX document") + } + + var err error + var encodeDoc any + switch e.cfg.Version { + case "2.1": + doc := v2_1.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + case "2.2": + doc := v2_2.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + + case "2.3": + doc := v2_3.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + default: + return fmt.Errorf("unsupported SPDX version %q", e.cfg.Version) + } + + if err != nil { + return fmt.Errorf("unable to convert SBOM to SPDX document: %w", err) + } + + enc := json.NewEncoder(writer) + // prevent > and < from being escaped in the payload + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + + return enc.Encode(encodeDoc) +} diff --git a/syft/format/spdxjson/encoder_test.go b/syft/format/spdxjson/encoder_test.go new file mode 100644 index 00000000000..bc2dfe66e3c --- /dev/null +++ b/syft/format/spdxjson/encoder_test.go @@ -0,0 +1,171 @@ +package spdxjson + +import ( + "bytes" + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/internal/testutil" + "github.com/anchore/syft/syft/sbom" +) + +var updateSnapshot = flag.Bool("update-spdx-json", false, "update the *.golden files for spdx-json encoders") +var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") + +func getEncoder(t testing.TB) sbom.FormatEncoder { + enc, err := NewFormatEncoderWithConfig(DefaultEncoderConfig()) + require.NoError(t, err) + return enc +} + +func TestSPDXJSONDirectoryEncoder(t *testing.T) { + dir := t.TempDir() + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: true, + Redactor: redactor(dir), + }, + ) +} + +func TestSPDXJSONImageEncoder(t *testing.T) { + testImage := "image-simple" + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage, testutil.FromSnapshot()), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: true, + Redactor: redactor(), + }, + ) +} + +func TestSPDXRelationshipOrder(t *testing.T) { + testImage := "image-simple" + + s := testutil.ImageInput(t, testImage, testutil.FromSnapshot()) + testutil.AddSampleFileRelationships(&s) + + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: s, + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: true, + Redactor: redactor(), + }, + ) +} + +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). + WithValuesRedacted(values...). + WithPatternRedactors( + map[string]string{ + // each SBOM reports the time it was generated, which is not useful during snapshot testing + `"created":\s+"[^"]*"`: `"created":"redacted"`, + + // each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing + `"documentNamespace":\s+"[^"]*"`: `"documentNamespace":"redacted"`, + + // the license list will be updated periodically, the value here should not be directly tested in snapshot tests + `"licenseListVersion":\s+"[^"]*"`: `"licenseListVersion":"redacted"`, + }, + ) +} + +func TestSupportedVersions(t *testing.T) { + encs := defaultFormatEncoders() + require.NotEmpty(t, encs) + + versions := SupportedVersions() + require.Equal(t, len(versions), len(encs)) + + subject := testutil.DirectoryInput(t, t.TempDir()) + dec := NewFormatDecoder() + + relationshipOffsetPerVersion := map[string]int{ + // the package representing the source gets a relationship from the source package to all other packages found + // these relationships cannot be removed until the primaryPackagePurpose info is available in 2.3 + "2.1": 2, + "2.2": 2, + // the source-to-package relationships can be removed since the primaryPackagePurpose info is available in 2.3 + "2.3": 0, + } + + pkgCountOffsetPerVersion := map[string]int{ + "2.1": 1, // the source is mapped as a package, but cannot distinguish it since the primaryPackagePurpose info is not available until 2.3 + "2.2": 1, // the source is mapped as a package, but cannot distinguish it since the primaryPackagePurpose info is not available until 2.3 + "2.3": 0, // the source package can be removed since the primaryPackagePurpose info is available + } + + for _, enc := range encs { + t.Run(enc.Version(), func(t *testing.T) { + require.Contains(t, versions, enc.Version()) + + var buf bytes.Buffer + require.NoError(t, enc.Encode(&buf, subject)) + + id, version := dec.Identify(bytes.NewReader(buf.Bytes())) + assert.Equal(t, enc.ID(), id) + assert.Equal(t, enc.Version(), version) + + var s *sbom.SBOM + var err error + s, id, version, err = dec.Decode(bytes.NewReader(buf.Bytes())) + require.NoError(t, err) + + assert.Equal(t, enc.ID(), id) + assert.Equal(t, enc.Version(), version) + + require.NotEmpty(t, s.Artifacts.Packages.PackageCount()) + + offset := relationshipOffsetPerVersion[enc.Version()] + + assert.Equal(t, len(subject.Relationships)+offset, len(s.Relationships), "mismatched relationship count") + + offset = pkgCountOffsetPerVersion[enc.Version()] + + if !assert.Equal(t, subject.Artifacts.Packages.PackageCount()+offset, s.Artifacts.Packages.PackageCount(), "mismatched package count") { + t.Logf("expected: %d", subject.Artifacts.Packages.PackageCount()) + for _, p := range subject.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + t.Logf("actual: %d", s.Artifacts.Packages.PackageCount()) + for _, p := range s.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + } + }) + } +} + +func defaultFormatEncoders() []sbom.FormatEncoder { + var encs []sbom.FormatEncoder + for _, version := range SupportedVersions() { + enc, err := NewFormatEncoderWithConfig(EncoderConfig{Version: version}) + if err != nil { + panic(err) + } + encs = append(encs, enc) + } + return encs +} diff --git a/syft/format/spdxjson/test-fixtures/identify/2.2.json b/syft/format/spdxjson/test-fixtures/identify/2.2.json new file mode 100644 index 00000000000..2b8f711b0ed --- /dev/null +++ b/syft/format/spdxjson/test-fixtures/identify/2.2.json @@ -0,0 +1,104 @@ +{ + "spdxVersion": "SPDX-2.2", + "dataLicense": "CC0-1.0", + "SPDXID": "SPDXRef-DOCUMENT", + "name": "go.mod", + "documentNamespace": "https://anchore.com/syft/file/go.mod-3f39882d-5e0e-43a8-901e-a09668b09aea", + "creationInfo": { + "licenseListVersion": "3.21", + "creators": [ + "Organization: Anchore, Inc", + "Tool: syft-0.91.0" + ], + "created": "2023-09-29T15:18:10Z" + }, + "packages": [ + { + "name": "github.com/wagoodman/go-partybus", + "SPDXID": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "versionInfo": "v0.0.0-20230516145632-8ccac152c651", + "supplier": "NOASSERTION", + "downloadLocation": "NOASSERTION", + "filesAnalyzed": false, + "packageVerificationCode": { + "packageVerificationCodeValue": "" + }, + "sourceInfo": "acquired package info from go module information: /go.mod", + "licenseConcluded": "NOASSERTION", + "licenseInfoFromFiles": null, + "licenseDeclared": "NOASSERTION", + "copyrightText": "NOASSERTION", + "externalRefs": [ + { + "referenceCategory": "SECURITY", + "referenceType": "cpe23Type", + "referenceLocator": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "referenceCategory": "SECURITY", + "referenceType": "cpe23Type", + "referenceLocator": "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "referenceCategory": "PACKAGE_MANAGER", + "referenceType": "purl", + "referenceLocator": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651" + } + ] + }, + { + "name": "go.mod", + "SPDXID": "SPDXRef-DocumentRoot-File-go.mod", + "versionInfo": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd", + "supplier": "NOASSERTION", + "downloadLocation": "NOASSERTION", + "filesAnalyzed": false, + "packageVerificationCode": { + "packageVerificationCodeValue": "" + }, + "checksums": [ + { + "algorithm": "SHA256", + "checksumValue": "dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + ], + "licenseConcluded": "", + "licenseInfoFromFiles": null, + "licenseDeclared": "", + "copyrightText": "" + } + ], + "files": [ + { + "fileName": "/go.mod", + "SPDXID": "SPDXRef-File-go.mod-3fc5a8d3d86e9790", + "checksums": [ + { + "algorithm": "SHA1", + "checksumValue": "0000000000000000000000000000000000000000" + } + ], + "licenseConcluded": "NOASSERTION", + "licenseInfoInFiles": null, + "copyrightText": "" + } + ], + "relationships": [ + { + "spdxElementId": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "relatedSpdxElement": "SPDXRef-File-go.mod-3fc5a8d3d86e9790", + "relationshipType": "OTHER", + "comment": "evident-by: indicates the package's existence is evident by the given file" + }, + { + "spdxElementId": "SPDXRef-DocumentRoot-File-go.mod", + "relatedSpdxElement": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "relationshipType": "CONTAINS" + }, + { + "spdxElementId": "SPDXRef-DOCUMENT", + "relatedSpdxElement": "SPDXRef-DocumentRoot-File-go.mod", + "relationshipType": "DESCRIBES" + } + ] +} diff --git a/syft/format/spdxjson/test-fixtures/identify/2.3.json b/syft/format/spdxjson/test-fixtures/identify/2.3.json new file mode 100644 index 00000000000..57d255ab691 --- /dev/null +++ b/syft/format/spdxjson/test-fixtures/identify/2.3.json @@ -0,0 +1,93 @@ +{ + "spdxVersion": "SPDX-2.3", + "dataLicense": "CC0-1.0", + "SPDXID": "SPDXRef-DOCUMENT", + "name": "go.mod", + "documentNamespace": "https://anchore.com/syft/file/go.mod-3b649f47-dca2-45ca-9d30-147003de8594", + "creationInfo": { + "licenseListVersion": "3.21", + "creators": [ + "Organization: Anchore, Inc", + "Tool: syft-0.91.0" + ], + "created": "2023-09-29T15:18:10Z" + }, + "packages": [ + { + "name": "github.com/wagoodman/go-partybus", + "SPDXID": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "versionInfo": "v0.0.0-20230516145632-8ccac152c651", + "supplier": "NOASSERTION", + "downloadLocation": "NOASSERTION", + "filesAnalyzed": false, + "sourceInfo": "acquired package info from go module information: /go.mod", + "licenseConcluded": "NOASSERTION", + "licenseDeclared": "NOASSERTION", + "copyrightText": "NOASSERTION", + "externalRefs": [ + { + "referenceCategory": "SECURITY", + "referenceType": "cpe23Type", + "referenceLocator": "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "referenceCategory": "SECURITY", + "referenceType": "cpe23Type", + "referenceLocator": "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + }, + { + "referenceCategory": "PACKAGE-MANAGER", + "referenceType": "purl", + "referenceLocator": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651" + } + ] + }, + { + "name": "go.mod", + "SPDXID": "SPDXRef-DocumentRoot-File-go.mod", + "versionInfo": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd", + "supplier": "NOASSERTION", + "downloadLocation": "NOASSERTION", + "filesAnalyzed": false, + "checksums": [ + { + "algorithm": "SHA256", + "checksumValue": "dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + ], + "primaryPackagePurpose": "FILE" + } + ], + "files": [ + { + "fileName": "/go.mod", + "SPDXID": "SPDXRef-File-go.mod-3fc5a8d3d86e9790", + "checksums": [ + { + "algorithm": "SHA1", + "checksumValue": "0000000000000000000000000000000000000000" + } + ], + "licenseConcluded": "NOASSERTION", + "copyrightText": "" + } + ], + "relationships": [ + { + "spdxElementId": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "relatedSpdxElement": "SPDXRef-File-go.mod-3fc5a8d3d86e9790", + "relationshipType": "OTHER", + "comment": "evident-by: indicates the package's existence is evident by the given file" + }, + { + "spdxElementId": "SPDXRef-DocumentRoot-File-go.mod", + "relatedSpdxElement": "SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86", + "relationshipType": "CONTAINS" + }, + { + "spdxElementId": "SPDXRef-DOCUMENT", + "relatedSpdxElement": "SPDXRef-DocumentRoot-File-go.mod", + "relationshipType": "DESCRIBES" + } + ] +} diff --git a/syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXJSONDirectoryEncoder.golden b/syft/format/spdxjson/test-fixtures/snapshot/TestSPDXJSONDirectoryEncoder.golden similarity index 100% rename from syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXJSONDirectoryEncoder.golden rename to syft/format/spdxjson/test-fixtures/snapshot/TestSPDXJSONDirectoryEncoder.golden diff --git a/syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXJSONImageEncoder.golden b/syft/format/spdxjson/test-fixtures/snapshot/TestSPDXJSONImageEncoder.golden similarity index 100% rename from syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXJSONImageEncoder.golden rename to syft/format/spdxjson/test-fixtures/snapshot/TestSPDXJSONImageEncoder.golden diff --git a/syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden b/syft/format/spdxjson/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden similarity index 100% rename from syft/formats/spdxjson/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden rename to syft/format/spdxjson/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden diff --git a/syft/formats/spdxjson/test-fixtures/spdx/alpine-3.10.syft.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/alpine-3.10.syft.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/alpine-3.10.syft.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/alpine-3.10.syft.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/alpine-3.10.vendor.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/alpine-3.10.vendor.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/alpine-3.10.vendor.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/alpine-3.10.vendor.spdx.json diff --git a/syft/format/spdxjson/test-fixtures/spdx/bad/bad-sbom b/syft/format/spdxjson/test-fixtures/spdx/bad/bad-sbom new file mode 100644 index 00000000000..42d2ee6cd5f --- /dev/null +++ b/syft/format/spdxjson/test-fixtures/spdx/bad/bad-sbom @@ -0,0 +1 @@ +bogus! \ No newline at end of file diff --git a/syft/formats/spdxjson/test-fixtures/spdx/bad/example7-bin.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/bad/example7-bin.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/bad/example7-bin.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/bad/example7-bin.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/bad/example7-go-module.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/bad/example7-go-module.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/bad/example7-go-module.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/bad/example7-go-module.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/bad/example7-golang.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/bad/example7-golang.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/bad/example7-golang.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/bad/example7-golang.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/bad/example7-third-party-modules.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/bad/example7-third-party-modules.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/bad/example7-third-party-modules.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/bad/example7-third-party-modules.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/example7-bin.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/example7-bin.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/example7-bin.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/example7-bin.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/example7-go-module.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/example7-go-module.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/example7-go-module.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/example7-go-module.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/example7-golang.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/example7-golang.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/example7-golang.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/example7-golang.spdx.json diff --git a/syft/formats/spdxjson/test-fixtures/spdx/example7-third-party-modules.spdx.json b/syft/format/spdxjson/test-fixtures/spdx/example7-third-party-modules.spdx.json similarity index 100% rename from syft/formats/spdxjson/test-fixtures/spdx/example7-third-party-modules.spdx.json rename to syft/format/spdxjson/test-fixtures/spdx/example7-third-party-modules.spdx.json diff --git a/syft/format/spdxtagvalue/decoder.go b/syft/format/spdxtagvalue/decoder.go new file mode 100644 index 00000000000..4f70e382cf3 --- /dev/null +++ b/syft/format/spdxtagvalue/decoder.go @@ -0,0 +1,109 @@ +package spdxtagvalue + +import ( + "bufio" + "fmt" + "io" + "strings" + + "github.com/spdx/tools-golang/tagvalue" + + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/format/common/spdxhelpers" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatDecoder = (*decoder)(nil) + +type decoder struct { +} + +func NewFormatDecoder() sbom.FormatDecoder { + return decoder{} +} + +func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + + // since spdx lib will always return the latest version of the document, we need to identify the version + // first and then decode into the appropriate document object. Otherwise if we get the version info from the + // decoded object we will always get the latest version (instead of the version we decoded from). + id, version := d.Identify(reader) + if id != ID { + return nil, "", "", fmt.Errorf("not a spdx tag-value document") + } + if version == "" { + return nil, "", "", fmt.Errorf("unsupported spdx tag-value document version") + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + return nil, "", "", fmt.Errorf("unable to seek to start of SPDX Tag-Value SBOM: %+v", err) + } + + doc, err := tagvalue.Read(reader) + if err != nil { + return nil, id, version, fmt.Errorf("unable to decode spdx tag-value: %w", err) + } + + s, err := spdxhelpers.ToSyftModel(doc) + if err != nil { + return nil, id, version, err + } + return s, id, version, nil +} + +func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + log.Debugf("unable to seek to start of SPDX Tag-Value SBOM: %+v", err) + return "", "" + } + + // Example document + // SPDXVersion: SPDX-2.3 + // DataLicense: CC0-1.0 + // SPDXID: SPDXRef-DOCUMENT + + scanner := bufio.NewScanner(reader) + scanner.Split(bufio.ScanLines) + + var id sbom.FormatID + var version string + for i := 0; scanner.Scan() && i < 3; i++ { + line := scanner.Text() + if strings.HasPrefix(line, "SPDXVersion:") { + id, version = getFormatInfo(line) + break + } + } + + if version == "" || id != ID { + // not a spdx tag-value document + return "", "" + } + + return id, version +} + +func getFormatInfo(line string) (sbom.FormatID, string) { + // example input: SPDXVersion: SPDX-2.3 + fields := strings.SplitN(line, ":", 2) + if len(fields) != 2 { + return "", "" + } + spdxVersion := fields[1] + if !strings.HasPrefix(strings.TrimSpace(strings.ToLower(spdxVersion)), "spdx-") { + return "", "" + } + fields = strings.Split(spdxVersion, "-") + if len(fields) != 2 { + return ID, "" + } + + return ID, fields[1] +} diff --git a/syft/format/spdxtagvalue/decoder_test.go b/syft/format/spdxtagvalue/decoder_test.go new file mode 100644 index 00000000000..497b7d81229 --- /dev/null +++ b/syft/format/spdxtagvalue/decoder_test.go @@ -0,0 +1,167 @@ +package spdxtagvalue + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/file" + "github.com/anchore/syft/syft/sbom" +) + +func TestDecoder_Decode(t *testing.T) { + tests := []struct { + name string + file string + err bool + distro string + packages []string + }{ + { + name: "dir-scan", + file: "snapshot/TestSPDXTagValueDirectoryEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "image-scan", + file: "snapshot/TestSPDXTagValueImageEncoder.golden", + distro: "debian:1.2.3", + packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, + }, + { + name: "not-an-sbom", + file: "bad-sbom", + err: true, + }, + } + for _, test := range tests { + t.Run(test.file, func(t *testing.T) { + reader, err := os.Open(filepath.Join("test-fixtures", test.file)) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + if test.err { + assert.Equal(t, sbom.FormatID(""), formatID) + assert.Equal(t, "", formatVersion) + return + } + assert.Equal(t, ID, formatID) + assert.NotEmpty(t, formatVersion) + + bom, decodeID, decodeVersion, err := dec.Decode(reader) + require.NotNil(t, bom) + require.NoError(t, err) + + assert.Equal(t, ID, decodeID) + assert.Equal(t, formatVersion, decodeVersion) + + var pkgs []string + for p := range bom.Artifacts.Packages.Enumerate() { + pkgs = append(pkgs, fmt.Sprintf("%s:%s", p.Name, p.Version)) + } + + assert.ElementsMatch(t, test.packages, pkgs) + }) + } +} + +func Test_packageDirectFiles(t *testing.T) { + contents := ` +SPDXVersion: SPDX-2.2 +DataLicense: CC0-1.0 +SPDXID: SPDXRef-DOCUMENT +DocumentName: Some-SBOM +DocumentNamespace: https://example.org/some/namespace +Creator: Organization: Some-organization +Creator: Tool: Some-tool Version: 1.0 +Created: 2021-12-29T17:02:21Z +PackageName: Some-package +PackageVersion: 5.1.2 +SPDXID: SPDXRef-Package-43c51b08-cc7e-406d-8ad9-34aa292d1157 +PackageSupplier: Organization: Some-organization +PackageDownloadLocation: https://example.org/download/location +FilesAnalyzed: true +PackageLicenseInfoFromFiles: NOASSERTION +PackageVerificationCode: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 +PackageLicenseConcluded: NOASSERTION +PackageLicenseDeclared: NOASSERTION +PackageCopyrightText: NOASSERTION +PackageChecksum: SHA1: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 +FileName: Some-file-name +SPDXID: SPDXRef-99545d55-933d-4e08-9eb5-9d826111cb79 +FileContributor: Some-file-contributor +FileType: BINARY +FileChecksum: SHA1: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 +LicenseConcluded: NOASSERTION +LicenseInfoInFile: NOASSERTION +FileCopyrightText: NOASSERTION +` + + dec := NewFormatDecoder() + + s, id, version, err := dec.Decode(strings.NewReader(contents)) + require.NoError(t, err) + assert.Equal(t, ID, id) + assert.Equal(t, "2.2", version) + + pkgs := s.Artifacts.Packages.Sorted() + assert.Len(t, pkgs, 1) + assert.Len(t, s.Artifacts.FileMetadata, 1) + assert.Len(t, s.Relationships, 1) + p := pkgs[0] + r := s.Relationships[0] + f := file.Location{} + for c := range s.Artifacts.FileMetadata { + f = file.Location{ + LocationData: file.LocationData{ + Coordinates: c, + VirtualPath: "", + }, + LocationMetadata: file.LocationMetadata{}, + } + break // there should only be 1 + } + assert.Equal(t, p.ID(), r.From.ID()) + assert.Equal(t, f.ID(), r.To.ID()) +} + +func TestDecoder_Identify(t *testing.T) { + type testCase struct { + name string + file string + id sbom.FormatID + version string + } + + var cases []testCase + + for _, version := range SupportedVersions() { + cases = append(cases, testCase{ + name: fmt.Sprintf("v%s schema", version), + file: fmt.Sprintf("test-fixtures/identify/%s.sbom", version), + id: ID, + version: version, + }) + } + + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + reader, err := os.Open(test.file) + require.NoError(t, err) + + dec := NewFormatDecoder() + + formatID, formatVersion := dec.Identify(reader) + assert.Equal(t, test.id, formatID) + assert.Equal(t, test.version, formatVersion) + }) + } +} diff --git a/syft/format/spdxtagvalue/encoder.go b/syft/format/spdxtagvalue/encoder.go new file mode 100644 index 00000000000..7923801e97e --- /dev/null +++ b/syft/format/spdxtagvalue/encoder.go @@ -0,0 +1,90 @@ +package spdxtagvalue + +import ( + "fmt" + "io" + + "github.com/spdx/tools-golang/convert" + "github.com/spdx/tools-golang/spdx/v2/v2_1" + "github.com/spdx/tools-golang/spdx/v2/v2_2" + "github.com/spdx/tools-golang/spdx/v2/v2_3" + "github.com/spdx/tools-golang/tagvalue" + + "github.com/anchore/syft/syft/format/common/spdxhelpers" + "github.com/anchore/syft/syft/format/internal/spdxutil" + "github.com/anchore/syft/syft/sbom" +) + +const ID = spdxutil.TagValueFormatID + +func SupportedVersions() []string { + return spdxutil.SupportedVersions(ID) +} + +type EncoderConfig struct { + Version string +} + +type encoder struct { + cfg EncoderConfig +} + +func NewFormatEncoderWithConfig(cfg EncoderConfig) (sbom.FormatEncoder, error) { + return encoder{ + cfg: cfg, + }, nil +} + +func DefaultEncoderConfig() EncoderConfig { + return EncoderConfig{ + Version: spdxutil.DefaultVersion, + } +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "spdx", + "spdx-tv", + } +} + +func (e encoder) Version() string { + return e.cfg.Version +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { + latestDoc := spdxhelpers.ToFormatModel(s) + if latestDoc == nil { + return fmt.Errorf("unable to convert SBOM to SPDX document") + } + + var err error + var encodeDoc any + switch e.cfg.Version { + case "2.1": + doc := v2_1.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + case "2.2": + doc := v2_2.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + + case "2.3", "", "2", "2.x": + doc := v2_3.Document{} + err = convert.Document(latestDoc, &doc) + encodeDoc = doc + default: + return fmt.Errorf("unsupported SPDX version %q", e.cfg.Version) + } + + if err != nil { + return fmt.Errorf("unable to convert SBOM to SPDX document: %w", err) + } + + return tagvalue.Write(encodeDoc, writer) +} diff --git a/syft/format/spdxtagvalue/encoder_test.go b/syft/format/spdxtagvalue/encoder_test.go new file mode 100644 index 00000000000..7dc971a8b70 --- /dev/null +++ b/syft/format/spdxtagvalue/encoder_test.go @@ -0,0 +1,211 @@ +package spdxtagvalue + +import ( + "bytes" + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/internal/testutil" + "github.com/anchore/syft/syft/pkg" + "github.com/anchore/syft/syft/sbom" + "github.com/anchore/syft/syft/source" +) + +var updateSnapshot = flag.Bool("update-spdx-tv", false, "update the *.golden files for spdx-tv encoders") +var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") + +func getEncoder(t testing.TB) sbom.FormatEncoder { + enc, err := NewFormatEncoderWithConfig(DefaultEncoderConfig()) + require.NoError(t, err) + return enc +} + +func TestSPDXTagValueDirectoryEncoder(t *testing.T) { + dir := t.TempDir() + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(dir), + }, + ) +} + +func TestSPDXTagValueImageEncoder(t *testing.T) { + testImage := "image-simple" + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage, testutil.FromSnapshot()), + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(), + }, + ) +} + +func TestSPDXJSONSPDXIDs(t *testing.T) { + var pkgs []pkg.Package + for _, name := range []string{"some/slashes", "@at-sign", "under_scores"} { + p := pkg.Package{ + Name: name, + } + p.SetID() + pkgs = append(pkgs, p) + } + + s := sbom.SBOM{ + Artifacts: sbom.Artifacts{ + Packages: pkg.NewCollection(pkgs...), + }, + Relationships: nil, + Source: source.Description{ + Name: "foobar/baz", // in this case, foobar is used as the spdx document name + Metadata: source.DirectorySourceMetadata{}, + }, + Descriptor: sbom.Descriptor{ + Name: "syft", + Version: "v0.42.0-bogus", + Configuration: map[string]string{ + "config-key": "config-value", + }, + }, + } + + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: s, + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(), + }, + ) +} + +func TestSPDXRelationshipOrder(t *testing.T) { + testImage := "image-simple" + s := testutil.ImageInput(t, testImage, testutil.FromSnapshot()) + testutil.AddSampleFileRelationships(&s) + + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ + Image: testImage, + UpdateImageSnapshot: *updateImage, + }, + testutil.EncoderSnapshotTestConfig{ + Subject: s, + Format: getEncoder(t), + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + Redactor: redactor(), + }, + ) +} + +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). + WithValuesRedacted(values...). + WithPatternRedactors( + map[string]string{ + // each SBOM reports the time it was generated, which is not useful during snapshot testing + `Created: .*`: "Created: redacted", + + // each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing + `DocumentNamespace: https://anchore.com/.*`: "DocumentNamespace: redacted", + + // the license list will be updated periodically, the value here should not be directly tested in snapshot tests + `LicenseListVersion: .*`: "LicenseListVersion: redacted", + }, + ) +} + +func TestSupportedVersions(t *testing.T) { + encs := defaultFormatEncoders() + require.NotEmpty(t, encs) + + versions := SupportedVersions() + require.Equal(t, len(versions), len(encs)) + + subject := testutil.DirectoryInput(t, t.TempDir()) + dec := NewFormatDecoder() + + relationshipOffsetPerVersion := map[string]int{ + // the package representing the source gets a relationship from the source package to all other packages found + // these relationships cannot be removed until the primaryPackagePurpose info is available in 2.3 + "2.1": 2, + "2.2": 2, + // the source-to-package relationships can be removed since the primaryPackagePurpose info is available in 2.3 + "2.3": 0, + } + + pkgCountOffsetPerVersion := map[string]int{ + "2.1": 1, // the source is mapped as a package, but cannot distinguish it since the primaryPackagePurpose info is not available until 2.3 + "2.2": 1, // the source is mapped as a package, but cannot distinguish it since the primaryPackagePurpose info is not available until 2.3 + "2.3": 0, // the source package can be removed since the primaryPackagePurpose info is available + } + + for _, enc := range encs { + t.Run(enc.Version(), func(t *testing.T) { + require.Contains(t, versions, enc.Version()) + + var buf bytes.Buffer + require.NoError(t, enc.Encode(&buf, subject)) + + id, version := dec.Identify(bytes.NewReader(buf.Bytes())) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + var s *sbom.SBOM + var err error + s, id, version, err = dec.Decode(bytes.NewReader(buf.Bytes())) + require.NoError(t, err) + require.Equal(t, enc.ID(), id) + require.Equal(t, enc.Version(), version) + + require.NotEmpty(t, s.Artifacts.Packages.PackageCount()) + + offset := relationshipOffsetPerVersion[enc.Version()] + + assert.Equal(t, len(subject.Relationships)+offset, len(s.Relationships), "mismatched relationship count") + + offset = pkgCountOffsetPerVersion[enc.Version()] + + if !assert.Equal(t, subject.Artifacts.Packages.PackageCount()+offset, s.Artifacts.Packages.PackageCount(), "mismatched package count") { + t.Logf("expected: %d", subject.Artifacts.Packages.PackageCount()) + for _, p := range subject.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + t.Logf("actual: %d", s.Artifacts.Packages.PackageCount()) + for _, p := range s.Artifacts.Packages.Sorted() { + t.Logf(" - %s", p.String()) + } + } + }) + } +} + +func defaultFormatEncoders() []sbom.FormatEncoder { + var encs []sbom.FormatEncoder + for _, version := range SupportedVersions() { + enc, err := NewFormatEncoderWithConfig(EncoderConfig{Version: version}) + if err != nil { + panic(err) + } + encs = append(encs, enc) + } + return encs +} diff --git a/syft/format/spdxtagvalue/test-fixtures/bad-sbom b/syft/format/spdxtagvalue/test-fixtures/bad-sbom new file mode 100644 index 00000000000..bb8a8152e0e --- /dev/null +++ b/syft/format/spdxtagvalue/test-fixtures/bad-sbom @@ -0,0 +1 @@ +not an sbom! \ No newline at end of file diff --git a/syft/format/spdxtagvalue/test-fixtures/identify/2.1.sbom b/syft/format/spdxtagvalue/test-fixtures/identify/2.1.sbom new file mode 100644 index 00000000000..39cb3b08589 --- /dev/null +++ b/syft/format/spdxtagvalue/test-fixtures/identify/2.1.sbom @@ -0,0 +1,50 @@ +SPDXVersion: SPDX-2.1 +DataLicense: CC0-1.0 +SPDXID: SPDXRef-DOCUMENT +DocumentName: go.mod +DocumentNamespace: https://anchore.com/syft/file/go.mod-91ab66ec-bf3a-4c1b-85a9-e480f6296a1c +LicenseListVersion: 3.21 +Creator: Organization: Anchore, Inc +Creator: Tool: syft-0.91.0 +Created: 2023-09-29T15:16:29Z + +##### Unpackaged files + +FileName: /go.mod +SPDXID: SPDXRef-File-go.mod-3fc5a8d3d86e9790 +FileChecksum: SHA1: 0000000000000000000000000000000000000000 +LicenseConcluded: NOASSERTION + +##### Package: go.mod + +PackageName: go.mod +SPDXID: SPDXRef-DocumentRoot-File-go.mod +PackageVersion: sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +FilesAnalyzed: false +PackageChecksum: SHA256: dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + +##### Package: github.com/wagoodman/go-partybus + +PackageName: github.com/wagoodman/go-partybus +SPDXID: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +PackageVersion: v0.0.0-20230516145632-8ccac152c651 +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +FilesAnalyzed: false +PackageSourceInfo: acquired package info from go module information: /go.mod +PackageLicenseConcluded: NOASSERTION +PackageLicenseDeclared: NOASSERTION +PackageCopyrightText: NOASSERTION +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: PACKAGE-MANAGER purl pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + +##### Relationships + +Relationship: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 OTHER SPDXRef-File-go.mod-3fc5a8d3d86e9790 +RelationshipComment: evident-by: indicates the package's existence is evident by the given file +Relationship: SPDXRef-DocumentRoot-File-go.mod CONTAINS SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-DocumentRoot-File-go.mod + diff --git a/syft/format/spdxtagvalue/test-fixtures/identify/2.2.sbom b/syft/format/spdxtagvalue/test-fixtures/identify/2.2.sbom new file mode 100644 index 00000000000..d48cf18ff0f --- /dev/null +++ b/syft/format/spdxtagvalue/test-fixtures/identify/2.2.sbom @@ -0,0 +1,50 @@ +SPDXVersion: SPDX-2.2 +DataLicense: CC0-1.0 +SPDXID: SPDXRef-DOCUMENT +DocumentName: go.mod +DocumentNamespace: https://anchore.com/syft/file/go.mod-022ab85e-2140-4cce-9d31-180ceb152d81 +LicenseListVersion: 3.21 +Creator: Organization: Anchore, Inc +Creator: Tool: syft-0.91.0 +Created: 2023-09-29T15:16:29Z + +##### Unpackaged files + +FileName: /go.mod +SPDXID: SPDXRef-File-go.mod-3fc5a8d3d86e9790 +FileChecksum: SHA1: 0000000000000000000000000000000000000000 +LicenseConcluded: NOASSERTION + +##### Package: go.mod + +PackageName: go.mod +SPDXID: SPDXRef-DocumentRoot-File-go.mod +PackageVersion: sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +FilesAnalyzed: false +PackageChecksum: SHA256: dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + +##### Package: github.com/wagoodman/go-partybus + +PackageName: github.com/wagoodman/go-partybus +SPDXID: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +PackageVersion: v0.0.0-20230516145632-8ccac152c651 +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +FilesAnalyzed: false +PackageSourceInfo: acquired package info from go module information: /go.mod +PackageLicenseConcluded: NOASSERTION +PackageLicenseDeclared: NOASSERTION +PackageCopyrightText: NOASSERTION +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: PACKAGE-MANAGER purl pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + +##### Relationships + +Relationship: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 OTHER SPDXRef-File-go.mod-3fc5a8d3d86e9790 +RelationshipComment: evident-by: indicates the package's existence is evident by the given file +Relationship: SPDXRef-DocumentRoot-File-go.mod CONTAINS SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-DocumentRoot-File-go.mod + diff --git a/syft/format/spdxtagvalue/test-fixtures/identify/2.3.sbom b/syft/format/spdxtagvalue/test-fixtures/identify/2.3.sbom new file mode 100644 index 00000000000..8c4e9f0d5bf --- /dev/null +++ b/syft/format/spdxtagvalue/test-fixtures/identify/2.3.sbom @@ -0,0 +1,51 @@ +SPDXVersion: SPDX-2.3 +DataLicense: CC0-1.0 +SPDXID: SPDXRef-DOCUMENT +DocumentName: go.mod +DocumentNamespace: https://anchore.com/syft/file/go.mod-63b8e1d7-2f51-4996-bd88-bd2dca400a39 +LicenseListVersion: 3.21 +Creator: Organization: Anchore, Inc +Creator: Tool: syft-0.91.0 +Created: 2023-09-29T15:16:29Z + +##### Unpackaged files + +FileName: /go.mod +SPDXID: SPDXRef-File-go.mod-3fc5a8d3d86e9790 +FileChecksum: SHA1: 0000000000000000000000000000000000000000 +LicenseConcluded: NOASSERTION + +##### Package: go.mod + +PackageName: go.mod +SPDXID: SPDXRef-DocumentRoot-File-go.mod +PackageVersion: sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +PrimaryPackagePurpose: FILE +FilesAnalyzed: false +PackageChecksum: SHA256: dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd + +##### Package: github.com/wagoodman/go-partybus + +PackageName: github.com/wagoodman/go-partybus +SPDXID: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +PackageVersion: v0.0.0-20230516145632-8ccac152c651 +PackageSupplier: NOASSERTION +PackageDownloadLocation: NOASSERTION +FilesAnalyzed: false +PackageSourceInfo: acquired package info from go module information: /go.mod +PackageLicenseConcluded: NOASSERTION +PackageLicenseDeclared: NOASSERTION +PackageCopyrightText: NOASSERTION +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: SECURITY cpe23Type cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:* +ExternalRef: PACKAGE-MANAGER purl pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651 + +##### Relationships + +Relationship: SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 OTHER SPDXRef-File-go.mod-3fc5a8d3d86e9790 +RelationshipComment: evident-by: indicates the package's existence is evident by the given file +Relationship: SPDXRef-DocumentRoot-File-go.mod CONTAINS SPDXRef-Package-go-module-github.com-wagoodman-go-partybus-2ff71a67fb024c86 +Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-DocumentRoot-File-go.mod + diff --git a/syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXJSONSPDXIDs.golden b/syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXJSONSPDXIDs.golden similarity index 100% rename from syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXJSONSPDXIDs.golden rename to syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXJSONSPDXIDs.golden diff --git a/syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden b/syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden similarity index 100% rename from syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden rename to syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXRelationshipOrder.golden diff --git a/syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueDirectoryEncoder.golden b/syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueDirectoryEncoder.golden similarity index 100% rename from syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueDirectoryEncoder.golden rename to syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueDirectoryEncoder.golden diff --git a/syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueImageEncoder.golden b/syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueImageEncoder.golden similarity index 100% rename from syft/formats/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueImageEncoder.golden rename to syft/format/spdxtagvalue/test-fixtures/snapshot/TestSPDXTagValueImageEncoder.golden diff --git a/syft/formats/spdxtagvalue/test-fixtures/tag-value.spdx b/syft/format/spdxtagvalue/test-fixtures/tag-value.spdx similarity index 100% rename from syft/formats/spdxtagvalue/test-fixtures/tag-value.spdx rename to syft/format/spdxtagvalue/test-fixtures/tag-value.spdx diff --git a/syft/format/syftjson/decoder.go b/syft/format/syftjson/decoder.go new file mode 100644 index 00000000000..39c7bd16242 --- /dev/null +++ b/syft/format/syftjson/decoder.go @@ -0,0 +1,113 @@ +package syftjson + +import ( + "encoding/json" + "fmt" + "io" + "strings" + + "github.com/Masterminds/semver" + + "github.com/anchore/syft/internal" + "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/format/syftjson/model" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatDecoder = (*decoder)(nil) + +type decoder struct{} + +type DecoderConfig struct { +} + +func DefaultDecoderConfig() DecoderConfig { + return DecoderConfig{} +} + +func NewFormatDecoder() sbom.FormatDecoder { + return NewFormatDecoderWithConfig(DefaultDecoderConfig()) +} + +func NewFormatDecoderWithConfig(DecoderConfig) sbom.FormatDecoder { + return decoder{} +} + +func (d decoder) Decode(reader io.ReadSeeker) (*sbom.SBOM, sbom.FormatID, string, error) { + if reader == nil { + return nil, "", "", fmt.Errorf("no SBOM bytes provided") + } + + id, version := d.Identify(reader) + if version == "" || id != ID { + return nil, "", "", fmt.Errorf("not a syft-json document") + } + var doc model.Document + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + return nil, "", "", fmt.Errorf("unable to seek to start of Syft JSON SBOM: %+v", err) + } + + dec := json.NewDecoder(reader) + + err := dec.Decode(&doc) + if err != nil { + return nil, "", "", fmt.Errorf("unable to decode syft-json document: %w", err) + } + + if err := checkSupportedSchema(doc.Schema.Version, internal.JSONSchemaVersion); err != nil { + log.Warn(err) + } + + return toSyftModel(doc), ID, doc.Schema.Version, nil +} + +func (d decoder) Identify(reader io.ReadSeeker) (sbom.FormatID, string) { + if reader == nil { + return "", "" + } + + if _, err := reader.Seek(0, io.SeekStart); err != nil { + log.Debugf("unable to seek to start of Syft JSON SBOM: %+v", err) + return "", "" + } + + type Document struct { + Schema model.Schema `json:"schema"` + } + + dec := json.NewDecoder(reader) + + var doc Document + err := dec.Decode(&doc) + if err != nil { + // maybe not json? maybe not valid? doesn't matter, we won't process it. + return "", "" + } + + if !strings.Contains(doc.Schema.URL, "anchore/syft") { + // not a syft-json document + return "", "" + } + + // note: we support all previous schema versions + return ID, doc.Schema.Version +} + +func checkSupportedSchema(documentVersion string, parserVersion string) error { + documentV, err := semver.NewVersion(documentVersion) + if err != nil { + return fmt.Errorf("error comparing document schema version with parser schema version: %w", err) + } + + parserV, err := semver.NewVersion(parserVersion) + if err != nil { + return fmt.Errorf("error comparing document schema version with parser schema version: %w", err) + } + + if documentV.GreaterThan(parserV) { + return fmt.Errorf("document has schema version %s, but parser has older schema version (%s)", documentVersion, parserVersion) + } + + return nil +} diff --git a/syft/formats/syftjson/decoder_test.go b/syft/format/syftjson/decoder_test.go similarity index 88% rename from syft/formats/syftjson/decoder_test.go rename to syft/format/syftjson/decoder_test.go index de8490f613e..a28e9d7a3b7 100644 --- a/syft/formats/syftjson/decoder_test.go +++ b/syft/format/syftjson/decoder_test.go @@ -12,24 +12,30 @@ import ( "github.com/stretchr/testify/require" stereoscopeFile "github.com/anchore/stereoscope/pkg/file" + "github.com/anchore/syft/internal" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/internal/testutils" + "github.com/anchore/syft/syft/format/internal/testutil" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) -func TestEncodeDecodeCycle(t *testing.T) { +func Test_EncodeDecodeCycle(t *testing.T) { testImage := "image-simple" - originalSBOM := testutils.ImageInput(t, testImage) + originalSBOM := testutil.ImageInput(t, testImage) + + enc := NewFormatEncoder() + dec := NewFormatDecoder() var buf bytes.Buffer - assert.NoError(t, encoder(&buf, originalSBOM)) + assert.NoError(t, enc.Encode(&buf, originalSBOM)) - actualSBOM, err := decoder(bytes.NewReader(buf.Bytes())) + actualSBOM, decodedID, decodedVersion, err := dec.Decode(bytes.NewReader(buf.Bytes())) assert.NoError(t, err) + assert.Equal(t, ID, decodedID) + assert.Equal(t, internal.JSONSchemaVersion, decodedVersion) for _, d := range deep.Equal(originalSBOM.Source, actualSBOM.Source) { if strings.HasSuffix(d, " != []") { @@ -228,11 +234,16 @@ func Test_encodeDecodeFileMetadata(t *testing.T) { } buf := &bytes.Buffer{} - err := encoder(buf, s) + enc := NewFormatEncoder() + err := enc.Encode(buf, s) require.NoError(t, err) - got, err := decoder(buf) + dec := NewFormatDecoder() + + got, decodedID, decodedVersion, err := dec.Decode(bytes.NewReader(buf.Bytes())) require.NoError(t, err) + assert.Equal(t, ID, decodedID) + assert.Equal(t, internal.JSONSchemaVersion, decodedVersion) require.Equal(t, s, *got) } diff --git a/syft/format/syftjson/encoder.go b/syft/format/syftjson/encoder.go new file mode 100644 index 00000000000..4d6695709d4 --- /dev/null +++ b/syft/format/syftjson/encoder.go @@ -0,0 +1,46 @@ +package syftjson + +import ( + "encoding/json" + "io" + + "github.com/anchore/syft/internal" + "github.com/anchore/syft/syft/sbom" +) + +var _ sbom.FormatEncoder = (*encoder)(nil) + +const ID sbom.FormatID = "syft-json" + +type encoder struct { +} + +func NewFormatEncoder() sbom.FormatEncoder { + return encoder{} +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "json", + "syft", + } +} + +func (e encoder) Version() string { + return internal.JSONSchemaVersion +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { + doc := ToFormatModel(s) + + enc := json.NewEncoder(writer) + // prevent > and < from being escaped in the payload + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + + return enc.Encode(&doc) +} diff --git a/syft/formats/syftjson/encoder_test.go b/syft/format/syftjson/encoder_test.go similarity index 84% rename from syft/formats/syftjson/encoder_test.go rename to syft/format/syftjson/encoder_test.go index 8ad7ababda2..488f95837a9 100644 --- a/syft/formats/syftjson/encoder_test.go +++ b/syft/format/syftjson/encoder_test.go @@ -5,10 +5,11 @@ import ( "testing" stereoFile "github.com/anchore/stereoscope/pkg/file" + "github.com/anchore/syft/internal" "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/internal/testutils" + "github.com/anchore/syft/syft/format/internal/testutil" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" @@ -18,12 +19,24 @@ import ( var updateSnapshot = flag.Bool("update-json", false, "update the *.golden files for json encoders") var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") +func TestDefaultNameAndVersion(t *testing.T) { + expectedID, expectedVersion := ID, internal.JSONSchemaVersion + enc := NewFormatEncoder() + if enc.ID() != expectedID { + t.Errorf("expected ID %q, got %q", expectedID, enc.ID()) + } + + if enc.Version() != expectedVersion { + t.Errorf("expected version %q, got %q", expectedVersion, enc.Version()) + } +} + func TestDirectoryEncoder(t *testing.T) { dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: true, @@ -34,14 +47,14 @@ func TestDirectoryEncoder(t *testing.T) { func TestImageEncoder(t *testing.T) { testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ Image: testImage, UpdateImageSnapshot: *updateImage, }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage, testutils.FromSnapshot()), - Format: Format(), + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage, testutil.FromSnapshot()), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: true, @@ -219,10 +232,10 @@ func TestEncodeFullJSONDocument(t *testing.T) { }, } - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ Subject: s, - Format: Format(), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: true, @@ -231,8 +244,8 @@ func TestEncodeFullJSONDocument(t *testing.T) { ) } -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). WithValuesRedacted(values...). WithPatternRedactors( map[string]string{ diff --git a/syft/formats/syftjson/model/document.go b/syft/format/syftjson/model/document.go similarity index 100% rename from syft/formats/syftjson/model/document.go rename to syft/format/syftjson/model/document.go diff --git a/syft/formats/syftjson/model/file.go b/syft/format/syftjson/model/file.go similarity index 100% rename from syft/formats/syftjson/model/file.go rename to syft/format/syftjson/model/file.go diff --git a/syft/formats/syftjson/model/linux_release.go b/syft/format/syftjson/model/linux_release.go similarity index 100% rename from syft/formats/syftjson/model/linux_release.go rename to syft/format/syftjson/model/linux_release.go diff --git a/syft/formats/syftjson/model/linux_release_test.go b/syft/format/syftjson/model/linux_release_test.go similarity index 100% rename from syft/formats/syftjson/model/linux_release_test.go rename to syft/format/syftjson/model/linux_release_test.go diff --git a/syft/formats/syftjson/model/package.go b/syft/format/syftjson/model/package.go similarity index 100% rename from syft/formats/syftjson/model/package.go rename to syft/format/syftjson/model/package.go diff --git a/syft/formats/syftjson/model/package_test.go b/syft/format/syftjson/model/package_test.go similarity index 100% rename from syft/formats/syftjson/model/package_test.go rename to syft/format/syftjson/model/package_test.go diff --git a/syft/formats/syftjson/model/relationship.go b/syft/format/syftjson/model/relationship.go similarity index 100% rename from syft/formats/syftjson/model/relationship.go rename to syft/format/syftjson/model/relationship.go diff --git a/syft/formats/syftjson/model/secrets.go b/syft/format/syftjson/model/secrets.go similarity index 100% rename from syft/formats/syftjson/model/secrets.go rename to syft/format/syftjson/model/secrets.go diff --git a/syft/formats/syftjson/model/source.go b/syft/format/syftjson/model/source.go similarity index 100% rename from syft/formats/syftjson/model/source.go rename to syft/format/syftjson/model/source.go diff --git a/syft/formats/syftjson/model/source_test.go b/syft/format/syftjson/model/source_test.go similarity index 100% rename from syft/formats/syftjson/model/source_test.go rename to syft/format/syftjson/model/source_test.go diff --git a/syft/format/syftjson/test-fixtures/identify/11.0.0.json b/syft/format/syftjson/test-fixtures/identify/11.0.0.json new file mode 100644 index 00000000000..d8bd1743c56 --- /dev/null +++ b/syft/format/syftjson/test-fixtures/identify/11.0.0.json @@ -0,0 +1,152 @@ +{ + "artifacts": [ + { + "id": "2ff71a67fb024c86", + "name": "github.com/wagoodman/go-partybus", + "version": "v0.0.0-20230516145632-8ccac152c651", + "type": "go-module", + "foundBy": "go-mod-file-cataloger", + "locations": [ + { + "path": "/go.mod", + "annotations": { + "evidence": "primary" + } + } + ], + "licenses": [], + "language": "go", + "cpes": [ + "cpe:2.3:a:wagoodman:go-partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*", + "cpe:2.3:a:wagoodman:go_partybus:v0.0.0-20230516145632-8ccac152c651:*:*:*:*:*:*:*" + ], + "purl": "pkg:golang/github.com/wagoodman/go-partybus@v0.0.0-20230516145632-8ccac152c651", + "metadataType": "GolangModMetadata", + "metadata": {} + } + ], + "artifactRelationships": [ + { + "parent": "0bc762d3cbc1598aacb33550d32b58cf8a6f6c4b639a8520f1a8987a6747c288", + "child": "2ff71a67fb024c86", + "type": "contains" + }, + { + "parent": "2ff71a67fb024c86", + "child": "3fc5a8d3d86e9790", + "type": "evident-by" + } + ], + "files": [ + { + "id": "3fc5a8d3d86e9790", + "location": { + "path": "/go.mod" + } + } + ], + "source": { + "id": "0bc762d3cbc1598aacb33550d32b58cf8a6f6c4b639a8520f1a8987a6747c288", + "name": "go.mod", + "version": "sha256:sha256:dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd", + "type": "file", + "metadata": { + "path": ".tmp/go.mod", + "digests": [ + { + "algorithm": "sha256", + "value": "dc333f342905248a52e424d8dfd061251d01867d01a4f9d7397144a775ff9ebd" + } + ], + "mimeType": "text/plain" + } + }, + "distro": {}, + "descriptor": { + "name": "syft", + "version": "0.91.0", + "configuration": { + "catalogers": null, + "package": { + "cataloger": { + "enabled": true, + "scope": "Squashed" + }, + "search-unindexed-archives": false, + "search-indexed-archives": true + }, + "golang": { + "search-local-mod-cache-licenses": false, + "local-mod-cache-dir": "", + "search-remote-licenses": false, + "proxy": "", + "no-proxy": "" + }, + "linux-kernel": { + "catalog-modules": true + }, + "python": { + "guess-unpinned-requirements": false + }, + "file-metadata": { + "cataloger": { + "enabled": false, + "scope": "Squashed" + }, + "digests": [ + "sha256" + ] + }, + "file-classification": { + "cataloger": { + "enabled": false, + "scope": "Squashed" + } + }, + "file-contents": { + "cataloger": { + "enabled": false, + "scope": "Squashed" + }, + "skip-files-above-size": 1048576, + "globs": null + }, + "secrets": { + "cataloger": { + "enabled": false, + "scope": "AllLayers" + }, + "additional-patterns": null, + "exclude-pattern-names": null, + "reveal-values": false, + "skip-files-above-size": 1048576 + }, + "registry": { + "insecure-skip-tls-verify": false, + "insecure-use-http": false, + "auth": null, + "ca-cert": "" + }, + "exclude": [], + "platform": "", + "name": "", + "source": { + "name": "", + "version": "", + "file": { + "digests": [ + "sha256" + ] + } + }, + "parallelism": 1, + "default-image-pull-source": "", + "base-path": "", + "exclude-binary-overlap-by-ownership": true + } + }, + "schema": { + "version": "11.0.0", + "url": "https://raw.githubusercontent.com/anchore/syft/main/schema/json/schema-11.0.0.json" + } +} diff --git a/syft/formats/syftjson/test-fixtures/snapshot/TestDirectoryEncoder.golden b/syft/format/syftjson/test-fixtures/snapshot/TestDirectoryEncoder.golden similarity index 100% rename from syft/formats/syftjson/test-fixtures/snapshot/TestDirectoryEncoder.golden rename to syft/format/syftjson/test-fixtures/snapshot/TestDirectoryEncoder.golden diff --git a/syft/formats/syftjson/test-fixtures/snapshot/TestEncodeFullJSONDocument.golden b/syft/format/syftjson/test-fixtures/snapshot/TestEncodeFullJSONDocument.golden similarity index 100% rename from syft/formats/syftjson/test-fixtures/snapshot/TestEncodeFullJSONDocument.golden rename to syft/format/syftjson/test-fixtures/snapshot/TestEncodeFullJSONDocument.golden diff --git a/syft/formats/syftjson/test-fixtures/snapshot/TestImageEncoder.golden b/syft/format/syftjson/test-fixtures/snapshot/TestImageEncoder.golden similarity index 62% rename from syft/formats/syftjson/test-fixtures/snapshot/TestImageEncoder.golden rename to syft/format/syftjson/test-fixtures/snapshot/TestImageEncoder.golden index ba156fb881f..3155b7d8cef 100644 --- a/syft/formats/syftjson/test-fixtures/snapshot/TestImageEncoder.golden +++ b/syft/format/syftjson/test-fixtures/snapshot/TestImageEncoder.golden @@ -9,7 +9,7 @@ "locations": [ { "path": "/somefile-1.txt", - "layerID": "sha256:ab62016f9bec7286af65604081564cadeeb364a48faca2346c3f5a5a1f5ef777" + "layerID": "sha256:100d5a55f9032faead28b7427fa3e650e4f0158f86ea89d06e1489df00cb8c6f" } ], "licenses": [ @@ -45,7 +45,7 @@ "locations": [ { "path": "/somefile-2.txt", - "layerID": "sha256:f1803845b6747d94d6e4ecce2331457e5f1c4fb97de5216f392a76f4582f63b2" + "layerID": "sha256:000fb9200890d3a19138478b20023023c0dce1c54352007c2863716780f049eb" } ], "licenses": [], @@ -69,13 +69,13 @@ ], "artifactRelationships": [], "source": { - "id": "c8ac88bbaf3d1c036f6a1d601c3d52bafbf05571c97d68322e7cb3a7ecaa304f", + "id": "34d40fdc6ca13e9a3fa18415db216b50bff047716fae7d95a225c09732fe83fb", "name": "user-image-input", "version": "sha256:2731251dc34951c0e50fcc643b4c5f74922dad1a5d98f302b504cf46cd5d9368", "type": "image", "metadata": { "userInput": "user-image-input", - "imageID": "sha256:a3c61dc134d2f31b415c50324e75842d7f91622f39a89468e51938330b3fd3af", + "imageID": "sha256:bf783ea304a3f02b5c7d2ece521800f5e2182e65ed5bb5116f578e17d6e82be4", "manifestDigest": "sha256:2731251dc34951c0e50fcc643b4c5f74922dad1a5d98f302b504cf46cd5d9368", "mediaType": "application/vnd.docker.distribution.manifest.v2+json", "tags": [ @@ -85,17 +85,17 @@ "layers": [ { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", - "digest": "sha256:ab62016f9bec7286af65604081564cadeeb364a48faca2346c3f5a5a1f5ef777", + "digest": "sha256:100d5a55f9032faead28b7427fa3e650e4f0158f86ea89d06e1489df00cb8c6f", "size": 22 }, { "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", - "digest": "sha256:f1803845b6747d94d6e4ecce2331457e5f1c4fb97de5216f392a76f4582f63b2", + "digest": "sha256:000fb9200890d3a19138478b20023023c0dce1c54352007c2863716780f049eb", "size": 16 } ], - "manifest": "eyJzY2hlbWFWZXJzaW9uIjoyLCJtZWRpYVR5cGUiOiJhcHBsaWNhdGlvbi92bmQuZG9ja2VyLmRpc3RyaWJ1dGlvbi5tYW5pZmVzdC52Mitqc29uIiwiY29uZmlnIjp7Im1lZGlhVHlwZSI6ImFwcGxpY2F0aW9uL3ZuZC5kb2NrZXIuY29udGFpbmVyLmltYWdlLnYxK2pzb24iLCJzaXplIjo2NzMsImRpZ2VzdCI6InNoYTI1NjphM2M2MWRjMTM0ZDJmMzFiNDE1YzUwMzI0ZTc1ODQyZDdmOTE2MjJmMzlhODk0NjhlNTE5MzgzMzBiM2ZkM2FmIn0sImxheWVycyI6W3sibWVkaWFUeXBlIjoiYXBwbGljYXRpb24vdm5kLmRvY2tlci5pbWFnZS5yb290ZnMuZGlmZi50YXIuZ3ppcCIsInNpemUiOjIwNDgsImRpZ2VzdCI6InNoYTI1NjphYjYyMDE2ZjliZWM3Mjg2YWY2NTYwNDA4MTU2NGNhZGVlYjM2NGE0OGZhY2EyMzQ2YzNmNWE1YTFmNWVmNzc3In0seyJtZWRpYVR5cGUiOiJhcHBsaWNhdGlvbi92bmQuZG9ja2VyLmltYWdlLnJvb3Rmcy5kaWZmLnRhci5nemlwIiwic2l6ZSI6MjA0OCwiZGlnZXN0Ijoic2hhMjU2OmYxODAzODQ1YjY3NDdkOTRkNmU0ZWNjZTIzMzE0NTdlNWYxYzRmYjk3ZGU1MjE2ZjM5MmE3NmY0NTgyZjYzYjIifV19", - "config": "eyJhcmNoaXRlY3R1cmUiOiJhbWQ2NCIsImNvbmZpZyI6eyJFbnYiOlsiUEFUSD0vdXNyL2xvY2FsL3NiaW46L3Vzci9sb2NhbC9iaW46L3Vzci9zYmluOi91c3IvYmluOi9zYmluOi9iaW4iXSwiV29ya2luZ0RpciI6Ii8iLCJPbkJ1aWxkIjpudWxsfSwiY3JlYXRlZCI6IjIwMjMtMDQtMjFUMTk6MTA6MzcuNjUxODMxMjM0WiIsImhpc3RvcnkiOlt7ImNyZWF0ZWQiOiIyMDIzLTA0LTIxVDE5OjEwOjM3LjYwNzYxMzU1NVoiLCJjcmVhdGVkX2J5IjoiQUREIGZpbGUtMS50eHQgL3NvbWVmaWxlLTEudHh0ICMgYnVpbGRraXQiLCJjb21tZW50IjoiYnVpbGRraXQuZG9ja2VyZmlsZS52MCJ9LHsiY3JlYXRlZCI6IjIwMjMtMDQtMjFUMTk6MTA6MzcuNjUxODMxMjM0WiIsImNyZWF0ZWRfYnkiOiJBREQgZmlsZS0yLnR4dCAvc29tZWZpbGUtMi50eHQgIyBidWlsZGtpdCIsImNvbW1lbnQiOiJidWlsZGtpdC5kb2NrZXJmaWxlLnYwIn1dLCJvcyI6ImxpbnV4Iiwicm9vdGZzIjp7InR5cGUiOiJsYXllcnMiLCJkaWZmX2lkcyI6WyJzaGEyNTY6YWI2MjAxNmY5YmVjNzI4NmFmNjU2MDQwODE1NjRjYWRlZWIzNjRhNDhmYWNhMjM0NmMzZjVhNWExZjVlZjc3NyIsInNoYTI1NjpmMTgwMzg0NWI2NzQ3ZDk0ZDZlNGVjY2UyMzMxNDU3ZTVmMWM0ZmI5N2RlNTIxNmYzOTJhNzZmNDU4MmY2M2IyIl19fQ==", + "manifest": "eyJzY2hlbWFWZXJzaW9uIjoyLCJtZWRpYVR5cGUiOiJhcHBsaWNhdGlvbi92bmQuZG9ja2VyLmRpc3RyaWJ1dGlvbi5tYW5pZmVzdC52Mitqc29uIiwiY29uZmlnIjp7Im1lZGlhVHlwZSI6ImFwcGxpY2F0aW9uL3ZuZC5kb2NrZXIuY29udGFpbmVyLmltYWdlLnYxK2pzb24iLCJzaXplIjo2NzIsImRpZ2VzdCI6InNoYTI1NjpiZjc4M2VhMzA0YTNmMDJiNWM3ZDJlY2U1MjE4MDBmNWUyMTgyZTY1ZWQ1YmI1MTE2ZjU3OGUxN2Q2ZTgyYmU0In0sImxheWVycyI6W3sibWVkaWFUeXBlIjoiYXBwbGljYXRpb24vdm5kLmRvY2tlci5pbWFnZS5yb290ZnMuZGlmZi50YXIuZ3ppcCIsInNpemUiOjIwNDgsImRpZ2VzdCI6InNoYTI1NjoxMDBkNWE1NWY5MDMyZmFlYWQyOGI3NDI3ZmEzZTY1MGU0ZjAxNThmODZlYTg5ZDA2ZTE0ODlkZjAwY2I4YzZmIn0seyJtZWRpYVR5cGUiOiJhcHBsaWNhdGlvbi92bmQuZG9ja2VyLmltYWdlLnJvb3Rmcy5kaWZmLnRhci5nemlwIiwic2l6ZSI6MjA0OCwiZGlnZXN0Ijoic2hhMjU2OjAwMGZiOTIwMDg5MGQzYTE5MTM4NDc4YjIwMDIzMDIzYzBkY2UxYzU0MzUyMDA3YzI4NjM3MTY3ODBmMDQ5ZWIifV19", + "config": "eyJhcmNoaXRlY3R1cmUiOiJhcm02NCIsImNvbmZpZyI6eyJFbnYiOlsiUEFUSD0vdXNyL2xvY2FsL3NiaW46L3Vzci9sb2NhbC9iaW46L3Vzci9zYmluOi91c3IvYmluOi9zYmluOi9iaW4iXSwiV29ya2luZ0RpciI6Ii8iLCJPbkJ1aWxkIjpudWxsfSwiY3JlYXRlZCI6IjIwMjMtMDktMjhUMTI6MjM6MzUuNDAwNjcyODg1WiIsImhpc3RvcnkiOlt7ImNyZWF0ZWQiOiIyMDIzLTA5LTI4VDEyOjIzOjM1LjM5Mzk4NjUxWiIsImNyZWF0ZWRfYnkiOiJBREQgZmlsZS0xLnR4dCAvc29tZWZpbGUtMS50eHQgIyBidWlsZGtpdCIsImNvbW1lbnQiOiJidWlsZGtpdC5kb2NrZXJmaWxlLnYwIn0seyJjcmVhdGVkIjoiMjAyMy0wOS0yOFQxMjoyMzozNS40MDA2NzI4ODVaIiwiY3JlYXRlZF9ieSI6IkFERCBmaWxlLTIudHh0IC9zb21lZmlsZS0yLnR4dCAjIGJ1aWxka2l0IiwiY29tbWVudCI6ImJ1aWxka2l0LmRvY2tlcmZpbGUudjAifV0sIm9zIjoibGludXgiLCJyb290ZnMiOnsidHlwZSI6ImxheWVycyIsImRpZmZfaWRzIjpbInNoYTI1NjoxMDBkNWE1NWY5MDMyZmFlYWQyOGI3NDI3ZmEzZTY1MGU0ZjAxNThmODZlYTg5ZDA2ZTE0ODlkZjAwY2I4YzZmIiwic2hhMjU2OjAwMGZiOTIwMDg5MGQzYTE5MTM4NDc4YjIwMDIzMDIzYzBkY2UxYzU0MzUyMDA3YzI4NjM3MTY3ODBmMDQ5ZWIiXX19", "repoDigests": [], "architecture": "", "os": "" diff --git a/syft/formats/syftjson/to_format_model.go b/syft/format/syftjson/to_format_model.go similarity index 99% rename from syft/formats/syftjson/to_format_model.go rename to syft/format/syftjson/to_format_model.go index 097a64618a4..cdc31163511 100644 --- a/syft/formats/syftjson/to_format_model.go +++ b/syft/format/syftjson/to_format_model.go @@ -11,7 +11,7 @@ import ( "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/syftjson/model" + "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/internal/sourcemetadata" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" diff --git a/syft/formats/syftjson/to_format_model_test.go b/syft/format/syftjson/to_format_model_test.go similarity index 99% rename from syft/formats/syftjson/to_format_model_test.go rename to syft/format/syftjson/to_format_model_test.go index 8dd3475d8dd..55ef8933125 100644 --- a/syft/formats/syftjson/to_format_model_test.go +++ b/syft/format/syftjson/to_format_model_test.go @@ -9,7 +9,7 @@ import ( stereoscopeFile "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/syftjson/model" + "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/internal/sourcemetadata" "github.com/anchore/syft/syft/source" ) diff --git a/syft/formats/syftjson/to_syft_model.go b/syft/format/syftjson/to_syft_model.go similarity index 98% rename from syft/formats/syftjson/to_syft_model.go rename to syft/format/syftjson/to_syft_model.go index 3d9c48d75be..af523be981f 100644 --- a/syft/formats/syftjson/to_syft_model.go +++ b/syft/format/syftjson/to_syft_model.go @@ -14,14 +14,14 @@ import ( "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/syftjson/model" + "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/linux" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) -func toSyftModel(doc model.Document) (*sbom.SBOM, error) { +func toSyftModel(doc model.Document) *sbom.SBOM { idAliases := make(map[string]string) catalog := toSyftCatalog(doc.Artifacts, idAliases) @@ -40,7 +40,7 @@ func toSyftModel(doc model.Document) (*sbom.SBOM, error) { Source: *toSyftSourceData(doc.Source), Descriptor: toSyftDescriptor(doc.Descriptor), Relationships: warnConversionErrors(toSyftRelationships(&doc, catalog, doc.ArtifactRelationships, idAliases)), - }, nil + } } func warnConversionErrors[T any](converted []T, errors []error) []T { diff --git a/syft/formats/syftjson/to_syft_model_test.go b/syft/format/syftjson/to_syft_model_test.go similarity index 98% rename from syft/formats/syftjson/to_syft_model_test.go rename to syft/format/syftjson/to_syft_model_test.go index c4a6c034811..7bbb604da17 100644 --- a/syft/formats/syftjson/to_syft_model_test.go +++ b/syft/format/syftjson/to_syft_model_test.go @@ -10,7 +10,7 @@ import ( stereoFile "github.com/anchore/stereoscope/pkg/file" "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/syftjson/model" + "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/internal/sourcemetadata" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/sbom" @@ -172,7 +172,7 @@ func Test_toSyftSourceData(t *testing.T) { } func Test_idsHaveChanged(t *testing.T) { - s, err := toSyftModel(model.Document{ + s := toSyftModel(model.Document{ Source: model.Source{ Type: "file", Metadata: source.FileSourceMetadata{Path: "some/path"}, @@ -200,7 +200,6 @@ func Test_idsHaveChanged(t *testing.T) { }, }) - require.NoError(t, err) require.Len(t, s.Relationships, 1) r := s.Relationships[0] diff --git a/syft/formats/table/encoder.go b/syft/format/table/encoder.go similarity index 74% rename from syft/formats/table/encoder.go rename to syft/format/table/encoder.go index 7b6c817b7f2..f7dba75fd3f 100644 --- a/syft/formats/table/encoder.go +++ b/syft/format/table/encoder.go @@ -11,7 +11,30 @@ import ( "github.com/anchore/syft/syft/sbom" ) -func encoder(output io.Writer, s sbom.SBOM) error { +const ID sbom.FormatID = "syft-table" + +type encoder struct { +} + +func NewFormatEncoder() sbom.FormatEncoder { + return encoder{} +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "table", + } +} + +func (e encoder) Version() string { + return sbom.AnyVersion +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { var rows [][]string columns := []string{"Name", "Version", "Type"} @@ -25,7 +48,7 @@ func encoder(output io.Writer, s sbom.SBOM) error { } if len(rows) == 0 { - _, err := fmt.Fprintln(output, "No packages discovered") + _, err := fmt.Fprintln(writer, "No packages discovered") return err } @@ -40,7 +63,7 @@ func encoder(output io.Writer, s sbom.SBOM) error { }) rows = removeDuplicateRows(rows) - table := tablewriter.NewWriter(output) + table := tablewriter.NewWriter(writer) table.SetHeader(columns) table.SetHeaderLine(false) diff --git a/syft/formats/table/encoder_test.go b/syft/format/table/encoder_test.go similarity index 76% rename from syft/formats/table/encoder_test.go rename to syft/format/table/encoder_test.go index d0c672237d8..fe55f50ae28 100644 --- a/syft/formats/table/encoder_test.go +++ b/syft/format/table/encoder_test.go @@ -6,16 +6,16 @@ import ( "github.com/go-test/deep" - "github.com/anchore/syft/syft/formats/internal/testutils" + "github.com/anchore/syft/syft/format/internal/testutil" ) var updateSnapshot = flag.Bool("update-table", false, "update the *.golden files for table format") func TestTableEncoder(t *testing.T) { - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, t.TempDir()), - Format: Format(), + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, t.TempDir()), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: false, diff --git a/syft/formats/table/test-fixtures/snapshot/TestTableEncoder.golden b/syft/format/table/test-fixtures/snapshot/TestTableEncoder.golden similarity index 100% rename from syft/formats/table/test-fixtures/snapshot/TestTableEncoder.golden rename to syft/format/table/test-fixtures/snapshot/TestTableEncoder.golden diff --git a/syft/format/template/encoder.go b/syft/format/template/encoder.go new file mode 100644 index 00000000000..ba3d67ae0d2 --- /dev/null +++ b/syft/format/template/encoder.go @@ -0,0 +1,92 @@ +package template + +import ( + "errors" + "fmt" + "io" + "os" + "reflect" + "text/template" + + "github.com/Masterminds/sprig/v3" + "github.com/mitchellh/go-homedir" + + "github.com/anchore/syft/syft/format/syftjson" + "github.com/anchore/syft/syft/sbom" +) + +const ID sbom.FormatID = "template" + +type EncoderConfig struct { + TemplatePath string +} + +type encoder struct { + cfg EncoderConfig + funcMap template.FuncMap +} + +func NewFormatEncoder(cfg EncoderConfig) (sbom.FormatEncoder, error) { + // TODO: revisit this... should no template file be an error or simply render an empty result? or render the json output? + // Note: do not check for the existence of the template file here, as the default encoder cannot provide one. + f := sprig.HermeticTxtFuncMap() + f["getLastIndex"] = func(collection interface{}) int { + if v := reflect.ValueOf(collection); v.Kind() == reflect.Slice { + return v.Len() - 1 + } + + return 0 + } + // Checks if a field is defined + f["hasField"] = func(obj interface{}, field string) bool { + t := reflect.TypeOf(obj) + _, ok := t.FieldByName(field) + return ok + } + + return encoder{ + cfg: cfg, + // These are custom functions available to template authors. + funcMap: f, + }, nil +} + +func DefaultEncoderConfig() EncoderConfig { + return EncoderConfig{} +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{} +} + +func (e encoder) Version() string { + return sbom.AnyVersion +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { + if e.cfg.TemplatePath == "" { + return errors.New("no template file provided") + } + + templatePath, err := homedir.Expand(e.cfg.TemplatePath) + if err != nil { + return fmt.Errorf("unable to expand path %s", e.cfg.TemplatePath) + } + + templateContents, err := os.ReadFile(templatePath) + if err != nil { + return fmt.Errorf("unable to get template content: %w", err) + } + + tmpl, err := template.New(templatePath).Funcs(e.funcMap).Parse(string(templateContents)) + if err != nil { + return fmt.Errorf("unable to parse template: %w", err) + } + + doc := syftjson.ToFormatModel(s) + return tmpl.Execute(writer, doc) +} diff --git a/syft/format/template/encoder_test.go b/syft/format/template/encoder_test.go new file mode 100644 index 00000000000..976659e9f56 --- /dev/null +++ b/syft/format/template/encoder_test.go @@ -0,0 +1,55 @@ +package template + +import ( + "flag" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/internal/testutil" +) + +var updateSnapshot = flag.Bool("update-template", false, "update the *.golden files for json encoders") + +func TestFormatWithOption(t *testing.T) { + f, err := NewFormatEncoder(EncoderConfig{ + TemplatePath: "test-fixtures/csv.template", + }) + require.NoError(t, err) + + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, t.TempDir()), + Format: f, + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + }, + ) +} + +func TestFormatWithOptionAndHasField(t *testing.T) { + f, err := NewFormatEncoder(EncoderConfig{ + TemplatePath: "test-fixtures/csv-hasField.template", + }) + require.NoError(t, err) + + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInputWithAuthorField(t), + Format: f, + UpdateSnapshot: *updateSnapshot, + PersistRedactionsInSnapshot: true, + IsJSON: false, + }, + ) + +} + +func TestFormatWithoutOptions(t *testing.T) { + f, err := NewFormatEncoder(DefaultEncoderConfig()) + require.NoError(t, err) + err = f.Encode(nil, testutil.DirectoryInput(t, t.TempDir())) + assert.ErrorContains(t, err, "no template file provided") +} diff --git a/syft/formats/template/test-fixtures/csv-hasField.template b/syft/format/template/test-fixtures/csv-hasField.template similarity index 100% rename from syft/formats/template/test-fixtures/csv-hasField.template rename to syft/format/template/test-fixtures/csv-hasField.template diff --git a/syft/formats/template/test-fixtures/csv.template b/syft/format/template/test-fixtures/csv.template similarity index 100% rename from syft/formats/template/test-fixtures/csv.template rename to syft/format/template/test-fixtures/csv.template diff --git a/syft/formats/template/test-fixtures/snapshot/TestFormatWithOption.golden b/syft/format/template/test-fixtures/snapshot/TestFormatWithOption.golden similarity index 100% rename from syft/formats/template/test-fixtures/snapshot/TestFormatWithOption.golden rename to syft/format/template/test-fixtures/snapshot/TestFormatWithOption.golden diff --git a/syft/formats/template/test-fixtures/snapshot/TestFormatWithOptionAndHasField.golden b/syft/format/template/test-fixtures/snapshot/TestFormatWithOptionAndHasField.golden similarity index 100% rename from syft/formats/template/test-fixtures/snapshot/TestFormatWithOptionAndHasField.golden rename to syft/format/template/test-fixtures/snapshot/TestFormatWithOptionAndHasField.golden diff --git a/syft/formats/test-fixtures/alpine-syft.json b/syft/format/test-fixtures/alpine-syft.json similarity index 100% rename from syft/formats/test-fixtures/alpine-syft.json rename to syft/format/test-fixtures/alpine-syft.json diff --git a/syft/formats/text/encoder.go b/syft/format/text/encoder.go similarity index 70% rename from syft/formats/text/encoder.go rename to syft/format/text/encoder.go index 1c19084d5b3..acf98ffcfad 100644 --- a/syft/formats/text/encoder.go +++ b/syft/format/text/encoder.go @@ -9,10 +9,33 @@ import ( "github.com/anchore/syft/syft/source" ) -func encoder(output io.Writer, s sbom.SBOM) error { +const ID sbom.FormatID = "syft-text" + +type encoder struct { +} + +func NewFormatEncoder() sbom.FormatEncoder { + return encoder{} +} + +func (e encoder) ID() sbom.FormatID { + return ID +} + +func (e encoder) Aliases() []string { + return []string{ + "text", + } +} + +func (e encoder) Version() string { + return sbom.AnyVersion +} + +func (e encoder) Encode(writer io.Writer, s sbom.SBOM) error { // init the tabular writer w := new(tabwriter.Writer) - w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight) + w.Init(writer, 0, 8, 0, '\t', tabwriter.AlignRight) switch metadata := s.Source.Metadata.(type) { case source.DirectorySourceMetadata: @@ -47,7 +70,7 @@ func encoder(output io.Writer, s sbom.SBOM) error { } if rows == 0 { - fmt.Fprintln(output, "No packages discovered") + fmt.Fprintln(writer, "No packages discovered") return nil } diff --git a/syft/formats/text/encoder_test.go b/syft/format/text/encoder_test.go similarity index 58% rename from syft/formats/text/encoder_test.go rename to syft/format/text/encoder_test.go index 4a52d28344f..2a11f1eb4b1 100644 --- a/syft/formats/text/encoder_test.go +++ b/syft/format/text/encoder_test.go @@ -4,7 +4,7 @@ import ( "flag" "testing" - "github.com/anchore/syft/syft/formats/internal/testutils" + "github.com/anchore/syft/syft/format/internal/testutil" ) var updateSnapshot = flag.Bool("update-text", false, "update the *.golden files for text encoder") @@ -12,10 +12,10 @@ var updateImage = flag.Bool("update-image", false, "update the golden image used func TestTextDirectoryEncoder(t *testing.T) { dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), + testutil.AssertEncoderAgainstGoldenSnapshot(t, + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.DirectoryInput(t, dir), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: false, @@ -26,14 +26,14 @@ func TestTextDirectoryEncoder(t *testing.T) { func TestTextImageEncoder(t *testing.T) { testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ + testutil.AssertEncoderAgainstGoldenImageSnapshot(t, + testutil.ImageSnapshotTestConfig{ Image: testImage, UpdateImageSnapshot: *updateImage, }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage, testutils.FromSnapshot()), - Format: Format(), + testutil.EncoderSnapshotTestConfig{ + Subject: testutil.ImageInput(t, testImage, testutil.FromSnapshot()), + Format: NewFormatEncoder(), UpdateSnapshot: *updateSnapshot, PersistRedactionsInSnapshot: true, IsJSON: false, @@ -42,7 +42,7 @@ func TestTextImageEncoder(t *testing.T) { ) } -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). +func redactor(values ...string) testutil.Redactor { + return testutil.NewRedactions(). WithValuesRedacted(values...) } diff --git a/syft/formats/cyclonedxxml/test-fixtures/image-simple/Dockerfile b/syft/format/text/test-fixtures/image-simple/Dockerfile similarity index 100% rename from syft/formats/cyclonedxxml/test-fixtures/image-simple/Dockerfile rename to syft/format/text/test-fixtures/image-simple/Dockerfile diff --git a/syft/formats/cyclonedxxml/test-fixtures/image-simple/file-1.txt b/syft/format/text/test-fixtures/image-simple/file-1.txt similarity index 100% rename from syft/formats/cyclonedxxml/test-fixtures/image-simple/file-1.txt rename to syft/format/text/test-fixtures/image-simple/file-1.txt diff --git a/syft/formats/cyclonedxxml/test-fixtures/image-simple/file-2.txt b/syft/format/text/test-fixtures/image-simple/file-2.txt similarity index 100% rename from syft/formats/cyclonedxxml/test-fixtures/image-simple/file-2.txt rename to syft/format/text/test-fixtures/image-simple/file-2.txt diff --git a/syft/formats/text/test-fixtures/snapshot/TestTextDirectoryEncoder.golden b/syft/format/text/test-fixtures/snapshot/TestTextDirectoryEncoder.golden similarity index 100% rename from syft/formats/text/test-fixtures/snapshot/TestTextDirectoryEncoder.golden rename to syft/format/text/test-fixtures/snapshot/TestTextDirectoryEncoder.golden diff --git a/syft/formats/text/test-fixtures/snapshot/TestTextImageEncoder.golden b/syft/format/text/test-fixtures/snapshot/TestTextImageEncoder.golden similarity index 100% rename from syft/formats/text/test-fixtures/snapshot/TestTextImageEncoder.golden rename to syft/format/text/test-fixtures/snapshot/TestTextImageEncoder.golden diff --git a/syft/formats/text/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/format/text/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden similarity index 100% rename from syft/formats/text/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden rename to syft/format/text/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden diff --git a/syft/formats.go b/syft/formats.go deleted file mode 100644 index 71dc8ebfaa3..00000000000 --- a/syft/formats.go +++ /dev/null @@ -1,49 +0,0 @@ -package syft - -import ( - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/cyclonedxjson" - "github.com/anchore/syft/syft/formats/cyclonedxxml" - "github.com/anchore/syft/syft/formats/github" - "github.com/anchore/syft/syft/formats/spdxjson" - "github.com/anchore/syft/syft/formats/spdxtagvalue" - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" - "github.com/anchore/syft/syft/formats/text" - "github.com/anchore/syft/syft/sbom" -) - -// these have been exported for the benefit of API users -// TODO: deprecated: now that the formats package has been moved to syft/formats, will be removed in v1.0.0 -const ( - JSONFormatID = syftjson.ID - TextFormatID = text.ID - TableFormatID = table.ID - CycloneDxXMLFormatID = cyclonedxxml.ID - CycloneDxJSONFormatID = cyclonedxjson.ID - GitHubFormatID = github.ID - SPDXTagValueFormatID = spdxtagvalue.ID - SPDXJSONFormatID = spdxjson.ID - TemplateFormatID = template.ID -) - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func FormatIDs() (ids []sbom.FormatID) { - return formats.AllIDs() -} - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func FormatByID(id sbom.FormatID) sbom.Format { - return formats.ByNameAndVersion(string(id), "") -} - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func FormatByName(name string) sbom.Format { - return formats.ByName(name) -} - -// TODO: deprecated, moved to syft/formats/formats.go. will be removed in v1.0.0 -func IdentifyFormat(by []byte) sbom.Format { - return formats.Identify(by) -} diff --git a/syft/formats/cyclonedxjson/decoder_test.go b/syft/formats/cyclonedxjson/decoder_test.go deleted file mode 100644 index f969732a160..00000000000 --- a/syft/formats/cyclonedxjson/decoder_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package cyclonedxjson - -import ( - "fmt" - "os" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -func Test_decodeJSON(t *testing.T) { - tests := []struct { - file string - err bool - distro string - packages []string - }{ - { - file: "snapshot/TestCycloneDxDirectoryEncoder.golden", - distro: "debian:1.2.3", - packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, - }, - { - file: "snapshot/TestCycloneDxImageEncoder.golden", - distro: "debian:1.2.3", - packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, - }, - { - file: "image-simple/Dockerfile", - err: true, - }, - } - for _, test := range tests { - t.Run(test.file, func(t *testing.T) { - reader, err := os.Open("test-fixtures/" + test.file) - assert.NoError(t, err) - - if test.err { - err = Format().Validate(reader) - assert.Error(t, err) - return - } - - bom, err := Format().Decode(reader) - - assert.NoError(t, err) - - split := strings.SplitN(test.distro, ":", 2) - name := split[0] - version := split[1] - assert.Equal(t, bom.Artifacts.LinuxDistribution.ID, name) - assert.Equal(t, bom.Artifacts.LinuxDistribution.Version, version) - - pkgs: - for _, pkg := range test.packages { - split = strings.SplitN(pkg, ":", 2) - name = split[0] - version = split[1] - for p := range bom.Artifacts.Packages.Enumerate() { - if p.Name == name { - assert.Equal(t, version, p.Version) - continue pkgs - } - } - assert.Fail(t, fmt.Sprintf("package should be present: %s", pkg)) - } - }) - } -} diff --git a/syft/formats/cyclonedxjson/encoder.go b/syft/formats/cyclonedxjson/encoder.go deleted file mode 100644 index 297b8026580..00000000000 --- a/syft/formats/cyclonedxjson/encoder.go +++ /dev/null @@ -1,48 +0,0 @@ -package cyclonedxjson - -import ( - "io" - - "github.com/CycloneDX/cyclonedx-go" - - "github.com/anchore/syft/syft/formats/common/cyclonedxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func encoderV1_0(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_0) -} - -func encoderV1_1(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_1) -} - -func encoderV1_2(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_2) -} - -func encoderV1_3(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_3) -} - -func encoderV1_4(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_4) -} - -func encoderV1_5(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_5) -} - -func buildEncoder(output io.Writer, s sbom.SBOM) (cyclonedx.BOMEncoder, *cyclonedx.BOM) { - bom := cyclonedxhelpers.ToFormatModel(s) - enc := cyclonedx.NewBOMEncoder(output, cyclonedx.BOMFileFormatJSON) - enc.SetPretty(true) - enc.SetEscapeHTML(false) - return enc, bom -} diff --git a/syft/formats/cyclonedxjson/encoder_test.go b/syft/formats/cyclonedxjson/encoder_test.go deleted file mode 100644 index dec29164e06..00000000000 --- a/syft/formats/cyclonedxjson/encoder_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package cyclonedxjson - -import ( - "flag" - "testing" - - "github.com/anchore/syft/syft/formats/internal/testutils" -) - -var updateSnapshot = flag.Bool("update-cyclonedx-json", false, "update the *.golden files for cyclone-dx JSON encoders") -var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") - -func TestCycloneDxDirectoryEncoder(t *testing.T) { - dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: true, - Redactor: redactor(dir), - }, - ) -} - -func TestCycloneDxImageEncoder(t *testing.T) { - testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: true, - Redactor: redactor(), - }, - ) -} - -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). - WithValuesRedacted(values...). - WithPatternRedactors( - map[string]string{ - // UUIDs - `urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`: `urn:uuid:redacted`, - - // timestamps - `([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`: `timestamp:redacted`, - - // image hashes - `sha256:[A-Fa-f0-9]{64}`: `sha256:redacted`, - - // BOM refs - `"bom-ref":\s*"[^"]+"`: `"bom-ref":"redacted"`, - }, - ) -} diff --git a/syft/formats/cyclonedxjson/format.go b/syft/formats/cyclonedxjson/format.go deleted file mode 100644 index 97a088aaea7..00000000000 --- a/syft/formats/cyclonedxjson/format.go +++ /dev/null @@ -1,72 +0,0 @@ -package cyclonedxjson - -import ( - "github.com/CycloneDX/cyclonedx-go" - - "github.com/anchore/syft/syft/formats/common/cyclonedxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "cyclonedx-json" - -var Format = Format1_4 - -func Format1_0() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_0.String(), - encoderV1_0, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} - -func Format1_1() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_1.String(), - encoderV1_1, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} - -func Format1_2() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_2.String(), - encoderV1_2, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} - -func Format1_3() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_3.String(), - encoderV1_3, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} - -func Format1_4() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_4.String(), - encoderV1_4, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} - -func Format1_5() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_5.String(), - encoderV1_5, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatJSON), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatJSON), - ID, - ) -} diff --git a/syft/formats/cyclonedxjson/format_test.go b/syft/formats/cyclonedxjson/format_test.go deleted file mode 100644 index 2cfd5e54504..00000000000 --- a/syft/formats/cyclonedxjson/format_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package cyclonedxjson - -import ( - "testing" - - "github.com/CycloneDX/cyclonedx-go" -) - -func TestFormatVersions(t *testing.T) { - tests := []struct { - name string - expectedVersion string - }{ - { - - "cyclonedx-json should default to v1.4", - cyclonedx.SpecVersion1_4.String(), - }, - } - - for _, c := range tests { - c := c - t.Run(c.name, func(t *testing.T) { - sbomFormat := Format() - if sbomFormat.ID() != ID { - t.Errorf("expected ID %q, got %q", ID, sbomFormat.ID()) - } - - if sbomFormat.Version() != c.expectedVersion { - t.Errorf("expected version %q, got %q", c.expectedVersion, sbomFormat.Version()) - } - }) - } -} diff --git a/syft/formats/cyclonedxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/formats/cyclonedxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden deleted file mode 100644 index f4aa1e7bb74..00000000000 Binary files a/syft/formats/cyclonedxjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden and /dev/null differ diff --git a/syft/formats/cyclonedxxml/decoder_test.go b/syft/formats/cyclonedxxml/decoder_test.go deleted file mode 100644 index c0ab823ac46..00000000000 --- a/syft/formats/cyclonedxxml/decoder_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package cyclonedxxml - -import ( - "fmt" - "os" - "strings" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func Test_decodeXML(t *testing.T) { - tests := []struct { - file string - err bool - distro string - packages []string - }{ - { - file: "snapshot/TestCycloneDxDirectoryEncoder.golden", - distro: "debian:1.2.3", - packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, - }, - { - file: "snapshot/TestCycloneDxImageEncoder.golden", - distro: "debian:1.2.3", - packages: []string{"package-1:1.0.1", "package-2:2.0.1"}, - }, - { - file: "image-simple/Dockerfile", - err: true, - }, - } - for _, test := range tests { - t.Run(test.file, func(t *testing.T) { - reader, err := os.Open("test-fixtures/" + test.file) - require.NoError(t, err) - - if test.err { - err = Format().Validate(reader) - assert.Error(t, err) - return - } - - bom, err := Format().Decode(reader) - - require.NoError(t, err) - - split := strings.SplitN(test.distro, ":", 2) - name := split[0] - version := split[1] - assert.Equal(t, bom.Artifacts.LinuxDistribution.ID, name) - assert.Equal(t, bom.Artifacts.LinuxDistribution.Version, version) - - pkgs: - for _, pkg := range test.packages { - split = strings.SplitN(pkg, ":", 2) - name = split[0] - version = split[1] - for p := range bom.Artifacts.Packages.Enumerate() { - if p.Name == name { - assert.Equal(t, version, p.Version) - continue pkgs - } - } - assert.Fail(t, fmt.Sprintf("package should be present: %s", pkg)) - } - }) - } -} diff --git a/syft/formats/cyclonedxxml/encoder.go b/syft/formats/cyclonedxxml/encoder.go deleted file mode 100644 index 3941feca00b..00000000000 --- a/syft/formats/cyclonedxxml/encoder.go +++ /dev/null @@ -1,48 +0,0 @@ -package cyclonedxxml - -import ( - "io" - - "github.com/CycloneDX/cyclonedx-go" - - "github.com/anchore/syft/syft/formats/common/cyclonedxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func encoderV1_0(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_0) -} - -func encoderV1_1(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_1) -} - -func encoderV1_2(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_2) -} - -func encoderV1_3(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_3) -} - -func encoderV1_4(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_4) -} - -func encoderV1_5(output io.Writer, s sbom.SBOM) error { - enc, bom := buildEncoder(output, s) - return enc.EncodeVersion(bom, cyclonedx.SpecVersion1_5) -} - -func buildEncoder(output io.Writer, s sbom.SBOM) (cyclonedx.BOMEncoder, *cyclonedx.BOM) { - bom := cyclonedxhelpers.ToFormatModel(s) - enc := cyclonedx.NewBOMEncoder(output, cyclonedx.BOMFileFormatXML) - enc.SetPretty(true) - enc.SetEscapeHTML(false) - return enc, bom -} diff --git a/syft/formats/cyclonedxxml/encoder_test.go b/syft/formats/cyclonedxxml/encoder_test.go deleted file mode 100644 index 17e1eb5974d..00000000000 --- a/syft/formats/cyclonedxxml/encoder_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package cyclonedxxml - -import ( - "flag" - "testing" - - "github.com/anchore/syft/syft/formats/internal/testutils" -) - -var updateSnapshot = flag.Bool("update-cyclonedx-xml", false, "update the *.golden files for cyclone-dx XML encoders") -var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") - -func TestCycloneDxDirectoryEncoder(t *testing.T) { - dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(dir), - }, - ) -} - -func TestCycloneDxImageEncoder(t *testing.T) { - testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(), - }, - ) -} - -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). - WithValuesRedacted(values...). - WithPatternRedactors( - map[string]string{ - // serial numbers - `serialNumber="[a-zA-Z0-9\-:]+`: `serialNumber="redacted`, - - // dates - `([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`: `redacted`, - - // image hashes - `sha256:[A-Za-z0-9]{64}`: `sha256:redacted`, - - // BOM refs - `bom-ref="[a-zA-Z0-9\-:]+"`: `bom-ref="redacted"`, - }, - ) -} diff --git a/syft/formats/cyclonedxxml/format.go b/syft/formats/cyclonedxxml/format.go deleted file mode 100644 index 1b22cee1476..00000000000 --- a/syft/formats/cyclonedxxml/format.go +++ /dev/null @@ -1,72 +0,0 @@ -package cyclonedxxml - -import ( - "github.com/CycloneDX/cyclonedx-go" - - "github.com/anchore/syft/syft/formats/common/cyclonedxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "cyclonedx-xml" - -var Format = Format1_4 - -func Format1_0() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_0.String(), - encoderV1_0, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} - -func Format1_1() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_1.String(), - encoderV1_1, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} - -func Format1_2() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_2.String(), - encoderV1_2, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} - -func Format1_3() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_3.String(), - encoderV1_3, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} - -func Format1_4() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_4.String(), - encoderV1_4, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} - -func Format1_5() sbom.Format { - return sbom.NewFormat( - cyclonedx.SpecVersion1_5.String(), - encoderV1_5, - cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML), - cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML), - ID, "cyclonedx", "cyclone", - ) -} diff --git a/syft/formats/cyclonedxxml/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/formats/cyclonedxxml/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden deleted file mode 100644 index f4aa1e7bb74..00000000000 Binary files a/syft/formats/cyclonedxxml/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden and /dev/null differ diff --git a/syft/formats/formats.go b/syft/formats/formats.go deleted file mode 100644 index e18b1a852d4..00000000000 --- a/syft/formats/formats.go +++ /dev/null @@ -1,157 +0,0 @@ -package formats - -import ( - "bytes" - "errors" - "fmt" - "io" - "regexp" - "slices" - "strings" - - "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/syft/formats/cyclonedxjson" - "github.com/anchore/syft/syft/formats/cyclonedxxml" - "github.com/anchore/syft/syft/formats/github" - "github.com/anchore/syft/syft/formats/spdxjson" - "github.com/anchore/syft/syft/formats/spdxtagvalue" - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" - "github.com/anchore/syft/syft/formats/text" - "github.com/anchore/syft/syft/sbom" -) - -func Formats() []sbom.Format { - return []sbom.Format{ - syftjson.Format(), - github.Format(), - table.Format(), - text.Format(), - template.Format(), - cyclonedxxml.Format1_0(), - cyclonedxxml.Format1_1(), - cyclonedxxml.Format1_2(), - cyclonedxxml.Format1_3(), - cyclonedxxml.Format1_4(), - cyclonedxxml.Format1_5(), - cyclonedxjson.Format1_0(), - cyclonedxjson.Format1_1(), - cyclonedxjson.Format1_2(), - cyclonedxjson.Format1_3(), - cyclonedxjson.Format1_4(), - cyclonedxjson.Format1_5(), - spdxtagvalue.Format2_1(), - spdxtagvalue.Format2_2(), - spdxtagvalue.Format2_3(), - spdxjson.Format2_2(), - spdxjson.Format2_3(), - } -} - -func Identify(by []byte) sbom.Format { - for _, f := range Formats() { - if err := f.Validate(bytes.NewReader(by)); err != nil { - if !errors.Is(err, sbom.ErrValidationNotSupported) { - log.WithFields("error", err).Tracef("format validation for %s failed", f.ID()) - } - continue - } - return f - } - return nil -} - -// ByName accepts a name@version string, such as: -// -// spdx-json@2.1 or cyclonedx@1.5 -func ByName(name string) sbom.Format { - parts := strings.SplitN(name, "@", 2) - version := sbom.AnyVersion - if len(parts) > 1 { - version = parts[1] - } - return ByNameAndVersion(parts[0], version) -} - -func ByNameAndVersion(name string, version string) sbom.Format { - name = cleanFormatName(name) - var mostRecentFormat sbom.Format - for _, f := range Formats() { - for _, n := range f.IDs() { - if cleanFormatName(string(n)) == name && versionMatches(f.Version(), version) { - // if the version is not specified and the format is cyclonedx, then we want to return the most recent version up to 1.4 - // If more aliases like cdx are added this will not catch those - we want to eventually provide a way for - // formats to inform this function what their default version is - // TODO: remove this check when 1.5 is stable or default formats are designed. PR below should be merged. - // https://github.com/CycloneDX/cyclonedx-go/pull/90 - if version == sbom.AnyVersion && strings.Contains(string(n), "cyclone") { - if f.Version() == "1.5" { - continue - } - } - if mostRecentFormat == nil || f.Version() > mostRecentFormat.Version() { - mostRecentFormat = f - } - } - } - } - return mostRecentFormat -} - -func versionMatches(version string, match string) bool { - if version == sbom.AnyVersion || match == sbom.AnyVersion { - return true - } - - match = strings.ReplaceAll(match, ".", "\\.") - match = strings.ReplaceAll(match, "*", ".*") - match = fmt.Sprintf("^%s(\\..*)*$", match) - matcher, err := regexp.Compile(match) - if err != nil { - return false - } - return matcher.MatchString(version) -} - -func cleanFormatName(name string) string { - r := strings.NewReplacer("-", "", "_", "") - return strings.ToLower(r.Replace(name)) -} - -// Encode takes all SBOM elements and a format option and encodes an SBOM document. -func Encode(s sbom.SBOM, f sbom.Format) ([]byte, error) { - buff := bytes.Buffer{} - - if err := f.Encode(&buff, s); err != nil { - return nil, fmt.Errorf("unable to encode sbom: %w", err) - } - - return buff.Bytes(), nil -} - -// Decode takes a reader for an SBOM and generates all internal SBOM elements. -func Decode(reader io.Reader) (*sbom.SBOM, sbom.Format, error) { - by, err := io.ReadAll(reader) - if err != nil { - return nil, nil, fmt.Errorf("unable to read sbom: %w", err) - } - - f := Identify(by) - if f == nil { - return nil, nil, fmt.Errorf("unable to identify format") - } - - s, err := f.Decode(bytes.NewReader(by)) - return s, f, err -} - -func AllIDs() (ids []sbom.FormatID) { - for _, f := range Formats() { - if slices.Contains(ids, f.ID()) { - continue - } - ids = append(ids, f.ID()) - } - return ids -} diff --git a/syft/formats/formats_test.go b/syft/formats/formats_test.go deleted file mode 100644 index 2cdc99b8a52..00000000000 --- a/syft/formats/formats_test.go +++ /dev/null @@ -1,292 +0,0 @@ -package formats - -import ( - "bytes" - "io" - "os" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/anchore/syft/syft/formats/cyclonedxjson" - "github.com/anchore/syft/syft/formats/cyclonedxxml" - "github.com/anchore/syft/syft/formats/github" - "github.com/anchore/syft/syft/formats/spdxjson" - "github.com/anchore/syft/syft/formats/spdxtagvalue" - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/formats/table" - "github.com/anchore/syft/syft/formats/template" - "github.com/anchore/syft/syft/formats/text" - "github.com/anchore/syft/syft/sbom" -) - -func TestIdentify(t *testing.T) { - tests := []struct { - fixture string - expected sbom.FormatID - }{ - { - fixture: "test-fixtures/alpine-syft.json", - expected: syftjson.ID, - }, - } - for _, test := range tests { - t.Run(test.fixture, func(t *testing.T) { - f, err := os.Open(test.fixture) - assert.NoError(t, err) - by, err := io.ReadAll(f) - assert.NoError(t, err) - frmt := Identify(by) - assert.NotNil(t, frmt) - assert.Equal(t, test.expected, frmt.ID()) - }) - } -} - -func TestFormats_EmptyInput(t *testing.T) { - for _, format := range Formats() { - t.Run(format.ID().String(), func(t *testing.T) { - t.Run("format.Decode", func(t *testing.T) { - input := bytes.NewReader(nil) - - assert.NotPanics(t, func() { - decodedSBOM, err := format.Decode(input) - assert.Error(t, err) - assert.Nil(t, decodedSBOM) - }) - }) - - t.Run("format.Validate", func(t *testing.T) { - input := bytes.NewReader(nil) - - assert.NotPanics(t, func() { - err := format.Validate(input) - assert.Error(t, err) - }) - }) - }) - } -} - -func TestByName(t *testing.T) { - tests := []struct { - name string - want sbom.FormatID - }{ - // SPDX Tag-Value - { - name: "spdx", - want: spdxtagvalue.ID, - }, - { - name: "spdx-tag-value", - want: spdxtagvalue.ID, - }, - { - name: "spdx-tv", - want: spdxtagvalue.ID, - }, - { - name: "spdxtv", // clean variant - want: spdxtagvalue.ID, - }, - - // SPDX JSON - { - name: "spdx-json", - want: spdxjson.ID, - }, - { - name: "spdxjson", // clean variant - want: spdxjson.ID, - }, - - // Cyclonedx JSON - { - name: "cyclonedx-json", - want: cyclonedxjson.ID, - }, - { - name: "cyclonedxjson", // clean variant - want: cyclonedxjson.ID, - }, - - // Cyclonedx XML - { - name: "cyclonedx", - want: cyclonedxxml.ID, - }, - { - name: "cyclonedx-xml", - want: cyclonedxxml.ID, - }, - { - name: "cyclonedxxml", // clean variant - want: cyclonedxxml.ID, - }, - - // Syft Table - { - name: "table", - want: table.ID, - }, - { - name: "syft-table", - want: table.ID, - }, - - // Syft Text - { - name: "text", - want: text.ID, - }, - { - name: "syft-text", - want: text.ID, - }, - - // Syft JSON - { - name: "json", - want: syftjson.ID, - }, - { - name: "syft-json", - want: syftjson.ID, - }, - { - name: "syftjson", // clean variant - want: syftjson.ID, - }, - - // GitHub JSON - { - name: "github", - want: github.ID, - }, - { - name: "github-json", - want: github.ID, - }, - - // Syft template - { - name: "template", - want: template.ID, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - f := ByName(tt.name) - if tt.want == "" { - require.Nil(t, f) - return - } - require.NotNil(t, f) - assert.Equal(t, tt.want, f.ID()) - }) - } -} - -func Test_versionMatches(t *testing.T) { - tests := []struct { - name string - version string - match string - matches bool - }{ - { - name: "any version matches number", - version: string(sbom.AnyVersion), - match: "6", - matches: true, - }, - { - name: "number matches any version", - version: "6", - match: string(sbom.AnyVersion), - matches: true, - }, - { - name: "same number matches", - version: "3", - match: "3", - matches: true, - }, - { - name: "same major number matches", - version: "3.1", - match: "3", - matches: true, - }, - { - name: "same minor number matches", - version: "3.1", - match: "3.1", - matches: true, - }, - { - name: "wildcard-version matches minor", - version: "7.1.3", - match: "7.*", - matches: true, - }, - { - name: "wildcard-version matches patch", - version: "7.4.8", - match: "7.4.*", - matches: true, - }, - { - name: "sub-version matches major", - version: "7.19.11", - match: "7", - matches: true, - }, - { - name: "sub-version matches minor", - version: "7.55.2", - match: "7.55", - matches: true, - }, - { - name: "sub-version matches patch", - version: "7.32.6", - match: "7.32.6", - matches: true, - }, - // negative tests - { - name: "different number does not match", - version: "3", - match: "4", - matches: false, - }, - { - name: "sub-version doesn't match major", - version: "7.2.5", - match: "8.2.5", - matches: false, - }, - { - name: "sub-version doesn't match minor", - version: "7.2.9", - match: "7.1", - matches: false, - }, - { - name: "sub-version doesn't match patch", - version: "7.32.6", - match: "7.32.5", - matches: false, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - matches := versionMatches(test.version, test.match) - assert.Equal(t, test.matches, matches) - }) - } -} diff --git a/syft/formats/github/format.go b/syft/formats/github/format.go deleted file mode 100644 index 4e3946716f0..00000000000 --- a/syft/formats/github/format.go +++ /dev/null @@ -1,28 +0,0 @@ -package github - -import ( - "encoding/json" - "io" - - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "github-json" - -func Format() sbom.Format { - return sbom.NewFormat( - sbom.AnyVersion, - func(writer io.Writer, sbom sbom.SBOM) error { - bom := toGithubModel(&sbom) - - encoder := json.NewEncoder(writer) - encoder.SetEscapeHTML(false) - encoder.SetIndent("", " ") - - return encoder.Encode(bom) - }, - nil, - nil, - ID, "github", - ) -} diff --git a/syft/formats/spdxjson/decoder.go b/syft/formats/spdxjson/decoder.go deleted file mode 100644 index 28a477a18cf..00000000000 --- a/syft/formats/spdxjson/decoder.go +++ /dev/null @@ -1,20 +0,0 @@ -package spdxjson - -import ( - "fmt" - "io" - - "github.com/spdx/tools-golang/json" - - "github.com/anchore/syft/syft/formats/common/spdxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func decoder(reader io.Reader) (s *sbom.SBOM, err error) { - doc, err := json.Read(reader) - if err != nil { - return nil, fmt.Errorf("unable to decode spdx-json: %w", err) - } - - return spdxhelpers.ToSyftModel(doc) -} diff --git a/syft/formats/spdxjson/decoder_test.go b/syft/formats/spdxjson/decoder_test.go deleted file mode 100644 index 12f88481572..00000000000 --- a/syft/formats/spdxjson/decoder_test.go +++ /dev/null @@ -1,105 +0,0 @@ -package spdxjson - -import ( - "fmt" - "os" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/anchore/syft/syft/pkg" -) - -func TestSPDXJSONDecoder(t *testing.T) { - tests := []struct { - path string - fail bool - packages []string - relationships []string - }{ - { - path: "alpine-3.10.syft.spdx.json", - packages: []string{"busybox", "libssl1.1", "ssl_client"}, - relationships: []string{"busybox", "busybox", "libssl1.1", "libssl1.1"}, - }, - { - path: "alpine-3.10.vendor.spdx.json", - packages: []string{"alpine", "busybox", "ssl_client"}, - relationships: []string{}, - }, - { - path: "example7-bin.spdx.json", - }, - { - path: "example7-go-module.spdx.json", - }, - { - path: "example7-golang.spdx.json", - }, - { - path: "example7-third-party-modules.spdx.json", - }, - { - path: "bad/example7-bin.spdx.json", - fail: true, - }, - { - path: "bad/example7-go-module.spdx.json", - fail: true, - }, - { - path: "bad/example7-golang.spdx.json", - fail: true, - }, - { - path: "bad/example7-third-party-modules.spdx.json", - fail: true, - }, - } - - for _, test := range tests { - t.Run(test.path, func(t *testing.T) { - f, err := os.Open("test-fixtures/spdx/" + test.path) - require.NoError(t, err) - - sbom, err := decoder(f) - - if test.fail { - require.Error(t, err) - return - } else { - require.NoError(t, err) - } - - if test.packages != nil { - assert.Equal(t, sbom.Artifacts.Packages.PackageCount(), len(test.packages)) - - packages: - for _, pkgName := range test.packages { - for _, p := range sbom.Artifacts.Packages.Sorted() { - if p.Name == pkgName { - continue packages - } - } - assert.NoError(t, fmt.Errorf("Unable to find package: %s", pkgName)) - } - } - - if test.relationships != nil { - assert.Len(t, sbom.Relationships, len(test.relationships)) - - relationships: - for _, pkgName := range test.relationships { - for _, rel := range sbom.Relationships { - p, ok := rel.From.(pkg.Package) - if ok && p.Name == pkgName { - continue relationships - } - } - assert.NoError(t, fmt.Errorf("Unable to find relationship: %s", pkgName)) - } - } - }) - } -} diff --git a/syft/formats/spdxjson/encoder.go b/syft/formats/spdxjson/encoder.go deleted file mode 100644 index 8a2c89f7262..00000000000 --- a/syft/formats/spdxjson/encoder.go +++ /dev/null @@ -1,38 +0,0 @@ -package spdxjson - -import ( - "encoding/json" - "io" - - "github.com/spdx/tools-golang/convert" - "github.com/spdx/tools-golang/spdx/v2/v2_2" - - "github.com/anchore/syft/syft/formats/common/spdxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func encoder2_3(output io.Writer, s sbom.SBOM) error { - doc := spdxhelpers.ToFormatModel(s) - return encodeJSON(output, doc) -} - -func encoder2_2(output io.Writer, s sbom.SBOM) error { - doc := spdxhelpers.ToFormatModel(s) - - var out v2_2.Document - err := convert.Document(doc, &out) - if err != nil { - return err - } - - return encodeJSON(output, out) -} - -func encodeJSON(output io.Writer, doc interface{}) error { - enc := json.NewEncoder(output) - // prevent > and < from being escaped in the payload - enc.SetEscapeHTML(false) - enc.SetIndent("", " ") - - return enc.Encode(doc) -} diff --git a/syft/formats/spdxjson/encoder_test.go b/syft/formats/spdxjson/encoder_test.go deleted file mode 100644 index b684777aa35..00000000000 --- a/syft/formats/spdxjson/encoder_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package spdxjson - -import ( - "flag" - "testing" - - "github.com/anchore/syft/syft/formats/internal/testutils" -) - -var updateSnapshot = flag.Bool("update-spdx-json", false, "update the *.golden files for spdx-json encoders") -var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") - -func TestSPDXJSONDirectoryEncoder(t *testing.T) { - dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: true, - Redactor: redactor(dir), - }, - ) -} - -func TestSPDXJSONImageEncoder(t *testing.T) { - testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage, testutils.FromSnapshot()), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: true, - Redactor: redactor(), - }, - ) -} - -func TestSPDXRelationshipOrder(t *testing.T) { - testImage := "image-simple" - - s := testutils.ImageInput(t, testImage, testutils.FromSnapshot()) - testutils.AddSampleFileRelationships(&s) - - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: s, - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: true, - Redactor: redactor(), - }, - ) -} - -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). - WithValuesRedacted(values...). - WithPatternRedactors( - map[string]string{ - // each SBOM reports the time it was generated, which is not useful during snapshot testing - `"created":\s+"[^"]*"`: `"created":"redacted"`, - - // each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing - `"documentNamespace":\s+"[^"]*"`: `"documentNamespace":"redacted"`, - - // the license list will be updated periodically, the value here should not be directly tested in snapshot tests - `"licenseListVersion":\s+"[^"]*"`: `"licenseListVersion":"redacted"`, - }, - ) -} diff --git a/syft/formats/spdxjson/format.go b/syft/formats/spdxjson/format.go deleted file mode 100644 index 26d543e45a5..00000000000 --- a/syft/formats/spdxjson/format.go +++ /dev/null @@ -1,33 +0,0 @@ -package spdxjson - -import ( - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "spdx-json" - -var IDs = []sbom.FormatID{ID} - -// note: this format is LOSSY relative to the syftjson format - -func Format2_2() sbom.Format { - return sbom.NewFormat( - "2.2", - encoder2_2, - decoder, - validator, - IDs..., - ) -} - -func Format2_3() sbom.Format { - return sbom.NewFormat( - "2.3", - encoder2_3, - decoder, - validator, - IDs..., - ) -} - -var Format = Format2_3 diff --git a/syft/formats/spdxjson/test-fixtures/image-simple/Dockerfile b/syft/formats/spdxjson/test-fixtures/image-simple/Dockerfile deleted file mode 100644 index 79cfa759e35..00000000000 --- a/syft/formats/spdxjson/test-fixtures/image-simple/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one. -FROM scratch -ADD file-1.txt /somefile-1.txt -ADD file-2.txt /somefile-2.txt diff --git a/syft/formats/spdxjson/test-fixtures/image-simple/file-1.txt b/syft/formats/spdxjson/test-fixtures/image-simple/file-1.txt deleted file mode 100644 index 985d3408e98..00000000000 --- a/syft/formats/spdxjson/test-fixtures/image-simple/file-1.txt +++ /dev/null @@ -1 +0,0 @@ -this file has contents \ No newline at end of file diff --git a/syft/formats/spdxjson/test-fixtures/image-simple/file-2.txt b/syft/formats/spdxjson/test-fixtures/image-simple/file-2.txt deleted file mode 100644 index 396d08bbc72..00000000000 --- a/syft/formats/spdxjson/test-fixtures/image-simple/file-2.txt +++ /dev/null @@ -1 +0,0 @@ -file-2 contents! \ No newline at end of file diff --git a/syft/formats/spdxjson/validator.go b/syft/formats/spdxjson/validator.go deleted file mode 100644 index 774309068ac..00000000000 --- a/syft/formats/spdxjson/validator.go +++ /dev/null @@ -1,10 +0,0 @@ -package spdxjson - -import ( - "io" -) - -func validator(reader io.Reader) error { - _, err := decoder(reader) - return err -} diff --git a/syft/formats/spdxtagvalue/decoder.go b/syft/formats/spdxtagvalue/decoder.go deleted file mode 100644 index 44ad09a7b2f..00000000000 --- a/syft/formats/spdxtagvalue/decoder.go +++ /dev/null @@ -1,20 +0,0 @@ -package spdxtagvalue - -import ( - "fmt" - "io" - - "github.com/spdx/tools-golang/tagvalue" - - "github.com/anchore/syft/syft/formats/common/spdxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func decoder(reader io.Reader) (*sbom.SBOM, error) { - doc, err := tagvalue.Read(reader) - if err != nil { - return nil, fmt.Errorf("unable to decode spdx-tag-value: %w", err) - } - - return spdxhelpers.ToSyftModel(doc) -} diff --git a/syft/formats/spdxtagvalue/decoder_test.go b/syft/formats/spdxtagvalue/decoder_test.go deleted file mode 100644 index 6802e1b5cd2..00000000000 --- a/syft/formats/spdxtagvalue/decoder_test.go +++ /dev/null @@ -1,96 +0,0 @@ -package spdxtagvalue - -import ( - "os" - "strings" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/anchore/syft/syft/file" -) - -// TODO: this is a temporary coverage see below -// This test should be covered within the encode decode life cycle however -// we're currently blocked on a couple of SPDX fields that change often -// which causes backward compatibility issues. -// This test was added specifically to smoke test the decode function when -// It failed on a released version of syft. -func TestSPDXTagValueDecoder(t *testing.T) { - tests := []struct { - name string - fixture string - }{ - { - name: "simple", - fixture: "tag-value.spdx", - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - reader, err := os.Open("test-fixtures/" + test.fixture) - assert.NoError(t, err) - - _, err = Format().Decode(reader) - assert.NoError(t, err) - }) - } -} - -func Test_packageDirectFiles(t *testing.T) { - contents := ` -SPDXVersion: SPDX-2.2 -DataLicense: CC0-1.0 -SPDXID: SPDXRef-DOCUMENT -DocumentName: Some-SBOM -DocumentNamespace: https://example.org/some/namespace -Creator: Organization: Some-organization -Creator: Tool: Some-tool Version: 1.0 -Created: 2021-12-29T17:02:21Z -PackageName: Some-package -PackageVersion: 5.1.2 -SPDXID: SPDXRef-Package-43c51b08-cc7e-406d-8ad9-34aa292d1157 -PackageSupplier: Organization: Some-organization -PackageDownloadLocation: https://example.org/download/location -FilesAnalyzed: true -PackageLicenseInfoFromFiles: NOASSERTION -PackageVerificationCode: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 -PackageLicenseConcluded: NOASSERTION -PackageLicenseDeclared: NOASSERTION -PackageCopyrightText: NOASSERTION -PackageChecksum: SHA1: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 -FileName: Some-file-name -SPDXID: SPDXRef-99545d55-933d-4e08-9eb5-9d826111cb79 -FileContributor: Some-file-contributor -FileType: BINARY -FileChecksum: SHA1: 23460C5559C8D4DE3F6504E0E84E844CAC8B1D95 -LicenseConcluded: NOASSERTION -LicenseInfoInFile: NOASSERTION -FileCopyrightText: NOASSERTION -` - - s, err := decoder(strings.NewReader(contents)) - require.NoError(t, err) - - pkgs := s.Artifacts.Packages.Sorted() - assert.Len(t, pkgs, 1) - assert.Len(t, s.Artifacts.FileMetadata, 1) - assert.Len(t, s.Relationships, 1) - p := pkgs[0] - r := s.Relationships[0] - f := file.Location{} - for c := range s.Artifacts.FileMetadata { - f = file.Location{ - LocationData: file.LocationData{ - Coordinates: c, - VirtualPath: "", - }, - LocationMetadata: file.LocationMetadata{}, - } - break // there should only be 1 - } - assert.Equal(t, p.ID(), r.From.ID()) - assert.Equal(t, f.ID(), r.To.ID()) -} diff --git a/syft/formats/spdxtagvalue/encoder.go b/syft/formats/spdxtagvalue/encoder.go deleted file mode 100644 index 5949bfc8d52..00000000000 --- a/syft/formats/spdxtagvalue/encoder.go +++ /dev/null @@ -1,38 +0,0 @@ -package spdxtagvalue - -import ( - "io" - - "github.com/spdx/tools-golang/convert" - "github.com/spdx/tools-golang/spdx/v2/v2_1" - "github.com/spdx/tools-golang/spdx/v2/v2_2" - "github.com/spdx/tools-golang/tagvalue" - - "github.com/anchore/syft/syft/formats/common/spdxhelpers" - "github.com/anchore/syft/syft/sbom" -) - -func encoder2_3(output io.Writer, s sbom.SBOM) error { - model := spdxhelpers.ToFormatModel(s) - return tagvalue.Write(model, output) -} - -func encoder2_2(output io.Writer, s sbom.SBOM) error { - model := spdxhelpers.ToFormatModel(s) - var out v2_2.Document - err := convert.Document(model, &out) - if err != nil { - return err - } - return tagvalue.Write(out, output) -} - -func encoder2_1(output io.Writer, s sbom.SBOM) error { - model := spdxhelpers.ToFormatModel(s) - var out v2_1.Document - err := convert.Document(model, &out) - if err != nil { - return err - } - return tagvalue.Write(out, output) -} diff --git a/syft/formats/spdxtagvalue/encoder_test.go b/syft/formats/spdxtagvalue/encoder_test.go deleted file mode 100644 index e5fe4a5af2a..00000000000 --- a/syft/formats/spdxtagvalue/encoder_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package spdxtagvalue - -import ( - "flag" - "testing" - - "github.com/anchore/syft/syft/formats/internal/testutils" - "github.com/anchore/syft/syft/pkg" - "github.com/anchore/syft/syft/sbom" - "github.com/anchore/syft/syft/source" -) - -var updateSnapshot = flag.Bool("update-spdx-tv", false, "update the *.golden files for spdx-tv encoders") -var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing") - -func TestSPDXTagValueDirectoryEncoder(t *testing.T) { - dir := t.TempDir() - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, dir), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(dir), - }, - ) -} - -func TestSPDXTagValueImageEncoder(t *testing.T) { - testImage := "image-simple" - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.ImageInput(t, testImage, testutils.FromSnapshot()), - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(), - }, - ) -} - -func TestSPDXJSONSPDXIDs(t *testing.T) { - var pkgs []pkg.Package - for _, name := range []string{"some/slashes", "@at-sign", "under_scores"} { - p := pkg.Package{ - Name: name, - } - p.SetID() - pkgs = append(pkgs, p) - } - - s := sbom.SBOM{ - Artifacts: sbom.Artifacts{ - Packages: pkg.NewCollection(pkgs...), - }, - Relationships: nil, - Source: source.Description{ - Name: "foobar/baz", // in this case, foobar is used as the spdx document name - Metadata: source.DirectorySourceMetadata{}, - }, - Descriptor: sbom.Descriptor{ - Name: "syft", - Version: "v0.42.0-bogus", - Configuration: map[string]string{ - "config-key": "config-value", - }, - }, - } - - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: s, - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(), - }, - ) -} - -func TestSPDXRelationshipOrder(t *testing.T) { - testImage := "image-simple" - s := testutils.ImageInput(t, testImage, testutils.FromSnapshot()) - testutils.AddSampleFileRelationships(&s) - - testutils.AssertEncoderAgainstGoldenImageSnapshot(t, - testutils.ImageSnapshotTestConfig{ - Image: testImage, - UpdateImageSnapshot: *updateImage, - }, - testutils.EncoderSnapshotTestConfig{ - Subject: s, - Format: Format(), - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - Redactor: redactor(), - }, - ) -} - -func redactor(values ...string) testutils.Redactor { - return testutils.NewRedactions(). - WithValuesRedacted(values...). - WithPatternRedactors( - map[string]string{ - // each SBOM reports the time it was generated, which is not useful during snapshot testing - `Created: .*`: "Created: redacted", - - // each SBOM reports a unique documentNamespace when generated, this is not useful for snapshot testing - `DocumentNamespace: https://anchore.com/.*`: "DocumentNamespace: redacted", - - // the license list will be updated periodically, the value here should not be directly tested in snapshot tests - `LicenseListVersion: .*`: "LicenseListVersion: redacted", - }, - ) -} diff --git a/syft/formats/spdxtagvalue/format.go b/syft/formats/spdxtagvalue/format.go deleted file mode 100644 index 46db72d6fea..00000000000 --- a/syft/formats/spdxtagvalue/format.go +++ /dev/null @@ -1,42 +0,0 @@ -package spdxtagvalue - -import ( - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "spdx-tag-value" - -var IDs = []sbom.FormatID{ID, "spdx", "spdx-tv"} - -// note: this format is LOSSY relative to the syftjson format -func Format2_1() sbom.Format { - return sbom.NewFormat( - "2.1", - encoder2_1, - decoder, - validator, - IDs..., - ) -} - -func Format2_2() sbom.Format { - return sbom.NewFormat( - "2.2", - encoder2_2, - decoder, - validator, - IDs..., - ) -} - -func Format2_3() sbom.Format { - return sbom.NewFormat( - "2.3", - encoder2_3, - decoder, - validator, - IDs..., - ) -} - -var Format = Format2_3 diff --git a/syft/formats/spdxtagvalue/test-fixtures/image-simple/Dockerfile b/syft/formats/spdxtagvalue/test-fixtures/image-simple/Dockerfile deleted file mode 100644 index 79cfa759e35..00000000000 --- a/syft/formats/spdxtagvalue/test-fixtures/image-simple/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one. -FROM scratch -ADD file-1.txt /somefile-1.txt -ADD file-2.txt /somefile-2.txt diff --git a/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-1.txt b/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-1.txt deleted file mode 100644 index 985d3408e98..00000000000 --- a/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-1.txt +++ /dev/null @@ -1 +0,0 @@ -this file has contents \ No newline at end of file diff --git a/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-2.txt b/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-2.txt deleted file mode 100644 index 396d08bbc72..00000000000 --- a/syft/formats/spdxtagvalue/test-fixtures/image-simple/file-2.txt +++ /dev/null @@ -1 +0,0 @@ -file-2 contents! \ No newline at end of file diff --git a/syft/formats/spdxtagvalue/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/formats/spdxtagvalue/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden deleted file mode 100644 index d7e3532665c..00000000000 Binary files a/syft/formats/spdxtagvalue/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden and /dev/null differ diff --git a/syft/formats/spdxtagvalue/validator.go b/syft/formats/spdxtagvalue/validator.go deleted file mode 100644 index e68875f2ecf..00000000000 --- a/syft/formats/spdxtagvalue/validator.go +++ /dev/null @@ -1,10 +0,0 @@ -package spdxtagvalue - -import ( - "io" -) - -func validator(reader io.Reader) error { - _, err := decoder(reader) - return err -} diff --git a/syft/formats/syftjson/decoder.go b/syft/formats/syftjson/decoder.go deleted file mode 100644 index 845c1fc0ba3..00000000000 --- a/syft/formats/syftjson/decoder.go +++ /dev/null @@ -1,48 +0,0 @@ -package syftjson - -import ( - "encoding/json" - "fmt" - "io" - - "github.com/Masterminds/semver" - - "github.com/anchore/syft/internal" - "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/syft/formats/syftjson/model" - "github.com/anchore/syft/syft/sbom" -) - -func decoder(reader io.Reader) (*sbom.SBOM, error) { - dec := json.NewDecoder(reader) - - var doc model.Document - err := dec.Decode(&doc) - if err != nil { - return nil, fmt.Errorf("unable to decode syft-json: %w", err) - } - - if err := checkSupportedSchema(doc.Schema.Version, internal.JSONSchemaVersion); err != nil { - log.Warn(err) - } - - return toSyftModel(doc) -} - -func checkSupportedSchema(documentVerion string, parserVersion string) error { - documentV, err := semver.NewVersion(documentVerion) - if err != nil { - return fmt.Errorf("error comparing document schema version with parser schema version: %w", err) - } - - parserV, err := semver.NewVersion(parserVersion) - if err != nil { - return fmt.Errorf("error comparing document schema version with parser schema version: %w", err) - } - - if documentV.GreaterThan(parserV) { - return fmt.Errorf("document has schema version %s, but parser has older schema version (%s)", documentVerion, parserVersion) - } - - return nil -} diff --git a/syft/formats/syftjson/encoder.go b/syft/formats/syftjson/encoder.go deleted file mode 100644 index ae52818f344..00000000000 --- a/syft/formats/syftjson/encoder.go +++ /dev/null @@ -1,19 +0,0 @@ -package syftjson - -import ( - "encoding/json" - "io" - - "github.com/anchore/syft/syft/sbom" -) - -func encoder(output io.Writer, s sbom.SBOM) error { - doc := ToFormatModel(s) - - enc := json.NewEncoder(output) - // prevent > and < from being escaped in the payload - enc.SetEscapeHTML(false) - enc.SetIndent("", " ") - - return enc.Encode(&doc) -} diff --git a/syft/formats/syftjson/format.go b/syft/formats/syftjson/format.go deleted file mode 100644 index 5f336871bee..00000000000 --- a/syft/formats/syftjson/format.go +++ /dev/null @@ -1,18 +0,0 @@ -package syftjson - -import ( - "github.com/anchore/syft/internal" - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "syft-json" - -func Format() sbom.Format { - return sbom.NewFormat( - internal.JSONSchemaVersion, - encoder, - decoder, - validator, - ID, "json", "syft", - ) -} diff --git a/syft/formats/syftjson/format_test.go b/syft/formats/syftjson/format_test.go deleted file mode 100644 index 4ef7a21a167..00000000000 --- a/syft/formats/syftjson/format_test.go +++ /dev/null @@ -1,33 +0,0 @@ -package syftjson - -import ( - "testing" - - "github.com/anchore/syft/internal" -) - -func TestFormat(t *testing.T) { - tests := []struct { - name string - version string - }{ - { - name: "default version should use latest internal version", - version: "", - }, - } - - for _, c := range tests { - c := c - t.Run(c.name, func(t *testing.T) { - sbomFormat := Format() - if sbomFormat.ID() != ID { - t.Errorf("expected ID %q, got %q", ID, sbomFormat.ID()) - } - - if sbomFormat.Version() != internal.JSONSchemaVersion { - t.Errorf("expected version %q, got %q", c.version, sbomFormat.Version()) - } - }) - } -} diff --git a/syft/formats/syftjson/test-fixtures/image-simple/Dockerfile b/syft/formats/syftjson/test-fixtures/image-simple/Dockerfile deleted file mode 100644 index 79cfa759e35..00000000000 --- a/syft/formats/syftjson/test-fixtures/image-simple/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one. -FROM scratch -ADD file-1.txt /somefile-1.txt -ADD file-2.txt /somefile-2.txt diff --git a/syft/formats/syftjson/test-fixtures/image-simple/file-1.txt b/syft/formats/syftjson/test-fixtures/image-simple/file-1.txt deleted file mode 100644 index 985d3408e98..00000000000 --- a/syft/formats/syftjson/test-fixtures/image-simple/file-1.txt +++ /dev/null @@ -1 +0,0 @@ -this file has contents \ No newline at end of file diff --git a/syft/formats/syftjson/test-fixtures/image-simple/file-2.txt b/syft/formats/syftjson/test-fixtures/image-simple/file-2.txt deleted file mode 100644 index 396d08bbc72..00000000000 --- a/syft/formats/syftjson/test-fixtures/image-simple/file-2.txt +++ /dev/null @@ -1 +0,0 @@ -file-2 contents! \ No newline at end of file diff --git a/syft/formats/syftjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden b/syft/formats/syftjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden deleted file mode 100644 index f4aa1e7bb74..00000000000 Binary files a/syft/formats/syftjson/test-fixtures/snapshot/stereoscope-fixture-image-simple.golden and /dev/null differ diff --git a/syft/formats/syftjson/validator.go b/syft/formats/syftjson/validator.go deleted file mode 100644 index daf541b8858..00000000000 --- a/syft/formats/syftjson/validator.go +++ /dev/null @@ -1,31 +0,0 @@ -package syftjson - -import ( - "encoding/json" - "fmt" - "io" - "strings" - - "github.com/anchore/syft/syft/formats/syftjson/model" -) - -func validator(reader io.Reader) error { - type Document struct { - Schema model.Schema `json:"schema"` - } - - dec := json.NewDecoder(reader) - - var doc Document - err := dec.Decode(&doc) - if err != nil { - return fmt.Errorf("unable to decode: %w", err) - } - - // note: we accept all schema versions - // TODO: add per-schema version parsing - if strings.Contains(doc.Schema.URL, "anchore/syft") { - return nil - } - return fmt.Errorf("could not extract syft schema") -} diff --git a/syft/formats/table/format.go b/syft/formats/table/format.go deleted file mode 100644 index 7d96237261e..00000000000 --- a/syft/formats/table/format.go +++ /dev/null @@ -1,17 +0,0 @@ -package table - -import ( - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "syft-table" - -func Format() sbom.Format { - return sbom.NewFormat( - sbom.AnyVersion, - encoder, - nil, - nil, - ID, "table", - ) -} diff --git a/syft/formats/template/encoder.go b/syft/formats/template/encoder.go deleted file mode 100644 index 6cdce172881..00000000000 --- a/syft/formats/template/encoder.go +++ /dev/null @@ -1,55 +0,0 @@ -package template - -import ( - "errors" - "fmt" - "os" - "reflect" - "text/template" - - "github.com/Masterminds/sprig/v3" - "github.com/mitchellh/go-homedir" -) - -func makeTemplateExecutor(templateFilePath string) (*template.Template, error) { - if templateFilePath == "" { - return nil, errors.New("no template file: please provide a template path") - } - - expandedPathToTemplateFile, err := homedir.Expand(templateFilePath) - if err != nil { - return nil, fmt.Errorf("unable to expand path %s", templateFilePath) - } - - templateContents, err := os.ReadFile(expandedPathToTemplateFile) - if err != nil { - return nil, fmt.Errorf("unable to get template content: %w", err) - } - - templateName := expandedPathToTemplateFile - tmpl, err := template.New(templateName).Funcs(funcMap).Parse(string(templateContents)) - if err != nil { - return nil, fmt.Errorf("unable to parse template: %w", err) - } - - return tmpl, nil -} - -// These are custom functions available to template authors. -var funcMap = func() template.FuncMap { - f := sprig.HermeticTxtFuncMap() - f["getLastIndex"] = func(collection interface{}) int { - if v := reflect.ValueOf(collection); v.Kind() == reflect.Slice { - return v.Len() - 1 - } - - return 0 - } - // Checks if a field is defined - f["hasField"] = func(obj interface{}, field string) bool { - t := reflect.TypeOf(obj) - _, ok := t.FieldByName(field) - return ok - } - return f -}() diff --git a/syft/formats/template/encoder_test.go b/syft/formats/template/encoder_test.go deleted file mode 100644 index 82a54498a07..00000000000 --- a/syft/formats/template/encoder_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package template - -import ( - "flag" - "testing" - - "github.com/stretchr/testify/assert" - - "github.com/anchore/syft/syft/formats/internal/testutils" -) - -var updateSnapshot = flag.Bool("update-template", false, "update the *.golden files for json encoders") - -func TestFormatWithOption(t *testing.T) { - f := OutputFormat{} - f.SetTemplatePath("test-fixtures/csv.template") - - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInput(t, t.TempDir()), - Format: f, - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - }, - ) -} - -func TestFormatWithOptionAndHasField(t *testing.T) { - f := OutputFormat{} - f.SetTemplatePath("test-fixtures/csv-hasField.template") - - testutils.AssertEncoderAgainstGoldenSnapshot(t, - testutils.EncoderSnapshotTestConfig{ - Subject: testutils.DirectoryInputWithAuthorField(t), - Format: f, - UpdateSnapshot: *updateSnapshot, - PersistRedactionsInSnapshot: true, - IsJSON: false, - }, - ) - -} - -func TestFormatWithoutOptions(t *testing.T) { - f := Format() - err := f.Encode(nil, testutils.DirectoryInput(t, t.TempDir())) - assert.ErrorContains(t, err, "no template file: please provide a template path") -} diff --git a/syft/formats/template/format.go b/syft/formats/template/format.go deleted file mode 100644 index 4d6fb0ab28c..00000000000 --- a/syft/formats/template/format.go +++ /dev/null @@ -1,62 +0,0 @@ -package template - -import ( - "fmt" - "io" - - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "template" - -func Format() sbom.Format { - return OutputFormat{} -} - -// implementation of sbom.Format interface -// to make use of format options -type OutputFormat struct { - templateFilePath string -} - -func (f OutputFormat) ID() sbom.FormatID { - return ID -} - -func (f OutputFormat) IDs() []sbom.FormatID { - return []sbom.FormatID{ID} -} - -func (f OutputFormat) Version() string { - return sbom.AnyVersion -} - -func (f OutputFormat) String() string { - return fmt.Sprintf("template: " + f.templateFilePath) -} - -func (f OutputFormat) Decode(_ io.Reader) (*sbom.SBOM, error) { - return nil, sbom.ErrDecodingNotSupported -} - -func (f OutputFormat) Encode(output io.Writer, s sbom.SBOM) error { - tmpl, err := makeTemplateExecutor(f.templateFilePath) - if err != nil { - return err - } - - doc := syftjson.ToFormatModel(s) - return tmpl.Execute(output, doc) -} - -func (f OutputFormat) Validate(_ io.Reader) error { - return sbom.ErrValidationNotSupported -} - -// SetTemplatePath sets path for template file -func (f *OutputFormat) SetTemplatePath(filePath string) { - f.templateFilePath = filePath -} - -var _ sbom.Format = (*OutputFormat)(nil) diff --git a/syft/formats/text/format.go b/syft/formats/text/format.go deleted file mode 100644 index 387bbd52a8c..00000000000 --- a/syft/formats/text/format.go +++ /dev/null @@ -1,17 +0,0 @@ -package text - -import ( - "github.com/anchore/syft/syft/sbom" -) - -const ID sbom.FormatID = "syft-text" - -func Format() sbom.Format { - return sbom.NewFormat( - sbom.AnyVersion, - encoder, - nil, - nil, - ID, "text", - ) -} diff --git a/syft/formats/text/test-fixtures/image-simple/Dockerfile b/syft/formats/text/test-fixtures/image-simple/Dockerfile deleted file mode 100644 index 79cfa759e35..00000000000 --- a/syft/formats/text/test-fixtures/image-simple/Dockerfile +++ /dev/null @@ -1,4 +0,0 @@ -# Note: changes to this file will result in updating several test values. Consider making a new image fixture instead of editing this one. -FROM scratch -ADD file-1.txt /somefile-1.txt -ADD file-2.txt /somefile-2.txt diff --git a/syft/formats/text/test-fixtures/image-simple/file-1.txt b/syft/formats/text/test-fixtures/image-simple/file-1.txt deleted file mode 100644 index 985d3408e98..00000000000 --- a/syft/formats/text/test-fixtures/image-simple/file-1.txt +++ /dev/null @@ -1 +0,0 @@ -this file has contents \ No newline at end of file diff --git a/syft/formats/text/test-fixtures/image-simple/file-2.txt b/syft/formats/text/test-fixtures/image-simple/file-2.txt deleted file mode 100644 index 396d08bbc72..00000000000 --- a/syft/formats/text/test-fixtures/image-simple/file-2.txt +++ /dev/null @@ -1 +0,0 @@ -file-2 contents! \ No newline at end of file diff --git a/syft/internal/jsonschema/main.go b/syft/internal/jsonschema/main.go index 26148eb23d7..53dea6f5abb 100644 --- a/syft/internal/jsonschema/main.go +++ b/syft/internal/jsonschema/main.go @@ -14,7 +14,7 @@ import ( "github.com/invopop/jsonschema" "github.com/anchore/syft/internal" - syftJsonModel "github.com/anchore/syft/syft/formats/syftjson/model" + syftJsonModel "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/internal/packagemetadata" ) diff --git a/syft/pkg/cataloger/sbom/cataloger.go b/syft/pkg/cataloger/sbom/cataloger.go index a08c2c2a942..548e5cad4e3 100644 --- a/syft/pkg/cataloger/sbom/cataloger.go +++ b/syft/pkg/cataloger/sbom/cataloger.go @@ -1,10 +1,14 @@ package sbom import ( + "bytes" + "fmt" + "io" + "github.com/anchore/syft/internal/log" "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats" + "github.com/anchore/syft/syft/format" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg/cataloger/generic" ) @@ -30,7 +34,11 @@ func NewSBOMCataloger() *generic.Cataloger { } func parseSBOM(_ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) { - s, _, err := formats.Decode(reader) + readSeeker, err := adaptToReadSeeker(reader) + if err != nil { + return nil, nil, fmt.Errorf("unable to read SBOM file %q: %w", reader.Location.RealPath, err) + } + s, _, _, err := format.Decode(readSeeker) if err != nil { return nil, nil, err } @@ -62,3 +70,17 @@ func parseSBOM(_ file.Resolver, _ *generic.Environment, reader file.LocationRead return pkgs, relationships, nil } + +func adaptToReadSeeker(reader io.Reader) (io.ReadSeeker, error) { + // with the stereoscope API and default file.Resolver implementation here in syft, odds are very high that + // the underlying reader is already a ReadSeeker, so we can just return it as-is. We still want to + if rs, ok := reader.(io.ReadSeeker); ok { + return rs, nil + } + + log.Debug("SBOM cataloger reader is not a ReadSeeker, reading entire SBOM into memory") + + var buff bytes.Buffer + _, err := io.Copy(&buff, reader) + return bytes.NewReader(buff.Bytes()), err +} diff --git a/syft/pkg/cataloger/sbom/cataloger_test.go b/syft/pkg/cataloger/sbom/cataloger_test.go index 46332a2a745..a8cd184106e 100644 --- a/syft/pkg/cataloger/sbom/cataloger_test.go +++ b/syft/pkg/cataloger/sbom/cataloger_test.go @@ -8,10 +8,8 @@ import ( "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/cpe" "github.com/anchore/syft/syft/file" - "github.com/anchore/syft/syft/formats/syftjson" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest" - "github.com/anchore/syft/syft/sbom" ) func mustCPEs(s ...string) (c []cpe.CPE) { @@ -405,7 +403,6 @@ func Test_parseSBOM(t *testing.T) { tests := []struct { name string - format sbom.Format fixture string wantPkgs []pkg.Package wantRelationships []artifact.Relationship @@ -413,7 +410,6 @@ func Test_parseSBOM(t *testing.T) { }{ { name: "parse syft JSON", - format: syftjson.Format(), fixture: "test-fixtures/alpine/syft-json", wantPkgs: expectedPkgs, wantRelationships: expectedRelationships, diff --git a/syft/sbom/format.go b/syft/sbom/format.go index 5247845b32e..94901ffce84 100644 --- a/syft/sbom/format.go +++ b/syft/sbom/format.go @@ -1,17 +1,9 @@ package sbom import ( - "errors" - "fmt" "io" ) -var ( - ErrEncodingNotSupported = errors.New("encoding not supported") - ErrDecodingNotSupported = errors.New("decoding not supported") - ErrValidationNotSupported = errors.New("validation not supported") -) - type FormatID string // String returns a string representation of the FormatID. @@ -21,88 +13,19 @@ func (f FormatID) String() string { const AnyVersion = "" -type Format interface { +type FormatEncoder interface { ID() FormatID - IDs() []FormatID + Aliases() []string Version() string Encode(io.Writer, SBOM) error - Decode(io.Reader) (*SBOM, error) - Validate(io.Reader) error - fmt.Stringer -} - -type format struct { - ids []FormatID - version string - encoder Encoder - decoder Decoder - validator Validator -} - -func (f format) IDs() []FormatID { - return f.ids -} - -func (f format) Version() string { - return f.version -} - -func (f format) String() string { - if f.version == AnyVersion { - return f.ID().String() - } - return fmt.Sprintf("%s@%s", f.ID(), f.version) -} - -// Decoder is a function that can convert an SBOM document of a specific format from a reader into Syft native objects. -type Decoder func(reader io.Reader) (*SBOM, error) - -// Encoder is a function that can transform Syft native objects into an SBOM document of a specific format written to the given writer. -type Encoder func(io.Writer, SBOM) error - -// Validator reads the SBOM from the given reader and assesses whether the document conforms to the specific SBOM format. -// The validator should positively confirm if the SBOM is not only the format but also has the minimal set of values -// that the format requires. For example, all syftjson formatted documents have a schema section which should have -// "anchore/syft" within the version --if this isn't found then the validator should raise an error. These active -// assertions protect against "simple" format decoding validations that may lead to false positives (e.g. I decoded -// json successfully therefore this must be the target format, however, all values are their default zero-value and -// really represent a different format that also uses json) -type Validator func(reader io.Reader) error - -func NewFormat(version string, encoder Encoder, decoder Decoder, validator Validator, ids ...FormatID) Format { - return format{ - ids: ids, - version: version, - encoder: encoder, - decoder: decoder, - validator: validator, - } } -func (f format) ID() FormatID { - return f.ids[0] -} +type FormatDecoder interface { + // Decode will return an SBOM from the given reader. If the bytes are not a valid SBOM for the given format + // then an error will be returned. + Decode(io.ReadSeeker) (*SBOM, FormatID, string, error) -func (f format) Encode(output io.Writer, s SBOM) error { - if f.encoder == nil { - return ErrEncodingNotSupported - } - return f.encoder(output, s) + // Identify will return the format ID and version for the given reader. Note: this does not validate the + // full SBOM, only pulls the minimal information necessary to identify the format. + Identify(io.ReadSeeker) (FormatID, string) } - -func (f format) Decode(reader io.Reader) (*SBOM, error) { - if f.decoder == nil { - return nil, ErrDecodingNotSupported - } - return f.decoder(reader) -} - -func (f format) Validate(reader io.Reader) error { - if f.validator == nil { - return ErrValidationNotSupported - } - - return f.validator(reader) -} - -var _ Format = (*format)(nil) diff --git a/test/cli/all_formats_expressible_test.go b/test/cli/all_formats_expressible_test.go index a5a9a710b3b..13c0f079f19 100644 --- a/test/cli/all_formats_expressible_test.go +++ b/test/cli/all_formats_expressible_test.go @@ -6,8 +6,9 @@ import ( "github.com/stretchr/testify/require" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/template" + "github.com/anchore/syft/cmd/syft/cli/options" + "github.com/anchore/syft/syft/format" + "github.com/anchore/syft/syft/format/template" ) func TestAllFormatsExpressible(t *testing.T) { @@ -20,9 +21,15 @@ func TestAllFormatsExpressible(t *testing.T) { }, assertSuccessfulReturnCode, } - formatNames := formats.AllIDs() - require.NotEmpty(t, formatNames) - for _, o := range formatNames { + + opts := options.DefaultOutput() + encoders, err := opts.Encoders() + require.NoError(t, err) + + encs := format.NewEncoderCollection(encoders...) + formatIDs := encs.IDs() + require.NotEmpty(t, formatIDs) + for _, o := range formatIDs { t.Run(fmt.Sprintf("format:%s", o), func(t *testing.T) { args := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", string(o)} if o == template.ID { diff --git a/test/cli/cyclonedx_valid_test.go b/test/cli/cyclonedx_valid_test.go index 5a5b240ab88..c8728f7f30b 100644 --- a/test/cli/cyclonedx_valid_test.go +++ b/test/cli/cyclonedx_valid_test.go @@ -5,7 +5,11 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/anchore/stereoscope/pkg/imagetest" + "github.com/anchore/syft/syft/format/cyclonedxjson" ) // We have schema validation mechanims in schema/cyclonedx/ @@ -95,13 +99,13 @@ func assertValidCycloneDX(tb testing.TB, stdout, stderr string, rc int) { // validate --input-format json --input-version v1_4 --input-file bom.json func validateCycloneDXJSON(t *testing.T, stdout string) { f, err := os.CreateTemp("", "tmpfile-") - if err != nil { - t.Fatal(err) - } + require.NoError(t, err) // close and remove the temporary file at the end of the program - defer f.Close() - defer os.Remove(f.Name()) + t.Cleanup(func() { + assert.NoError(t, f.Close()) + assert.NoError(t, os.Remove(f.Name())) + }) data := []byte(stdout) @@ -109,18 +113,25 @@ func validateCycloneDXJSON(t *testing.T, stdout string) { t.Fatal(err) } + // get the latest supported version of CycloneDX by syft and convert the expression to the format used by cyclonedx-cli + // e.g. "1.5" -> "v1_5" + versions := cyclonedxjson.SupportedVersions() + version := versions[len(versions)-1] + versionInput := "v" + strings.Replace(version, ".", "_", -1) + args := []string{ "validate", "--input-format", "json", "--input-version", - "v1_4", + versionInput, "--input-file", "/sbom", } cmd, stdout, stderr := runCycloneDXInDocker(t, nil, "cyclonedx/cyclonedx-cli", f, args...) if strings.Contains(stdout, "BOM is not valid") { + t.Log("STDOUT:\n", stdout) t.Errorf("expected no validation failures for cyclonedx-cli but found invalid BOM") } diff --git a/test/integration/convert_test.go b/test/integration/convert_test.go index f1c0eb3269d..43670c52e70 100644 --- a/test/integration/convert_test.go +++ b/test/integration/convert_test.go @@ -9,17 +9,23 @@ import ( "github.com/anchore/syft/cmd/syft/cli/commands" "github.com/anchore/syft/cmd/syft/cli/options" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/cyclonedxjson" - "github.com/anchore/syft/syft/formats/cyclonedxxml" - "github.com/anchore/syft/syft/formats/spdxjson" - "github.com/anchore/syft/syft/formats/spdxtagvalue" - "github.com/anchore/syft/syft/formats/syftjson" - "github.com/anchore/syft/syft/formats/table" + "github.com/anchore/syft/syft/format" + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/cyclonedxxml" + "github.com/anchore/syft/syft/format/spdxjson" + "github.com/anchore/syft/syft/format/spdxtagvalue" + "github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) +func mustEncoder(enc sbom.FormatEncoder, err error) sbom.FormatEncoder { + if err != nil { + panic(err) + } + return enc +} + // TestConvertCmd tests if the converted SBOM is a valid document according // to spec. // TODO: This test can, but currently does not, check the converted SBOM content. It @@ -28,33 +34,33 @@ import ( func TestConvertCmd(t *testing.T) { tests := []struct { name string - format sbom.Format + format sbom.FormatEncoder }{ { name: "syft-json", - format: syftjson.Format(), + format: syftjson.NewFormatEncoder(), }, { name: "spdx-json", - format: spdxjson.Format(), + format: mustEncoder(spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig())), }, { name: "spdx-tag-value", - format: spdxtagvalue.Format(), + format: mustEncoder(spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.DefaultEncoderConfig())), }, { name: "cyclonedx-json", - format: cyclonedxjson.Format(), + format: mustEncoder(cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())), }, { name: "cyclonedx-xml", - format: cyclonedxxml.Format(), + format: mustEncoder(cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.DefaultEncoderConfig())), }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { syftSbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, nil) - syftFormat := syftjson.Format() + syftFormat := syftjson.NewFormatEncoder() syftFile, err := os.CreateTemp("", "test-convert-sbom-") require.NoError(t, err) @@ -72,10 +78,11 @@ func TestConvertCmd(t *testing.T) { }() opts := &commands.ConvertOptions{ - MultiOutput: options.MultiOutput{ + Output: options.Output{ Outputs: []string{fmt.Sprintf("%s=%s", test.format.ID().String(), formatFile.Name())}, }, } + require.NoError(t, opts.PostLoad()) // stdout reduction of test noise rescue := os.Stdout // keep backup of the real stdout @@ -86,15 +93,9 @@ func TestConvertCmd(t *testing.T) { err = commands.RunConvert(opts, syftFile.Name()) require.NoError(t, err) - contents, err := os.ReadFile(formatFile.Name()) - require.NoError(t, err) - formatFound := formats.Identify(contents) - if test.format.ID() == table.ID { - require.Nil(t, formatFound) - return - } - require.Equal(t, test.format.ID(), formatFound.ID()) + foundID, _ := format.Identify(formatFile) + require.Equal(t, test.format.ID(), foundID) }) } } diff --git a/test/integration/encode_decode_cycle_test.go b/test/integration/encode_decode_cycle_test.go index ce839eca6f5..ba99fe88f84 100644 --- a/test/integration/encode_decode_cycle_test.go +++ b/test/integration/encode_decode_cycle_test.go @@ -10,10 +10,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/anchore/syft/syft/formats" - "github.com/anchore/syft/syft/formats/cyclonedxjson" - "github.com/anchore/syft/syft/formats/cyclonedxxml" - "github.com/anchore/syft/syft/formats/syftjson" + "github.com/anchore/syft/cmd/syft/cli/options" + "github.com/anchore/syft/syft/format" + "github.com/anchore/syft/syft/format/cyclonedxjson" + "github.com/anchore/syft/syft/format/cyclonedxxml" + "github.com/anchore/syft/syft/format/syftjson" "github.com/anchore/syft/syft/sbom" "github.com/anchore/syft/syft/source" ) @@ -65,24 +66,36 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) { }, } + opts := options.DefaultOutput() + encoderList, err := opts.Encoders() + require.NoError(t, err) + + encoders := format.NewEncoderCollection(encoderList...) + decoders := format.NewDecoderCollection(format.Decoders()...) + for _, test := range tests { t.Run(string(test.formatOption), func(t *testing.T) { for _, image := range images { originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil) - format := formats.ByName(string(test.formatOption)) - require.NotNil(t, format) + f := encoders.GetByString(string(test.formatOption)) + require.NotNil(t, f) - by1, err := formats.Encode(originalSBOM, format) + var buff1 bytes.Buffer + err := f.Encode(&buff1, originalSBOM) require.NoError(t, err) - newSBOM, newFormat, err := formats.Decode(bytes.NewReader(by1)) + newSBOM, formatID, formatVersion, err := decoders.Decode(bytes.NewReader(buff1.Bytes())) require.NoError(t, err) - require.Equal(t, format.ID(), newFormat.ID()) + require.Equal(t, f.ID(), formatID) + require.Equal(t, f.Version(), formatVersion) - by2, err := formats.Encode(*newSBOM, format) + var buff2 bytes.Buffer + err = f.Encode(&buff2, *newSBOM) require.NoError(t, err) + by1 := buff1.Bytes() + by2 := buff2.Bytes() if test.redactor != nil { by1 = test.redactor(by1) by2 = test.redactor(by2) diff --git a/test/integration/package_ownership_relationship_test.go b/test/integration/package_ownership_relationship_test.go index a984972897d..d3ce0745404 100644 --- a/test/integration/package_ownership_relationship_test.go +++ b/test/integration/package_ownership_relationship_test.go @@ -5,8 +5,10 @@ import ( "encoding/json" "testing" - "github.com/anchore/syft/syft/formats/syftjson" - syftjsonModel "github.com/anchore/syft/syft/formats/syftjson/model" + "github.com/stretchr/testify/require" + + "github.com/anchore/syft/syft/format/syftjson" + syftjsonModel "github.com/anchore/syft/syft/format/syftjson/model" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/source" ) @@ -26,10 +28,8 @@ func TestPackageOwnershipRelationships(t *testing.T) { sbom, _ := catalogFixtureImage(t, test.fixture, source.SquashedScope, nil) output := bytes.NewBufferString("") - err := syftjson.Format().Encode(output, sbom) - if err != nil { - t.Fatalf("unable to present: %+v", err) - } + err := syftjson.NewFormatEncoder().Encode(output, sbom) + require.NoError(t, err) var doc syftjsonModel.Document decoder := json.NewDecoder(output)