FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
secureCodeBox/parser-sdk/nodejs/parser-wrapper.js at main · Spritekin/secureCodeBox · GitHub
Spritekin
/
secureCodeBox
Public
forked from
secureCodeBox/secureCodeBox
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
secureCodeBox
/
parser-sdk
/
nodejs
/
parser-wrapper.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
165 lines (145 loc) · 4.6 KB
Breadcrumbs
secureCodeBox
/
parser-sdk
/
nodejs
/
parser-wrapper.js
Copy path
File metadata and controls
165 lines (145 loc) · 4.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
const
axios
=
require
(
"axios"
)
;
const
{
parse
}
=
require
(
"./parser/parser"
)
;
const
{
validate
,
addIdsAndDates
}
=
require
(
"./parser-utils"
)
;
const
k8s
=
require
(
"@kubernetes/client-node"
)
;
const
kc
=
new
k8s
.
KubeConfig
(
)
;
kc
.
loadFromCluster
(
)
;
const
k8sApi
=
kc
.
makeApiClient
(
k8s
.
CustomObjectsApi
)
;
const
scanName
=
process
.
env
[
"SCAN_NAME"
]
;
const
namespace
=
process
.
env
[
"NAMESPACE"
]
;
function
severityCount
(
findings
,
severity
)
{
return
findings
.
filter
(
(
{
severity
:
findingSeverity
}
)
=>
findingSeverity
.
toUpperCase
(
)
===
severity
)
.
length
;
}
async
function
uploadResultToFileStorageService
(
resultUploadUrl
,
findingsWithIdsAndDates
)
{
return
axios
.
put
(
resultUploadUrl
,
findingsWithIdsAndDates
,
{
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
(
`Finding 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
updateScanStatus
(
findings
)
{
try
{
const
findingCategories
=
new
Map
(
)
;
for
(
const
{
category
}
of
findings
)
{
if
(
findingCategories
.
has
(
category
)
)
{
findingCategories
.
set
(
category
,
findingCategories
.
get
(
category
)
+
1
)
;
}
else
{
findingCategories
.
set
(
category
,
1
)
;
}
}
await
k8sApi
.
patchNamespacedCustomObjectStatus
(
"execution.securecodebox.io"
,
"v1"
,
namespace
,
"scans"
,
scanName
,
{
status
:
{
findings
:
{
count
:
findings
.
length
,
severities
:
{
informational
:
severityCount
(
findings
,
"INFORMATIONAL"
)
,
low
:
severityCount
(
findings
,
"LOW"
)
,
medium
:
severityCount
(
findings
,
"MEDIUM"
)
,
high
:
severityCount
(
findings
,
"HIGH"
)
,
}
,
categories
:
Object
.
fromEntries
(
findingCategories
.
entries
(
)
)
,
}
,
}
,
}
,
undefined
,
undefined
,
undefined
,
{
headers
:
{
"content-type"
:
"application/merge-patch+json"
}
}
)
;
console
.
log
(
"Updated status successfully"
)
;
}
catch
(
err
)
{
console
.
error
(
"Failed to update Scan Status via the kubernetes api"
)
;
console
.
error
(
err
)
;
process
.
exit
(
1
)
;
}
}
async
function
extractScan
(
)
{
try
{
const
{
body
}
=
await
k8sApi
.
getNamespacedCustomObject
(
"execution.securecodebox.io"
,
"v1"
,
namespace
,
"scans"
,
scanName
)
;
return
body
;
}
catch
(
err
)
{
console
.
error
(
"Failed to get Scan from the kubernetes api"
)
;
console
.
error
(
err
)
;
process
.
exit
(
1
)
;
}
}
async
function
main
(
)
{
console
.
log
(
"Starting Parser"
)
;
let
scan
=
await
extractScan
(
)
;
const
resultFileUrl
=
process
.
argv
[
2
]
;
const
resultUploadUrl
=
process
.
argv
[
3
]
;
console
.
log
(
"Fetching result file"
)
;
const
{
data
}
=
await
axios
.
get
(
resultFileUrl
)
;
console
.
log
(
"Fetched result file"
)
;
let
findings
=
[
]
;
try
{
findings
=
await
parse
(
data
,
scan
)
;
}
catch
(
error
)
{
console
.
error
(
"Parser failed with error:"
)
;
console
.
error
(
error
)
;
process
.
exit
(
1
)
;
}
console
.
log
(
`Transformed raw result file into
${
findings
.
length
}
findings`
)
;
console
.
log
(
"Adding UUIDs and Dates to the findings"
)
;
const
findingsWithIdsAndDates
=
addIdsAndDates
(
findings
)
;
const
crash_on_failed_validation
=
process
.
env
[
"CRASH_ON_FAILED_VALIDATION"
]
===
"true"
console
.
log
(
"Validating Findings. Environment variable CRASH_ON_FAILED_VALIDATION is set to %s"
,
crash_on_failed_validation
)
;
try
{
await
validate
(
findingsWithIdsAndDates
)
;
console
.
log
(
"The Findings were successfully validated"
)
}
catch
(
error
)
{
console
.
error
(
"The Findings Validation failed with error(s):"
)
;
console
.
error
(
error
)
;
if
(
crash_on_failed_validation
)
{
process
.
exit
(
1
)
;
}
}
await
updateScanStatus
(
findings
)
;
console
.
log
(
`Uploading results to the file storage service`
)
;
await
uploadResultToFileStorageService
(
resultUploadUrl
,
findingsWithIdsAndDates
)
;
console
.
log
(
`Completed parser`
)
;
}
main
(
)
;
module
.
exports
.
addIdsAndDates
=
addIdsAndDates
;
Back
|
FazBrowse Home
|
New Git URL