-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds convenience methods for using the Syft CLI to create SBoM inform…
…ation - Supports running syft against a directory & generating one or more output formats - Automatically converts syft generated CycloneDX XML to JSON, which is what buildpacks require - This is working around a couple features not present in syft at the moment, such as support for multiple output formats or conversion, and being able to output directly to CycloneDX JSON. When those features are added, we can trim this code back accordingly. The function signatures should not need to change for that. Signed-off-by: Daniel Mikusa <[email protected]>
- Loading branch information
Daniel Mikusa
committed
Nov 16, 2021
1 parent
2a99c61
commit af0aa6a
Showing
8 changed files
with
392 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package sherpa | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/CycloneDX/cyclonedx-go" | ||
"github.com/buildpacks/libcnb" | ||
"github.com/paketo-buildpacks/libpak/bard" | ||
"github.com/paketo-buildpacks/libpak/effect" | ||
) | ||
|
||
//go:generate mockery -name BOMScanner -case=underscore | ||
|
||
type BOMScanner interface { | ||
Scan(location libcnb.BOMLocation, scanDir string, formats ...libcnb.BOMFormat) | ||
} | ||
|
||
type SyftCLIBOMScanner struct { | ||
Layer libcnb.Layer | ||
Executor effect.Executor | ||
Logger bard.Logger | ||
} | ||
|
||
// Scan will use syft CLI to scan the scanDir and write it's output to the location in the given formats | ||
func (b SyftCLIBOMScanner) Scan(location libcnb.BOMLocation, scanDir string, formats ...libcnb.BOMFormat) error { | ||
// syft doesn't presently support outputting multiple formats at once | ||
// to workaround this we are running syft multiple times | ||
// when syft supports multiple output formats or conversion between formats, this method should change | ||
for _, format := range formats { | ||
if err := b.runSyft(location, scanDir, format); err != nil { | ||
return fmt.Errorf("unable to run syft\n%w", err) | ||
} | ||
|
||
if format == libcnb.CycloneDXJSON { | ||
// syft doesn't presently support cyclonedx JSON output and we need to convert | ||
// until https://github.com/anchore/syft/issues/631 is addressed | ||
if err := b.ConvertCycloneDXXMLtoJSON(location, false); err != nil { | ||
return fmt.Errorf("unable convert XML to JSON\n%w", err) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// ConvertCycloneDXXMLtoJSON reads input CycloneDX XML, converts to JSON and overwrites the XML optionally keeping a backup copy of the xml | ||
func (b SyftCLIBOMScanner) ConvertCycloneDXXMLtoJSON(location libcnb.BOMLocation, backup bool) error { | ||
inputPath := b.Layer.BOMPath(location, libcnb.CycloneDXJSON) | ||
|
||
if backup { | ||
if err := b.backupXMLFile(inputPath); err != nil { | ||
return fmt.Errorf("unable to backup file\n%w", err) | ||
} | ||
} | ||
|
||
bom, err := b.readXMLBOM(inputPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to backup file\n%w", err) | ||
} | ||
|
||
if err := b.writeJSONBOM(inputPath, bom); err != nil { | ||
return fmt.Errorf("unable to backup file\n%w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b SyftCLIBOMScanner) writeJSONBOM(outputPath string, bom cyclonedx.BOM) error { | ||
outputFile, err := os.Create(outputPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to create BOM file %s\n%w", outputPath, err) | ||
} | ||
defer outputFile.Close() | ||
|
||
decoder := cyclonedx.NewBOMEncoder(outputFile, cyclonedx.BOMFileFormatJSON) | ||
if err = decoder.Encode(&bom); err != nil { | ||
return fmt.Errorf("unable to decode BOM\n%w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b SyftCLIBOMScanner) readXMLBOM(inputPath string) (cyclonedx.BOM, error) { | ||
inputFile, err := os.Open(inputPath) | ||
if err != nil { | ||
return cyclonedx.BOM{}, fmt.Errorf("unable to read file to convert %s\n%w", inputPath, err) | ||
} | ||
defer inputFile.Close() | ||
|
||
var bom cyclonedx.BOM | ||
decoder := cyclonedx.NewBOMDecoder(inputFile, cyclonedx.BOMFileFormatXML) | ||
if err = decoder.Decode(&bom); err != nil { | ||
return cyclonedx.BOM{}, fmt.Errorf("unable to decode BOM\n%w", err) | ||
} | ||
|
||
return bom, nil | ||
} | ||
|
||
func (b SyftCLIBOMScanner) backupXMLFile(inputPath string) error { | ||
backupPath := fmt.Sprintf("%s.bak", inputPath) | ||
outputFile, err := os.Create(backupPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to create backup file %s\n%w", backupPath, err) | ||
} | ||
defer outputFile.Close() | ||
|
||
inputFile, err := os.Open(inputPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to read file for backup %s\n%w", inputPath, err) | ||
} | ||
defer inputFile.Close() | ||
|
||
_, err = io.Copy(outputFile, inputFile) | ||
return err | ||
} | ||
|
||
func (b SyftCLIBOMScanner) runSyft(location libcnb.BOMLocation, scanDir string, format libcnb.BOMFormat) error { | ||
bomOutputPath := b.Layer.BOMPath(location, format) | ||
writer, err := os.Create(bomOutputPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to open output BOM file %s\n%w", bomOutputPath, err) | ||
} | ||
defer writer.Close() | ||
|
||
err = b.Executor.Execute(effect.Execution{ | ||
Command: "syft", | ||
Args: []string{"packges", "-o", BOMFormatToSyftOutputFormat(format), fmt.Sprintf("dir:%s", scanDir)}, | ||
Stdout: writer, | ||
Stderr: b.Logger.TerminalErrorWriter(), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("unable to run syft on directory %s\n%w", scanDir, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// BOMFormatToSyftOutputFormat converts a libcnb.BOMFormat to the syft matching syft output format string | ||
func BOMFormatToSyftOutputFormat(format libcnb.BOMFormat) string { | ||
var formatRaw string | ||
|
||
switch format { | ||
case libcnb.CycloneDXJSON: | ||
formatRaw = "cyclonedx" | ||
case libcnb.SPDXJSON: | ||
formatRaw = "spdx-json" | ||
case libcnb.SyftJSON: | ||
formatRaw = "json" | ||
} | ||
|
||
return formatRaw | ||
} |
Oops, something went wrong.