From 109020fbc39b76e48461c3c498230ff48b320f78 Mon Sep 17 00:00:00 2001 From: Jannik Hollenbach Date: Wed, 9 Apr 2025 19:26:15 +0200 Subject: [PATCH 1/3] Drop axios from hook-sdk Signed-off-by: Jannik Hollenbach --- hook-sdk/nodejs/hook-wrapper.js | 68 ++++++++++++++++----------------- hook-sdk/nodejs/package.json | 1 - 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/hook-sdk/nodejs/hook-wrapper.js b/hook-sdk/nodejs/hook-wrapper.js index c3a74a4891..6549f1353a 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,49 +15,46 @@ 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, { - 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); - }); +async function uploadFile(url, fileContents) { + try { + const response = await fetch(url, { method: "PUT", headers: { "content-type": "" } }) + } 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}` + ); + 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); + } } function updateRawResults(fileContents) { 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" } } From 14d55d0a790e967d2308341ec9256152ce0d4080 Mon Sep 17 00:00:00 2001 From: Jannik Hollenbach Date: Wed, 9 Apr 2025 19:27:06 +0200 Subject: [PATCH 2/3] Auto format w/ prettier Signed-off-by: Jannik Hollenbach --- hook-sdk/nodejs/hook-wrapper.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/hook-sdk/nodejs/hook-wrapper.js b/hook-sdk/nodejs/hook-wrapper.js index 6549f1353a..f5496c8d09 100644 --- a/hook-sdk/nodejs/hook-wrapper.js +++ b/hook-sdk/nodejs/hook-wrapper.js @@ -20,33 +20,36 @@ function downloadFile(url) { async function getRawResults() { const rawResultUrl = process.argv[2]; - const response = await downloadFile(rawResultUrl) + const response = await downloadFile(rawResultUrl); console.log(`Fetched raw result file contents from the file storage`); - return await response.text() + return await response.text(); } async function getFindings() { const findingsUrl = process.argv[3]; - const response = await downloadFile(findingsUrl) - const findings = await response.json() + const response = await downloadFile(findingsUrl); + const findings = await response.json(); console.log(`Fetched ${findings.length} findings from the file storage`); return findings; } async function uploadFile(url, fileContents) { try { - const response = await fetch(url, { method: "PUT", headers: { "content-type": "" } }) + const response = await fetch(url, { + method: "PUT", + headers: { "content-type": "" }, + }); } 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}` + `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" + "No response received from FileStorage when uploading finding", ); console.error(error); } else { @@ -61,11 +64,11 @@ 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); @@ -74,7 +77,7 @@ function updateRawResults(fileContents) { function severityCount(findings, severity) { return findings.filter( ({ severity: findingSeverity }) => - findingSeverity.toUpperCase() === severity + findingSeverity.toUpperCase() === severity, ).length; } @@ -82,11 +85,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)); @@ -124,7 +127,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"); } @@ -137,7 +140,7 @@ async function main() { "v1", namespace, "scans", - scanName + scanName, ); scan = body; } catch (err) { From 1426264f754bb0d2f7d51909229e6ce7882e81ec Mon Sep 17 00:00:00 2001 From: Jannik Hollenbach Date: Wed, 9 Apr 2025 19:57:02 +0200 Subject: [PATCH 3/3] Improve error handling in uploadFile function for better response feedback Signed-off-by: Jannik Hollenbach --- hook-sdk/nodejs/hook-wrapper.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/hook-sdk/nodejs/hook-wrapper.js b/hook-sdk/nodejs/hook-wrapper.js index f5496c8d09..4d71cf81c4 100644 --- a/hook-sdk/nodejs/hook-wrapper.js +++ b/hook-sdk/nodejs/hook-wrapper.js @@ -39,6 +39,14 @@ async function uploadFile(url, fileContents) { method: "PUT", headers: { "content-type": "" }, }); + + 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 @@ -46,15 +54,11 @@ async function uploadFile(url, fileContents) { 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); + 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.log("Error", error.message); + console.error("Error uploading findings from hook", error.message); } process.exit(1); }