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

Add initial support for generating SPDX SBOM documents (COMPOSER-2274) #930

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions Schutzfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
"centos-8": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
}
},
"centos-9": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
}
},
"fedora-39": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
},
"repos": [
Expand Down Expand Up @@ -65,7 +65,7 @@
"fedora-40": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
},
"repos": [
Expand Down
7 changes: 4 additions & 3 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
)

func makeManifest(
Expand Down Expand Up @@ -133,12 +134,12 @@ func depsolve(cacheDir string, packageSets map[string][]rpmmd.PackageSet, d dist
depsolvedSets := make(map[string][]rpmmd.PackageSpec)
repoSets := make(map[string][]rpmmd.RepoConfig)
for name, pkgSet := range packageSets {
pkgs, repos, err := solver.Depsolve(pkgSet)
res, err := solver.Depsolve(pkgSet, sbom.StandardTypeNone)
if err != nil {
return nil, nil, err
}
depsolvedSets[name] = pkgs
repoSets[name] = repos
depsolvedSets[name] = res.Packages
repoSets[name] = res.Repos
}
return depsolvedSets, repoSets, nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/osbuild/images/pkg/reporegistry"
"github.com/osbuild/images/pkg/rhsm/facts"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
)

type buildRequest struct {
Expand Down Expand Up @@ -357,12 +358,12 @@ func depsolve(cacheDir string, packageSets map[string][]rpmmd.PackageSet, d dist
depsolvedSets := make(map[string][]rpmmd.PackageSpec)
repoSets := make(map[string][]rpmmd.RepoConfig)
for name, pkgSet := range packageSets {
packages, repos, err := solver.Depsolve(pkgSet)
res, err := solver.Depsolve(pkgSet, sbom.StandardTypeNone)
if err != nil {
return nil, nil, err
}
depsolvedSets[name] = packages
repoSets[name] = repos
depsolvedSets[name] = res.Packages
repoSets[name] = res.Repos
}
return depsolvedSets, repoSets, nil
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/osbuild-playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/runner"
"github.com/osbuild/images/pkg/sbom"
)

func RunPlayground(img image.ImageKind, d distro.Distro, arch distro.Arch, repos map[string][]rpmmd.RepoConfig, state_dir string) {
Expand All @@ -36,11 +37,11 @@ func RunPlayground(img image.ImageKind, d distro.Distro, arch distro.Arch, repos

packageSpecs := make(map[string][]rpmmd.PackageSpec)
for name, chain := range manifest.GetPackageSetChains() {
packages, _, err := solver.Depsolve(chain)
res, err := solver.Depsolve(chain, sbom.StandardTypeNone)
if err != nil {
panic(fmt.Sprintf("failed to depsolve for pipeline %s: %s\n", name, err.Error()))
}
packageSpecs[name] = packages
packageSpecs[name] = res.Packages
}

if err := solver.CleanCache(); err != nil {
Expand Down
51 changes: 43 additions & 8 deletions pkg/dnfjson/dnfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/rhsm"
"github.com/osbuild/images/pkg/rpmmd"
"github.com/osbuild/images/pkg/sbom"
)

// BaseSolver defines the basic solver configuration without platform
Expand Down Expand Up @@ -155,6 +156,13 @@ type Solver struct {
subscriptions *rhsm.Subscriptions
}

// DepsolveResult contains the results of a depsolve operation.
type DepsolveResult struct {
Packages []rpmmd.PackageSpec
Repos []rpmmd.RepoConfig
SBOM *sbom.Document
}

// Create a new Solver with the given configuration. Initialising a Solver also loads system subscription information.
func NewSolver(modulePlatformID, releaseVer, arch, distro, cacheDir string) *Solver {
s := NewBaseSolver(cacheDir)
Expand Down Expand Up @@ -193,10 +201,10 @@ func (s *Solver) SetProxy(proxy string) error {
// their associated repositories. Each package set is depsolved as a separate
// transactions in a chain. It returns a list of all packages (with solved
// dependencies) that will be installed into the system.
func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet) ([]rpmmd.PackageSpec, []rpmmd.RepoConfig, error) {
req, rhsmMap, err := s.makeDepsolveRequest(pkgSets)
func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet, sbomType sbom.StandardType) (*DepsolveResult, error) {
req, rhsmMap, err := s.makeDepsolveRequest(pkgSets, sbomType)
if err != nil {
return nil, nil, fmt.Errorf("makeDepsolveRequest failed: %w", err)
return nil, fmt.Errorf("makeDepsolveRequest failed: %w", err)
}

// get non-exclusive read lock
Expand All @@ -205,7 +213,7 @@ func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet) ([]rpmmd.PackageSpec, []rp

output, err := run(s.dnfJsonCmd, req)
if err != nil {
return nil, nil, fmt.Errorf("running osbuild-depsolve-dnf failed:\n%w", err)
return nil, fmt.Errorf("running osbuild-depsolve-dnf failed:\n%w", err)
}
// touch repos to now
now := time.Now().Local()
Expand All @@ -219,11 +227,24 @@ func (s *Solver) Depsolve(pkgSets []rpmmd.PackageSet) ([]rpmmd.PackageSpec, []rp
dec := json.NewDecoder(bytes.NewReader(output))
dec.DisallowUnknownFields()
if err := dec.Decode(&result); err != nil {
return nil, nil, fmt.Errorf("decoding depsolve result failed: %w", err)
return nil, fmt.Errorf("decoding depsolve result failed: %w", err)
}

packages, repos := result.toRPMMD(rhsmMap)
return packages, repos, nil

var sbomDoc *sbom.Document
if sbomType != sbom.StandardTypeNone {
sbomDoc, err = sbom.NewDocument(sbomType, result.SBOM)
if err != nil {
return nil, fmt.Errorf("creating SBOM document failed: %w", err)
}
}

return &DepsolveResult{
Packages: packages,
Repos: repos,
SBOM: sbomDoc,
}, nil
}

// FetchMetadata returns the list of all the available packages in repos and
Expand Down Expand Up @@ -411,7 +432,7 @@ func (r *repoConfig) Hash() string {
// NOTE: Due to implementation limitations of DNF and dnf-json, each package set
// in the chain must use all of the repositories used by its predecessor.
// An error is returned if this requirement is not met.
func (s *Solver) makeDepsolveRequest(pkgSets []rpmmd.PackageSet) (*Request, map[string]bool, error) {
func (s *Solver) makeDepsolveRequest(pkgSets []rpmmd.PackageSet, sbomType sbom.StandardType) (*Request, map[string]bool, error) {
// dedupe repository configurations but maintain order
// the order in which repositories are added to the request affects the
// order of the dependencies in the result
Expand Down Expand Up @@ -478,6 +499,10 @@ func (s *Solver) makeDepsolveRequest(pkgSets []rpmmd.PackageSet) (*Request, map[
Arguments: args,
}

if sbomType != sbom.StandardTypeNone {
req.Arguments.Sbom = &sbomRequest{Type: sbomType.String()}
}

return &req, rhsmMap, nil
}

Expand Down Expand Up @@ -642,6 +667,10 @@ func (r *Request) Hash() string {
return fmt.Sprintf("%x", h.Sum(nil))
}

type sbomRequest struct {
Type string `json:"type"`
}

// arguments for a dnf-json request
type arguments struct {
// Repositories to use for depsolving
Expand All @@ -659,6 +688,9 @@ type arguments struct {

// Optional metadata to download for the repositories
OptionalMetadata []string `json:"optional-metadata,omitempty"`

// Optionally request an SBOM from depsolving
Sbom *sbomRequest `json:"sbom,omitempty"`
}

type searchArgs struct {
Expand Down Expand Up @@ -692,7 +724,10 @@ type depsolveResult struct {
Repos map[string]repoConfig `json:"repos"`

// (optional) contains the solver used, e.g. "dnf5"
Solver string `json:"solver"`
Solver string `json:"solver,omitempty"`

// (optional) contains the SBOM for the depsolved transaction
SBOM json.RawMessage `json:"sbom,omitempty"`
}

// Package specification
Expand Down
Loading
Loading