Skip to content

Commit

Permalink
support verb if enabled OPERATOR_SEND_DELETED_REPORTS of trivy-operator
Browse files Browse the repository at this point in the history
  • Loading branch information
takumakume committed Sep 4, 2023
1 parent 232770f commit 3fea6c8
Show file tree
Hide file tree
Showing 5 changed files with 1,016 additions and 16 deletions.
43 changes: 31 additions & 12 deletions sbomreport/sbomreport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (
type SbomReport struct {
rawJSON []byte
bom []byte
verb string
}

func New(rawJSON []byte) (*SbomReport, error) {
bom, err := getBOM(rawJSON)
bom, verb, err := getBOMAndVerb(rawJSON)
if err != nil {
return nil, err
}
return &SbomReport{
rawJSON: rawJSON,
verb: verb,
bom: bom,
}, nil
}
Expand All @@ -34,37 +36,54 @@ func (s *SbomReport) BOM() []byte {
return s.bom
}

func getBOM(rawJSON []byte) ([]byte, error) {
func (s *SbomReport) ISVerbUpdate() bool {
return s.verb == "update"
}

func getBOMAndVerb(rawJSON []byte) ([]byte, string, error) {
verb := "update"
var data map[string]interface{}

if err := json.Unmarshal(rawJSON, &data); err != nil {
return nil, err
return nil, verb, err
}

kind, ok := data["kind"].(string)
obj := data

v, ok := data["verb"].(string)
if ok {
verb = v
if operatorObject, ok := data["operatorObject"].(map[string]interface{}); ok {
obj = operatorObject
} else {
return nil, verb, errors.New("operatorObject is not found")
}
}

kind, ok := obj["kind"].(string)
if !ok || kind != "SbomReport" {
return nil, errors.New("kind is not SbomReport")
return nil, verb, errors.New("kind is not SbomReport")
}

apiVersion, ok := data["apiVersion"].(string)
apiVersion, ok := obj["apiVersion"].(string)
if !ok {
return nil, fmt.Errorf("apiVersion %q is not found", apiVersion)
return nil, verb, fmt.Errorf("apiVersion %q is not found", apiVersion)
}

report, ok := data["report"].(map[string]interface{})
report, ok := obj["report"].(map[string]interface{})
if !ok {
return nil, errors.New("report is not found")
return nil, verb, errors.New("report is not found")
}

bom, ok := report["components"].(map[string]interface{})
if !ok {
return nil, errors.New("bom is not found")
return nil, verb, errors.New("bom is not found")
}

jsonBytes, err := json.Marshal(bom)
if err != nil {
return nil, err
return nil, verb, err
}

return jsonBytes, nil
return jsonBytes, verb, nil
}
25 changes: 21 additions & 4 deletions sbomreport/sbomreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,35 @@ func TestNew(t *testing.T) {
if err != nil {
t.Fatal(err)
}
sbomReportV1alpha1WithVerb, err := os.ReadFile("../testdata/v1alpha1_with_verb.json")
if err != nil {
t.Fatal(err)
}

type args struct {
rawJSON []byte
}
tests := []struct {
name string
args args
wantErr bool
name string
args args
wantVerb string
wantErr bool
}{
{
name: "success v1alpha1",
args: args{
rawJSON: sbomReportV1alpha1,
},
wantErr: false,
wantVerb: "update",
wantErr: false,
},
{
name: "success v1alpha1 with verb",
args: args{
rawJSON: sbomReportV1alpha1WithVerb,
},
wantVerb: "delete",
wantErr: false,
},
}
for _, tt := range tests {
Expand All @@ -37,6 +51,9 @@ func TestNew(t *testing.T) {
if got.bom == nil {
t.Errorf("New() bom is nil")
}
if got.verb != tt.wantVerb {
t.Errorf("New() verb = %v, wantVerb %v", got.verb, tt.wantVerb)
}
})
}
}
Loading

0 comments on commit 3fea6c8

Please sign in to comment.