Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal Syft JSON with missing metadata #1338

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion syft/formats/common/cyclonedxhelpers/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cyclonedxhelpers

import (
"fmt"
"reflect"
"testing"

"github.com/CycloneDX/cyclonedx-go"
Expand Down Expand Up @@ -200,6 +201,7 @@ func Test_decodeComponent(t *testing.T) {
component cyclonedx.Component
wantLanguage pkg.Language
wantMetadataType pkg.MetadataType
wantMetadata interface{}
}{
{
name: "derive language from pURL if missing",
Expand All @@ -213,7 +215,7 @@ func Test_decodeComponent(t *testing.T) {
wantLanguage: pkg.Java,
},
{
name: "handle existing RpmdbMetadata type",
name: "handle RpmdbMetadata type without properties",
component: cyclonedx.Component{
Name: "acl",
Version: "2.2.53-1.el8",
Expand All @@ -228,6 +230,31 @@ func Test_decodeComponent(t *testing.T) {
},
},
wantMetadataType: pkg.RpmMetadataType,
kzantow marked this conversation as resolved.
Show resolved Hide resolved
wantMetadata: pkg.RpmMetadata{},
},
{
name: "handle RpmdbMetadata type with properties",
component: cyclonedx.Component{
Name: "acl",
Version: "2.2.53-1.el8",
PackageURL: "pkg:rpm/centos/[email protected]?arch=x86_64&upstream=acl-2.2.53-1.el8.src.rpm&distro=centos-8",
Type: "library",
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&upstream=acl-2.2.53-1.el8.src.rpm&distro=centos-8",
Properties: &[]cyclonedx.Property{
{
Name: "syft:package:metadataType",
Value: "RpmMetadata",
},
{
Name: "syft:metadata:release",
Value: "some-release",
},
},
},
wantMetadataType: pkg.RpmMetadataType,
wantMetadata: pkg.RpmMetadata{
Release: "some-release",
},
},
}

Expand All @@ -240,6 +267,9 @@ func Test_decodeComponent(t *testing.T) {
if tt.wantMetadataType != "" {
assert.Equal(t, tt.wantMetadataType, p.MetadataType)
}
if tt.wantMetadata != nil {
assert.Truef(t, reflect.DeepEqual(tt.wantMetadata, p.Metadata), "metadata should match: %+v != %+v", tt.wantMetadata, p.Metadata)
}
})
}
}
2 changes: 1 addition & 1 deletion syft/formats/common/property_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func decode(vals map[string]string, value reflect.Value, prefix string, fn Field

func PtrToStruct(ptr interface{}) interface{} {
v := reflect.ValueOf(ptr)
if v.IsZero() {
if v.IsZero() && v.Type().Kind() != reflect.Struct {
return nil
}
switch v.Type().Kind() {
Expand Down
6 changes: 4 additions & 2 deletions syft/formats/syftjson/model/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ func unpackMetadata(p *Package, unpacker packageMetadataUnpacker) error {
typ, ok := pkg.MetadataTypeByName[p.MetadataType]
if ok {
val := reflect.New(typ).Interface()
if err := json.Unmarshal(unpacker.Metadata, val); err != nil {
return err
if len(unpacker.Metadata) > 0 {
if err := json.Unmarshal(unpacker.Metadata, val); err != nil {
return err
}
}
p.Metadata = reflect.ValueOf(val).Elem().Interface()
return nil
Expand Down
31 changes: 31 additions & 0 deletions syft/formats/syftjson/model/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,37 @@ func Test_unpackMetadata(t *testing.T) {
"thing": "thing-1",
},
},
{
name: "can handle package with metadata type but missing metadata",
packageData: []byte(`{
"metadataType": "GolangBinMetadata"
}`),
metadataType: pkg.GolangBinMetadataType,
wantMetadata: pkg.GolangBinMetadata{},
},
{
name: "can handle package with unknonwn metadata type and missing metadata",
packageData: []byte(`{
"metadataType": "BadMetadata"
}`),
wantErr: require.Error,
metadataType: "BadMetadata",
wantMetadata: nil,
},
{
name: "can handle package with unknonwn metadata type and metadata",
packageData: []byte(`{
"metadataType": "BadMetadata",
"metadata": {
"random": "thing"
}
}`),
wantErr: require.Error,
metadataType: "BadMetadata",
wantMetadata: map[string]interface{}{
"random": "thing",
},
},
}

for _, test := range tests {
Expand Down