Skip to content

Commit

Permalink
use syft cli - bps should supply w syft bp
Browse files Browse the repository at this point in the history
  • Loading branch information
arjun024 committed Aug 7, 2024
1 parent d558b87 commit 236792d
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
package sbom

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

"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/cpe"
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/pkg/cataloger"
"github.com/anchore/syft/syft/sbom"
"github.com/anchore/syft/syft/source"
"github.com/paketo-buildpacks/packit/v2/pexec"
"github.com/paketo-buildpacks/packit/v2/postal"
)

Expand Down Expand Up @@ -118,6 +122,79 @@ func GenerateFromDependency(dependency postal.Dependency, path string) (SBOM, er
}, nil
}

func GenerateWithSyftCli(layersPath, layerName, scanDir string, mediaTypes ...string) error {

args := []string{"scan", "-q"}
for _, mediatype := range mediaTypes {
sbomWriteLocation := filepath.Join(layersPath, fmt.Sprintf("%s.sbom.%s", layerName, getExtension(mediatype)))

// TODO add @<version>
args = append(args, "--output", fmt.Sprintf("%s=%s", sbomFormatToSyftOutputFormat(mediatype), sbomWriteLocation))
// todo temporary
fmt.Printf("Writing SBOM to %s\n", sbomWriteLocation)
}

args = append(args, fmt.Sprintf("dir:%s", scanDir))

buffer := bytes.NewBuffer(nil)
if err := pexec.NewExecutable("syft").Execute(pexec.Execution{
Args: args,
Dir: scanDir,
Stdout: buffer,
Stderr: buffer,
}); err != nil {
return fmt.Errorf("unable to run `syft %s`\n%w\n%s", args, err, buffer)
}
// todo remove
fmt.Println("Finished syft command. output:")
fmt.Println(buffer)
fmt.Printf("args=%+v\n", args)

// TODO clean cyclonedx file which has a timestamp and unique id which always change
return nil
}

func getExtension(mediatype string) string {
switch {
case strings.HasPrefix(mediatype, CycloneDXFormat):
return "cdx.json"
case strings.HasPrefix(mediatype, SPDXFormat):
return "spdx.json"
// The syft tool does not support providing a version for its in-house standard.
case mediatype == SyftFormat:
return "syft.json"
default:
return ""
}
}

func sbomFormatToSyftOutputFormat(mediatype string) string {
fmt.Println("PPP: mediatype is " + mediatype)
optionalVersionSegment := extractVersionSegment(mediatype)

switch {
case strings.HasPrefix(mediatype, CycloneDXFormat):
return "cyclonedx-json" + optionalVersionSegment
case strings.HasPrefix(mediatype, SPDXFormat):
return "spdx-json" + optionalVersionSegment
// The syft tool does not support providing a version for its in-house standard.
case mediatype == SyftFormat:
return "syft-json"
default:
return ""
}
}

// look for a pattern like "application/vnd.cyclonedx+json;version=1.4"
func extractVersionSegment(input string) string {
parts := strings.Split(input, ";")
versionPart := parts[len(parts)-1]
if strings.HasPrefix(versionPart, "version=") {
return "@" + strings.Split(versionPart, "=")[1]
}
return ""
}

// InFormats returns a Formatter containing mappings for the given Formats.
func (s SBOM) InFormats(mediaTypes ...string) (Formatter, error) {
var fs []sbom.FormatID
Expand Down

0 comments on commit 236792d

Please sign in to comment.