Skip to content
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
85 changes: 44 additions & 41 deletions hook-sdk/nodejs/hook-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// SPDX-License-Identifier: Apache-2.0

const axios = require("axios");
const { handle } = require("./hook/hook");
const k8s = require("@kubernetes/client-node");

Expand All @@ -16,60 +15,64 @@ kc.loadFromCluster();
const k8sApi = kc.makeApiClient(k8s.CustomObjectsApi);

function downloadFile(url) {
return axios.get(url);
return fetch(url);
}

function getRawResults() {
async function getRawResults() {
const rawResultUrl = process.argv[2];
return downloadFile(rawResultUrl).then(({ data }) => {
console.log(`Fetched raw result file contents from the file storage`);
return data;
});
const response = await downloadFile(rawResultUrl);
console.log(`Fetched raw result file contents from the file storage`);
return await response.text();
}

function getFindings() {
async function getFindings() {
const findingsUrl = process.argv[3];
return downloadFile(findingsUrl).then(({ data: findings }) => {
console.log(`Fetched ${findings.length} findings from the file storage`);
return findings;
});
const response = await downloadFile(findingsUrl);
const findings = await response.json();
console.log(`Fetched ${findings.length} findings from the file storage`);
return findings;
}

function uploadFile(url, fileContents) {
return axios
.put(url, fileContents, {
async function uploadFile(url, fileContents) {
try {
const response = await fetch(url, {
method: "PUT",
headers: { "content-type": "" },
})
.catch(function(error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(
`File Upload Failed with Response Code: ${error.response.status}`
);
console.error(`Error Response Body: ${error.response.data}`);
} else if (error.request) {
console.error(
"No response received from FileStorage when uploading finding"
);
console.error(error);
} else {
// Something happened in setting up the request that triggered an Error
console.log("Error", error.message);
}
process.exit(1);
});

if (!response.ok) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const error = new Error(`HTTP error! status: ${response.status}`);
error.response = response;
throw error;
}
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error(
`File Upload Failed with Response Code: ${error.response.status}`,
);
const errorBody = await error.response.text();
console.error(`Error Response Body: ${errorBody}`);
} else {
// Something happened in setting up the request that triggered an Error
console.error("Error uploading findings from hook", error.message);
}
process.exit(1);
}
}

function updateRawResults(fileContents) {
const rawResultUploadUrl = process.argv[4];
if (rawResultUploadUrl === undefined) {
console.error(
"Tried to upload RawResults but didn't find a valid URL to upload the findings to."
"Tried to upload RawResults but didn't find a valid URL to upload the findings to.",
);
console.error("This probably means that this hook is a ReadOnly hook.");
console.error(
"If you want to change RawResults you'll need to use a ReadAndWrite Hook."
"If you want to change RawResults you'll need to use a ReadAndWrite Hook.",
);
}
return uploadFile(rawResultUploadUrl, fileContents);
Expand All @@ -78,19 +81,19 @@ function updateRawResults(fileContents) {
function severityCount(findings, severity) {
return findings.filter(
({ severity: findingSeverity }) =>
findingSeverity.toUpperCase() === severity
findingSeverity.toUpperCase() === severity,
).length;
}

async function updateFindings(findings) {
const findingsUploadUrl = process.argv[5];
if (findingsUploadUrl === undefined) {
console.error(
"Tried to upload Findings but didn't find a valid URL to upload the findings to."
"Tried to upload Findings but didn't find a valid URL to upload the findings to.",
);
console.error("This probably means that this hook is a ReadOnly hook.");
console.error(
"If you want to change Findings you'll need to use a ReadAndWrite Hook."
"If you want to change Findings you'll need to use a ReadAndWrite Hook.",
);
}
await uploadFile(findingsUploadUrl, JSON.stringify(findings));
Expand Down Expand Up @@ -128,7 +131,7 @@ async function updateFindings(findings) {
undefined,
undefined,
undefined,
{ headers: { "content-type": "application/merge-patch+json" } }
{ headers: { "content-type": "application/merge-patch+json" } },
);
console.log("Updated status successfully");
}
Expand All @@ -141,7 +144,7 @@ async function main() {
"v1",
namespace,
"scans",
scanName
scanName,
);
scan = body;
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion hook-sdk/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"license": "Apache-2.0",
"dependencies": {
"@kubernetes/client-node": "^0.22.3",
"axios": "^1.7.9",
"ws": "^8.13.0"
}
}
Loading