Skip to content

Commit 560b98a

Browse files
authored
Enrich command - handle errors when no response (#251)
1 parent da21ea9 commit 560b98a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Diff for: commands/enrich/enrich.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,21 @@ func AppendVulnsToJson(cmdResults *results.SecurityCommandResults) error {
7373
fileName := getScaScanFileName(cmdResults)
7474
fileContent, err := os.ReadFile(fileName)
7575
if err != nil {
76-
fmt.Println("Error reading file:", err)
77-
return err
76+
return fmt.Errorf("error reading file: %s", err.Error())
7877
}
7978
var data map[string]interface{}
8079
err = json.Unmarshal(fileContent, &data)
8180
if err != nil {
82-
fmt.Println("Error parsing XML:", err)
83-
return err
81+
return fmt.Errorf("error parsing JSON: %s", err.Error())
8482
}
8583
var vulnerabilities []map[string]string
86-
xrayResults := cmdResults.GetScaScansXrayResults()[0]
87-
for _, vuln := range xrayResults.Vulnerabilities {
84+
xrayResults := cmdResults.GetScaScansXrayResults()
85+
if len(xrayResults) == 0 {
86+
return fmt.Errorf("failed while getting sca scan from xray: %s", err.Error())
87+
} else if len(xrayResults) > 1 {
88+
log.Warn("Received %d results, parsing only first result", len(xrayResults))
89+
}
90+
for _, vuln := range xrayResults[0].Vulnerabilities {
8891
for component := range vuln.Components {
8992
vulnerability := map[string]string{"bom-ref": component, "id": vuln.Cves[0].Id}
9093
vulnerabilities = append(vulnerabilities, vulnerability)
@@ -102,9 +105,14 @@ func AppendVulnsToXML(cmdResults *results.SecurityCommandResults) error {
102105
return err
103106
}
104107
destination := result.FindElements("//bom")[0]
105-
xrayResults := cmdResults.GetScaScansXrayResults()[0]
108+
xrayResults := cmdResults.GetScaScansXrayResults()
109+
if len(xrayResults) == 0 {
110+
return fmt.Errorf("failed while getting sca scan from xray: %s", err.Error())
111+
} else if len(xrayResults) > 1 {
112+
log.Warn("Received %d results, parsing only first result", len(xrayResults))
113+
}
106114
vulns := destination.CreateElement("vulnerabilities")
107-
for _, vuln := range xrayResults.Vulnerabilities {
115+
for _, vuln := range xrayResults[0].Vulnerabilities {
108116
for component := range vuln.Components {
109117
addVuln := vulns.CreateElement("vulnerability")
110118
addVuln.CreateAttr("bom-ref", component)

0 commit comments

Comments
 (0)