Documentation
¶
Overview ¶
Package sbomgen provides a library API for generating CycloneDX SBOMs.
This package wraps the scanner and output internals to provide a simple, programmatic interface for SBOM generation without requiring CLI dependencies.
Usage:
result, err := sbomgen.GenerateSBOM([]string{"./my-project"}, sbomgen.DefaultOptions())
if err != nil {
log.Fatal(err)
}
fmt.Println(string(result))
Index ¶
- func AddNpmWorkspaceEdges(bom *cyclonedx.BOM, rootDirs ...string)
- func GenerateSBOM(dirs []string, opts Options) ([]byte, error)
- func GetBuildFileTrees(sbom []byte, filters ...FileType) map[BuildFile]BuildFileRelations
- func RegisterBuildFileProcessor(ft FileType, p BuildFileProcessor)
- type BuildFile
- type BuildFileProcessor
- type BuildFileRelations
- type BuildFileWithHopCount
- type FileType
- type Options
- type ProcessorContext
- type SimpleProcessor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddNpmWorkspaceEdges ¶ added in v1.17.4
AddNpmWorkspaceEdges enriches bom.Dependencies with inter-workspace dependency edges for npm/yarn/pnpm/bun monorepos. It must be called after the BOM is built but before serialization, while the scan root directories are still known.
For each package.json file component in the BOM, this function reads the file from disk by trying each rootDir in order until one succeeds (multi-root scans have BOMRefs relative to different roots). It checks dependencies against the workspace name→path index built from all npm file components' datadog:maven-package properties, and adds any missing edges to bom.Dependencies.
This has no effect on normal package scanning or SBOM output — it only enriches the dependency graph used by GetBuildFileTrees.
func GenerateSBOM ¶
GenerateSBOM scans the given directories for lockfiles and returns a CycloneDX 1.5 SBOM as pretty-printed JSON bytes.
It returns an error if dirs is empty or if the scan finds no packages.
func GetBuildFileTrees ¶
func GetBuildFileTrees(sbom []byte, filters ...FileType) map[BuildFile]BuildFileRelations
GetBuildFileTrees parses a CycloneDX JSON SBOM and returns a map of all manifest build files found in component evidence occurrences, each enriched with its resolved relationships (parent, children).
Build files are grouped by FileType and dispatched to a registered BuildFileProcessor for that type. Each processor receives deduplicated BuildFiles of its type and a ProcessorContext derived from the SBOM dependencies section, and returns the files enriched with their relationships. Results from all processors are merged into the returned map.
If no processor is registered for a FileType, a no-op processor is used that returns each file with empty relations.
If filters are provided, only BuildFiles whose FileType matches one of the filters are included.
func RegisterBuildFileProcessor ¶
func RegisterBuildFileProcessor(ft FileType, p BuildFileProcessor)
RegisterBuildFileProcessor registers a processor for the given FileType. It is intended to be called from init() functions.
Types ¶
type BuildFile ¶
type BuildFile struct {
FileType FileType
FilePath string // relative path within the repo (e.g. "backend/Cargo.toml")
RepoPath string // absolute filesystem path of the repo root (empty when not available)
}
BuildFile identifies a build/manifest file within a repository.
type BuildFileProcessor ¶
type BuildFileProcessor interface {
Process(files []BuildFile, ctx ProcessorContext) map[BuildFile]BuildFileRelations
}
BuildFileProcessor enriches a group of build files of the same FileType. Implementations receive all deduplicated BuildFiles of one type and a ProcessorContext derived from the SBOM, and return a map of each file to its resolved relationships.
Processors are registered per FileType via RegisterBuildFileProcessor, typically from an init() function.
type BuildFileRelations ¶
type BuildFileRelations struct {
// ID is an ecosystem-specific identifier for the build file.
// For Maven this is "groupId:artifactId"; for other ecosystems it is empty.
ID string
// Dependencies lists all transitively reachable build files, sorted by
// FilePath, each annotated with its hop distance from this file.
Dependencies []BuildFileWithHopCount
}
BuildFileRelations holds the resolved relationships of a build file.
ID is an ecosystem-specific identifier (e.g. Maven "groupId:artifactId"). Dependencies lists ALL transitively reachable build files, sorted by FilePath. For Maven, this is the transitive closure walking up the parent chain (parent, grandparent, etc.). For ecosystems without a registered processor the slice is empty.
type BuildFileWithHopCount ¶ added in v1.17.0
BuildFileWithHopCount wraps a BuildFile with its hop distance from the source file. HopCount is 1 for direct dependencies, 2 for dependencies-of-dependencies, and so on.
type FileType ¶
type FileType string
FileType represents a recognized build/manifest file type.
const ( FileTypeBUILDBazel FileType = "BUILD.bazel" FileTypeBUILD FileType = "BUILD" FileTypePomXML FileType = "pom.xml" FileTypeCargoToml FileType = "Cargo.toml" FileTypePackageJSON FileType = "package.json" FileTypeBuildGradle FileType = "build.gradle" FileTypeBuildGradleKts FileType = "build.gradle.kts" FileTypeComposerJSON FileType = "composer.json" FileTypeGemfile FileType = "Gemfile" FileTypePackageSwift FileType = "Package.swift" FileTypePyprojectToml FileType = "pyproject.toml" FileTypePipfile FileType = "Pipfile" FileTypeRequirementsTxt FileType = "requirements.txt" FileTypeCsproj FileType = "*.csproj" FileTypeGoMod FileType = "go.mod" )
type Options ¶
type Options struct {
// Recursive controls whether subdirectories are scanned.
Recursive bool
// ExcludePaths is a list of glob patterns to exclude from scanning.
ExcludePaths []string
// ManifestParsers enables extractors that read manifest files (e.g.
// pyproject.toml, package.json) as package sources when no lockfile is
// present. Additive to the default lockfile extractor set.
ManifestParsers bool
// ExtractArtifactIds controls whether build file artifact IDs and dependency
// relationships are extracted and included in the SBOM. When true, extractors
// that implement ArtifactExtractor produce file-type components and dependency
// edges, enabling GetBuildFileTrees dependency and ID resolution.
// Enabling this also activates manifest parsers internally, because manifest
// extractors (e.g. PyProjectTOMLExtractor) are needed to call GetArtifact.
// Defaults to true in DefaultOptions.
ExtractArtifactIds bool
}
Options controls the behavior of GenerateSBOM.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns Options with sensible defaults: recursive scanning enabled, no exclusions, and artifact extraction enabled.
type ProcessorContext ¶
type ProcessorContext struct {
// FileDependencies maps each file's path to the paths it directly depends
// on, as declared in the SBOM dependencies section.
FileDependencies map[string][]string
// ArtifactIDs maps each file path to its ecosystem-specific artifact
// identifier, extracted from the "datadog:maven-package" purl property on
// file-type components. For Maven this is "groupId:artifactId"; for PyPI
// it is the normalized package name.
ArtifactIDs map[string]string
}
ProcessorContext carries SBOM-derived data that processors can use for enrichment without needing filesystem access.
type SimpleProcessor ¶ added in v1.17.0
type SimpleProcessor struct{}
SimpleProcessor enriches BuildFiles with transitive dependencies and ecosystem-specific IDs derived from the SBOM. It uses BFS over ProcessorContext.FileDependencies to compute the transitive closure, with a depth limit that scales with the number of build files in scope.
The ID field is populated from ProcessorContext.ArtifactIDs.
SimpleProcessor is suitable for any file type that follows the "BFS + optional artifact ID" pattern. Register it for each such FileType in init().
func (*SimpleProcessor) Process ¶ added in v1.17.0
func (p *SimpleProcessor) Process(files []BuildFile, ctx ProcessorContext) map[BuildFile]BuildFileRelations
Process resolves transitive dependencies and IDs for a set of BuildFiles.