@@ -2,6 +2,15 @@ const axios = require("axios");
22const { handle } = require ( "./hook/hook" ) ;
33const k8s = require ( "@kubernetes/client-node" ) ;
44
5+ const scanName = process . env [ "SCAN_NAME" ] ;
6+ const namespace = process . env [ "NAMESPACE" ] ;
7+ console . log ( `Starting hook for Scan "${ scanName } "` ) ;
8+
9+ const kc = new k8s . KubeConfig ( ) ;
10+ kc . loadFromCluster ( ) ;
11+
12+ const k8sApi = kc . makeApiClient ( k8s . CustomObjectsApi ) ;
13+
514function downloadFile ( url ) {
615 return axios . get ( url ) ;
716}
@@ -62,7 +71,14 @@ function updateRawResults(fileContents) {
6271 return uploadFile ( rawResultUploadUrl , fileContents ) ;
6372}
6473
65- function updateFindings ( findings ) {
74+ function severityCount ( findings , severity ) {
75+ return findings . filter (
76+ ( { severity : findingSeverity } ) =>
77+ findingSeverity . toUpperCase ( ) === severity
78+ ) . length ;
79+ }
80+
81+ async function updateFindings ( findings ) {
6682 const findingsUploadUrl = process . argv [ 5 ] ;
6783 if ( findingsUploadUrl === undefined ) {
6884 console . error (
@@ -73,19 +89,55 @@ function updateFindings(findings) {
7389 "If you want to change Findings you'll need to use a ReadAndWrite Hook."
7490 ) ;
7591 }
76- return uploadFile ( findingsUploadUrl , JSON . stringify ( findings ) ) ;
77- }
92+ await uploadFile ( findingsUploadUrl , JSON . stringify ( findings ) ) ;
7893
79- async function main ( ) {
80- const scanName = process . env [ "SCAN_NAME" ] ;
81- const namespace = process . env [ "NAMESPACE" ] ;
82- console . log ( `Starting hook for Scan "${ scanName } "` ) ;
94+ // Update the scans findingStats (severities, categories, or the count) of the scan results
95+ const findingCategories = new Map ( ) ;
96+ for ( const { category } of findings ) {
97+ if ( findingCategories . has ( category ) ) {
98+ findingCategories . set ( category , findingCategories . get ( category ) + 1 ) ;
99+ } else {
100+ findingCategories . set ( category , 1 ) ;
101+ }
102+ }
83103
84- const kc = new k8s . KubeConfig ( ) ;
85- kc . loadFromCluster ( ) ;
104+ const findingStats = {
105+ count : findings . length ,
106+ severities : {
107+ informational : severityCount ( findings , "INFORMATIONAL" ) ,
108+ low : severityCount ( findings , "LOW" ) ,
109+ medium : severityCount ( findings , "MEDIUM" ) ,
110+ high : severityCount ( findings , "HIGH" ) ,
111+ } ,
112+ categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
113+ } ;
86114
87- const k8sApi = kc . makeApiClient ( k8s . CustomObjectsApi ) ;
115+ await k8sApi . patchNamespacedCustomObjectStatus (
116+ "execution.experimental.securecodebox.io" ,
117+ "v1" ,
118+ namespace ,
119+ "scans" ,
120+ scanName ,
121+ {
122+ status : {
123+ findings : {
124+ count : findings . length ,
125+ severities : {
126+ informational : severityCount ( findings , "INFORMATIONAL" ) ,
127+ low : severityCount ( findings , "LOW" ) ,
128+ medium : severityCount ( findings , "MEDIUM" ) ,
129+ high : severityCount ( findings , "HIGH" ) ,
130+ } ,
131+ categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
132+ } ,
133+ } ,
134+ } ,
135+ { headers : { "content-type" : "application/merge-patch+json" } }
136+ ) ;
137+ console . log ( "Updated status successfully" ) ;
138+ }
88139
140+ async function main ( ) {
89141 let scan ;
90142 try {
91143 const { body } = await k8sApi . getNamespacedCustomObject (
0 commit comments