22//
33// SPDX-License-Identifier: Apache-2.0
44
5- const { handle } = require ( "./hook/hook" ) ;
6- const k8s = require ( "@kubernetes/client-node" ) ;
5+ import {
6+ KubeConfig ,
7+ CustomObjectsApi ,
8+ PatchStrategy ,
9+ } from "@kubernetes/client-node" ;
10+
11+ import { handle } from "./hook/hook.js" ;
712
813const scanName = process . env [ "SCAN_NAME" ] ;
914const namespace = process . env [ "NAMESPACE" ] ;
15+
1016console . log ( `Starting hook for Scan "${ scanName } "` ) ;
1117
12- const kc = new k8s . KubeConfig ( ) ;
18+ const kc = new KubeConfig ( ) ;
1319kc . loadFromCluster ( ) ;
1420
15- const k8sApi = kc . makeApiClient ( k8s . CustomObjectsApi ) ;
21+ const k8sApi = kc . makeApiClient ( CustomObjectsApi ) ;
1622
1723function downloadFile ( url ) {
1824 return fetch ( url ) ;
@@ -38,6 +44,7 @@ async function uploadFile(url, fileContents) {
3844 const response = await fetch ( url , {
3945 method : "PUT" ,
4046 headers : { "content-type" : "" } ,
47+ body : fileContents ,
4148 } ) ;
4249
4350 if ( ! response . ok ) {
@@ -109,44 +116,42 @@ async function updateFindings(findings) {
109116 }
110117
111118 await k8sApi . patchNamespacedCustomObjectStatus (
112- "execution.securecodebox.io" ,
113- "v1" ,
114- namespace ,
115- "scans" ,
116- scanName ,
117119 {
118- status : {
119- findings : {
120- count : findings . length ,
121- severities : {
122- informational : severityCount ( findings , "INFORMATIONAL" ) ,
123- low : severityCount ( findings , "LOW" ) ,
124- medium : severityCount ( findings , "MEDIUM" ) ,
125- high : severityCount ( findings , "HIGH" ) ,
120+ group : "execution.securecodebox.io" ,
121+ version : "v1" ,
122+ namespace,
123+ plural : "scans" ,
124+ name : scanName ,
125+ body : {
126+ status : {
127+ findings : {
128+ count : findings . length ,
129+ severities : {
130+ informational : severityCount ( findings , "INFORMATIONAL" ) ,
131+ low : severityCount ( findings , "LOW" ) ,
132+ medium : severityCount ( findings , "MEDIUM" ) ,
133+ high : severityCount ( findings , "HIGH" ) ,
134+ } ,
135+ categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
126136 } ,
127- categories : Object . fromEntries ( findingCategories . entries ( ) ) ,
128137 } ,
129138 } ,
130139 } ,
131- undefined ,
132- undefined ,
133- undefined ,
134- { headers : { "content-type" : "application/merge-patch+json" } } ,
140+ setHeaderOptions ( "Content-Type" , PatchStrategy . MergePatch ) ,
135141 ) ;
136142 console . log ( "Updated status successfully" ) ;
137143}
138144
139145async function main ( ) {
140146 let scan ;
141147 try {
142- const { body } = await k8sApi . getNamespacedCustomObject (
143- "execution.securecodebox.io" ,
144- "v1" ,
145- namespace ,
146- "scans" ,
147- scanName ,
148- ) ;
149- scan = body ;
148+ scan = await k8sApi . getNamespacedCustomObject ( {
149+ group : "execution.securecodebox.io" ,
150+ version : "v1" ,
151+ namespace : namespace ,
152+ plural : "scans" ,
153+ name : scanName ,
154+ } ) ;
150155 } catch ( err ) {
151156 console . error ( "Failed to get Scan from the kubernetes api" ) ;
152157 console . error ( err ) ;
0 commit comments