Skip to content

Commit 8d9d672

Browse files
author
Mithilesh Pawar
authored
Added support for getting BFL using 'results bfl' command (#89)
* Added support for getting BFL using 'results bfl' command Added unit tests * Returning an index for the BFlnode found in the results. * Fixed compilation error after resolving the conflicts.
1 parent b3ae170 commit 8d9d672

File tree

6 files changed

+138
-18
lines changed

6 files changed

+138
-18
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: AST Java Wrapper CI
22

3-
on: [pull_request]
3+
on: [ pull_request ]
44

55
jobs:
66
integration-tests:
@@ -40,8 +40,7 @@ jobs:
4040
- name: Checkmarx AST CLI Action
4141
uses: checkmarxDev/ast-github-action@main
4242
with:
43-
project_name: ast-cli-java-wrapper
44-
branch: master
43+
project_name: ${{ github.repository }}
4544
base_uri: ${{ secrets.CX_BASE_URI }}
4645
cx_tenant: ${{ secrets.CX_TENANT }}
4746
cx_client_id: ${{ secrets.CX_CLIENT_ID }}

src/main/java/com/checkmarx/ast/results/result/Node.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
44
import com.fasterxml.jackson.annotation.JsonInclude;
55
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.JavaType;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
68
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
9+
import com.fasterxml.jackson.databind.type.TypeFactory;
710
import lombok.Value;
11+
import org.apache.commons.lang3.StringUtils;
12+
13+
import java.io.IOException;
14+
import java.util.List;
815

916
@Value
1017
@JsonDeserialize()
@@ -53,4 +60,57 @@ public Node(@JsonProperty("id") String id,
5360
this.methodLine = methodLine;
5461
this.definitions = definitions;
5562
}
63+
64+
public static <T> T fromLine(String line) {
65+
return parse(line, TypeFactory.defaultInstance().constructType(Node.class));
66+
}
67+
68+
public static <T> List<T> listFromLine(String line) {
69+
return parse(line, TypeFactory.defaultInstance().constructCollectionType(List.class, Node.class));
70+
}
71+
72+
protected static <T> T parse(String line, JavaType type) {
73+
T result = null;
74+
try {
75+
if (!StringUtils.isBlank(line) && isValidJSON(line)) {
76+
result = new ObjectMapper().readValue(line, type);
77+
78+
}
79+
} catch (IOException e) {
80+
e.printStackTrace();
81+
}
82+
return result;
83+
}
84+
85+
private static boolean isValidJSON(final String json) {
86+
try {
87+
final ObjectMapper mapper = new ObjectMapper();
88+
mapper.readTree(json);
89+
return true;
90+
} catch (IOException e) {
91+
return false;
92+
}
93+
}
94+
95+
@Override
96+
public boolean equals(Object obj) {
97+
if (this == obj) {
98+
return true;
99+
}
100+
if (obj == null || getClass() != obj.getClass()) {
101+
return false;
102+
}
103+
Node node = (Node) obj;
104+
return line == node.line &&
105+
column == node.column &&
106+
length == node.length &&
107+
name.equals(node.name) &&
108+
method.equals(node.method) &&
109+
domType.equals(node.domType) &&
110+
fileName.equals(node.fileName) &&
111+
fullName.equals(node.fullName) &&
112+
methodLine.equals(node.methodLine);
113+
114+
}
115+
56116
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public final class CxConstants {
2424
static final String SUB_CMD_BRANCHES = "branches";
2525
static final String CMD_SCAN = "scan";
2626
static final String SUB_CMD_SHOW = "show";
27+
static final String RESULTS_BFL_SUB_CMD = "bfl";
2728
static final String SUB_CMD_LIST = "list";
2829
static final String SUB_CMD_CREATE = "create";
2930
static final String CMD_TRIAGE = "triage";
@@ -36,6 +37,7 @@ public final class CxConstants {
3637
static final String SCAN_ID = "--scan-id";
3738
static final String PROJECT_ID = "--project-id";
3839
static final String SIMILARITY_ID = "--similarity-id";
40+
static final String QUERY_ID = "--query-id";
3941
static final String STATE = "--state";
4042
static final String COMMENT = "--comment";
4143
static final String SEVERITY = "--severity";

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.checkmarx.ast.results.ReportFormat;
77
import com.checkmarx.ast.results.Results;
88
import com.checkmarx.ast.results.ResultsSummary;
9+
import com.checkmarx.ast.results.result.Node;
910
import com.checkmarx.ast.scan.Scan;
1011
import com.fasterxml.jackson.databind.ObjectMapper;
1112
import com.fasterxml.jackson.databind.type.CollectionType;
@@ -255,6 +256,38 @@ public String results(@NonNull UUID scanId, ReportFormat reportFormat)
255256
fileName + reportFormat.getExtension());
256257
}
257258

259+
public int getResultsBfl(@NonNull UUID scanId, @NonNull String queryId, List<Node> resultNodes)
260+
throws IOException, InterruptedException, CxException {
261+
this.logger.info("Executing 'results bfl' command using the CLI.");
262+
this.logger.info("Fetching the best fix location for ScanId {} and QueryId {}", scanId, queryId);
263+
264+
List<String> arguments = new ArrayList<>();
265+
arguments.add(CxConstants.CMD_RESULT);
266+
arguments.add(CxConstants.RESULTS_BFL_SUB_CMD);
267+
arguments.add(CxConstants.SCAN_ID);
268+
arguments.add(scanId.toString());
269+
arguments.add(CxConstants.QUERY_ID);
270+
arguments.add(queryId);
271+
arguments.addAll(jsonArguments());
272+
273+
List<Node> bflNodes = Execution.executeCommand(withConfigArguments(arguments), logger, Node::listFromLine);
274+
return getIndexOfBfLNode(bflNodes, resultNodes);
275+
276+
}
277+
278+
private int getIndexOfBfLNode(List<Node> bflNodes, List<Node> resultNodes) {
279+
280+
int bflNodeIndex = -1;
281+
for (Node bflNode : bflNodes) {
282+
for (Node resultNode : resultNodes) {
283+
if (bflNode.equals(resultNode)) {
284+
bflNodeIndex = resultNodes.indexOf(resultNode);
285+
}
286+
}
287+
}
288+
return bflNodeIndex;
289+
}
290+
258291
private List<String> withConfigArguments(List<String> commands) {
259292
List<String> arguments = new ArrayList<>();
260293

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@
1212

1313
public abstract class BaseTest {
1414

15-
protected CxWrapper wrapper;
16-
private String projectId;
17-
18-
@BeforeEach
19-
public void init() throws Exception {
20-
wrapper = new CxWrapper(getConfig(), getLogger());
21-
}
22-
2315
public static final String CX_SCAN_ID = getEnvOrNull("CX_SCAN_ID");
24-
2516
private static final String CX_BASE_URI = getEnvOrNull("CX_BASE_URI");
2617
private static final String CX_BASE_AUTH_URI = getEnvOrNull("CX_BASE_AUTH_URI");
2718
private static final String CX_TENANT = getEnvOrNull("CX_TENANT");
@@ -31,10 +22,8 @@ public void init() throws Exception {
3122
private static final String CX_ADDITIONAL_PARAMETERS = getEnvOrNull("CX_ADDITIONAL_PARAMETERS");
3223
private static final String PATH_TO_EXECUTABLE = getEnvOrNull("PATH_TO_EXECUTABLE");
3324
private final Logger logger = LoggerFactory.getLogger(this.getClass());
34-
35-
protected Logger getLogger() {
36-
return logger;
37-
}
25+
protected CxWrapper wrapper;
26+
private String projectId;
3827

3928
protected static CxConfig getConfig() {
4029
return CxConfig.builder()
@@ -53,9 +42,18 @@ private static String getEnvOrNull(String key) {
5342
return System.getenv().getOrDefault(key, null);
5443
}
5544

45+
@BeforeEach
46+
public void init() throws Exception {
47+
wrapper = new CxWrapper(getConfig(), getLogger());
48+
}
49+
50+
protected Logger getLogger() {
51+
return logger;
52+
}
53+
5654
protected Map<String, String> commonParams() {
5755
Map<String, String> params = new HashMap<>();
58-
params.put(CxConstants.PROJECT_NAME, "CLI-Java-Wrapper-Tests");
56+
params.put(CxConstants.PROJECT_NAME, "cli-java-wrapper-tests");
5957
params.put(CxConstants.SOURCE, ".");
6058
params.put(CxConstants.FILE_FILTER, "!test");
6159
params.put(CxConstants.BRANCH, "main");

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
import com.checkmarx.ast.results.ReportFormat;
55
import com.checkmarx.ast.results.Results;
66
import com.checkmarx.ast.results.ResultsSummary;
7+
import com.checkmarx.ast.results.result.Data;
8+
import com.checkmarx.ast.results.result.Node;
9+
import com.checkmarx.ast.results.result.Result;
710
import com.checkmarx.ast.scan.Scan;
11+
import com.checkmarx.ast.wrapper.CxConstants;
812
import org.junit.jupiter.api.Assertions;
913
import org.junit.jupiter.api.Test;
1014

15+
import java.util.ArrayList;
1116
import java.util.List;
1217
import java.util.UUID;
1318

1419
class ResultTest extends BaseTest {
1520
private static String CWE_ID = "79";
1621
private static String LANGUAGE = "PHP";
1722
private static String QUERY_NAME = "Reflected XSS All Clients";
23+
1824
@Test
1925
void testResultsHTML() throws Exception {
2026
List<Scan> scanList = wrapper.scanList();
@@ -54,9 +60,31 @@ void testResultsStructure() throws Exception {
5460

5561
@Test()
5662
void testResultsCodeBashing() throws Exception {
57-
List<CodeBashing> codeBashingList = wrapper.codeBashingList(CWE_ID,LANGUAGE,QUERY_NAME);
63+
List<CodeBashing> codeBashingList = wrapper.codeBashingList(CWE_ID, LANGUAGE, QUERY_NAME);
5864
Assertions.assertTrue(codeBashingList.size() > 0);
5965
String path = codeBashingList.get(0).getPath();
6066
Assertions.assertTrue(path.length() > 0);
6167
}
68+
69+
@Test
70+
void testResultsBflJSON() throws Exception {
71+
72+
UUID scanId = UUID.fromString(CX_SCAN_ID);
73+
Results results = wrapper.results(scanId);
74+
Result result = results.getResults().stream().filter(res -> res.getType().equalsIgnoreCase(CxConstants.SAST)).findFirst().get();
75+
Data data = result.getData();
76+
String queryId = data.getQueryId();
77+
int bflNodeIndex = wrapper.getResultsBfl(scanId, queryId, data.getNodes());
78+
Assertions.assertTrue(bflNodeIndex == -1 || bflNodeIndex >= 0);
79+
80+
}
81+
82+
@Test
83+
void testResultsBflWithInvalidQueryId() throws Exception {
84+
85+
UUID scanId = UUID.fromString(CX_SCAN_ID);
86+
String queryId = "0000";
87+
int bflNodeIndex = wrapper.getResultsBfl(scanId, queryId, new ArrayList<Node>());
88+
Assertions.assertEquals(-1, bflNodeIndex);
89+
}
6290
}

0 commit comments

Comments
 (0)