Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/checkmarx/ast/predicate/CustomState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.checkmarx.ast.predicate;

import com.checkmarx.ast.utils.JsonParser;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.Value;

import java.util.List;

@Value
@JsonDeserialize()
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomState {
Integer id;
String name;
String type;

public CustomState(@JsonProperty("id") Integer id,
@JsonProperty("name") String name,
@JsonProperty("type") String type) {
this.id = id;
this.name = name;
this.type = type;
}

public static <T> T fromLine(String line) {
return JsonParser.parse(line, TypeFactory.defaultInstance().constructType(CustomState.class));
}

public static <T> List<T> listFromLine(String line) {
return JsonParser.parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, CustomState.class));
}
}
21 changes: 20 additions & 1 deletion src/main/java/com/checkmarx/ast/predicate/Predicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;

@Value
Expand All @@ -29,13 +30,14 @@ public class Predicate {
String createdBy;
String createdAt;
String updatedAt;
String stateId;

@JsonCreator
public Predicate(@JsonProperty("ID") String id, @JsonProperty("SimilarityID") String similarityId,
@JsonProperty("ProjectID") String projectId, @JsonProperty("State") String state,
@JsonProperty("Severity") String severity, @JsonProperty("Comment") String comment,
@JsonProperty("CreatedBy") String createdBy, @JsonProperty("CreatedAt") String createdAt,
@JsonProperty("UpdatedAt") String updatedAt) {
@JsonProperty("UpdatedAt") String updatedAt, @JsonProperty("StateId") String stateId) {
this.id = id;
this.similarityId = similarityId;
this.projectId = projectId;
Expand All @@ -45,6 +47,7 @@ public Predicate(@JsonProperty("ID") String id, @JsonProperty("SimilarityID") St
this.createdBy = createdBy;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
this.stateId = stateId;
}

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

public static boolean validator(List<String> arguments, Object parsedLine) {
{
for (Field field : parsedLine.getClass().getDeclaredFields()) {
field.setAccessible(true);
try {
if (field.get(parsedLine) == null && !field.getName().equals("stateId")) {
return false;
}
} catch (IllegalAccessException e) {
return false;
}
}
return true;
}
}

private static boolean isValidJSON(final String json) {
try {
final ObjectMapper mapper = new ObjectMapper();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/checkmarx/ast/wrapper/CxConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public final class CxConstants {
static final String SUB_CMD_CANCEL = "cancel";
static final String CMD_TRIAGE = "triage";
static final String SUB_CMD_UPDATE = "update";
static final String SUB_CMD_GET_STATES = "get-states";
static final String ALL_STATES_FLAG = "--all";
static final String CMD_RESULT = "results";
static final String FORMAT = "--format";
static final String SCAN_INFO_FORMAT = "--scan-info-format";
Expand All @@ -44,6 +46,7 @@ public final class CxConstants {
static final String STATE = "--state";
static final String COMMENT = "--comment";
static final String SEVERITY = "--severity";
static final String CUSTOM_STATE_ID = "--custom-state-id";
static final String REPORT_FORMAT = "--report-format";
static final String OUTPUT_NAME = "--output-name";
static final String OUTPUT_PATH = "--output-path";
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/com/checkmarx/ast/wrapper/CxWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.checkmarx.ast.codebashing.CodeBashing;
import com.checkmarx.ast.kicsRealtimeResults.KicsRealtimeResults;
import com.checkmarx.ast.learnMore.LearnMore;
import com.checkmarx.ast.predicate.CustomState;
import com.checkmarx.ast.predicate.Predicate;
import com.checkmarx.ast.project.Project;
import com.checkmarx.ast.remediation.KicsRemediation;
Expand All @@ -23,6 +24,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -161,12 +163,32 @@ public List<Predicate> triageShow(@NonNull UUID projectId, String similarityId,

arguments.addAll(jsonArguments());

return Execution.executeCommand(withConfigArguments(arguments), logger, Predicate::listFromLine);
return Execution.executeCommand(withConfigArguments(arguments), logger, Predicate::listFromLine, Predicate::validator);
}

public List<Predicate> triageGetStates(boolean all) throws IOException, InterruptedException, CxException {
this.logger.info("Executing 'triage get-states' command using the CLI.");

List<String> arguments = new ArrayList<>();
arguments.add(CxConstants.CMD_TRIAGE);
arguments.add(CxConstants.SUB_CMD_GET_STATES);
if (all) {
arguments.add(CxConstants.ALL_STATES_FLAG);
}

return Execution.executeCommand(withConfigArguments(arguments), logger, CustomState::listFromLine);
}

public void triageUpdate(@NonNull UUID projectId, String similarityId, String scanType, String state, String comment, String severity) throws IOException, InterruptedException, CxException {
triageUpdate(projectId, similarityId, scanType, state, comment, severity, null);
}

public void triageUpdate(@NonNull UUID projectId, String similarityId, String scanType, String state, String comment, String severity, String customStateId) throws IOException, InterruptedException, CxException {
this.logger.info("Executing 'triage update' command using the CLI.");
this.logger.info("Updating the similarityId {} with state {} and severity {}.", similarityId, state, severity);
this.logger.info("Updating the similarityId {} with state {} with customStateId {} and severity {}.", similarityId, state, customStateId, severity);

boolean emptyState = state == null || state.isEmpty();
boolean emptyCustomStateId = customStateId == null || customStateId.isEmpty();

List<String> arguments = new ArrayList<>();
arguments.add(CxConstants.CMD_TRIAGE);
Expand All @@ -179,6 +201,10 @@ public void triageUpdate(@NonNull UUID projectId, String similarityId, String sc
arguments.add(scanType);
arguments.add(CxConstants.STATE);
arguments.add(state);
if (!emptyCustomStateId) {
arguments.add(CxConstants.CUSTOM_STATE_ID);
arguments.add(customStateId);
}
if (!StringUtils.isBlank(comment)) {
arguments.add(CxConstants.COMMENT);
arguments.add(comment);
Expand Down Expand Up @@ -232,7 +258,9 @@ public ScanResult ScanAsca(String fileSource, boolean ascaLatestVersion, String

appendAgentToArguments(agent, arguments);

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

private static void appendAgentToArguments(String agent, List<String> arguments) {
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/checkmarx/ast/wrapper/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;

public final class Execution {
Expand All @@ -42,34 +43,37 @@ static <T> T executeCommand(List<String> arguments,
Logger logger,
Function<String, T> lineParser)
throws IOException, InterruptedException, CxException {
return executeCommand(arguments, logger, lineParser, Execution::areAllFieldsNotNull);
}

static <T> T executeCommand(List<String> arguments,
Logger logger,
Function<String, T> lineParser,
BiFunction<List<String>, T, Boolean> customValidator)
throws IOException, InterruptedException, CxException {
Process process = buildProcess(arguments);
try (BufferedReader br = getReader(process)) {
T executionResult = null;
String line;
StringBuilder stringBuilder = new StringBuilder();
StringBuilder output = new StringBuilder();
while ((line = br.readLine()) != null) {
logger.info(line);
stringBuilder.append(line).append(LINE_SEPARATOR);
output.append(line).append(LINE_SEPARATOR);
T parsedLine = lineParser.apply(line);
if (parsedLine != null) {
if (areAllFieldsNotNull(parsedLine) || isAscaRequest(arguments)) {
executionResult = parsedLine;
}
executionResult = customValidator.apply(arguments, parsedLine) ? parsedLine : null;
}
}
process.waitFor();
if (process.exitValue() != 0) {
throw new CxException(process.exitValue(), stringBuilder.toString());
throw new CxException(process.exitValue(), output.toString());
}
return executionResult;
}
}

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

private static boolean areAllFieldsNotNull(Object obj) {
private static boolean areAllFieldsNotNull(List<String> arguments, Object obj) {
for (Field field : obj.getClass().getDeclaredFields()) {
field.setAccessible(true);
try {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/checkmarx/ast/PredicateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.checkmarx.ast.results.result.Result;
import com.checkmarx.ast.scan.Scan;
import com.checkmarx.ast.wrapper.CxConstants;
import org.junit.Ignore;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import java.util.List;
Expand Down Expand Up @@ -44,4 +46,11 @@ void testTriage() throws Exception {
Assertions.fail("Triage update failed. Should not throw exception");
}
}

@Test
@Disabled("Ignore this tests until get states api will be in production")
void testGetStates() throws Exception {
List<Predicate> states = wrapper.triageGetStates(false);
Assertions.assertNotNull(states);
}
}