Skip to content

Commit fdffab3

Browse files
authored
Feature/change exit code (#8)
* Added the exit code functionality to the returned object
1 parent a40e7de commit fdffab3

File tree

9 files changed

+79
-49
lines changed

9 files changed

+79
-49
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ jobs:
3030
env:
3131
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
3232
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
33-
CX_BASE_URI : ${{ secrets.BASE_URI }}
33+
CX_BASE_URI: ${{ secrets.BASE_URI }}
3434
PATH_TO_EXECUTABLE: /tmp/cx-linux
3535
run: mvn -B test --file pom.xml

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.6</version>
7+
<version>1.0.7</version>
88
<packaging>jar</packaging>
99

1010
<dependencies>

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

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525

2626
public class CxAuth {
2727
private Logger log = LoggerFactory.getLogger(CxAuth.class.getName());
28-
private String baseuri;
29-
private String baseAuthUri;
30-
private String tenant;
31-
private String key;
32-
private String secret;
33-
private String apikey;
34-
private URI executable = null;
28+
private final String baseuri;
29+
private final String baseAuthUri;
30+
private final String tenant;
31+
private final String key;
32+
private final String secret;
33+
private final String apikey;
34+
private final URI executable;
3535
private static final Gson gson = new Gson();
3636

3737
public CxAuth(CxScanConfig scanConfig, Logger log)
@@ -101,7 +101,7 @@ private URI getFile(URI jarLocation, final String fileName) throws IOException {
101101

102102
try {
103103
fileURI = extract(zipFile, fileName);
104-
log.info("Location of the jar file: " + fileURI);
104+
log.info("Location of the jar file: {}",fileURI) ;
105105
} finally {
106106
zipFile.close();
107107
}
@@ -156,34 +156,49 @@ private static void close(final Closeable stream) {
156156
}
157157
}
158158

159-
public CxScan cxScanShow(String id) throws IOException, InterruptedException {
160-
log.info("Initialized scan retrieval for id: " + id);
159+
public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedException {
160+
log.info("Initialized scan retrieval for id: {}" , id);
161161
List<String> commands = initialCommands();
162162
commands.add("scan");
163163
commands.add("show");
164164
commands.add("--scan-id");
165165
commands.add(id);
166-
CxScan scanObject = runExecutionCommands(commands);
167-
if (scanObject != null)
166+
CxCommandOutput scanObject = runExecutionCommands(commands);
167+
if (scanObject.getScanObjectList() != null && scanObject.getScanObjectList().size() == 1)
168168
log.info("Scan retrieved");
169169
else
170170
log.info("Did not receive the scan");
171+
171172
return scanObject;
172173
}
173174

174-
private CxScan runExecutionCommands(List<String> commands) throws IOException, InterruptedException {
175+
private CxCommandOutput runExecutionCommands(List<String> commands) throws IOException, InterruptedException {
175176
log.info("Process submitting to the executor");
176177
ExecutionService exec = new ExecutionService();
177-
BufferedReader br = exec.executeCommand(commands);
178+
Process process = exec.executeCommand(commands);
178179
String line;
179180
CxScan scanObject = null;
181+
InputStream is = process.getInputStream();
182+
InputStreamReader isr = new InputStreamReader(is);
183+
BufferedReader br = new BufferedReader(isr);
184+
CxCommandOutput cxCommandOutput = new CxCommandOutput();
180185
while ((line = br.readLine()) != null) {
181186
log.info(line);
182-
if (isJSONValid(line, CxScan.class))
187+
if (isJSONValid(line, CxScan.class)) {
183188
scanObject = transformToCxScanObject(line);
189+
List<CxScan> scanList = new ArrayList<>();
190+
scanList.add(scanObject);
191+
cxCommandOutput.setScanObjectList(scanList);
192+
}
193+
}
194+
br.close();
195+
if(!process.isAlive()) {
196+
cxCommandOutput.setExitCode(process.exitValue());
197+
log.info("Exit code from AST-CLI: {}", process.exitValue());
184198
}
185199
log.info("Process returned from the executor");
186-
return scanObject;
200+
process.destroy();
201+
return cxCommandOutput;
187202
}
188203

189204
private CxScan transformToCxScanObject(String line) {
@@ -199,7 +214,7 @@ private CxScan transformToCxScanObject(String line) {
199214
}
200215

201216
public List<String> initialCommandsCommon() {
202-
List<String> commands = new ArrayList<String>();
217+
List<String> commands = new ArrayList<>();
203218
commands.add(executable.getPath());
204219
addAuthCredentials(commands);
205220

@@ -237,30 +252,36 @@ public Integer cxAuthValidate() throws IOException, InterruptedException {
237252
return executionService.executeCommandSync(commands);
238253
}
239254

240-
public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
255+
public CxCommandOutput cxAstScanList() throws IOException, InterruptedException {
241256
log.info("Initialized scan list retrieval");
242257
List<String> commands = initialCommands();
243258
commands.add("scan");
244259
commands.add("list");
245260

246261
ExecutionService exec = new ExecutionService();
247-
BufferedReader br = exec.executeCommand(commands);
262+
Process process = exec.executeCommand(commands);
248263
String line;
249264
List<CxScan> list = new ArrayList<>();
265+
InputStream is = process.getInputStream();
266+
InputStreamReader isr = new InputStreamReader(is);
267+
BufferedReader br = new BufferedReader(isr);
250268
while ((line = br.readLine()) != null) {
251269
if (isJSONValid(line, List.class) && !line.isEmpty())
252270
list = transformToCxScanList(line);
253271
}
254272
br.close();
273+
CxCommandOutput cxCommandOutput = new CxCommandOutput();
274+
cxCommandOutput.setScanObjectList(list);
275+
cxCommandOutput.setExitCode(process.exitValue());
255276
if (list != null && !list.isEmpty())
256-
log.info("Retrieved scan list with size: " + list.size());
277+
log.info("Retrieved scan list with size: {}" , list.size());
257278
else
258279
log.info("Not able to retrieve scan list");
259280

260-
return list;
281+
return cxCommandOutput;
261282
}
262283

263-
public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException, InterruptedException {
284+
public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOException, InterruptedException {
264285
log.info("Initialized scan creation");
265286
List<String> commands = initialCommands();
266287
commands.add("scan");
@@ -291,6 +312,8 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
291312
return runExecutionCommands(commands);
292313
}
293314

315+
316+
294317
private void addIndividualParams(List<String> commands, String value) {
295318
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(value);
296319
while (m.find())
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.checkmarx.ast;
2+
3+
4+
import lombok.*;
5+
6+
7+
import java.util.List;
8+
9+
@Getter
10+
@Setter
11+
public class CxCommandOutput {
12+
private int exitCode;
13+
private List<CxScan> scanObjectList;
14+
15+
}

src/main/java/com/checkmarx/ast/ExecutionService.java

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

3-
import java.io.BufferedReader;
43
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
74
import java.util.List;
85

96
public class ExecutionService {
10-
public BufferedReader executeCommand(List<String> commands) throws IOException {
7+
public Process executeCommand(List<String> commands) throws IOException {
118
ProcessBuilder lmBuilder = new ProcessBuilder(commands);
129
lmBuilder.redirectErrorStream(true);
13-
final Process lmProcess = lmBuilder.start();
14-
InputStream is = lmProcess.getInputStream();
15-
InputStreamReader isr = new InputStreamReader(is);
16-
BufferedReader br = new BufferedReader(isr);
17-
return br;
10+
return lmBuilder.start();
1811
}
1912

13+
2014
public Integer executeCommandSync(List<String> commands) throws IOException, InterruptedException {
2115
ProcessBuilder lmBuilder = new ProcessBuilder(commands);
2216
lmBuilder.redirectErrorStream(true);

src/main/resources/cx-linux

-5.66 MB
Binary file not shown.

src/main/resources/cx-mac

-4.1 MB
Binary file not shown.

src/main/resources/cx.exe

-5.49 MB
Binary file not shown.

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.Assert.*;
1918

2019
@RunWith(JUnit4.class)
2120
public class CxAuthTest {
@@ -47,7 +46,7 @@ public void init() throws IOException, URISyntaxException {
4746
@NotNull
4847
private Map<CxParamType, String> createParams() {
4948
Map<CxParamType, String> params = new HashMap<>();
50-
params.put(CxParamType.PROJECT_NAME, "TestCaseWrapper");
49+
params.put(CxParamType.PROJECT_NAME, "JavaWrapperTestCases");
5150
params.put(CxParamType.SCAN_TYPES, "sast");
5251
params.put(CxParamType.S, ".");
5352
params.put(CxParamType.FILTER, "*.java");
@@ -57,50 +56,49 @@ private Map<CxParamType, String> createParams() {
5756

5857
@Test
5958
public void cxScanShow() throws InterruptedException, IOException {
60-
List<CxScan> scanList = auth.cxAstScanList();
61-
62-
assertTrue(scanList.get(0) instanceof CxScan);
59+
CxCommandOutput scanList = auth.cxAstScanList(); //scan ID
60+
assertNotNull(scanList.getScanObjectList().get(0));
6361
}
6462

6563
@Test
6664
public void cxAstAuthValidate() throws IOException, InterruptedException {
6765
Integer validate = auth.cxAuthValidate();
68-
6966
assertEquals(VALID_RETURN_CODE, validate.intValue());
7067
}
7168

7269
@Test
7370
public void cxAstScanList() throws IOException, InterruptedException {
74-
List<CxScan> scanList = auth.cxAstScanList();
75-
76-
assertTrue(scanList.size() > 0);
71+
CxCommandOutput scanList = auth.cxAstScanList();
72+
assertTrue(scanList.getScanObjectList().size() > 0);
7773
}
7874

7975
@Test
8076
public void cxScanCreationWithBranchName() throws InterruptedException, IOException {
8177
Map<CxParamType, String> params = createParams();
8278
params.put(CxParamType.BRANCH, "test");
8379

84-
CxScan scanResult = auth.cxScanCreate(params);
85-
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(COMPLETED));
80+
CxCommandOutput scanResult = auth.cxScanCreate(params);
81+
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(COMPLETED));
8682
}
8783

8884
@Test
8985
public void cxScanCreationWrongPreset() throws InterruptedException, IOException {
9086
Map<CxParamType, String> params = createParams();
9187
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default Jay");
9288

93-
CxScan scanResult = auth.cxScanCreate(params);
89+
CxCommandOutput scanResult = auth.cxScanCreate(params);
9490

95-
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(FAILED));
91+
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(FAILED));
9692
}
9793

94+
9895
@Test
9996
public void cxScanCreationSuccess() throws InterruptedException, IOException {
10097
Map<CxParamType, String> params = createParams();
10198
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
99+
//params.put(CxParamType.ADDITIONAL_PARAMETERS,"--nowait");
102100

103-
CxScan scanResult = auth.cxScanCreate(params);
104-
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(COMPLETED));
101+
CxCommandOutput scanResult = auth.cxScanCreate(params);
102+
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(COMPLETED));
105103
}
106104
}

0 commit comments

Comments
 (0)