forked from apisec-inc/apisec-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrg_apisec_job_invoke_script.sh
More file actions
324 lines (240 loc) · 14.5 KB
/
Org_apisec_job_invoke_script.sh
File metadata and controls
324 lines (240 loc) · 14.5 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/bin/bash
# Begin
TEMP=$(getopt -n "$0" -a -l "host:,username:,password:,project:,profile:,scanner:,emailReport:,reportType:,tags:,fail-on-high-vulns,openApiSpecUrl,oas,playbookCreatePolicy:" -- -- "$@")
[ $? -eq 0 ] || exit
eval set -- "$TEMP"
while [ $# -gt 0 ]
do
case "$1" in
--host) FX_HOST="$2"; shift;;
--username) FX_USER="$2"; shift;;
--password) FX_PWD="$2"; shift;;
--project) FX_PROJECT_NAME="$2"; shift;;
--profile) JOB_NAME="$2"; shift;;
--scanner) REGION="$2"; shift;;
--emailReport) FX_EMAIL_REPORT="$2"; shift;;
--reportType) FX_REPORT_TYPE="$2"; shift;;
--fail-on-high-vulns) FAIL_ON_HIGH_VULNS="$2"; shift;;
--oas) OAS="$2"; shift;;
--openApiSpecUrl) OPEN_API_SPEC_URL="$2"; shift;;
--playbookCreatePolicy) PLAYBOOK_CREATE_POLICY="$2"; shift;;
--tags) FX_TAGS="$2"; shift;;
--) shift;;
esac
shift;
done
#FX_USER=$1
#FX_PWD=$2
#FX_JOBID=$3
#REGION=$4
#FX_ENVID=$5
#FX_PROJECTID=$6
#FX_EMAIL_REPORT=$7
#FX_TAGS=$8
if [ "$FX_HOST" = "" ];
then
FX_HOST="https://cloud.apisec.ai"
fi
FX_SCRIPT=""
if [ "$FX_TAGS" != "" ];
then
FX_SCRIPT="&tags=script:"+${FX_TAGS}
fi
if [ "$FAIL_ON_HIGH_VULNS" == "" ]; then
FAIL_ON_HIGH_VULNS=false
fi
if [ "$OAS" == "" ]; then
OAS=false
fi
#if [ "$PLAYBOOK_CREATE_POLICY" == "" ] || [ "$OAS" = true ]; then
if [ "$PLAYBOOK_CREATE_POLICY" == "" ]; then
PLAYBOOK_CREATE_POLICY=false
fi
token=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${FX_USER}'", "password": "'${FX_PWD}'"}' ${FX_HOST}/login | jq -r .token)
#echo "generated token is:" $token
if [ "$OAS" = true ]; then
getProjectName=$(curl -s -X GET "${FX_HOST}/api/v1/projects/find-by-name/${FX_PROJECT_NAME}" -H "accept: */*" --header "Authorization: Bearer "$token"" | jq -r '.data|.name')
if [ "$getProjectName" == null ];
then
curl -s -H "Accept: application/json" -H "Content-Type: application/json" --location --request POST "${FX_HOST}/api/v1/projects" --header "Authorization: Bearer "$token"" -d '{"name":"'${FX_PROJECT_NAME}'","openAPISpec":"'${OPEN_API_SPEC_URL}'","planType":"ENTERPRISE","isFileLoad": false,"personalizedCoverage":{"auths":[]}}' > /dev/null
sleep 5
dto=$(curl -s --location --request GET "${FX_HOST}/api/v1/projects/find-by-name/${FX_PROJECT_NAME}" --header "Accept: application/json" --header "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '.data')
projectId=$(echo "$dto" | jq -r '.id')
curl -s -X PUT "${FX_HOST}/api/v1/projects/${projectId}/refresh-specs" -H "accept: */*" -H "Content-Type: application/json" --header "Authorization: Bearer "$token"" -d "$dto" > /dev/null
playbookTaskStatus="In_progress"
echo "playbookTaskStatus = " $playbookTaskStatus
retryCount=0
pCount=0
while [ "$playbookTaskStatus" == "In_progress" ]
do
if [ $pCount -eq 0 ]; then
echo "Checking playbooks generate task Status...."
fi
pCount=`expr $pCount + 1`
retryCount=`expr $retryCount + 1`
sleep 2
playbookTaskStatus=$(curl -s -X GET "${FX_HOST}/api/v1/events/project/${projectId}/Sync" -H "accept: */*" -H "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '."data".status')
#playbookTaskStatus="In_progress"
if [ "$playbookTaskStatus" == "Done" ]; then
echo "Playbooks generation task for the registered project $FX_PROJECT_NAME is succesfully completed!!!"
fi
if [ $retryCount -ge 55 ]; then
echo " "
retryCount=`expr $retryCount \* 2`
echo "Playbooks Generation Task Status $playbookTaskStatus even after $retryCount seconds, so halting/breaking script execution!!!"
exit 1
fi
done
PLAYBOOK_CREATE_POLICY=false
else
echo "$FX_PROJECT_NAME project already exists."
echo " "
fi
fi
if [ "$PLAYBOOK_CREATE_POLICY" = true ]; then
dto=$(curl -s --location --request GET "${FX_HOST}/api/v1/projects/find-by-name/${FX_PROJECT_NAME}" --header "Accept: application/json" --header "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '.data')
projectId=$(echo "$dto" | jq -r '.id')
curl -s -X PUT "${FX_HOST}/api/v1/projects/${projectId}/refresh-specs" -H "accept: */*" -H "Content-Type: application/json" --header "Authorization: Bearer "$token"" -d "$dto" > /dev/null
playbookTaskStatus="In_progress"
echo "playbookTaskStatus = " $playbookTaskStatus
retryCount=0
pCount=0
while [ "$playbookTaskStatus" == "In_progress" ]
do
if [ $pCount -eq 0 ]; then
echo "Checking playbooks regenerate task Status...."
fi
pCount=`expr $pCount + 1`
retryCount=`expr $retryCount + 1`
sleep 2
playbookTaskStatus=$(curl -s -X GET "${FX_HOST}/api/v1/events/project/${projectId}/Sync" -H "accept: */*" -H "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '."data".status')
#playbookTaskStatus="In_progress"
if [ "$playbookTaskStatus" == "Done" ]; then
echo "Playbooks regenerate task is succesfully completed!!!"
fi
if [ $retryCount -ge 55 ]; then
echo " "
retryCount=`expr $retryCount \* 2`
echo "Playbook Regenerate Task Status $playbookTaskStatus even after $retryCount seconds, so halting script execution!!!"
exit 1
fi
done
fi
URL="${FX_HOST}/api/v1/runs/project/${FX_PROJECT_NAME}?jobName=${JOB_NAME}®ion=${REGION}&emailReport=${FX_EMAIL_REPORT}&reportType=${FX_REPORT_TYPE}${FX_SCRIPT}"
url=$( echo "$URL" | sed 's/ /%20/g' )
data=$(curl -s --location --request POST "$url" --header "Authorization: Bearer "$token"" | jq -r '.["data"]')
runId=$( jq -r '.id' <<< "$data")
projectId=$( jq -r '.job.project.id' <<< "$data")
#runId=$(curl -s --location --request POST "$url" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
echo "runId =" $runId
#if [ -z "$runId" ]
if [ "$runId" == null ]
then
echo "RunId = " "$runId"
echo "Invalid runid"
echo $(curl -s --location --request POST "${FX_HOST}/api/v1/runs/project/${FX_PROJECT_NAME}?jobName=${JOB_NAME}®ion=${REGION}&emailReport=${FX_EMAIL_REPORT}&reportType=${FX_REPORT_TYPE}${FX_SCRIPT}" --header "Authorization: Bearer "$token"" | jq -r '.["data"]|.id')
exit 1
fi
taskStatus="WAITING"
echo "taskStatus = " $taskStatus
while [ "$taskStatus" == "WAITING" -o "$taskStatus" == "PROCESSING" ]
do
sleep 5
echo "Checking Status...."
passPercent=$(curl -s --location --request GET "${FX_HOST}/api/v1/runs/${runId}" --header "Authorization: Bearer "$token""| jq -r '.["data"]|.ciCdStatus')
IFS=':' read -r -a array <<< "$passPercent"
taskStatus="${array[0]}"
echo "Status =" "${array[0]}" " Success Percent =" "${array[1]}" " Total Tests =" "${array[2]}" " Total Failed =" "${array[3]}" " Run =" "${array[6]}"
# VAR2=$(echo "Status =" "${array[0]}" " Success Percent =" "${array[1]}" " Total Tests =" "${array[2]}" " Total Failed =" "${array[3]}" " Run =" "${array[6]}")
if [ "$taskStatus" == "COMPLETED" ];then
echo "------------------------------------------------"
# echo "Run detail link ${FX_HOST}/${array[7]}"
echo "Run detail link ${FX_HOST}${array[7]}"
echo "-----------------------------------------------"
echo "Scan Successfully Completed"
echo " "
if [ "$FAIL_ON_HIGH_VULNS" = true ]; then
severity=$(curl -s -X GET "${FX_HOST}/api/v1/projects/${projectId}/vulnerabilities?&severity=All&page=0&pageSize=20" -H "accept: */*" "Content-Type: application/json" --header "Authorization: Bearer "$token"" | jq -r '.data[] | .severity')
cVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Critical" ]; then
cVulCount=`expr $cVulCount + 1`
fi
done
echo "Found $cVulCount Critical Severity Vulnerabilities!!!"
echo " "
hVulCount=0
for vul in ${severity}
do
if [ "$vul" == "High" ]; then
hVulCount=`expr $hVulCount + 1`
fi
done
echo "Found $hVulCount High Severity Vulnerabilities!!!"
echo " "
majVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Major" ]; then
majVulCount=`expr $majVulCount + 1`
fi
done
echo "Found $majVulCount Major Severity Vulnerabilities!!!"
echo " "
medVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Medium" ]; then
medVulCount=`expr $medVulCount + 1`
fi
done
echo "Found $medVulCount Medium Severity Vulnerabilities!!!"
echo " "
minVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Minor" ]; then
minVulCount=`expr $minVulCount + 1`
fi
done
echo "Found $minVulCount Minor Severity Vulnerabilities!!!"
echo " "
lowVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Low" ]; then
lowVulCount=`expr $lowVulCount + 1`
fi
done
echo "Found $lowVulCount Low Severity Vulnerabilities!!!"
echo " "
triVulCount=0
for vul in ${severity}
do
if [ "$vul" == "Trivial" ]; then
triVulCount=`expr $triVulCount + 1`
fi
done
echo "Found $triVulCount Trivial Severity Vulnerabilities!!!"
echo " "
vCount=1
for vul in ${severity}
do
if [ "$vul" == "Critical" ] || [ "$vul" == "High" ] ; then
echo "Failing script execution since we have found "$vul" severity vulnerability!!!"
exit 1
fi
vCount=`expr $vCount + 1`
done
fi
exit 0
fi
done
if [ "$taskStatus" == "TIMEOUT" ];then
echo "Task Status = " $taskStatus
exit 1
fi
echo "$(curl -s --location --request GET "${FX_HOST}/api/v1/runs/${runId}" --header "Authorization: Bearer "$token"")"
exit 1
return 0