Skip to content

Commit 52846da

Browse files
committed
Add custom states support
1 parent 7b6f2e8 commit 52846da

File tree

5 files changed

+116
-11
lines changed

5 files changed

+116
-11
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.checkmarx.ast.predicate;
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.JavaType;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
9+
import com.fasterxml.jackson.databind.type.TypeFactory;
10+
import lombok.Value;
11+
import org.apache.commons.lang3.StringUtils;
12+
13+
import java.io.IOException;
14+
import java.util.List;
15+
16+
@Value
17+
@JsonDeserialize()
18+
@JsonInclude(JsonInclude.Include.NON_NULL)
19+
@JsonIgnoreProperties(ignoreUnknown = true)
20+
public class CustomState {
21+
private Long id;
22+
private String name;
23+
private String type;
24+
25+
public CustomState(@JsonProperty("id") Long id,
26+
@JsonProperty("name") String name,
27+
@JsonProperty("type") String type) {
28+
this.id = id;
29+
this.name = name;
30+
this.type = type;
31+
}
32+
33+
public static <T> T fromLine(String line) {
34+
return parse(line, TypeFactory.defaultInstance().constructType(CustomState.class));
35+
}
36+
37+
public static <T> List<T> listFromLine(String line) {
38+
return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, CustomState.class));
39+
}
40+
41+
protected static <T> T parse(String line, JavaType type) {
42+
T result = null;
43+
try {
44+
if (!StringUtils.isBlank(line) && isValidJSON(line)) {
45+
result = new ObjectMapper().readValue(line, type);
46+
47+
}
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
return result;
52+
}
53+
54+
private static boolean isValidJSON(final String json) {
55+
try {
56+
final ObjectMapper mapper = new ObjectMapper();
57+
mapper.readTree(json);
58+
return true;
59+
} catch (IOException e) {
60+
return false;
61+
}
62+
}
63+
}

src/main/java/com/checkmarx/ast/predicate/Predicate.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.commons.lang3.StringUtils;
1313

1414
import java.io.IOException;
15+
import java.lang.reflect.Field;
1516
import java.util.List;
1617

1718
@Value
@@ -29,13 +30,14 @@ public class Predicate {
2930
String createdBy;
3031
String createdAt;
3132
String updatedAt;
33+
Long stateId;
3234

3335
@JsonCreator
3436
public Predicate(@JsonProperty("ID") String id, @JsonProperty("SimilarityID") String similarityId,
3537
@JsonProperty("ProjectID") String projectId, @JsonProperty("State") String state,
3638
@JsonProperty("Severity") String severity, @JsonProperty("Comment") String comment,
3739
@JsonProperty("CreatedBy") String createdBy, @JsonProperty("CreatedAt") String createdAt,
38-
@JsonProperty("UpdatedAt") String updatedAt) {
40+
@JsonProperty("UpdatedAt") String updatedAt, @JsonProperty("StateId") Long stateId) {
3941
this.id = id;
4042
this.similarityId = similarityId;
4143
this.projectId = projectId;
@@ -45,6 +47,7 @@ public Predicate(@JsonProperty("ID") String id, @JsonProperty("SimilarityID") St
4547
this.createdBy = createdBy;
4648
this.createdAt = createdAt;
4749
this.updatedAt = updatedAt;
50+
this.stateId = stateId;
4851
}
4952

5053
public static <T> T fromLine(String line) {
@@ -68,6 +71,22 @@ protected static <T> T parse(String line, JavaType type) {
6871
return result;
6972
}
7073

74+
public static boolean validator(List<String> arguments, Object parsedLine) {
75+
{
76+
for (Field field : parsedLine.getClass().getDeclaredFields()) {
77+
field.setAccessible(true);
78+
try {
79+
if (field.get(parsedLine) == null && field.getName().equals("stateId")) {
80+
return false;
81+
}
82+
} catch (IllegalAccessException e) {
83+
return false;
84+
}
85+
}
86+
return true;
87+
}
88+
}
89+
7190
private static boolean isValidJSON(final String json) {
7291
try {
7392
final ObjectMapper mapper = new ObjectMapper();

src/main/java/com/checkmarx/ast/wrapper/CxConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public final class CxConstants {
3232
static final String SUB_CMD_CANCEL = "cancel";
3333
static final String CMD_TRIAGE = "triage";
3434
static final String SUB_CMD_UPDATE = "update";
35+
static final String SUB_CMD_GET_STATES = "get-states";
3536
static final String CMD_RESULT = "results";
3637
static final String FORMAT = "--format";
3738
static final String SCAN_INFO_FORMAT = "--scan-info-format";

src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.checkmarx.ast.codebashing.CodeBashing;
55
import com.checkmarx.ast.kicsRealtimeResults.KicsRealtimeResults;
66
import com.checkmarx.ast.learnMore.LearnMore;
7+
import com.checkmarx.ast.predicate.CustomState;
78
import com.checkmarx.ast.predicate.Predicate;
89
import com.checkmarx.ast.project.Project;
910
import com.checkmarx.ast.remediation.KicsRemediation;
@@ -23,6 +24,7 @@
2324
import org.slf4j.LoggerFactory;
2425

2526
import java.io.IOException;
27+
import java.lang.reflect.Field;
2628
import java.nio.file.Files;
2729
import java.util.ArrayList;
2830
import java.util.List;
@@ -161,7 +163,17 @@ public List<Predicate> triageShow(@NonNull UUID projectId, String similarityId,
161163

162164
arguments.addAll(jsonArguments());
163165

164-
return Execution.executeCommand(withConfigArguments(arguments), logger, Predicate::listFromLine);
166+
return Execution.executeCommand(withConfigArguments(arguments), logger, Predicate::listFromLine, Predicate::validator);
167+
}
168+
169+
public List<Predicate> triageGetStates() throws IOException, InterruptedException, CxException {
170+
this.logger.info("Executing 'triage get-states' command using the CLI.");
171+
172+
List<String> arguments = new ArrayList<>();
173+
arguments.add(CxConstants.CMD_TRIAGE);
174+
arguments.add(CxConstants.SUB_CMD_SHOW);
175+
176+
return Execution.executeCommand(withConfigArguments(arguments), logger, CustomState::listFromLine);
165177
}
166178

167179
public void triageUpdate(@NonNull UUID projectId, String similarityId, String scanType, String state, String comment, String severity) throws IOException, InterruptedException, CxException {
@@ -232,7 +244,9 @@ public ScanResult ScanAsca(String fileSource, boolean ascaLatestVersion, String
232244

233245
appendAgentToArguments(agent, arguments);
234246

235-
return Execution.executeCommand(withConfigArguments(arguments), logger, ScanResult::fromLine);
247+
return Execution.executeCommand(withConfigArguments(arguments), logger, ScanResult::fromLine,
248+
(args, ignored) ->
249+
(args.size() >= 3 && args.get(1).equals(CxConstants.CMD_SCAN) && args.get(2).equals(CxConstants.SUB_CMD_ASCA)));
236250
}
237251

238252
private static void appendAgentToArguments(String agent, List<String> arguments) {

src/main/java/com/checkmarx/ast/wrapper/Execution.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.List;
1717
import java.util.Locale;
1818
import java.util.Objects;
19+
import java.util.function.BiFunction;
1920
import java.util.function.Function;
2021

2122
public final class Execution {
@@ -42,32 +43,39 @@ static <T> T executeCommand(List<String> arguments,
4243
Logger logger,
4344
Function<String, T> lineParser)
4445
throws IOException, InterruptedException, CxException {
46+
return executeCommand(arguments, logger, lineParser, null);
47+
}
48+
49+
static <T> T executeCommand(List<String> arguments,
50+
Logger logger,
51+
Function<String, T> lineParser,
52+
BiFunction<List<String>, T, Boolean> customValidator)
53+
throws IOException, InterruptedException, CxException {
4554
Process process = buildProcess(arguments);
4655
try (BufferedReader br = getReader(process)) {
4756
T executionResult = null;
4857
String line;
49-
StringBuilder stringBuilder = new StringBuilder();
58+
StringBuilder output = new StringBuilder();
5059
while ((line = br.readLine()) != null) {
5160
logger.info(line);
52-
stringBuilder.append(line).append(LINE_SEPARATOR);
61+
output.append(line).append(LINE_SEPARATOR);
5362
T parsedLine = lineParser.apply(line);
5463
if (parsedLine != null) {
55-
if (areAllFieldsNotNull(parsedLine) || isAscaRequest(arguments)) {
56-
executionResult = parsedLine;
64+
if (Objects.isNull(customValidator)) {
65+
executionResult = areAllFieldsNotNull(parsedLine) ? parsedLine : null;
66+
} else {
67+
executionResult = (areAllFieldsNotNull(parsedLine) || customValidator.apply(arguments, parsedLine)) ? parsedLine : null;
5768
}
5869
}
5970
}
6071
process.waitFor();
6172
if (process.exitValue() != 0) {
62-
throw new CxException(process.exitValue(), stringBuilder.toString());
73+
throw new CxException(process.exitValue(), output.toString());
6374
}
6475
return executionResult;
6576
}
6677
}
6778

68-
public static boolean isAscaRequest(List<String> arguments) {
69-
return (arguments.size() >= 3 && arguments.get(1).equals("scan") && arguments.get(2).equals("asca"));
70-
}
7179

7280
private static boolean areAllFieldsNotNull(Object obj) {
7381
for (Field field : obj.getClass().getDeclaredFields()) {

0 commit comments

Comments
 (0)