@@ -2,21 +2,81 @@ const axios = require("axios");
2
2
const { persist } = require ( "./persistence/persist" ) ;
3
3
const k8s = require ( "@kubernetes/client-node" ) ;
4
4
5
- async function main ( ) {
5
+ function downloadFile ( url ) {
6
+ return axios . get ( url ) ;
7
+ }
8
+
9
+ function getRawResults ( ) {
6
10
const rawResultUrl = process . argv [ 2 ] ;
7
- const getRawResult = ( ( url ) => ( ) =>
8
- axios . get ( url ) . then ( ( { data : findings } ) => {
9
- console . log ( `Fetched ${ findings . length } findings from the file storage` ) ;
10
- return findings ;
11
- } ) ) ( rawResultUrl ) ;
11
+ return downloadFile ( rawResultUrl ) . then ( ( { data } ) => {
12
+ console . log ( `Fetched raw result file contents from the file storage` ) ;
13
+ return data ;
14
+ } ) ;
15
+ }
12
16
17
+ function getFindings ( ) {
13
18
const findingsUrl = process . argv [ 3 ] ;
14
- const getFindings = ( ( url ) => ( ) =>
15
- axios . get ( url ) . then ( ( res ) => {
16
- console . log ( `Fetched raw result file contents from the file storage` ) ;
17
- return res . data ;
18
- } ) ) ( findingsUrl ) ;
19
+ return downloadFile ( findingsUrl ) . then ( ( { data : findings } ) => {
20
+ console . log ( `Fetched ${ findings . length } findings from the file storage` ) ;
21
+ return findings ;
22
+ } ) ;
23
+ }
24
+
25
+ function uploadFile ( url ) {
26
+ return axios
27
+ . put ( url , findingsWithIds , {
28
+ headers : { "content-type" : "" } ,
29
+ } )
30
+ . catch ( function ( error ) {
31
+ if ( error . response ) {
32
+ // The request was made and the server responded with a status code
33
+ // that falls out of the range of 2xx
34
+ console . error (
35
+ `File Upload Failed with Response Code: ${ error . response . status } `
36
+ ) ;
37
+ console . error ( `Error Response Body: ${ error . response . data } ` ) ;
38
+ } else if ( error . request ) {
39
+ console . error (
40
+ "No response received from FileStorage when uploading finding"
41
+ ) ;
42
+ console . error ( error ) ;
43
+ } else {
44
+ // Something happened in setting up the request that triggered an Error
45
+ console . log ( "Error" , error . message ) ;
46
+ }
47
+ process . exit ( 1 ) ;
48
+ } ) ;
49
+ }
50
+
51
+ function uploadRawResults ( ) {
52
+ const rawResultUploadUrl = process . argv [ 4 ] ;
53
+ if ( rawResultUploadUrl === undefined ) {
54
+ console . error (
55
+ "Tried to upload RawResults but didn't find a valid URL to upload the findings to."
56
+ ) ;
57
+ console . error ( "This probably means that this hook is a ReadOnly hook." ) ;
58
+ console . error (
59
+ "If you want to change RawResults you'll need to use a ReadAndWrite Hook."
60
+ ) ;
61
+ }
62
+ return uploadFile ( rawResultUploadUrl ) ;
63
+ }
19
64
65
+ function uploadFindings ( ) {
66
+ const findingsUploadUrl = process . argv [ 5 ] ;
67
+ if ( findingsUploadUrl === undefined ) {
68
+ console . error (
69
+ "Tried to upload Findings but didn't find a valid URL to upload the findings to."
70
+ ) ;
71
+ console . error ( "This probably means that this hook is a ReadOnly hook." ) ;
72
+ console . error (
73
+ "If you want to change Findings you'll need to use a ReadAndWrite Hook."
74
+ ) ;
75
+ }
76
+ return uploadFile ( findingsUploadUrl ) ;
77
+ }
78
+
79
+ async function main ( ) {
20
80
const scanName = process . env [ "SCAN_NAME" ] ;
21
81
const namespace = process . env [ "NAMESPACE" ] ;
22
82
console . log ( `Starting PersistenceProvider for Scan "${ scanName } "` ) ;
@@ -43,7 +103,13 @@ async function main() {
43
103
}
44
104
45
105
try {
46
- await persist ( { getRawResult, getFindings, scan } ) ;
106
+ await persist ( {
107
+ getRawResult,
108
+ getFindings,
109
+ uploadRawResults,
110
+ uploadFindings,
111
+ scan,
112
+ } ) ;
47
113
} catch ( error ) {
48
114
console . error (
49
115
"Error was thrown while running PersistenceProviders persist function"
0 commit comments