1
- package com .checkmarx .ast ;
1
+ package com .checkmarx .ast . scans ;
2
2
3
+ import com .checkmarx .ast .exceptions .CxException ;
4
+ import com .checkmarx .ast .executionservice .ExecutionService ;
5
+ import com .checkmarx .ast .results .CxCommandOutput ;
6
+ import com .fasterxml .jackson .core .JsonParser ;
3
7
import com .fasterxml .jackson .core .JsonProcessingException ;
4
8
import com .fasterxml .jackson .core .type .TypeReference ;
5
9
import com .fasterxml .jackson .databind .ObjectMapper ;
6
- import com .google .gson .Gson ;
7
10
import org .apache .commons .lang3 .StringUtils ;
8
11
import org .slf4j .Logger ;
9
12
import org .slf4j .LoggerFactory ;
10
13
11
14
import java .io .*;
12
- import java .lang .reflect .Type ;
13
15
import java .net .URI ;
14
16
import java .net .URISyntaxException ;
15
17
import java .net .URL ;
@@ -32,11 +34,10 @@ public class CxAuth {
32
34
private final String secret ;
33
35
private final String apikey ;
34
36
private final URI executable ;
35
- private static final Gson gson = new Gson ();
36
37
37
- public CxAuth (CxScanConfig scanConfig , Logger log )
38
- throws IOException , URISyntaxException , CxException {
39
- if ( scanConfig == null ) throw new CxException ("CxScanConfig object returned as null!" );
38
+ public CxAuth (CxScanConfig scanConfig , Logger log ) throws IOException , URISyntaxException , CxException {
39
+ if ( scanConfig == null )
40
+ throw new CxException ("CxScanConfig object returned as null!" );
40
41
41
42
this .baseuri = scanConfig .getBaseUri ();
42
43
this .baseAuthUri = scanConfig .getBaseAuthUri ();
@@ -101,7 +102,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
101
102
102
103
try {
103
104
fileURI = extract (zipFile , fileName );
104
- log .info ("Location of the jar file: {}" ,fileURI ) ;
105
+ log .info ("Location of the jar file: {}" , fileURI );
105
106
} finally {
106
107
zipFile .close ();
107
108
}
@@ -157,7 +158,7 @@ private static void close(final Closeable stream) {
157
158
}
158
159
159
160
public CxCommandOutput cxScanShow (String id ) throws IOException , InterruptedException {
160
- log .info ("Initialized scan retrieval for id: {}" , id );
161
+ log .info ("Initialized scan retrieval for id: {}" , id );
161
162
List <String > commands = initialCommands ();
162
163
commands .add ("scan" );
163
164
commands .add ("show" );
@@ -172,19 +173,76 @@ public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedExce
172
173
return scanObject ;
173
174
}
174
175
176
+ public String cxGetResultsSummary (String scanID , String formatType , String target )
177
+ throws IOException {
178
+ List <String > commands = initialCommandsCommon ();
179
+ commands .add ("result" );
180
+ commands .add ("summary" );
181
+ if (scanID .isEmpty ()) {
182
+ throw new CxException ("Please provide the scan id " );
183
+ }
184
+ commands .add ("--scan-id" );
185
+ commands .add (scanID );
186
+ if (!formatType .isEmpty ()) {
187
+ commands .add ("--format" );
188
+ commands .add (formatType );
189
+ }
190
+ if (!target .isEmpty ()) {
191
+ commands .add ("--target" );
192
+ commands .add (target );
193
+ }
194
+ return runResultExecutionCommands (commands );
195
+ }
196
+
197
+ public String cxGetResultsList (String scanID , String formatType )
198
+ throws IOException {
199
+ List <String > commands = initialCommandsCommon ();
200
+ commands .add ("result" );
201
+ commands .add ("list" );
202
+ if (scanID .isEmpty ()) {
203
+ throw new CxException ("Please provide the scan id " );
204
+ }
205
+ commands .add ("--scan-id" );
206
+ commands .add (scanID );
207
+ if (!formatType .isEmpty ()) {
208
+ commands .add ("--format" );
209
+ commands .add (formatType );
210
+ }
211
+
212
+ return runResultExecutionCommands (commands );
213
+ }
214
+
215
+ private String runResultExecutionCommands (List <String > commands ) throws IOException {
216
+ log .info ("Process submitting to the executor" );
217
+ ExecutionService exec = new ExecutionService ();
218
+ Process process = exec .executeCommand (commands );
219
+ BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()));
220
+ StringBuilder builder = new StringBuilder ();
221
+ String line = null ;
222
+ while ((line = reader .readLine ()) != null ) {
223
+ builder .append (line );
224
+ builder .append (System .getProperty ("line.separator" ));
225
+ }
226
+ if (!process .isAlive () && process .exitValue ()!= 0 ) {
227
+ log .info ("Exit code from CLI is: {} " , process .exitValue ());
228
+ return "" ;
229
+ }
230
+ return builder .toString ();
231
+ }
232
+
175
233
private CxCommandOutput runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
176
234
log .info ("Process submitting to the executor" );
177
235
ExecutionService exec = new ExecutionService ();
178
236
Process process = exec .executeCommand (commands );
179
237
String line ;
180
238
CxScan scanObject = null ;
181
239
InputStream is = process .getInputStream ();
182
- InputStreamReader isr = new InputStreamReader (is );
183
- BufferedReader br = new BufferedReader (isr );
184
- CxCommandOutput cxCommandOutput = new CxCommandOutput ();
240
+ InputStreamReader isr = new InputStreamReader (is );
241
+ BufferedReader br = new BufferedReader (isr );
242
+ CxCommandOutput cxCommandOutput = new CxCommandOutput ();
185
243
while ((line = br .readLine ()) != null ) {
186
244
log .info (line );
187
- if (!StringUtils .isBlank (line ) && isJSONValid (line , CxScan . class )) {
245
+ if (!StringUtils .isBlank (line ) && isValidJSON (line )) {
188
246
scanObject = transformToCxScanObject (line );
189
247
List <CxScan > scanList = new ArrayList <>();
190
248
scanList .add (scanObject );
@@ -193,8 +251,7 @@ private CxCommandOutput runExecutionCommands(List<String> commands) throws IOExc
193
251
}
194
252
br .close ();
195
253
process .waitFor ();
196
-
197
- if (!process .isAlive ()) {
254
+ if (!process .isAlive ()) {
198
255
cxCommandOutput .setExitCode (process .exitValue ());
199
256
log .info ("Exit code from AST-CLI: {}" , process .exitValue ());
200
257
}
@@ -259,7 +316,6 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
259
316
List <String > commands = initialCommands ();
260
317
commands .add ("scan" );
261
318
commands .add ("list" );
262
-
263
319
ExecutionService exec = new ExecutionService ();
264
320
Process process = exec .executeCommand (commands );
265
321
String line ;
@@ -268,7 +324,7 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
268
324
InputStreamReader isr = new InputStreamReader (is );
269
325
BufferedReader br = new BufferedReader (isr );
270
326
while ((line = br .readLine ()) != null ) {
271
- if (isJSONValid (line , List . class ) && !line .isEmpty ())
327
+ if (isValidJSON (line ) && !line .isEmpty ())
272
328
list = transformToCxScanList (line );
273
329
}
274
330
br .close ();
@@ -278,7 +334,7 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
278
334
cxCommandOutput .setScanObjectList (list );
279
335
cxCommandOutput .setExitCode (process .exitValue ());
280
336
if (list != null && !list .isEmpty ())
281
- log .info ("Retrieved scan list with size: {}" , list .size ());
337
+ log .info ("Retrieved scan list with size: {}" , list .size ());
282
338
else
283
339
log .info ("Not able to retrieve scan list" );
284
340
@@ -316,8 +372,6 @@ public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOEx
316
372
return runExecutionCommands (commands );
317
373
}
318
374
319
-
320
-
321
375
private void addIndividualParams (List <String > commands , String value ) {
322
376
Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
323
377
while (m .find ())
@@ -351,13 +405,17 @@ private List<CxScan> transformToCxScanList(String line) throws IOException {
351
405
352
406
}
353
407
354
- private boolean isJSONValid (String jsonInString , Object object ) {
408
+ public boolean isValidJSON (final String json ) {
409
+ boolean valid = false ;
355
410
try {
356
- gson .fromJson (jsonInString , (Type ) object );
357
- return true ;
358
- } catch (com .google .gson .JsonSyntaxException ex ) {
359
- return false ;
411
+ final JsonParser parser = new ObjectMapper ().createParser (json );
412
+ while (parser .nextToken () != null ) {
413
+ }
414
+ valid = true ;
415
+ } catch (IOException ignored ) {
360
416
}
417
+ ;
418
+ return valid ;
361
419
}
362
420
363
421
}
0 commit comments