Skip to content

Commit 50cfe0c

Browse files
authored
Feature/update results parameters (#11)
* Added feature to include results generation
1 parent 5529dbb commit 50cfe0c

File tree

14 files changed

+269
-37
lines changed

14 files changed

+269
-37
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.checkmarx.ast</groupId>
66
<artifactId>ast-cli-java-wrapper</artifactId>
7-
<version>1.0.8</version>
7+
<version>1.0.9</version>
88
<packaging>jar</packaging>
99

1010
<dependencies>

src/main/java/com/checkmarx/ast/CxException.java renamed to src/main/java/com/checkmarx/ast/exceptions/CxException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.checkmarx.ast;
1+
package com.checkmarx.ast.exceptions;
22

33
public class CxException extends RuntimeException{
44
public CxException(String errorMessage) {

src/main/java/com/checkmarx/ast/ExecutionService.java renamed to src/main/java/com/checkmarx/ast/executionservice/ExecutionService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.checkmarx.ast;
1+
package com.checkmarx.ast.executionservice;
22

33
import java.io.IOException;
44
import java.util.List;

src/main/java/com/checkmarx/ast/CxCommandOutput.java renamed to src/main/java/com/checkmarx/ast/results/CxCommandOutput.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package com.checkmarx.ast;
1+
package com.checkmarx.ast.results;
22

33

4+
import com.checkmarx.ast.scans.CxScan;
45
import lombok.*;
56

67

78
import java.util.List;
89

910
@Getter
1011
@Setter
11-
public class CxCommandOutput {
12+
public class CxCommandOutput extends CxOutput{
1213
private int exitCode;
1314
private List<CxScan> scanObjectList;
1415

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
package com.checkmarx.ast.results;
3+
4+
public class CxOutput {
5+
6+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.checkmarx.ast.results;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8+
import lombok.Builder;
9+
import lombok.Data;
10+
import lombok.Value;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
14+
15+
@Data
16+
@Builder
17+
@Value
18+
@JsonDeserialize()
19+
@JsonInclude(JsonInclude.Include.NON_NULL)
20+
@JsonIgnoreProperties(ignoreUnknown = true)
21+
public class CxResult {
22+
23+
private String comments;
24+
25+
private String type;
26+
27+
private String id;
28+
29+
private String similarityID;
30+
31+
private String status;
32+
33+
private String state;
34+
35+
private String severity;
36+
37+
private String firstFoundAt;
38+
39+
private String foundAt;
40+
41+
private String firstScan;
42+
43+
@JsonCreator
44+
public CxResult(@JsonProperty("comments") String comments, @JsonProperty("type") String type,
45+
@JsonProperty("id") String id, @JsonProperty("similarityID") String similarityID,
46+
@JsonProperty("status") String status, @JsonProperty("state") String state,
47+
@JsonProperty("severity") String severity, @JsonProperty("firstFoundAt") String firstFoundAt,
48+
@JsonProperty("foundAt") String foundAt, @JsonProperty("firstScan") String firstScan ) {
49+
this.comments = comments;
50+
this.type = type;
51+
this.id = id;
52+
this.similarityID = similarityID;
53+
this.status = status;
54+
this.state = state;
55+
this.severity = severity;
56+
this.firstFoundAt = firstFoundAt;
57+
this.foundAt = foundAt;
58+
this.firstScan = firstScan;
59+
}
60+
61+
@Override
62+
public boolean equals(Object o) {
63+
return EqualsBuilder.reflectionEquals(this, o);
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return HashCodeBuilder.reflectionHashCode(this);
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return ReflectionToStringBuilder.toString(this);
74+
}
75+
76+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.checkmarx.ast.results;
2+
3+
public enum CxResultFormatType {
4+
JSON, LIST
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.checkmarx.ast.results;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8+
import lombok.Builder;
9+
import lombok.Data;
10+
import lombok.Value;
11+
import org.apache.commons.lang3.builder.EqualsBuilder;
12+
import org.apache.commons.lang3.builder.HashCodeBuilder;
13+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
14+
15+
import java.util.List;
16+
17+
@Data
18+
@Builder
19+
@Value
20+
@JsonDeserialize()
21+
@JsonInclude(JsonInclude.Include.NON_NULL)
22+
@JsonIgnoreProperties(ignoreUnknown = true)
23+
public class CxResultOutput {
24+
25+
private int totalCount;
26+
private List<CxResult> results;
27+
28+
@JsonCreator
29+
public CxResultOutput(@JsonProperty("totalCount") int totalCount, @JsonProperty("results") List<CxResult> results) {
30+
this.totalCount = totalCount;
31+
this.results = results;
32+
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
return EqualsBuilder.reflectionEquals(this, o);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return HashCodeBuilder.reflectionHashCode(this);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return ReflectionToStringBuilder.toString(this);
47+
}
48+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.checkmarx.ast.results;
2+
3+
public enum CxResultType {
4+
LIST, SUMMARY
5+
}

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

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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;
37
import com.fasterxml.jackson.core.JsonProcessingException;
48
import com.fasterxml.jackson.core.type.TypeReference;
59
import com.fasterxml.jackson.databind.ObjectMapper;
6-
import com.google.gson.Gson;
710
import org.apache.commons.lang3.StringUtils;
811
import org.slf4j.Logger;
912
import org.slf4j.LoggerFactory;
1013

1114
import java.io.*;
12-
import java.lang.reflect.Type;
1315
import java.net.URI;
1416
import java.net.URISyntaxException;
1517
import 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
}

src/main/java/com/checkmarx/ast/CxParamType.java renamed to src/main/java/com/checkmarx/ast/scans/CxParamType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.checkmarx.ast;
1+
package com.checkmarx.ast.scans;
22

33
public enum CxParamType {
44
S, V, G, PROJECT_NAME, SCAN_TYPES, SAST_PRESET_NAME, FILTER, DIRECTORY, ADDITIONAL_PARAMETERS, AGENT, SOURCES, BRANCH

src/main/java/com/checkmarx/ast/CxScan.java renamed to src/main/java/com/checkmarx/ast/scans/CxScan.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.checkmarx.ast;
1+
package com.checkmarx.ast.scans;
22

33
import com.fasterxml.jackson.annotation.JsonCreator;
44
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

0 commit comments

Comments
 (0)