mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 22:12:28 +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:
@@ -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