Skip to content

Commit

Permalink
Move stage SBOM files logic to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed Jan 31, 2024
1 parent def07e2 commit 2524bcc
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 53 deletions.
17 changes: 0 additions & 17 deletions src/internal/packager/sbom/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ package sbom

import (
"fmt"
"os"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
)

Expand Down Expand Up @@ -39,18 +37,3 @@ func ViewSBOMFiles(directory string) {
message.Note("There were no images with software bill-of-materials (SBOM) included.")
}
}

// OutputSBOMFiles outputs the sbom files into a specified directory.
func OutputSBOMFiles(sourceDir, outputDir, packageName string) (string, error) {
packagePath := filepath.Join(outputDir, packageName)

if err := os.RemoveAll(packagePath); err != nil {
return "", err
}

if err := utils.CreateDirectory(packagePath, 0700); err != nil {
return "", err
}

return packagePath, utils.CreatePathAndCopy(sourceDir, packagePath)
}
29 changes: 22 additions & 7 deletions src/pkg/layout/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package layout

import (
"fmt"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -67,12 +68,26 @@ func (s *SBOMs) Archive() (err error) {
return os.RemoveAll(dir)
}

// IsDir returns true if the SBOMs are a directory.
func (s SBOMs) IsDir() bool {
return utils.IsDir(s.Path)
}
func (s *SBOMs) StageSBOMViewFiles() (sbomViewFiles, warnings []string, err error) {

Check warning on line 71 in src/pkg/layout/sbom.go

View workflow job for this annotation

GitHub Actions / validate

exported method SBOMs.StageSBOMViewFiles should have comment or be unexported
isTarball := !utils.IsDir(s.Path) && filepath.Ext(s.Path) == ".tar"
if isTarball {
return nil, nil, fmt.Errorf("unable to process the SBOM files for this package: %s is a tarball", s.Path)
}

// If SBOMs were loaded, temporarily place them in the deploy directory
if !utils.InvalidPath(s.Path) {
sbomViewFiles, err = filepath.Glob(filepath.Join(s.Path, "sbom-viewer-*"))
if err != nil {
return nil, nil, err
}

_, err := utils.OutputSBOMFiles(s.Path, SBOMDir, "")
if err != nil {
// Don't stop the deployment, let the user decide if they want to continue the deployment
warning := fmt.Sprintf("Unable to process the SBOM files for this package: %s", err.Error())
warnings = append(warnings, warning)
}
}

// IsTarball returns true if the SBOMs are a tarball.
func (s SBOMs) IsTarball() bool {
return !s.IsDir() && filepath.Ext(s.Path) == ".tar"
return sbomViewFiles, warnings, nil
}
21 changes: 0 additions & 21 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
Expand All @@ -17,7 +16,6 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/internal/packager/sbom"
"github.com/defenseunicorns/zarf/src/internal/packager/template"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/types"
Expand All @@ -40,7 +38,6 @@ type Packager struct {
valueTemplate *template.Values
hpaModified bool
connectStrings types.ConnectStrings
sbomViewFiles []string
source sources.PackageSource
generation int
}
Expand Down Expand Up @@ -281,21 +278,3 @@ func (p *Packager) validateLastNonBreakingVersion() (err error) {

return nil
}

func (p *Packager) stageSBOMViewFiles() error {
if p.layout.SBOMs.IsTarball() {
return fmt.Errorf("unable to process the SBOM files for this package: %s is a tarball", p.layout.SBOMs.Path)
}
// If SBOMs were loaded, temporarily place them in the deploy directory
sbomDir := p.layout.SBOMs.Path
if !utils.InvalidPath(sbomDir) {
p.sbomViewFiles, _ = filepath.Glob(filepath.Join(sbomDir, "sbom-viewer-*"))
_, err := sbom.OutputSBOMFiles(sbomDir, layout.SBOMDir, "")
if err != nil {
// Don't stop the deployment, let the user decide if they want to continue the deployment
warning := fmt.Sprintf("Unable to process the SBOM files for this package: %s", err.Error())
p.warnings = append(p.warnings, warning)
}
}
return nil
}
2 changes: 1 addition & 1 deletion src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *Packager) Create() (err error) {
return fmt.Errorf("unable to validate package: %w", err)
}

if !utils.ConfirmAction(config.ZarfCreateStage, layout.SBOMDir, p.sbomViewFiles, p.warnings, *loadedPkg, p.cfg.PkgOpts) {
if !utils.ConfirmAction(config.ZarfCreateStage, layout.SBOMDir, []string{}, p.warnings, *loadedPkg, p.cfg.PkgOpts) {
return fmt.Errorf("package creation canceled")
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (pc *PackageCreator) Output(loadedPkg *types.ZarfPackage, dst *layout.Packa
sbomDir = dst.SBOMs.Path

if outputSBOM != "" {
out, err := sbom.OutputSBOMFiles(sbomDir, outputSBOM, loadedPkg.Metadata.Name)
out, err := utils.OutputSBOMFiles(sbomDir, outputSBOM, loadedPkg.Metadata.Name)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ func (p *Packager) Deploy() (err error) {
return err
}

if err := p.stageSBOMViewFiles(); err != nil {
sbomViewFiles, sbomWarnings, err := p.layout.SBOMs.StageSBOMViewFiles()
if err != nil {
return err
}

p.warnings = append(p.warnings, sbomWarnings...)

// Confirm the overall package deployment
if !utils.ConfirmAction(config.ZarfCreateStage, layout.SBOMDir, p.sbomViewFiles, p.warnings, p.cfg.Pkg, p.cfg.PkgOpts) {
if !utils.ConfirmAction(config.ZarfDeployStage, layout.SBOMDir, sbomViewFiles, p.warnings, p.cfg.Pkg, p.cfg.PkgOpts) {
return fmt.Errorf("deployment cancelled")
}

Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (p *Packager) Inspect() (err error) {
sbomDir := p.layout.SBOMs.Path

if p.cfg.InspectOpts.SBOMOutputDir != "" {
out, err := sbom.OutputSBOMFiles(sbomDir, p.cfg.InspectOpts.SBOMOutputDir, p.cfg.Pkg.Metadata.Name)
out, err := utils.OutputSBOMFiles(sbomDir, p.cfg.InspectOpts.SBOMOutputDir, p.cfg.Pkg.Metadata.Name)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions src/pkg/packager/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@ func (p *Packager) Mirror() (err error) {
if err = p.source.LoadPackage(p.layout, true); err != nil {
return fmt.Errorf("unable to load the package: %w", err)
}

if err := utils.ReadYaml(p.layout.ZarfYAML, &p.cfg.Pkg); err != nil {
return err
}
if err := p.stageSBOMViewFiles(); err != nil {

sbomViewFiles, sbomWarnings, err := p.layout.SBOMs.StageSBOMViewFiles()
if err != nil {
return err
}

p.warnings = append(p.warnings, sbomWarnings...)

// Confirm the overall package mirror
if !utils.ConfirmAction(config.ZarfCreateStage, layout.SBOMDir, p.sbomViewFiles, p.warnings, p.cfg.Pkg, p.cfg.PkgOpts) {
if !utils.ConfirmAction(config.ZarfMirrorStage, layout.SBOMDir, sbomViewFiles, p.warnings, p.cfg.Pkg, p.cfg.PkgOpts) {
return fmt.Errorf("mirror cancelled")
}

Expand Down
25 changes: 25 additions & 0 deletions src/pkg/utils/sbom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package utils provides generic helper functions.
package utils

import (
"os"
"path/filepath"
)

// OutputSBOMFiles outputs the sbom files into a specified directory.
func OutputSBOMFiles(sourceDir, outputDir, packageName string) (string, error) {
packagePath := filepath.Join(outputDir, packageName)

if err := os.RemoveAll(packagePath); err != nil {
return "", err
}

if err := CreateDirectory(packagePath, 0700); err != nil {
return "", err
}

return packagePath, CreatePathAndCopy(sourceDir, packagePath)
}
2 changes: 1 addition & 1 deletion src/test/e2e/20_zarf_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestZarfInit(t *testing.T) {
require.NoError(t, err)
require.Contains(t, initStdErr, "an inventory of all software contained in this package")

logText := e2e.GetLogFileContents(t, initStdErr)
logText := e2e.GetLogFileContents(t, e2e.StripMessageFormatting(initStdErr))

// Verify that any state secrets were not included in the log
state := types.ZarfState{}
Expand Down

0 comments on commit 2524bcc

Please sign in to comment.