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

Add initial support for generating SPDX SBOM documents (COMPOSER-2274) #930

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions Schutzfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
"centos-8": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
}
},
"centos-9": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
}
},
"fedora-39": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
},
"repos": [
Expand Down Expand Up @@ -65,7 +65,7 @@
"fedora-40": {
"dependencies": {
"osbuild": {
"commit": "d67fa48c170a0093807b12d674f13b175ba9d59b"
"commit": "3df75de65a5414866fa43133309c1fc67490a373"
}
},
"repos": [
Expand Down
70 changes: 70 additions & 0 deletions pkg/sbom/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package sbom

import (
"encoding/json"
"fmt"
)

type StandardType uint64

const (
StandardTypeNone StandardType = iota
StandardTypeSpdx
)

func (t StandardType) String() string {
switch t {
case StandardTypeNone:
return "none"
case StandardTypeSpdx:
return "spdx"
default:
panic("invalid standard type")
}
}

func (t StandardType) MarshalJSON() ([]byte, error) {
var s string

if t == StandardTypeNone {
s = ""
kingsleyzissou marked this conversation as resolved.
Show resolved Hide resolved
} else {
s = t.String()
}

return json.Marshal(s)
}

func (t *StandardType) UnmarshalJSON(data []byte) error {
switch string(data) {
case `""`:
*t = StandardTypeNone
case `"spdx"`:
*t = StandardTypeSpdx
default:
return fmt.Errorf("invalid SBOM standard type: %s", data)
}
return nil
}

type Document struct {
// type of the document standard
DocType StandardType

// document in a specific standard JSON raw format
Document json.RawMessage
}

func NewDocument(docType StandardType, doc json.RawMessage) (*Document, error) {
switch docType {
case StandardTypeSpdx:
docType = StandardTypeSpdx
default:
return nil, fmt.Errorf("unsupported SBOM document type: %s", docType)
}

return &Document{
DocType: docType,
Document: doc,
}, nil
}
86 changes: 86 additions & 0 deletions pkg/sbom/document_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package sbom

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestStandardTypeJSONUnmarhsall(t *testing.T) {
type testStruct struct {
Type StandardType `json:"type"`
TypeOmit StandardType `json:"type_omit,omitempty"`
}

tests := []struct {
name string
data []byte
want testStruct
}{
{
name: "StandardTypeNone",
data: []byte(`{"type":""}`),
want: testStruct{
Type: StandardTypeNone,
TypeOmit: StandardTypeNone,
},
},
{
name: "StandardTypeSpdx",
data: []byte(`{"type":"spdx","type_omit":"spdx"}`),
want: testStruct{
Type: StandardTypeSpdx,
TypeOmit: StandardTypeSpdx,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var ts testStruct
err := json.Unmarshal(tt.data, &ts)
assert.NoError(t, err)
assert.Equal(t, tt.want, ts)
})
}
}

func TestStandardTypeJSONMarhsall(t *testing.T) {
type TestStruct struct {
Type StandardType `json:"type"`
TypeOmit StandardType `json:"type_omit,omitempty"`
}

tests := []struct {
name string
want []byte
data TestStruct
}{
{
name: "StandardTypeNone",
want: []byte(`{"type":""}`),
data: TestStruct{
Type: StandardTypeNone,
TypeOmit: StandardTypeNone,
},
},
{
name: "StandardTypeSpdx",
want: []byte(`{"type":"spdx","type_omit":"spdx"}`),
data: TestStruct{
Type: StandardTypeSpdx,
TypeOmit: StandardTypeSpdx,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got []byte
got, err := json.Marshal(tt.data)
assert.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}