diff --git a/hook-sdk/nodejs/hook-wrapper.js b/hook-sdk/nodejs/hook-wrapper.js index c3a74a4891..4d71cf81c4 100644 --- a/hook-sdk/nodejs/hook-wrapper.js +++ b/hook-sdk/nodejs/hook-wrapper.js @@ -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"); @@ -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); @@ -78,7 +81,7 @@ function updateRawResults(fileContents) { function severityCount(findings, severity) { return findings.filter( ({ severity: findingSeverity }) => - findingSeverity.toUpperCase() === severity + findingSeverity.toUpperCase() === severity, ).length; } @@ -86,11 +89,11 @@ 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)); @@ -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"); } @@ -141,7 +144,7 @@ async function main() { "v1", namespace, "scans", - scanName + scanName, ); scan = body; } catch (err) { diff --git a/hook-sdk/nodejs/package.json b/hook-sdk/nodejs/package.json index cc5fb0935a..ae396e3653 100644 --- a/hook-sdk/nodejs/package.json +++ b/hook-sdk/nodejs/package.json @@ -11,7 +11,6 @@ "license": "Apache-2.0", "dependencies": { "@kubernetes/client-node": "^0.22.3", - "axios": "^1.7.9", "ws": "^8.13.0" } }