Skip to content

Commit e59a127

Browse files
authored
add package data to java wrapper (#19)
* add package data to java wrapper * add validation to prevent null pointer exception when a test fails * add possibility of adding additional parameters globally to the scan config
1 parent 7a85021 commit e59a127

File tree

5 files changed

+121
-39
lines changed

5 files changed

+121
-39
lines changed

src/main/java/com/checkmarx/ast/results/structure/CxResultData.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
77
import lombok.*;
88

9-
import java.math.BigInteger;
109
import java.util.List;
1110

1211
@Data
@@ -26,20 +25,23 @@ public class CxResultData {
2625
String languageName;
2726
String description;
2827
List<CxResultDataNode> nodes;
28+
List<CxResultPackageData> packageData;
2929

3030
public CxResultData(@JsonProperty("queryId") String queryId,
3131
@JsonProperty("queryName") String queryName,
3232
@JsonProperty("group") String group,
3333
@JsonProperty("resultHash") String resultHash,
3434
@JsonProperty("languageName") String languageName,
3535
@JsonProperty("description") String description,
36-
@JsonProperty("nodes") List<CxResultDataNode> nodes) {
36+
@JsonProperty("nodes") List<CxResultDataNode> nodes,
37+
@JsonProperty("packageData") List<CxResultPackageData> packageData) {
3738
this.queryId = queryId;
3839
this.queryName = queryName;
3940
this.group = group;
4041
this.resultHash = resultHash;
4142
this.languageName = languageName;
4243
this.description = description;
4344
this.nodes = nodes;
45+
this.packageData = packageData;
4446
}
4547
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.checkmarx.ast.results.structure;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7+
import lombok.*;
8+
9+
@Data
10+
@Builder
11+
@Value
12+
@EqualsAndHashCode
13+
@ToString
14+
@JsonDeserialize()
15+
@JsonInclude(JsonInclude.Include.NON_NULL)
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
public class CxResultPackageData {
18+
19+
String comment;
20+
String type;
21+
String url;
22+
23+
public CxResultPackageData(@JsonProperty("comment") String comment,
24+
@JsonProperty("type") String type,
25+
@JsonProperty("url") String url) {
26+
this.comment = comment;
27+
this.type = type;
28+
this.url = url;
29+
}
30+
}

src/main/java/com/checkmarx/ast/scans/CxAuth.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
import com.checkmarx.ast.exceptions.CxException;
44
import com.checkmarx.ast.executionservice.ExecutionService;
55
import com.checkmarx.ast.results.CxCommandOutput;
6-
import com.checkmarx.ast.results.CxResultFormatType;
76
import com.checkmarx.ast.results.structure.CxResultOutput;
87
import com.fasterxml.jackson.core.JsonParser;
98
import com.fasterxml.jackson.core.JsonProcessingException;
109
import com.fasterxml.jackson.core.type.TypeReference;
1110
import com.fasterxml.jackson.databind.ObjectMapper;
1211
import org.apache.commons.lang3.StringUtils;
13-
import org.apache.commons.lang3.reflect.FieldUtils;
1412
import org.slf4j.Logger;
1513
import org.slf4j.LoggerFactory;
1614

@@ -27,6 +25,7 @@
2725
import java.util.ArrayList;
2826
import java.util.List;
2927
import java.util.Map;
28+
import java.util.Optional;
3029
import java.util.regex.Matcher;
3130
import java.util.regex.Pattern;
3231
import 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
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.checkmarx.ast.scans;
22

3+
import lombok.Data;
34
import lombok.Getter;
45
import lombok.Setter;
56

67
import java.io.Serializable;
78

8-
@Getter
9-
@Setter
9+
@Data
1010
public class CxScanConfig implements Serializable {
1111

1212
private String baseUri;
@@ -16,4 +16,5 @@ public class CxScanConfig implements Serializable {
1616
private String clientSecret;
1717
private String apiKey;
1818
private String pathToExecutable;
19+
private String additionalParameters;
1920
}

src/test/java/com/checkmarx/ast/CxAuthTest.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.checkmarx.ast;
22

33
import com.checkmarx.ast.results.CxCommandOutput;
4-
import com.checkmarx.ast.results.CxResultFormatType;
54
import com.checkmarx.ast.results.structure.CxResultOutput;
65
import com.checkmarx.ast.scans.CxAuth;
76
import com.checkmarx.ast.scans.CxParamType;
87
import com.checkmarx.ast.scans.CxScanConfig;
9-
import com.fasterxml.jackson.databind.ObjectMapper;
108
import org.junit.Assert;
119
import org.junit.Before;
1210
import org.junit.Test;
@@ -39,6 +37,12 @@ public void init() throws IOException, URISyntaxException {
3937
log.info("Init test");
4038

4139
Map<String, String> environmentVariables = System.getenv();
40+
CxScanConfig config = getCxScanConfig(environmentVariables);
41+
42+
auth = new CxAuth(config, log);
43+
}
44+
45+
private static CxScanConfig getCxScanConfig(Map<String, String> environmentVariables) {
4246
CxScanConfig config = new CxScanConfig();
4347
config.setClientId(environmentVariables.getOrDefault("CX_CLIENT_ID", null));
4448
config.setClientSecret(environmentVariables.getOrDefault("CX_CLIENT_SECRET", null));
@@ -47,11 +51,10 @@ public void init() throws IOException, URISyntaxException {
4751
config.setBaseAuthUri(environmentVariables.getOrDefault("CX_BASE_AUTH_URI", null));
4852
config.setTenant(environmentVariables.getOrDefault("CX_TENANT", null));
4953
config.setPathToExecutable(environmentVariables.getOrDefault("PATH_TO_EXECUTABLE", null));
50-
51-
auth = new CxAuth(config, log);
54+
return config;
5255
}
5356

54-
private Map<CxParamType, String> createParams() {
57+
private static Map<CxParamType, String> createParams() {
5558
Map<CxParamType, String> params = new HashMap<>();
5659
params.put(CxParamType.PROJECT_NAME, "JavaWrapperTestCases");
5760
params.put(CxParamType.SCAN_TYPES, "sast");
@@ -84,8 +87,12 @@ public void cxScanCreationWithBranchName() throws InterruptedException, IOExcept
8487
Map<CxParamType, String> params = createParams();
8588
params.put(CxParamType.BRANCH, "test");
8689

87-
CxCommandOutput scanResult = auth.cxScanCreate(params);
88-
String status = auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus();
90+
CxCommandOutput scanResult = validateCommandOutput(auth.cxScanCreate(params));
91+
String status = validateCommandOutput(auth.cxScanShow(scanResult.getScanObjectList()
92+
.get(0)
93+
.getID())).getScanObjectList()
94+
.get(0)
95+
.getStatus();
8996
assertTrue(status.equalsIgnoreCase(COMPLETED));
9097
}
9198

@@ -94,8 +101,11 @@ public void cxScanCreationWrongPreset() throws InterruptedException, IOException
94101
Map<CxParamType, String> params = createParams();
95102
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default Jay");
96103

97-
CxCommandOutput scanResult = auth.cxScanCreate(params);
98-
String status = auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus();
104+
CxCommandOutput scanResult = validateCommandOutput(auth.cxScanCreate(params));
105+
String status = validateCommandOutput(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()))
106+
.getScanObjectList()
107+
.get(0)
108+
.getStatus();
99109
assertTrue(status.equalsIgnoreCase(FAILED));
100110
}
101111

@@ -106,8 +116,12 @@ public void cxScanCreationSuccess() throws InterruptedException, IOException {
106116
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
107117
//params.put(CxParamType.ADDITIONAL_PARAMETERS,"--nowait");
108118

109-
CxCommandOutput scanResult = auth.cxScanCreate(params);
110-
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(COMPLETED));
119+
CxCommandOutput scanResult = validateCommandOutput(auth.cxScanCreate(params));
120+
assertTrue(validateCommandOutput(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()))
121+
.getScanObjectList()
122+
.get(0)
123+
.getStatus()
124+
.equalsIgnoreCase(COMPLETED));
111125
}
112126

113127

@@ -151,4 +165,30 @@ public void cxResultsStructure() {
151165
fail("Failed getting results object: " + e.getMessage());
152166
}
153167
}
168+
169+
private static CxCommandOutput validateCommandOutput(CxCommandOutput output) {
170+
if (output == null) {
171+
fail("invalid output for command: output is null");
172+
}
173+
if (output.getScanObjectList() == null) {
174+
fail("invalid output for command: scan object list is null");
175+
}
176+
if (output.getScanObjectList().size() == 0) {
177+
fail("invalid output for command: scan object list is empty");
178+
}
179+
return output;
180+
}
181+
182+
@Test
183+
public void cxAdditionalParameters() {
184+
try {
185+
CxScanConfig config = getCxScanConfig(System.getenv());
186+
config.setAdditionalParameters("--filter limit=1");
187+
CxAuth auth = new CxAuth(config, log);
188+
CxCommandOutput output = auth.cxAstScanList();
189+
Assert.assertEquals(1, output.getScanObjectList().size());
190+
} catch (IOException | InterruptedException | URISyntaxException e) {
191+
fail("failed getting scan list");
192+
}
193+
}
154194
}

0 commit comments

Comments
 (0)