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

Simplify the SBOM writer interface #1892

Merged
merged 3 commits into from
Jun 23, 2023
Merged

Simplify the SBOM writer interface #1892

merged 3 commits into from
Jun 23, 2023

Conversation

wagoodman
Copy link
Contributor

@wagoodman wagoodman commented Jun 23, 2023

This PR attempts to simplify the sbom.Writer interface by removing required methods and consolidating helper functions for writers to the cmd/options package.

This reduces the current interface:

type Writer interface {
	// Write writes the provided SBOM
	Write(SBOM) error

	// Bytes returns the bytes of the SBOM that would be written
	Bytes(SBOM) ([]byte, error)

	// Closer a resource cleanup hook which will be called after SBOM
	// is written or if an error occurs before Write is called
	io.Closer
}

To this:

type Writer interface {
	Write(SBOM) error}
}

The primary reasons being:

  • we don't support writing multiple SBOMs or streaming, so a Close() isn't necessary. The Write() call should implicitly close any destination.
  • Bytes(...) []bytes ... is ultimately a reading call, not a writing call, which is the opposite meaning of this interface

Additionally, many of the helpers around making writers are in the same sbom package, however, these helpers seem primarily targeted towards the CLI argument parsing, which should reside under the cmd package. This is why many of the writer helpers have been moved to cmd/options.

This change cause downstream changes necessary in the attest command so took additional time simplifying sbom.Writer usage in that command.

Partially addresses syft 1.0 work.

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
@wagoodman wagoodman added this to the Stabilize user surfaces milestone Jun 23, 2023
@wagoodman wagoodman requested a review from a team June 23, 2023 13:21
@wagoodman wagoodman self-assigned this Jun 23, 2023
@github-actions
Copy link

github-actions bot commented Jun 23, 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) CPU E5-2673 v4 @ 2.30GHz%0A                                                          │ ./.tmp/benchmark-c0a8ce6.txt │%0A                                                          │            sec/op            │%0AImagePackageCatalogers/alpmdb-cataloger-2                                   14.81m ±  6%25%0AImagePackageCatalogers/apkdb-cataloger-2                                    982.0µ ±  3%25%0AImagePackageCatalogers/binary-cataloger-2                                   278.4µ ±  4%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                   839.6µ ±  3%25%0AImagePackageCatalogers/dotnet-deps-cataloger-2                              1.644m ±  5%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                         139.8µ ±  5%25%0AImagePackageCatalogers/java-cataloger-2                                     17.37m ± 10%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                     135.6µ ±  8%25%0AImagePackageCatalogers/javascript-package-cataloger-2                       593.7µ ±  5%25%0AImagePackageCatalogers/nix-store-cataloger-2                                404.9µ ±  6%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                   1.114m ±  3%25%0AImagePackageCatalogers/portage-cataloger-2                                  689.6µ ±  3%25%0AImagePackageCatalogers/python-package-cataloger-2                           4.551m ±  2%25%0AImagePackageCatalogers/r-package-cataloger-2                                312.2µ ±  5%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                   753.2µ ±  3%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                             1.255m ±  4%25%0AImagePackageCatalogers/sbom-cataloger-2                                     155.5µ ±  2%25%0Ageomean                                                                     855.9µ%0A%0A                                                          │ ./.tmp/benchmark-c0a8ce6.txt │%0A                                                          │             B/op             │%0AImagePackageCatalogers/alpmdb-cataloger-2                                   5.127Mi ± 0%25%0AImagePackageCatalogers/apkdb-cataloger-2                                    205.3Ki ± 0%25%0AImagePackageCatalogers/binary-cataloger-2                                   30.18Ki ± 0%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                   168.9Ki ± 0%25%0AImagePackageCatalogers/dotnet-deps-cataloger-2                              404.8Ki ± 0%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                         9.906Ki ± 0%25%0AImagePackageCatalogers/java-cataloger-2                                     2.823Mi ± 0%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                     8.595Ki ± 0%25%0AImagePackageCatalogers/javascript-package-cataloger-2                       100.9Ki ± 0%25%0AImagePackageCatalogers/nix-store-cataloger-2                                49.14Ki ± 0%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                   186.7Ki ± 0%25%0AImagePackageCatalogers/portage-cataloger-2                                  120.0Ki ± 0%25%0AImagePackageCatalogers/python-package-cataloger-2                           1.003Mi ± 0%25%0AImagePackageCatalogers/r-package-cataloger-2                                53.29Ki ± 0%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                   181.0Ki ± 0%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                             144.1Ki ± 0%25%0AImagePackageCatalogers/sbom-cataloger-2                                     14.20Ki ± 0%25%0Ageomean                                                                     132.7Ki%0A%0A                                                          │ ./.tmp/benchmark-c0a8ce6.txt │%0A                                                          │          allocs/op           │%0AImagePackageCatalogers/alpmdb-cataloger-2                                    87.75k ± 0%25%0AImagePackageCatalogers/apkdb-cataloger-2                                     4.182k ± 0%25%0AImagePackageCatalogers/binary-cataloger-2                                     830.0 ± 0%25%0AImagePackageCatalogers/dpkgdb-cataloger-2                                    3.000k ± 0%25%0AImagePackageCatalogers/dotnet-deps-cataloger-2                               6.338k ± 0%25%0AImagePackageCatalogers/go-module-binary-cataloger-2                           281.0 ± 0%25%0AImagePackageCatalogers/java-cataloger-2                                      39.88k ± 0%25%0AImagePackageCatalogers/graalvm-native-image-cataloger-2                       228.0 ± 0%25%0AImagePackageCatalogers/javascript-package-cataloger-2                        1.404k ± 0%25%0AImagePackageCatalogers/nix-store-cataloger-2                                  895.0 ± 0%25%0AImagePackageCatalogers/php-composer-installed-cataloger-2                    4.079k ± 0%25%0AImagePackageCatalogers/portage-cataloger-2                                   2.269k ± 0%25%0AImagePackageCatalogers/python-package-cataloger-2                            16.44k ± 0%25%0AImagePackageCatalogers/r-package-cataloger-2                                  929.0 ± 0%25%0AImagePackageCatalogers/rpm-db-cataloger-2                                    3.989k ± 0%25%0AImagePackageCatalogers/ruby-gemspec-cataloger-2                              2.448k ± 0%25%0AImagePackageCatalogers/sbom-cataloger-2                                       394.0 ± 0%25%0Ageomean                                                                      2.583k

@wagoodman wagoodman marked this pull request as draft June 23, 2023 13:27
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
@wagoodman wagoodman marked this pull request as ready for review June 23, 2023 14:00
@wagoodman wagoodman marked this pull request as draft June 23, 2023 14:00
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
@wagoodman wagoodman marked this pull request as ready for review June 23, 2023 14:15
Comment on lines +117 to +123
type nopWriteCloser struct {
io.Writer
}

func (n nopWriteCloser) Close() error {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: organization -- it would probably be good to keep all the sbomMultiWriter stuff together

Copy link
Contributor Author

@wagoodman wagoodman Jun 23, 2023

Choose a reason for hiding this comment

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

yeah, this file got a little awkward, but believe it or not the #1888 PR is about to remove this (the nopWriteCloser) since it will write the the event bus. I split out this work to make the bubbletea PR smaller :)

@wagoodman wagoodman merged commit 25ce245 into main Jun 23, 2023
@wagoodman wagoodman deleted the simplify-sbom-writer branch June 23, 2023 15:21
GijsCalis pushed a commit to GijsCalis/syft that referenced this pull request Feb 19, 2024
* remove sbom.writer bytes call and consolidate helpers to options pkg

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* dont close stdout

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* remove close operation from multiwriter

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

---------

Signed-off-by: Alex Goodman <alex.goodman@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.

2 participants