Documentation
¶
Overview ¶
Package sbom parses Software Bill of Materials documents into a unified data model. It reads CycloneDX JSON/XML and SPDX JSON/tag-value and normalises both into the same Document/Package/Relationship shape so callers don't need to care which format they were handed.
The model and field mappings are a port of github.com/andrew/sbom (Ruby).
doc, err := sbom.Parse(data)
for _, p := range doc.Packages {
fmt.Println(p.Name, p.Version, p.PURL())
}
Index ¶
Constants ¶
const ( SupplierOrganization = "Organization" SupplierPerson = "Person" )
Supplier/originator type values, shared across both formats.
const ( RelDependsOn = "DEPENDS_ON" RelDescribes = "DESCRIBES" )
Relationship type values. SPDX has ~40 of these; we name the two the parsers emit and ClassifyScope reads. CycloneDX dependency edges and SPDX DEPENDENCY_OF edges are normalised to RelDependsOn.
const ( ScopeDirect = "direct" ScopeTransitive = "transitive" )
Scope values returned by ClassifyScope.
Variables ¶
var ErrUnrecognized = errors.New("sbom: unrecognized format")
ErrUnrecognized is returned when the input does not look like any supported SBOM format.
Functions ¶
Types ¶
type Ancestor ¶ added in v0.1.8
Ancestor is a component a package was derived from, such as the upstream project of a fork (CycloneDX pedigree.ancestors). Ancestors describe provenance only: they are not installed packages and never produce relationships. Version is optional because an ancestor may be known only as a repository. CycloneDX requires Name; Type defaults to library.
type Component ¶
type Component struct {
ID string
Type string
Name string
Version string
LicenseExpression string
LicenseNames []string
ExtractedLicenses []ExtractedLicense
}
Component is the root subject the SBOM describes (CycloneDX metadata.component / SPDX root package). ID is its CycloneDX bom-ref, the identifier dependency edges from the root use; it is empty when the document gives none.
type Document ¶
type Document struct {
Name string
ID string
Type Type
SpecVersion string
DataLicense string
Namespace string
Created string
Supplier string
Creators []Creator
Component Component
}
Document holds metadata about the SBOM document itself, distinct from the packages it describes.
type ExternalRef ¶
ExternalRef is a typed pointer to an external resource. PURLs and CPEs are stored here in both formats.
type ExtractedLicense ¶ added in v0.1.4
ExtractedLicense is non-SPDX license text associated with a component. ID is optional. Encode prefixes a supplied ID with LicenseRef- when needed, or generates a stable LicenseRef when ID is empty.
type Package ¶
type Package struct {
ID string
Name string
Version string
Type string
Description string
Supplier string
SupplierType string
Originator string
OriginatorType string
Homepage string
DownloadLocation string
Filename string
// SourceInfo is SPDX's free-text note on where the package information
// came from (PackageSourceInfo).
SourceInfo string
LicenseConcluded string
LicenseDeclared string
Copyright string
Checksums []Checksum
ExternalRefs []ExternalRef
Properties []Property
Ancestors []Ancestor
}
Package is a single component/package entry from the SBOM, regardless of source format.
type Relationship ¶
type Relationship struct {
SourceID string
Source string
TargetID string
Target string
Type string
}
Relationship links two elements in the SBOM. SourceID/TargetID are the raw identifiers from the document; Source/Target are resolved names where the parser could determine them.
type SBOM ¶
type SBOM struct {
Type Type
SpecVersion string
Document Document
Packages []Package
Relationships []Relationship
// contains filtered or unexported fields
}
SBOM is the unified parse result.
func New ¶
New returns an empty SBOM ready for AddPackage calls. Use this when building a document for Encode rather than parsing one.
func (*SBOM) AddPackage ¶
AddPackage appends p, replacing any existing package with the same name, version, document ID, and set of PURLs.
func (*SBOM) ClassifyScope ¶ added in v0.1.3
ClassifyScope derives direct-vs-transitive scope for each package from the relationship graph. A package is ScopeDirect if a root depends on it, ScopeTransitive if only a non-root does, and absent from the map if the graph doesn't mention it at all.
When the document declares its root, as the target of a DESCRIBES edge (SPDX's document-to-root-package link) or as Document.Component.ID (the CycloneDX metadata.component bom-ref), and that root has DEPENDS_ON edges, the declared roots are the only roots. Otherwise roots are inferred: nodes that originate DEPENDS_ON edges but are never a DEPENDS_ON target, plus any declared root. An inferred root is not necessarily the project: for a lock file that doesn't list the project itself the top-level dependencies are inferred as roots, except for packages syft records as coming from such a lock file (poetry.lock, pdm.lock, pnpm-lock.yaml and yarn v1's yarn.lock): those are ScopeDirect themselves, and what they depend on is transitive. A package from such a lock file that no edge names is ScopeDirect too, when other packages of the lock file have edges.
Returns nil for a flat-list SBOM with no dependency graph so callers can tell "no graph" apart from "graph present but this package is a root".
func (*SBOM) FilterProperties ¶ added in v0.1.2
FilterProperties walks every package and removes properties whose names keep returns false for. Useful when handing a document to a downstream consumer that doesn't recognise a particular property namespace — for example, strip a tool-specific "mytool:" prefix before sharing outside the team that produced it. The filter mutates the SBOM in place; callers wanting a copy should Encode + Parse around the call.
Document- and Component-level metadata is untouched; only the per-package Properties slice is filtered.
func (*SBOM) Roots ¶ added in v0.1.8
Roots returns the IDs ClassifyScope treats as roots, and whether all of them are declared by the document rather than inferred from the graph. Declared roots are the project; inferred ones may be top-level dependencies of a lock file that leaves the project out. It returns nil for a flat-list SBOM.