Documentation
¶
Overview ¶
Package sbom parses Software Bill of Materials documents into a unified data model. It reads CycloneDX and SPDX (JSON serialisations) 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 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 Component ¶
Component is the root subject the SBOM describes (CycloneDX metadata.component / SPDX root package).
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 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
LicenseConcluded string
LicenseDeclared string
Copyright string
Checksums []Checksum
ExternalRefs []ExternalRef
Properties []Property
}
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 Parse ¶
Parse sniffs the SBOM format from content and parses it. Only JSON serialisations are supported.
func (*SBOM) AddPackage ¶
AddPackage appends p, replacing any existing package with the same (Name, Version) pair.
func (*SBOM) ClassifyScope ¶ added in v0.1.3
ClassifyScope derives direct-vs-transitive scope for each package from the relationship graph. Roots are nodes that originate DEPENDS_ON edges but are never themselves a DEPENDS_ON target, plus anything a DESCRIBES edge points at (SPDX's document-to-root-package link). 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. 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.
type Type ¶
type Type string
Type identifies the source SBOM specification.
func Detect ¶
Detect inspects content and returns the SBOM Type without fully parsing it. Returns TypeUnknown if the format can't be determined.
Only the top-level object keys are scanned; nested arrays and objects are skipped without allocation so detection cost is independent of document size once a discriminator is found.