mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 14:02:26 +01:00
Split the sbom.Format interface by encode and decode use cases (#2186)
* split up sbom.Format into encode and decode ops Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update cmd pkg to inject format configs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * bump cyclonedx schema to 1.5 Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * redact image metadata from github encoder tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add more testing around format decoder identify Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add test case for format version options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix CLI test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] - review comments Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep encoder creation out of post load function Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep decider and identify functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add a few more doc comments Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * remove format encoder default function helpers Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * address PR feedback Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * move back to streaming based decode functions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * with common convention for encoder constructors Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests and allow for encoders to be created from cli options Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * buffer reads from stdin to support seeking Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
@@ -6,8 +6,9 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/formats"
|
||||
"github.com/anchore/syft/syft/formats/template"
|
||||
"github.com/anchore/syft/cmd/syft/cli/options"
|
||||
"github.com/anchore/syft/syft/format"
|
||||
"github.com/anchore/syft/syft/format/template"
|
||||
)
|
||||
|
||||
func TestAllFormatsExpressible(t *testing.T) {
|
||||
@@ -20,9 +21,15 @@ func TestAllFormatsExpressible(t *testing.T) {
|
||||
},
|
||||
assertSuccessfulReturnCode,
|
||||
}
|
||||
formatNames := formats.AllIDs()
|
||||
require.NotEmpty(t, formatNames)
|
||||
for _, o := range formatNames {
|
||||
|
||||
opts := options.DefaultOutput()
|
||||
encoders, err := opts.Encoders()
|
||||
require.NoError(t, err)
|
||||
|
||||
encs := format.NewEncoderCollection(encoders...)
|
||||
formatIDs := encs.IDs()
|
||||
require.NotEmpty(t, formatIDs)
|
||||
for _, o := range formatIDs {
|
||||
t.Run(fmt.Sprintf("format:%s", o), func(t *testing.T) {
|
||||
args := []string{"dir:./test-fixtures/image-pkg-coverage", "-o", string(o)}
|
||||
if o == template.ID {
|
||||
|
||||
@@ -5,7 +5,11 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/imagetest"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxjson"
|
||||
)
|
||||
|
||||
// We have schema validation mechanims in schema/cyclonedx/
|
||||
@@ -95,13 +99,13 @@ func assertValidCycloneDX(tb testing.TB, stdout, stderr string, rc int) {
|
||||
// validate --input-format json --input-version v1_4 --input-file bom.json
|
||||
func validateCycloneDXJSON(t *testing.T, stdout string) {
|
||||
f, err := os.CreateTemp("", "tmpfile-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
|
||||
// close and remove the temporary file at the end of the program
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
t.Cleanup(func() {
|
||||
assert.NoError(t, f.Close())
|
||||
assert.NoError(t, os.Remove(f.Name()))
|
||||
})
|
||||
|
||||
data := []byte(stdout)
|
||||
|
||||
@@ -109,18 +113,25 @@ func validateCycloneDXJSON(t *testing.T, stdout string) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// get the latest supported version of CycloneDX by syft and convert the expression to the format used by cyclonedx-cli
|
||||
// e.g. "1.5" -> "v1_5"
|
||||
versions := cyclonedxjson.SupportedVersions()
|
||||
version := versions[len(versions)-1]
|
||||
versionInput := "v" + strings.Replace(version, ".", "_", -1)
|
||||
|
||||
args := []string{
|
||||
"validate",
|
||||
"--input-format",
|
||||
"json",
|
||||
"--input-version",
|
||||
"v1_4",
|
||||
versionInput,
|
||||
"--input-file",
|
||||
"/sbom",
|
||||
}
|
||||
|
||||
cmd, stdout, stderr := runCycloneDXInDocker(t, nil, "cyclonedx/cyclonedx-cli", f, args...)
|
||||
if strings.Contains(stdout, "BOM is not valid") {
|
||||
t.Log("STDOUT:\n", stdout)
|
||||
t.Errorf("expected no validation failures for cyclonedx-cli but found invalid BOM")
|
||||
}
|
||||
|
||||
|
||||
@@ -9,17 +9,23 @@ import (
|
||||
|
||||
"github.com/anchore/syft/cmd/syft/cli/commands"
|
||||
"github.com/anchore/syft/cmd/syft/cli/options"
|
||||
"github.com/anchore/syft/syft/formats"
|
||||
"github.com/anchore/syft/syft/formats/cyclonedxjson"
|
||||
"github.com/anchore/syft/syft/formats/cyclonedxxml"
|
||||
"github.com/anchore/syft/syft/formats/spdxjson"
|
||||
"github.com/anchore/syft/syft/formats/spdxtagvalue"
|
||||
"github.com/anchore/syft/syft/formats/syftjson"
|
||||
"github.com/anchore/syft/syft/formats/table"
|
||||
"github.com/anchore/syft/syft/format"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxjson"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxxml"
|
||||
"github.com/anchore/syft/syft/format/spdxjson"
|
||||
"github.com/anchore/syft/syft/format/spdxtagvalue"
|
||||
"github.com/anchore/syft/syft/format/syftjson"
|
||||
"github.com/anchore/syft/syft/sbom"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
)
|
||||
|
||||
func mustEncoder(enc sbom.FormatEncoder, err error) sbom.FormatEncoder {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return enc
|
||||
}
|
||||
|
||||
// TestConvertCmd tests if the converted SBOM is a valid document according
|
||||
// to spec.
|
||||
// TODO: This test can, but currently does not, check the converted SBOM content. It
|
||||
@@ -28,33 +34,33 @@ import (
|
||||
func TestConvertCmd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
format sbom.Format
|
||||
format sbom.FormatEncoder
|
||||
}{
|
||||
{
|
||||
name: "syft-json",
|
||||
format: syftjson.Format(),
|
||||
format: syftjson.NewFormatEncoder(),
|
||||
},
|
||||
{
|
||||
name: "spdx-json",
|
||||
format: spdxjson.Format(),
|
||||
format: mustEncoder(spdxjson.NewFormatEncoderWithConfig(spdxjson.DefaultEncoderConfig())),
|
||||
},
|
||||
{
|
||||
name: "spdx-tag-value",
|
||||
format: spdxtagvalue.Format(),
|
||||
format: mustEncoder(spdxtagvalue.NewFormatEncoderWithConfig(spdxtagvalue.DefaultEncoderConfig())),
|
||||
},
|
||||
{
|
||||
name: "cyclonedx-json",
|
||||
format: cyclonedxjson.Format(),
|
||||
format: mustEncoder(cyclonedxjson.NewFormatEncoderWithConfig(cyclonedxjson.DefaultEncoderConfig())),
|
||||
},
|
||||
{
|
||||
name: "cyclonedx-xml",
|
||||
format: cyclonedxxml.Format(),
|
||||
format: mustEncoder(cyclonedxxml.NewFormatEncoderWithConfig(cyclonedxxml.DefaultEncoderConfig())),
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
syftSbom, _ := catalogFixtureImage(t, "image-pkg-coverage", source.SquashedScope, nil)
|
||||
syftFormat := syftjson.Format()
|
||||
syftFormat := syftjson.NewFormatEncoder()
|
||||
|
||||
syftFile, err := os.CreateTemp("", "test-convert-sbom-")
|
||||
require.NoError(t, err)
|
||||
@@ -72,10 +78,11 @@ func TestConvertCmd(t *testing.T) {
|
||||
}()
|
||||
|
||||
opts := &commands.ConvertOptions{
|
||||
MultiOutput: options.MultiOutput{
|
||||
Output: options.Output{
|
||||
Outputs: []string{fmt.Sprintf("%s=%s", test.format.ID().String(), formatFile.Name())},
|
||||
},
|
||||
}
|
||||
require.NoError(t, opts.PostLoad())
|
||||
|
||||
// stdout reduction of test noise
|
||||
rescue := os.Stdout // keep backup of the real stdout
|
||||
@@ -86,15 +93,9 @@ func TestConvertCmd(t *testing.T) {
|
||||
|
||||
err = commands.RunConvert(opts, syftFile.Name())
|
||||
require.NoError(t, err)
|
||||
contents, err := os.ReadFile(formatFile.Name())
|
||||
require.NoError(t, err)
|
||||
|
||||
formatFound := formats.Identify(contents)
|
||||
if test.format.ID() == table.ID {
|
||||
require.Nil(t, formatFound)
|
||||
return
|
||||
}
|
||||
require.Equal(t, test.format.ID(), formatFound.ID())
|
||||
foundID, _ := format.Identify(formatFile)
|
||||
require.Equal(t, test.format.ID(), foundID)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/formats"
|
||||
"github.com/anchore/syft/syft/formats/cyclonedxjson"
|
||||
"github.com/anchore/syft/syft/formats/cyclonedxxml"
|
||||
"github.com/anchore/syft/syft/formats/syftjson"
|
||||
"github.com/anchore/syft/cmd/syft/cli/options"
|
||||
"github.com/anchore/syft/syft/format"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxjson"
|
||||
"github.com/anchore/syft/syft/format/cyclonedxxml"
|
||||
"github.com/anchore/syft/syft/format/syftjson"
|
||||
"github.com/anchore/syft/syft/sbom"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
)
|
||||
@@ -65,24 +66,36 @@ func TestEncodeDecodeEncodeCycleComparison(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
opts := options.DefaultOutput()
|
||||
encoderList, err := opts.Encoders()
|
||||
require.NoError(t, err)
|
||||
|
||||
encoders := format.NewEncoderCollection(encoderList...)
|
||||
decoders := format.NewDecoderCollection(format.Decoders()...)
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(string(test.formatOption), func(t *testing.T) {
|
||||
for _, image := range images {
|
||||
originalSBOM, _ := catalogFixtureImage(t, image, source.SquashedScope, nil)
|
||||
|
||||
format := formats.ByName(string(test.formatOption))
|
||||
require.NotNil(t, format)
|
||||
f := encoders.GetByString(string(test.formatOption))
|
||||
require.NotNil(t, f)
|
||||
|
||||
by1, err := formats.Encode(originalSBOM, format)
|
||||
var buff1 bytes.Buffer
|
||||
err := f.Encode(&buff1, originalSBOM)
|
||||
require.NoError(t, err)
|
||||
|
||||
newSBOM, newFormat, err := formats.Decode(bytes.NewReader(by1))
|
||||
newSBOM, formatID, formatVersion, err := decoders.Decode(bytes.NewReader(buff1.Bytes()))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, format.ID(), newFormat.ID())
|
||||
require.Equal(t, f.ID(), formatID)
|
||||
require.Equal(t, f.Version(), formatVersion)
|
||||
|
||||
by2, err := formats.Encode(*newSBOM, format)
|
||||
var buff2 bytes.Buffer
|
||||
err = f.Encode(&buff2, *newSBOM)
|
||||
require.NoError(t, err)
|
||||
|
||||
by1 := buff1.Bytes()
|
||||
by2 := buff2.Bytes()
|
||||
if test.redactor != nil {
|
||||
by1 = test.redactor(by1)
|
||||
by2 = test.redactor(by2)
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/syft/syft/formats/syftjson"
|
||||
syftjsonModel "github.com/anchore/syft/syft/formats/syftjson/model"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/anchore/syft/syft/format/syftjson"
|
||||
syftjsonModel "github.com/anchore/syft/syft/format/syftjson/model"
|
||||
"github.com/anchore/syft/syft/pkg"
|
||||
"github.com/anchore/syft/syft/source"
|
||||
)
|
||||
@@ -26,10 +28,8 @@ func TestPackageOwnershipRelationships(t *testing.T) {
|
||||
sbom, _ := catalogFixtureImage(t, test.fixture, source.SquashedScope, nil)
|
||||
|
||||
output := bytes.NewBufferString("")
|
||||
err := syftjson.Format().Encode(output, sbom)
|
||||
if err != nil {
|
||||
t.Fatalf("unable to present: %+v", err)
|
||||
}
|
||||
err := syftjson.NewFormatEncoder().Encode(output, sbom)
|
||||
require.NoError(t, err)
|
||||
|
||||
var doc syftjsonModel.Document
|
||||
decoder := json.NewDecoder(output)
|
||||
|
||||
Reference in New Issue
Block a user