Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 8ef9021

Browse files
committed
Fail with errors when unable to upload findings
1 parent 31c69c4 commit 8ef9021

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

parser-sdk/nodejs/parser-wrapper.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const axios = require('axios');
2-
const { parse } = require('./parser/parser');
3-
const uuid = require('uuid/v4');
4-
const k8s = require('@kubernetes/client-node');
1+
const axios = require("axios");
2+
const { parse } = require("./parser/parser");
3+
const uuid = require("uuid/v4");
4+
const k8s = require("@kubernetes/client-node");
55

66
function severityCount(findings, severity) {
77
return findings.filter(
@@ -14,8 +14,8 @@ async function updateScanStatus(findings) {
1414
const kc = new k8s.KubeConfig();
1515
kc.loadFromCluster();
1616
const k8sApi = kc.makeApiClient(k8s.CustomObjectsApi);
17-
const scanName = process.env['SCAN_NAME'];
18-
const namespace = process.env['NAMESPACE'];
17+
const scanName = process.env["SCAN_NAME"];
18+
const namespace = process.env["NAMESPACE"];
1919

2020
try {
2121
const findingCategories = new Map();
@@ -28,57 +28,57 @@ async function updateScanStatus(findings) {
2828
}
2929

3030
await k8sApi.patchNamespacedCustomObjectStatus(
31-
'execution.experimental.securecodebox.io',
32-
'v1',
31+
"execution.experimental.securecodebox.io",
32+
"v1",
3333
namespace,
34-
'scans',
34+
"scans",
3535
scanName,
3636
{
3737
status: {
3838
findings: {
3939
count: findings.length,
4040
severities: {
41-
informational: severityCount(findings, 'INFORMATIONAL'),
42-
low: severityCount(findings, 'LOW'),
43-
medium: severityCount(findings, 'MEDIUM'),
44-
high: severityCount(findings, 'HIGH'),
41+
informational: severityCount(findings, "INFORMATIONAL"),
42+
low: severityCount(findings, "LOW"),
43+
medium: severityCount(findings, "MEDIUM"),
44+
high: severityCount(findings, "HIGH"),
4545
},
4646
categories: Object.fromEntries(findingCategories.entries()),
4747
},
4848
},
4949
},
50-
{ headers: { 'content-type': 'application/merge-patch+json' } }
50+
{ headers: { "content-type": "application/merge-patch+json" } }
5151
);
52-
console.log('Updated status successfully');
52+
console.log("Updated status successfully");
5353
} catch (err) {
54-
console.error('Failed to update Scan Status via the kubernetes api');
54+
console.error("Failed to update Scan Status via the kubernetes api");
5555
console.error(err);
5656
process.exit(1);
5757
}
5858
}
5959

6060
async function main() {
61-
console.log("Starting Parser")
61+
console.log("Starting Parser");
6262
const resultFileUrl = process.argv[2];
6363
const resultUploadUrl = process.argv[3];
6464

65-
console.log("Fetching result file")
65+
console.log("Fetching result file");
6666
const { data } = await axios.get(resultFileUrl);
67-
console.log("Fetched result file")
67+
console.log("Fetched result file");
6868

6969
let findings = [];
7070
try {
7171
findings = await parse(data);
7272
} catch (error) {
73-
console.error("Parser failed with error:")
74-
console.error(error)
75-
process.exit(1)
73+
console.error("Parser failed with error:");
74+
console.error(error);
75+
process.exit(1);
7676
}
7777

7878
console.log(`Transformed raw result file into ${findings.length} findings`);
7979

80-
console.log('Adding UUIDs to the findings');
81-
const findingsWithIds = findings.map(finding => {
80+
console.log("Adding UUIDs to the findings");
81+
const findingsWithIds = findings.map((finding) => {
8282
return {
8383
...finding,
8484
id: uuid(),
@@ -89,7 +89,25 @@ async function main() {
8989

9090
console.log(`Uploading results to the file storage service`);
9191

92-
await axios.put(resultUploadUrl, findingsWithIds);
92+
await axios.put(resultUploadUrl, findingsWithIds).catch(function(error) {
93+
if (error.response) {
94+
// The request was made and the server responded with a status code
95+
// that falls out of the range of 2xx
96+
console.error(
97+
`Finding Upload Failed with Response Code: ${error.response.status}`
98+
);
99+
console.error(`Error Response Body: ${error.response.data}`);
100+
} else if (error.request) {
101+
console.error(
102+
"No response received from FileStorage when uploading finding"
103+
);
104+
console.error(error);
105+
} else {
106+
// Something happened in setting up the request that triggered an Error
107+
console.log("Error", error.message);
108+
}
109+
process.exit(1);
110+
});
93111

94112
console.log(`Completed parser`);
95113
}

0 commit comments

Comments
 (0)