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

Fix - failing sbomcheck with empty components #89

Merged
merged 2 commits into from
Aug 13, 2024
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
2 changes: 1 addition & 1 deletion internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC

func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string) []LagoonFact {
var factsInput []LagoonFact
if len(*facts) == 0 {
if facts == nil || len(*facts) == 0 {
return factsInput
}

Expand Down
65 changes: 65 additions & 0 deletions internal/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,68 @@ func Test_processFactsFromSBOM(t *testing.T) {
})
}
}

func Test_processFactsFromSBOMWithNoComponents(t *testing.T) {
type args struct {
bom *[]cdx.Component
environmentId int
source string
}

testResponse, err := ioutil.ReadFile("./testassets/testSbomPayloadNoComponents.json")
if err != nil {
t.Fatalf("Could not open file")
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
t.Errorf("Expected to request '/fixedvalue', got: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
w.Write(testResponse)
}))
defer server.Close()

bom := new(cdx.BOM)
resp, err := http.Get(server.URL)
if err != nil {
panic(err)
}
decoder := cdx.NewBOMDecoder(resp.Body, cdx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
panic(err)
}

tests := []struct {
name string
args args
want []lagoonclient.AddFactInput
}{
{
name: "sbom.cdx.json",
args: args{
bom: bom.Components,
environmentId: 3,
source: "syft",
},
want: []lagoonclient.AddFactInput{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := processFactsFromSBOM(slog.Default(), tt.args.bom, tt.args.environmentId, tt.args.source)
if len(got) != len(tt.want) {
t.Errorf("processFactsFromSBOM() returned %d results, want %d", len(got), len(tt.want))
}
for i := range tt.want {
if got[i].Environment != tt.want[i].Environment ||
got[i].Name != tt.want[i].Name ||
got[i].Value != tt.want[i].Value ||
got[i].Source != tt.want[i].Source ||
got[i].Description != tt.want[i].Description ||
got[i].KeyFact != tt.want[i].KeyFact {
t.Errorf("processFactsFromSBOM()[%d] = %v, want %v", i, got[i], tt.want[i])
}
}
})
}
}
21 changes: 21 additions & 0 deletions internal/handler/testassets/testSbomPayloadNoComponents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"bomFormat": "CycloneDX",
"specVersion": "1.3",
"serialNumber": "urn:uuid:db9b54af-f4ea-4043-9f53-b0f1b4485d4d",
"version": 1,
"metadata": {
"timestamp": "2022-01-12T10:16:55Z",
"tools": [
{
"vendor": "anchore",
"name": "syft",
"version": "0.35.1"
}
],
"component": {
"type": "container",
"name": "uselagoon/php-8.1-cli-drupal",
"version": "sha256:b364c41e9c6bf5dea414e3a382f8088883265a7ad48bfecc83c6ff2f75998d10"
}
}
}