Skip to content

Commit

Permalink
fix sbom generation inconsistency, take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
damdo committed Oct 22, 2023
1 parent 84fe9d9 commit 796b7ff
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
9 changes: 9 additions & 0 deletions internal/gok/overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func (r *overwriteImplConfig) run(ctx context.Context, args []string, stdout, st
return err
}

// GenerateSBOM() must be called before any modifications to cfg.InternalCompatibilityFlags,
// as the SBOM should reflect what’s going into gokrazy,
// not its internal implementation details.
sbom, _, err := packer.GenerateSBOM(cfg)
if err != nil {
return err
}

if cfg.InternalCompatibilityFlags == nil {
cfg.InternalCompatibilityFlags = &config.InternalCompatibilityFlags{}
}
Expand Down Expand Up @@ -123,6 +131,7 @@ func (r *overwriteImplConfig) run(ctx context.Context, args []string, stdout, st
pack := &packer.Pack{
Cfg: cfg,
Output: &output,
SBOM: sbom,
}

pack.Main("gokrazy gok")
Expand Down
3 changes: 3 additions & 0 deletions internal/gok/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (r *sbomConfig) run(ctx context.Context, args []string, stdout, stderr io.W

updateflag.SetUpdate("yes")

// GenerateSBOM() must be called before any modifications to cfg.InternalCompatibilityFlags,
// as the SBOM should reflect what’s going into gokrazy,
// not its internal implementation details.
sbomMarshaled, sbomWithHash, err := packer.GenerateSBOM(cfg)
if os.IsNotExist(err) {
// Common case, handle with a good error message
Expand Down
11 changes: 10 additions & 1 deletion internal/gok/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func (r *updateImplConfig) run(ctx context.Context, args []string, stdout, stder
return err
}

// GenerateSBOM() must be called before any modifications to cfg.InternalCompatibilityFlags,
// as the SBOM should reflect what’s going into gokrazy,
// not its internal implementation details.
sbom, _, err := packer.GenerateSBOM(cfg)
if err != nil {
return err
}

if cfg.InternalCompatibilityFlags == nil {
cfg.InternalCompatibilityFlags = &config.InternalCompatibilityFlags{}
}
Expand Down Expand Up @@ -77,7 +85,8 @@ func (r *updateImplConfig) run(ctx context.Context, args []string, stdout, stder
}

pack := &packer.Pack{
Cfg: cfg,
Cfg: cfg,
SBOM: sbom,
}

pack.Main("gokrazy gok")
Expand Down
7 changes: 1 addition & 6 deletions internal/packer/gaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func (p *Pack) overwriteGaf(root *FileInfo) error {
return err
}

sbomMarshaled, _, err := GenerateSBOM(p.Cfg)
if err != nil {
return err
}

if _, err := tmpSBOM.Write(sbomMarshaled); err != nil {
if _, err := tmpSBOM.Write(p.SBOM); err != nil {
return err
}

Expand Down
7 changes: 2 additions & 5 deletions internal/packer/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ type Pack struct {

Cfg *config.Struct
Output *OutputStruct
SBOM []byte
}

func filterGoEnv(env []string) []string {
Expand Down Expand Up @@ -1366,14 +1367,10 @@ func (pack *Pack) logic(programName string) error {
FromLiteral: update.HTTPSPort,
})

sbom, _, err := GenerateSBOM(cfg)
if err != nil {
return err
}
etcGokrazy := &FileInfo{Filename: "gokrazy"}
etcGokrazy.Dirents = append(etcGokrazy.Dirents, &FileInfo{
Filename: "sbom.json",
FromLiteral: string(sbom),
FromLiteral: string(pack.SBOM),
})
etc.Dirents = append(etc.Dirents, etcGokrazy)

Expand Down
3 changes: 3 additions & 0 deletions internal/packer/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ type SBOMWithHash struct {

// GenerateSBOM generates a Software Bills Of Material (SBOM) for the
// local gokrazy instance.
// It must be called before any modifications to cfg.InternalCompatibilityFlags,
// as the SBOM should reflect what’s going into gokrazy,
// not its internal implementation details.
func GenerateSBOM(cfg *config.Struct) ([]byte, SBOMWithHash, error) {
wd, err := os.Getwd()
if err != nil {
Expand Down

0 comments on commit 796b7ff

Please sign in to comment.