Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cyclonedx schema version selection #2123

Merged
merged 9 commits into from
Sep 13, 2023
Merged

Conversation

spiffcs
Copy link
Contributor

@spiffcs spiffcs commented Sep 12, 2023

Summary

This PR moves the cyclone-dx format section closer to how we do spdx version selection.

syft -o cyclonedx-json@v1.3 alpine:latest

The above command should now provide the header:

 "$schema": "http://cyclonedx.org/schema/bom-1.3.schema.json",

Users can also look forward and try v1.5. Syft will move to this as the default version when this milestone is complete:
CycloneDX/cyclonedx-go#90

With the latest update to the cyclone-dx golang library v1.5 is minimally supported (v0.7.2). This PR keeps the default output of cyclone-dx (xml/json) to be v1.4, but allows users to specify if they'd like to try and generate v1.5 or any of the older specification versions.

TODO

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
@spiffcs spiffcs linked an issue Sep 12, 2023 that may be closed by this pull request
@github-actions
Copy link

github-actions bot commented Sep 12, 2023

Benchmark Test Results

Benchmark results from the latest changes vs base branch
goos: linux%0Agoarch: amd64%0Apkg: github.com/anchore/syft/test/integration%0Acpu: Intel(R) Xeon(R) Platinum 8370C CPU @ 2.80GHz%0A                                                              │ ./.tmp/benchmark-88cf0e1.txt │%0A                                                              │            sec/op            │%0AImagePackageCatalogers/alpmdb-cataloger-2                                       11.46m ±  0%25%0AImagePackageCatalogers/apkdb-cataloger-2                                        626.2µ ±  1%25%0AImagePackageCatalogers/binary-cataloger-2                                       204.2µ ±  0%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                       519.3µ ±  2%25%0AImagePackageCatalogers/dotnet-portable-executable-cataloger-2                   20.67µ ±  1%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                             92.00µ ±  6%25%0AImagePackageCatalogers/java-cataloger-2                                         20.94m ± 25%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                         90.57µ ±  1%25%0AImagePackageCatalogers/javascript-package-cataloger-2                           324.4µ ±  1%25%0AImagePackageCatalogers/nix-store-cataloger-2                                    248.4µ ±  2%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                       684.5µ ±  1%25%0AImagePackageCatalogers/portage-cataloger-2                                      399.8µ ±  1%25%0AImagePackageCatalogers/python-package-cataloger-2                               3.073m ±  1%25%0AImagePackageCatalogers/r-package-cataloger-2                                    167.0µ ±  1%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                       441.0µ ±  5%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                                 812.7µ ±  2%25%0AImagePackageCatalogers/sbom-cataloger-2                                         116.1µ ±  1%25%0Ageomean                                                                         450.4µ%0A%0A                                                              │ ./.tmp/benchmark-88cf0e1.txt │%0A                                                              │             B/op             │%0AImagePackageCatalogers/alpmdb-cataloger-2                                       5.066Mi ± 0%25%0AImagePackageCatalogers/apkdb-cataloger-2                                        181.6Ki ± 0%25%0AImagePackageCatalogers/binary-cataloger-2                                       30.79Ki ± 0%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                       139.3Ki ± 0%25%0AImagePackageCatalogers/dotnet-portable-executable-cataloger-2                   3.695Ki ± 0%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                             9.281Ki ± 0%25%0AImagePackageCatalogers/java-cataloger-2                                         3.312Mi ± 1%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                         8.000Ki ± 0%25%0AImagePackageCatalogers/javascript-package-cataloger-2                           82.94Ki ± 0%25%0AImagePackageCatalogers/nix-store-cataloger-2                                    38.38Ki ± 0%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                       152.6Ki ± 0%25%0AImagePackageCatalogers/portage-cataloger-2                                      108.4Ki ± 0%25%0AImagePackageCatalogers/python-package-cataloger-2                               982.9Ki ± 0%25%0AImagePackageCatalogers/r-package-cataloger-2                                    42.37Ki ± 0%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                       165.4Ki ± 0%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                                 122.5Ki ± 0%25%0AImagePackageCatalogers/sbom-cataloger-2                                         14.20Ki ± 0%25%0Ageomean                                                                         91.89Ki%0A%0A                                                              │ ./.tmp/benchmark-88cf0e1.txt │%0A                                                              │          allocs/op           │%0AImagePackageCatalogers/alpmdb-cataloger-2                                        74.97k ± 0%25%0AImagePackageCatalogers/apkdb-cataloger-2                                         3.670k ± 0%25%0AImagePackageCatalogers/binary-cataloger-2                                         866.0 ± 0%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                        2.721k ± 0%25%0AImagePackageCatalogers/dotnet-portable-executable-cataloger-2                     132.0 ± 0%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                               270.0 ± 0%25%0AImagePackageCatalogers/java-cataloger-2                                          43.40k ± 0%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                           218.0 ± 0%25%0AImagePackageCatalogers/javascript-package-cataloger-2                            1.203k ± 0%25%0AImagePackageCatalogers/nix-store-cataloger-2                                      751.0 ± 0%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                        3.461k ± 0%25%0AImagePackageCatalogers/portage-cataloger-2                                       2.011k ± 0%25%0AImagePackageCatalogers/python-package-cataloger-2                                15.76k ± 0%25%0AImagePackageCatalogers/r-package-cataloger-2                                      782.0 ± 0%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                        2.877k ± 0%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                                  2.185k ± 0%25%0AImagePackageCatalogers/sbom-cataloger-2                                           394.0 ± 0%25%0Ageomean                                                                          1.870k

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
@spiffcs spiffcs marked this pull request as ready for review September 12, 2023 17:46
@spiffcs spiffcs changed the title wip: add cyclonedx schema version selection feat: add cyclonedx schema version selection Sep 12, 2023
@@ -7,14 +7,66 @@ import (
"github.com/anchore/syft/syft/sbom"
)

const ID sbom.FormatID = "cyclonedx-xml"
const ID sbom.FormatID = "cyclonedx"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a consequential change, can you expound on this a bit? (I think there is an alias for cyclonedx already, which I want to say defaults to json, but I haven't verified this)

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, this doesn't seem right: this is changing the ID for this format to something more generic, but the ID should be as specific as possible. SPDX does this by having a list of IDs, where the first is the ID and the remaining are unique aliases

var IDs = []sbom.FormatID{ID, "spdx", "spdx-tv"}

Then that list is used in the format object

This PR seems to be reversing this, where the aliases are more specific

https://github.com/anchore/syft/pull/2123/files#diff-353e0a54e1d8afae37163dec0733b3e941580dc79824b40b9bf35e649efa28b0R20

Since all of these instances are the same can you make a list and share that similar to SPDX?

Copy link
Contributor Author

@spiffcs spiffcs Sep 13, 2023

Choose a reason for hiding this comment

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

Ahh - I think this was just an unexpected change on my part thanks for the catch - I looked back at the diff and the original format had the alias of cyclonedx. The original ID was cyclonedx-xml. I think a unit test was failing and to fix it I put the wrong alias back in the wrong spot. I'll fix this now. The default for cyclonedx is not json but actually xml - see here on main:

func Format() sbom.Format {
return sbom.NewFormat(
sbom.AnyVersion,
encoder,
cyclonedxhelpers.GetDecoder(cyclonedx.BOMFileFormatXML),
cyclonedxhelpers.GetValidator(cyclonedx.BOMFileFormatXML),
ID, "cyclonedx", "cyclone",
)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wagoodman this should now be reverted to where there are no ID changes and aliases are consistent across the new formats

@@ -71,6 +81,13 @@ func ByNameAndVersion(name string, version string) sbom.Format {
for _, f := range Formats() {
for _, n := range f.IDs() {
if cleanFormatName(string(n)) == name && versionMatches(f.Version(), version) {
if version == sbom.AnyVersion && strings.Contains(string(n), "cyclonedx") {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is looking for the string cyclonedx but we might introduce an alias like cdx. I think this should more formally ask the format for the latest supported version.

At the same time, I feel that this format object is about to be rethought anyway. I think there are strong signs to split up for encoding vs decoding use cases, which means trying to settle this detail in this PR wouldn't be worth it. I think it's worth at least adding a comment here about the limitations of this approach and a TODO to refactor later.

Copy link
Contributor

@wagoodman wagoodman Sep 13, 2023

Choose a reason for hiding this comment

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

also, how does ByName work for spdx for the latest version (I didn't read up on that yet)?

Copy link
Contributor Author

@spiffcs spiffcs Sep 13, 2023

Choose a reason for hiding this comment

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

 I feel that this format object is about to be rethought anyway. 
I think there are strong signs to split up for encoding vs decoding use cases, 
which means trying to settle this detail in this PR wouldn't be worth it.

^ Yea that sounds good to me

I think this should more formally ask the format for the latest supported version

I had tried just setting up a default version for each format rather than latest so we had more flexibility, but that would constitute more of a refactor for the format object that I didn't want to include in this PR.

The comment for this line should be below it I can move above and add a TODO:

// if the version is not specified and the format is cyclonedx, then we want to return the most recent version up to 1.4
// https://github.com/CycloneDX/cyclonedx-go/pull/90

For SPDX if you pass in syft -o spdx alpine:latest then no version is detected. mostRecentFormat will eventually become 2.3

Copy link
Contributor

@kzantow kzantow Sep 13, 2023

Choose a reason for hiding this comment

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

I would probably like to add a Default field to the format object. It could be accomplished without a huge refactor (see related comment below). At some point in the future, when we support SPDX 3, we will probably also want to continue to default to SPDX 2.3.

Copy link
Contributor Author

@spiffcs spiffcs Sep 13, 2023

Choose a reason for hiding this comment

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

I can follow this PR up with a stab at that. I really want to keep this PR small and focused on integrating the released cyclonedx library. Other changes on how we handle formats, their defaults or how the latest is selected can be their own change set. The reason this line had to be included was because we don't necessarily want to be on latest for cyclonedx at this moment. Rather than rewrite ByNameAndVersion into a totally new thing I thought a simple edge case with comment would be the correct change here

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
syft/formats/cyclonedxxml/format.go Outdated Show resolved Hide resolved
func Format() sbom.Format {
var Format = Format1_4

func Format1_0() sbom.Format {
Copy link
Contributor

@kzantow kzantow Sep 13, 2023

Choose a reason for hiding this comment

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

It might be nice to programmatically build these formats, something like this could live in the common/cyclonedxhelpers package:

func Formats(format cyclonedx.BOMFileFormat, ids ...sbom.FormatID) (out []sbom.Format) {
  versions := []cyclonedx.SpecVersion{
    cyclonedx.SpecVersion1_0,
    cyclonedx.SpecVersion1_1,
    cyclonedx.SpecVersion1_2,
    cyclonedx.SpecVersion1_3,
    cyclonedx.SpecVersion1_4,
    cyclonedx.SpecVersion1_5,
  }
  for i := range versions {
    version := versions[i]
    encoder := func(output io.Writer, s sbom.SBOM) error {
      return encodeCycloneDX(output, s, format, version)
    }
    if version == cyclonedx.SpecVersion1_4 {
      // I had another comment about maybe adding a `Default` field to the format struct, which seems to have been lost.
      // assuming that this was something we wanted to do, it could look like this without a huge refactor: 
      out = append(out, sbom.NewFormatDefault(
		version.String(),
		encoder,
		cyclonedxhelpers.GetDecoder(format),
		cyclonedxhelpers.GetValidator(format),
		ids...,
	))
    } else {
      out = append(out, sbom.NewFormat(
		version.String(),
		encoder,
		cyclonedxhelpers.GetDecoder(format),
		cyclonedxhelpers.GetValidator(format),
		ids...,
	))
    }
  }
}

This way there only needs to be a single encodeCycloneDX() function and adding new versions is just updating the list, which would update both XML and JSON variants. E.g. the XML variant would have:

func Formats() []sbom.Format {
  return cyclonedxhelpers.Formats(cyclonedx.BOMFileFormatXML, ID, "cyclonedx", "cyclone")
}

Copy link
Contributor Author

@spiffcs spiffcs Sep 13, 2023

Choose a reason for hiding this comment

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

This is a good suggestion, but I think we wanted to save this kind of work for when we go back and take a look at Formats before syft v1.0. The purpose of this PR was to just keep this as close to the same pattern as we have with SPDX. Getting creative here and trying to build these programmatically (different from the current SPDX pattern) would eventually get swallowed by that new work.

Copy link
Contributor

Choose a reason for hiding this comment

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

agreed to the both of you -- it's a good suggestion, but it would be better to be consistent with the current pattern first, then refactor to "the next" pattern.

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
// 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), "cyclonedx") {
Copy link
Contributor

Choose a reason for hiding this comment

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

This check should probably be strings.Contains(string(n), "cyclone") to use the same default when -o cyclone is used.

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
@spiffcs spiffcs merged commit 3e16c68 into main Sep 13, 2023
9 checks passed
@spiffcs spiffcs deleted the 2120-cyclonedx-v1.5-support branch September 13, 2023 18:50
GijsCalis pushed a commit to GijsCalis/syft that referenced this pull request Feb 19, 2024
---------

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for CycloneDX 1.5
3 participants