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(sbom): add primary package purpose field for SPDX #4119

Merged
merged 2 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions integration/testdata/conda-spdx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"name": "conda-pkg",
"SPDXID": "SPDXRef-Application-ee5ef1aa4ac89125",
"downloadLocation": "NONE",
"primaryPackagePurpose": "INSTALL",
"sourceInfo": "Conda"
},
{
Expand All @@ -33,6 +34,7 @@
"referenceLocator": "pkg:conda/openssl@1.1.1q"
}
],
"primaryPackagePurpose": "INSTALL",
"files": [
{
"fileName": "miniconda3/envs/testenv/conda-meta/openssl-1.1.1q-h7f8727e_0.json",
Expand Down Expand Up @@ -60,6 +62,7 @@
"referenceLocator": "pkg:conda/pip@22.2.2"
}
],
"primaryPackagePurpose": "INSTALL",
"files": [
{
"fileName": "miniconda3/envs/testenv/conda-meta/pip-22.2.2-py38h06a4308_0.json",
Expand Down
10 changes: 10 additions & 0 deletions pkg/fanal/types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,13 @@ const (

MixLock = "mix.lock"
)

// ApplicationTypes contains types we cat think of as Application
// Required to `primary purpose` field of SBOM
// https://github.com/interlynk-io/sbomqs/blob/main/Features.md#primary-purpose
var ApplicationTypes = []string{
GoBinary,
RustBinary,
Jar,
NodePkg,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure about this way.
I had idea: use struct instead of string for Type field.
Something like this:

type Type struct {
	name  string
	isApp bool // true for `jar`, `goBinary`, etc...
}

30 changes: 30 additions & 0 deletions pkg/sbom/spdx/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/spdx/tools-golang/spdx"
"github.com/spdx/tools-golang/spdx/v2/common"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"k8s.io/utils/clock"

Expand Down Expand Up @@ -51,6 +52,12 @@ const (
PropertyPkgID = "PkgID"
PropertyLayerDiffID = "LayerDiffID"
PropertyLayerDigest = "LayerDigest"
// Package Purpose fields
PackagePurposeOS = "OPERATING-SYSTEM"
PackagePurposeContainer = "CONTAINER"
PackagePurposeApplication = "APPLICATION"
PackagePurposeLibrary = "LIBRARY"
PackagePurposeInstall = "INSTALL"

RelationShipContains = common.TypeRelationshipContains
RelationShipDescribe = common.TypeRelationshipDescribe
Expand Down Expand Up @@ -252,12 +259,22 @@ func (m *Marshaler) rootPackage(r types.Report, pkgDownloadLocation string) (*sp
return nil, xerrors.Errorf("failed to get %s package ID: %w", err)
}

// If root package is filesystem, we can't correct determine package purpose
// Because it can contain both applications and libraries at same time
// This field is optional. Use this field for images only
// https://spdx.github.io/spdx-spec/v2.3/package-information/#724-primary-package-purpose-field
pkgPurpose := ""
if r.ArtifactType == ftypes.ArtifactContainerImage {
pkgPurpose = PackagePurposeContainer
}

return &spdx.Package{
PackageName: r.ArtifactName,
PackageSPDXIdentifier: elementID(camelCase(string(r.ArtifactType)), pkgID),
PackageDownloadLocation: pkgDownloadLocation,
PackageAttributionTexts: attributionTexts,
PackageExternalReferences: externalReferences,
PrimaryPackagePurpose: pkgPurpose,
}, nil
}

Expand All @@ -276,6 +293,7 @@ func (m *Marshaler) osPackage(osFound *ftypes.OS, pkgDownloadLocation string) (s
PackageVersion: osFound.Name,
PackageSPDXIdentifier: elementID(ElementOperatingSystem, pkgID),
PackageDownloadLocation: pkgDownloadLocation,
PrimaryPackagePurpose: PackagePurposeOS,
}, nil
}

Expand All @@ -290,6 +308,7 @@ func (m *Marshaler) langPackage(target, appType, pkgDownloadLocation string) (sp
PackageSourceInfo: target, // TODO: Files seems better
PackageSPDXIdentifier: elementID(ElementApplication, pkgID),
PackageDownloadLocation: pkgDownloadLocation,
PrimaryPackagePurpose: getPackagePurpose(appType, ""),
}, nil
}

Expand Down Expand Up @@ -337,6 +356,7 @@ func (m *Marshaler) pkgToSpdxPackage(t, pkgDownloadLocation string, class types.

PackageExternalReferences: pkgExtRefs,
PackageAttributionTexts: attrTexts,
PrimaryPackagePurpose: getPackagePurpose(t, class),
Files: files,
}, nil
}
Expand Down Expand Up @@ -485,3 +505,13 @@ func digestToSpdxFileChecksum(d digest.Digest) []common.Checksum {
},
}
}

func getPackagePurpose(typ string, class types.ResultClass) string {
if class == types.ClassOSPkg || typ == ftypes.CondaPkg {
return PackagePurposeInstall
}
if slices.Contains(ftypes.ApplicationTypes, typ) {
return PackagePurposeApplication
}
return PackagePurposeLibrary
}
21 changes: 19 additions & 2 deletions pkg/sbom/spdx/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestMarshaler_Marshal(t *testing.T) {
Locator: "pkg:gem/actioncontroller@7.0.1",
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Package-826226d056ff30c0"),
Expand All @@ -147,6 +148,7 @@ func TestMarshaler_Marshal(t *testing.T) {
Locator: "pkg:gem/actionpack@7.0.1",
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Package-fd0dc3cf913d5bc3"),
Expand All @@ -162,25 +164,29 @@ func TestMarshaler_Marshal(t *testing.T) {
Locator: "pkg:rpm/centos/binutils@2.30-93.el8?arch=aarch64&distro=centos-8.3.2011",
},
},
PackageSourceInfo: "built package from: binutils 2.30-93.el8",
PackageSourceInfo: "built package from: binutils 2.30-93.el8",
PrimaryPackagePurpose: tspdx.PackagePurposeInstall,
},
{
PackageSPDXIdentifier: spdx.ElementID("Application-73c871d73f3c8248"),
PackageDownloadLocation: "NONE",
PackageName: "bundler",
PackageSourceInfo: "app/subproject/Gemfile.lock",
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Application-c3fac92c1ac0a9fa"),
PackageDownloadLocation: "NONE",
PackageName: "bundler",
PackageSourceInfo: "app/Gemfile.lock",
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("OperatingSystem-197f9a00ebcb51f0"),
PackageDownloadLocation: "NONE",
PackageName: "centos",
PackageVersion: "8.3.2011",
PrimaryPackagePurpose: tspdx.PackagePurposeOS,
},
{
PackageSPDXIdentifier: spdx.ElementID("ContainerImage-9396d894cd0cb6cb"),
Expand All @@ -201,6 +207,7 @@ func TestMarshaler_Marshal(t *testing.T) {
"DiffID: sha256:d871dadfb37b53ef1ca45be04fc527562b91989991a8f545345ae3be0b93f92a",
"RepoTag: rails:latest",
},
PrimaryPackagePurpose: tspdx.PackagePurposeContainer,
},
},
Relationships: []*spdx.Relationship{
Expand Down Expand Up @@ -352,7 +359,8 @@ func TestMarshaler_Marshal(t *testing.T) {
Locator: "pkg:rpm/centos/acl@2.2.53-1.el8?arch=aarch64&epoch=1&distro=centos-8.3.2011",
},
},
PackageSourceInfo: "built package from: acl 1:2.2.53-1.el8",
PackageSourceInfo: "built package from: acl 1:2.2.53-1.el8",
PrimaryPackagePurpose: tspdx.PackagePurposeInstall,
},
{
PackageSPDXIdentifier: spdx.ElementID("Package-13fe667a0805e6b7"),
Expand Down Expand Up @@ -383,6 +391,7 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Package-d5443dbcbba0dbd4"),
Expand Down Expand Up @@ -413,12 +422,14 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("OperatingSystem-197f9a00ebcb51f0"),
PackageDownloadLocation: "NONE",
PackageName: "centos",
PackageVersion: "8.3.2011",
PrimaryPackagePurpose: tspdx.PackagePurposeOS,
},
{
PackageName: "centos:latest",
Expand All @@ -430,12 +441,14 @@ func TestMarshaler_Marshal(t *testing.T) {
"Size: 1024",
"RepoTag: centos:latest",
},
PrimaryPackagePurpose: tspdx.PackagePurposeContainer,
},
{
PackageSPDXIdentifier: spdx.ElementID("Application-441a648f2aeeee72"),
PackageDownloadLocation: "NONE",
PackageName: "gemspec",
PackageSourceInfo: "Ruby",
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
},
Relationships: []*spdx.Relationship{
Expand Down Expand Up @@ -530,12 +543,14 @@ func TestMarshaler_Marshal(t *testing.T) {
Locator: "pkg:gem/actioncable@6.1.4.1",
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Application-9dd4a4ba7077cc5a"),
PackageDownloadLocation: "NONE",
PackageName: "bundler",
PackageSourceInfo: "Gemfile.lock",
PrimaryPackagePurpose: tspdx.PackagePurposeLibrary,
},
{
PackageSPDXIdentifier: spdx.ElementID("Filesystem-5af0f1f08c20909a"),
Expand Down Expand Up @@ -623,6 +638,7 @@ func TestMarshaler_Marshal(t *testing.T) {
PackageDownloadLocation: "git+http://test-aggregate",
PackageName: "node-pkg",
PackageSourceInfo: "Node.js",
PrimaryPackagePurpose: tspdx.PackagePurposeApplication,
},
{
PackageSPDXIdentifier: spdx.ElementID("Package-daedb173cfd43058"),
Expand All @@ -647,6 +663,7 @@ func TestMarshaler_Marshal(t *testing.T) {
FileSPDXIdentifier: "File-a52825a3e5bc6dfe",
},
},
PrimaryPackagePurpose: tspdx.PackagePurposeApplication,
},
},
Relationships: []*spdx.Relationship{
Expand Down