22//
33// SPDX-License-Identifier: Apache-2.0
44
5- const axios = require ( "axios" ) ;
6- const { parse } = require ( "./parser/parser" ) ;
7- const { validate, addIdsAndDates, addScanMetadata } = require ( "./parser-utils" ) ;
8- const k8s = require ( "@kubernetes/client-node" ) ;
9-
10- const kc = new k8s . KubeConfig ( ) ;
5+ import axios from "axios" ;
6+ import {
7+ KubeConfig ,
8+ CustomObjectsApi ,
9+ setHeaderOptions ,
10+ PatchStrategy ,
11+ } from "@kubernetes/client-node" ;
12+
13+ import { parse } from "./parser/parser.js" ;
14+ import { validate , addIdsAndDates , addScanMetadata } from "./parser-utils.js" ;
15+
16+ const kc = new KubeConfig ( ) ;
1117kc . loadFromCluster ( ) ;
12- const k8sApi = kc . makeApiClient ( k8s . CustomObjectsApi ) ;
18+ const k8sApi = kc . makeApiClient ( CustomObjectsApi ) ;
19+
1320const scanName = process . env [ "SCAN_NAME" ] ;
1421const namespace = process . env [ "NAMESPACE" ] ;
1522
1623function severityCount ( findings , severity ) {
1724 return findings . filter (
1825 ( { severity : findingSeverity } ) =>
19- findingSeverity . toUpperCase ( ) === severity
26+ findingSeverity . toUpperCase ( ) === severity ,
2027 ) . length ;
2128}
2229
2330async function uploadResultToFileStorageService (
2431 resultUploadUrl ,
25- findingsWithIdsAndDates
32+ findingsWithIdsAndDates ,
2633) {
2734 return axios
2835 . put ( resultUploadUrl , findingsWithIdsAndDates , {
@@ -34,12 +41,12 @@ async function uploadResultToFileStorageService(
3441 // The request was made and the server responded with a status code
3542 // that falls out of the range of 2xx
3643 console . error (
37- `Finding Upload Failed with Response Code: ${ error . response . status } `
44+ `Finding Upload Failed with Response Code: ${ error . response . status } ` ,
3845 ) ;
3946 console . error ( `Error Response Body: ${ error . response . data } ` ) ;
4047 } else if ( error . request ) {
4148 console . error (
42- "No response received from FileStorage when uploading finding"
49+ "No response received from FileStorage when uploading finding" ,
4350 ) ;
4451 console . error ( error ) ;
4552 } else {
@@ -62,29 +69,28 @@ async function updateScanStatus(findings) {
6269 }
6370
6471 await k8sApi . patchNamespacedCustomObjectStatus (
65- "execution.securecodebox.io" ,
66- "v1" ,
67- namespace ,
68- "scans" ,
69- scanName ,
7072 {
71- status : {
72- findings : {
73- count : findings . length ,
74- severities : {
75- informational : severityCount ( findings , "INFORMATIONAL" ) ,
76- low : severityCount ( findings , "LOW" ) ,
77- medium : severityCount ( findings , "MEDIUM" ) ,
78- high : severityCount ( findings , "HIGH" ) ,
73+ group : "execution.securecodebox.io" ,
74+ version : "v1" ,
75+ namespace,
76+ plural : "scans" ,
77+ name : scanName ,
78+ body : {
79+ status : {
80+ findings : {
81+ count : findings . length ,
82+ severities : {
83+ informational : severityCount ( findings , "INFORMATIONAL" ) ,
84+ low : severityCount ( findings , "LOW" ) ,
85+ medium : severityCount ( findings , "MEDIUM" ) ,
86+ high : severityCount ( findings , "HIGH" ) ,
87+ } ,
88+ categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
7989 } ,
80- categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
8190 } ,
8291 } ,
8392 } ,
84- undefined ,
85- undefined ,
86- undefined ,
87- { headers : { "content-type" : "application/merge-patch+json" } }
93+ setHeaderOptions ( "Content-Type" , PatchStrategy . MergePatch ) ,
8894 ) ;
8995 console . log ( "Updated status successfully" ) ;
9096 } catch ( err ) {
@@ -96,32 +102,29 @@ async function updateScanStatus(findings) {
96102
97103async function extractScan ( ) {
98104 try {
99- const { body } = await k8sApi . getNamespacedCustomObject (
100- "execution.securecodebox.io" ,
101- "v1" ,
105+ return await k8sApi . getNamespacedCustomObject ( {
106+ group : "execution.securecodebox.io" ,
107+ version : "v1" ,
108+ plural : "scans" ,
109+ name : scanName ,
102110 namespace,
103- "scans" ,
104- scanName
105- ) ;
106- return body ;
111+ } ) ;
107112 } catch ( err ) {
108113 console . error ( "Failed to get Scan from the kubernetes api" ) ;
109114 console . error ( err ) ;
110115 process . exit ( 1 ) ;
111116 }
112-
113117}
114118
115119async function extractParseDefinition ( scan ) {
116120 try {
117- const { body } = await k8sApi . getNamespacedCustomObject (
118- "execution.securecodebox.io" ,
119- "v1" ,
121+ return await k8sApi . getNamespacedCustomObject ( {
122+ group : "execution.securecodebox.io" ,
123+ version : "v1" ,
124+ plural : "parsedefinitions" ,
125+ name : scan . status . rawResultType ,
120126 namespace,
121- "parsedefinitions" ,
122- scan . status . rawResultType
123- ) ;
124- return body ;
127+ } ) ;
125128 } catch ( err ) {
126129 console . error ( "Failed to get ParseDefinition from the kubernetes api" ) ;
127130 console . error ( err ) ;
@@ -138,8 +141,8 @@ async function main() {
138141
139142 console . log ( "Fetching result file" ) ;
140143 let response ;
141- if ( parseDefinition . spec . contentType === "Binary" ) {
142- response = await axios . get ( resultFileUrl , { responseType : ' arraybuffer' } ) ;
144+ if ( parseDefinition . spec . contentType === "Binary" ) {
145+ response = await axios . get ( resultFileUrl , { responseType : " arraybuffer" } ) ;
143146 } else {
144147 response = await axios . get ( resultFileUrl ) ;
145148 }
@@ -162,11 +165,15 @@ async function main() {
162165 console . log ( "Adding scan metadata to the findings" ) ;
163166 const findingsWithMetadata = addScanMetadata ( findingsWithIdsAndDates , scan ) ;
164167
165- const crash_on_failed_validation = process . env [ "CRASH_ON_FAILED_VALIDATION" ] === "true"
166- console . log ( "Validating Findings. Environment variable CRASH_ON_FAILED_VALIDATION is set to %s" , crash_on_failed_validation ) ;
168+ const crash_on_failed_validation =
169+ process . env [ "CRASH_ON_FAILED_VALIDATION" ] === "true" ;
170+ console . log (
171+ "Validating Findings. Environment variable CRASH_ON_FAILED_VALIDATION is set to %s" ,
172+ crash_on_failed_validation ,
173+ ) ;
167174 try {
168175 await validate ( findingsWithMetadata ) ;
169- console . log ( "The Findings were successfully validated" )
176+ console . log ( "The Findings were successfully validated" ) ;
170177 } catch ( error ) {
171178 console . error ( "The Findings Validation failed with error(s):" ) ;
172179 console . error ( error ) ;
@@ -179,15 +186,9 @@ async function main() {
179186
180187 console . log ( `Uploading results to the file storage service` ) ;
181188
182- await uploadResultToFileStorageService (
183- resultUploadUrl ,
184- findingsWithMetadata
185- ) ;
189+ await uploadResultToFileStorageService ( resultUploadUrl , findingsWithMetadata ) ;
186190
187191 console . log ( `Completed parser` ) ;
188192}
189193
190194main ( ) ;
191-
192- module . exports . addIdsAndDates = addIdsAndDates ;
193- module . exports . addScanMetadata = addScanMetadata ;
0 commit comments