1- package com .checkmarx .ast ;
1+ package com .checkmarx .ast . scans ;
22
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 ;
37import com .fasterxml .jackson .core .JsonProcessingException ;
48import com .fasterxml .jackson .core .type .TypeReference ;
59import com .fasterxml .jackson .databind .ObjectMapper ;
6- import com .google .gson .Gson ;
710import org .apache .commons .lang3 .StringUtils ;
811import org .slf4j .Logger ;
912import org .slf4j .LoggerFactory ;
1013
1114import java .io .*;
12- import java .lang .reflect .Type ;
1315import java .net .URI ;
1416import java .net .URISyntaxException ;
1517import java .net .URL ;
@@ -32,11 +34,10 @@ public class CxAuth {
3234 private final String secret ;
3335 private final String apikey ;
3436 private final URI executable ;
35- private static final Gson gson = new Gson ();
3637
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!" );
4041
4142 this .baseuri = scanConfig .getBaseUri ();
4243 this .baseAuthUri = scanConfig .getBaseAuthUri ();
@@ -101,7 +102,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
101102
102103 try {
103104 fileURI = extract (zipFile , fileName );
104- log .info ("Location of the jar file: {}" ,fileURI ) ;
105+ log .info ("Location of the jar file: {}" , fileURI );
105106 } finally {
106107 zipFile .close ();
107108 }
@@ -157,7 +158,7 @@ private static void close(final Closeable stream) {
157158 }
158159
159160 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 );
161162 List <String > commands = initialCommands ();
162163 commands .add ("scan" );
163164 commands .add ("show" );
@@ -172,19 +173,76 @@ public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedExce
172173 return scanObject ;
173174 }
174175
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+
175233 private CxCommandOutput runExecutionCommands (List <String > commands ) throws IOException , InterruptedException {
176234 log .info ("Process submitting to the executor" );
177235 ExecutionService exec = new ExecutionService ();
178236 Process process = exec .executeCommand (commands );
179237 String line ;
180238 CxScan scanObject = null ;
181239 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 ();
185243 while ((line = br .readLine ()) != null ) {
186244 log .info (line );
187- if (!StringUtils .isBlank (line ) && isJSONValid (line , CxScan . class )) {
245+ if (!StringUtils .isBlank (line ) && isValidJSON (line )) {
188246 scanObject = transformToCxScanObject (line );
189247 List <CxScan > scanList = new ArrayList <>();
190248 scanList .add (scanObject );
@@ -193,8 +251,7 @@ private CxCommandOutput runExecutionCommands(List<String> commands) throws IOExc
193251 }
194252 br .close ();
195253 process .waitFor ();
196-
197- if (!process .isAlive ()) {
254+ if (!process .isAlive ()) {
198255 cxCommandOutput .setExitCode (process .exitValue ());
199256 log .info ("Exit code from AST-CLI: {}" , process .exitValue ());
200257 }
@@ -259,7 +316,6 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
259316 List <String > commands = initialCommands ();
260317 commands .add ("scan" );
261318 commands .add ("list" );
262-
263319 ExecutionService exec = new ExecutionService ();
264320 Process process = exec .executeCommand (commands );
265321 String line ;
@@ -268,7 +324,7 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
268324 InputStreamReader isr = new InputStreamReader (is );
269325 BufferedReader br = new BufferedReader (isr );
270326 while ((line = br .readLine ()) != null ) {
271- if (isJSONValid (line , List . class ) && !line .isEmpty ())
327+ if (isValidJSON (line ) && !line .isEmpty ())
272328 list = transformToCxScanList (line );
273329 }
274330 br .close ();
@@ -278,7 +334,7 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
278334 cxCommandOutput .setScanObjectList (list );
279335 cxCommandOutput .setExitCode (process .exitValue ());
280336 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 ());
282338 else
283339 log .info ("Not able to retrieve scan list" );
284340
@@ -316,8 +372,6 @@ public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOEx
316372 return runExecutionCommands (commands );
317373 }
318374
319-
320-
321375 private void addIndividualParams (List <String > commands , String value ) {
322376 Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
323377 while (m .find ())
@@ -351,13 +405,17 @@ private List<CxScan> transformToCxScanList(String line) throws IOException {
351405
352406 }
353407
354- private boolean isJSONValid (String jsonInString , Object object ) {
408+ public boolean isValidJSON (final String json ) {
409+ boolean valid = false ;
355410 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 ) {
360416 }
417+ ;
418+ return valid ;
361419 }
362420
363421}
0 commit comments