Skip to content

feat(sbomgen): expose library API for programmatic SBOM generation (SCI Usage)#155

Merged
anderruiz merged 3 commits into
mainfrom
ander/library-mode-support
May 26, 2026
Merged

feat(sbomgen): expose library API for programmatic SBOM generation (SCI Usage)#155
anderruiz merged 3 commits into
mainfrom
ander/library-mode-support

Conversation

@anderruiz

Copy link
Copy Markdown
Contributor

Summary

  • Add pkg/sbomgen package with a GenerateSBOM(dirs []string, opts Options) ([]byte, error) function that wraps the scanner + CycloneDX output for external consumers
  • Add Options struct and DefaultOptions() with recursive scanning enabled by default
  • Add examples/main.go demonstrating library usage (temporary reference implementation)

Motivation

The tool was previously only usable as a CLI binary. Any Go project wanting to generate SBOMs programmatically had to shell out to the binary. This change exposes a clean public API in pkg/sbomgen/ that can be imported directly.

Usage

import "github.com/DataDog/datadog-sbom-generator/pkg/sbomgen"

sbom, err := sbomgen.GenerateSBOM([]string{"./my-project"}, sbomgen.DefaultOptions())
if err != nil {
    log.Fatal(err)
}
fmt.Println(string(sbom)) // CycloneDX 1.5 JSON

Test plan

  • TestGenerateSBOM_HappyPath — scans a Cargo.lock fixture, validates CycloneDX output
  • TestGenerateSBOM_EmptyDirs — returns error on empty dirs slice
  • TestDefaultOptions — validates default option values
  • examples/main.go compiles and runs, printing valid CycloneDX JSON

🤖 Generated with Claude Code

anderruiz and others added 2 commits May 26, 2026 09:58
- Add pkg/sbomgen package with GenerateSBOM() and DefaultOptions() API
- GenerateSBOM accepts directory paths and options, returns CycloneDX JSON bytes
- Options struct exposes Recursive and ExcludePaths configuration
- Wraps scanner.DoScan and internal/output serialization without CLI dependencies
- Add tests: happy path with fixture scan, empty dirs error, default options validation
- Add testdata fixture with minimal Cargo.lock for isolated testing

Rationale: The CLI-only interface prevents programmatic usage of the SBOM
generator from Go code. This library API enables embedding SBOM generation
in other tools (e.g., GitHub Actions, CI pipelines) without shelling out
to the CLI binary.

This commit made by [/dd:git:commit:atomic](https://github.com/DataDog/claude-marketplace/tree/main/dd/commands/git/commit/atomic.md)
…ibrary usage

- Add examples/main.go that calls sbomgen.GenerateSBOM() and prints CycloneDX JSON
- Accepts optional directory argument; defaults to pkg/sbomgen/testdata
- Demonstrates the minimal API: GenerateSBOM([]string{dir}, DefaultOptions())

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@anderruiz
anderruiz requested a review from a team as a code owner May 26, 2026 08:19
@datadog-prod-us1-5

This comment has been minimized.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 956e325e5e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread pkg/sbomgen/sbomgen.go
Recursive: opts.Recursive,
}

r := reporter.NewCycloneDXReporter(&bytes.Buffer{}, &bytes.Buffer{}, reporter.WarnLevel)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Do not discard scanner failures during partial scans

When a scan contains at least one valid lockfile plus another lockfile that fails to parse, scanner.DoScan only reports the failed file via Reporter.Warnf and still returns results for the valid packages. This reporter writes warnings into private buffers that are never returned or exposed, so GenerateSBOM can return a successful but incomplete SBOM with no indication to library callers that some dependencies were skipped. The CLI surfaces the same condition on stderr, so the library wrapper should either expose warnings/errors or use a caller-provided reporter/writer.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can do it later

@anderruiz anderruiz changed the title feat(sbomgen): expose library API for programmatic SBOM generation feat(sbomgen): expose library API for programmatic SBOM generation (SCI Integration) May 26, 2026
@anderruiz anderruiz changed the title feat(sbomgen): expose library API for programmatic SBOM generation (SCI Integration) feat(sbomgen): expose library API for programmatic SBOM generation (SCI Usage) May 26, 2026
dastrong
dastrong previously approved these changes May 26, 2026

@dastrong dastrong left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we update the readme with this?

@anderruiz
anderruiz merged commit 3854205 into main May 26, 2026
1 check passed
@anderruiz
anderruiz deleted the ander/library-mode-support branch May 26, 2026 11:19
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