Skip to content

Commit

Permalink
cleanup: small refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <git@ckotzbauer.de>
  • Loading branch information
ckotzbauer committed Jan 27, 2022
1 parent 4996730 commit 41d33fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
7 changes: 5 additions & 2 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ func (c *CronService) runBackgroundService() {
processedSbomFiles := []string{}

for _, d := range digests {
sbomPath := sy.ExecuteSyft(d)
processedSbomFiles = append(processedSbomFiles, sbomPath)
sbomPath, err := sy.ExecuteSyft(d)
// Error is already handled from syft module.
if err == nil {
processedSbomFiles = append(processedSbomFiles, sbomPath)
}
}

for _, t := range c.targets {
Expand Down
35 changes: 20 additions & 15 deletions internal/syft/syft.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package syft

import (
"fmt"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"
"os"
"path"
"path/filepath"
"runtime/debug"
"strings"

"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/format"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"

"github.com/anchore/syft/syft/source"
util "github.com/ckotzbauer/sbom-operator/internal"
"github.com/ckotzbauer/sbom-operator/internal/kubernetes"
Expand All @@ -33,14 +34,14 @@ func New(gitWorkingTree, gitPath, sbomFormat string) Syft {
}
}

func (s *Syft) ExecuteSyft(img kubernetes.ImageDigest) string {
func (s *Syft) ExecuteSyft(img kubernetes.ImageDigest) (string, error) {
fileName := GetFileName(s.SbomFormat)
filePath := strings.ReplaceAll(img.Digest, "@", "/")
filePath = strings.ReplaceAll(path.Join(s.GitWorkingTree, s.GitPath, filePath, fileName), ":", "_")

if util.PathExists(filePath) {
logrus.Debugf("Skip image %s", img.Digest)
return filePath
return filePath, nil
}

logrus.Debugf("Processing image %s", img.Digest)
Expand All @@ -53,13 +54,15 @@ func (s *Syft) ExecuteSyft(img kubernetes.ImageDigest) string {

if err != nil {
logrus.WithError(err).Error("Image-Pull failed")
return filePath
return "", err
}

src, cleanup, err := source.New(filepath.Join("oci-archive:", imagePath), nil, nil)
if err != nil {
panic(fmt.Errorf("failed to construct source from input %s: %w", imagePath, err))
logrus.WithError(fmt.Errorf("failed to construct source from input %s: %w", imagePath, err)).Error("Source-Creation failed")
return "", err
}

if cleanup != nil {
defer cleanup()
}
Expand All @@ -80,15 +83,17 @@ func (s *Syft) ExecuteSyft(img kubernetes.ImageDigest) string {
}

result := sbom.SBOM{
Source: src.Metadata,
Source: src.Metadata,
Descriptor: descriptor,
// TODO: we should have helper functions for getting this built from exported library functions
}

c := cataloger.DefaultConfig()
c.Search.Scope = source.SquashedScope
packageCatalog, relationships, theDistro, err := syft.CatalogPackages(src, c)
if err != nil {
panic(err)
logrus.WithError(err).Error("CatalogPackages failed")
return "", err
}

result.Artifacts.PackageCatalog = packageCatalog
Expand All @@ -98,26 +103,26 @@ func (s *Syft) ExecuteSyft(img kubernetes.ImageDigest) string {
// you can use other formats such as format.CycloneDxJSONOption or format.SPDXJSONOption ...
b, err := syft.Encode(result, format.Option(s.SbomFormat))
if err != nil {
panic(err)
logrus.WithError(err).Error("Encoding of result failed")
return "", err
}

os.RemoveAll(workDir)

dir := filepath.Dir(filePath)
err = os.MkdirAll(dir, 0777)

if err != nil {
logrus.WithError(err).Error("Directory could not be created")
return filePath
return "", err
}

err = os.WriteFile(filePath, b, 0640)

if err != nil {
logrus.WithError(err).Error("SBOM could not be saved")
return "", err
}

return filePath
return filePath, nil
}

func GetFileName(sbomFormat string) string {
Expand Down

0 comments on commit 41d33fa

Please sign in to comment.