33import com .checkmarx .ast .exceptions .CxException ;
44import com .checkmarx .ast .executionservice .ExecutionService ;
55import com .checkmarx .ast .results .CxCommandOutput ;
6- import com .checkmarx .ast .results .CxResultFormatType ;
76import com .checkmarx .ast .results .structure .CxResultOutput ;
87import com .fasterxml .jackson .core .JsonParser ;
98import com .fasterxml .jackson .core .JsonProcessingException ;
109import com .fasterxml .jackson .core .type .TypeReference ;
1110import com .fasterxml .jackson .databind .ObjectMapper ;
1211import org .apache .commons .lang3 .StringUtils ;
13- import org .apache .commons .lang3 .reflect .FieldUtils ;
1412import org .slf4j .Logger ;
1513import org .slf4j .LoggerFactory ;
1614
2725import java .util .ArrayList ;
2826import java .util .List ;
2927import java .util .Map ;
28+ import java .util .Optional ;
3029import java .util .regex .Matcher ;
3130import java .util .regex .Pattern ;
3231import java .util .zip .ZipEntry ;
@@ -40,18 +39,22 @@ public class CxAuth {
4039 private final String key ;
4140 private final String secret ;
4241 private final String apikey ;
42+ private final List <String > additionalParameters = new ArrayList <>();
4343 private final URI executable ;
4444
4545 public CxAuth (CxScanConfig scanConfig , Logger log ) throws IOException , URISyntaxException , CxException {
46- if (scanConfig == null )
46+ if (scanConfig == null ) {
4747 throw new CxException ("CxScanConfig object returned as null!" );
48+ }
4849
4950 this .baseuri = scanConfig .getBaseUri ();
5051 this .baseAuthUri = scanConfig .getBaseAuthUri ();
5152 this .tenant = scanConfig .getTenant ();
5253 this .key = scanConfig .getClientId ();
5354 this .secret = scanConfig .getClientSecret ();
5455 this .apikey = scanConfig .getApiKey ();
56+ addIndividualParams (this .additionalParameters ,
57+ Optional .ofNullable (scanConfig .getAdditionalParameters ()).orElse ("" ));
5558
5659 validateConfigValues ();
5760
@@ -67,7 +70,7 @@ public CxAuth(CxScanConfig scanConfig, Logger log) throws IOException, URISyntax
6770 }
6871 }
6972
70- private void validateConfigValues (){
73+ private void validateConfigValues () {
7174 if (StringUtils .isEmpty (this .baseuri )) {
7275 throw new CxException ("Checkmarx server URL was not set" );
7376 }
@@ -184,10 +187,11 @@ public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedExce
184187 commands .add ("--scan-id" );
185188 commands .add (id );
186189 CxCommandOutput scanObject = runExecutionCommands (commands );
187- if (scanObject .getScanObjectList () != null && scanObject .getScanObjectList ().size () == 1 )
190+ if (scanObject .getScanObjectList () != null && scanObject .getScanObjectList ().size () == 1 ) {
188191 log .info ("Scan retrieved" );
189- else
192+ } else {
190193 log .info ("Did not receive the scan" );
194+ }
191195
192196 return scanObject ;
193197 }
@@ -234,7 +238,6 @@ private List<String> buildResultCommand(String resultType, String scanId, String
234238 }
235239
236240
237-
238241 private String runResultExecutionCommands (String scanId , String resultType , String extension ) throws IOException {
239242 Path tempDir = Files .createTempDirectory ("cx" );
240243 String fileName = Long .toString (System .nanoTime ());
@@ -263,7 +266,7 @@ private String runResultExecutionCommands(List<String> commands) throws IOExcept
263266 builder .append (line );
264267 builder .append (System .getProperty ("line.separator" ));
265268 }
266- if (!process .isAlive () && process .exitValue ()!= 0 ) {
269+ if (!process .isAlive () && process .exitValue () != 0 ) {
267270 log .info ("Exit code from CLI is: {} " , process .exitValue ());
268271 return "" ;
269272 }
@@ -330,6 +333,8 @@ public List<String> initialCommandsCommon() {
330333 commands .add (this .baseAuthUri );
331334 }
332335
336+ commands .addAll (this .additionalParameters );
337+
333338 return commands ;
334339 }
335340
@@ -364,19 +369,21 @@ public CxCommandOutput cxAstScanList() throws IOException, InterruptedException
364369 InputStreamReader isr = new InputStreamReader (is );
365370 BufferedReader br = new BufferedReader (isr );
366371 while ((line = br .readLine ()) != null ) {
367- if (isValidJSON (line ) && !line .isEmpty ())
372+ if (isValidJSON (line ) && !line .isEmpty ()) {
368373 list = transformToCxScanList (line );
374+ }
369375 }
370376 br .close ();
371377 process .waitFor ();
372378
373379 CxCommandOutput cxCommandOutput = new CxCommandOutput ();
374380 cxCommandOutput .setScanObjectList (list );
375381 cxCommandOutput .setExitCode (process .exitValue ());
376- if (list != null && !list .isEmpty ())
382+ if (list != null && !list .isEmpty ()) {
377383 log .info ("Retrieved scan list with size: {}" , list .size ());
378- else
384+ } else {
379385 log .info ("Not able to retrieve scan list" );
386+ }
380387
381388 return cxCommandOutput ;
382389 }
@@ -396,32 +403,28 @@ public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOEx
396403 addIndividualParams (commands , param .getValue ());
397404 } else if (param .getKey ().toString ().length () == 1 ) {
398405 commands .add ("-" + param .getKey ().toString ().toLowerCase ());
399- if (param .getValue () != null )
406+ if (param .getValue () != null ) {
400407 commands .add (param .getValue ());
401- else
408+ } else {
402409 commands .add (" " );
410+ }
403411
404412 } else if (param .getKey () != CxParamType .ADDITIONAL_PARAMETERS ) {
405413 String paramValue = param .getKey ().toString ();
406414 paramValue = "--" + paramValue .replace ("_" , "-" ).toLowerCase ();
407415 commands .add (paramValue );
408- if (param .getValue () != null )
416+ if (param .getValue () != null ) {
409417 commands .add (param .getValue ());
410- else
418+ } else {
411419 commands .add (" " );
420+ }
412421
413422 }
414423 }
415424
416425 return runExecutionCommands (commands );
417426 }
418427
419- private void addIndividualParams (List <String > commands , String value ) {
420- Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
421- while (m .find ())
422- commands .add (m .group (1 ));
423- }
424-
425428 private void addAuthCredentials (List <String > commands ) {
426429 if (key != null && secret != null ) {
427430 commands .add ("--client-id" );
@@ -436,7 +439,14 @@ private void addAuthCredentials(List<String> commands) {
436439 }
437440 }
438441
439- private List <CxScan > transformToCxScanList (String line ) {
442+ private static void addIndividualParams (List <String > commands , String value ) {
443+ Matcher m = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" ).matcher (value );
444+ while (m .find ()) {
445+ commands .add (m .group (1 ));
446+ }
447+ }
448+
449+ private static List <CxScan > transformToCxScanList (String line ) {
440450 ObjectMapper objectMapper = new ObjectMapper ();
441451 List <CxScan > scanList ;
442452 try {
@@ -449,7 +459,7 @@ private List<CxScan> transformToCxScanList(String line) {
449459
450460 }
451461
452- public boolean isValidJSON (final String json ) {
462+ public static boolean isValidJSON (final String json ) {
453463 boolean valid = false ;
454464 try {
455465 final JsonParser parser = new ObjectMapper ().createParser (json );
@@ -461,5 +471,4 @@ public boolean isValidJSON(final String json) {
461471 }
462472 return valid ;
463473 }
464-
465474}
0 commit comments