Skip to content

Commit

Permalink
Feature/change exit code (#8)
Browse files Browse the repository at this point in the history
* Added the exit code functionality to the returned object
  • Loading branch information
jay-nanduri authored Jul 26, 2021
1 parent a40e7de commit fdffab3
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ jobs:
env:
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
CX_BASE_URI : ${{ secrets.BASE_URI }}
CX_BASE_URI: ${{ secrets.BASE_URI }}
PATH_TO_EXECUTABLE: /tmp/cx-linux
run: mvn -B test --file pom.xml
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.checkmarx.ast</groupId>
<artifactId>ast-cli-java-wrapper</artifactId>
<version>1.0.6</version>
<version>1.0.7</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
67 changes: 45 additions & 22 deletions src/main/java/com/checkmarx/ast/CxAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

public class CxAuth {
private Logger log = LoggerFactory.getLogger(CxAuth.class.getName());
private String baseuri;
private String baseAuthUri;
private String tenant;
private String key;
private String secret;
private String apikey;
private URI executable = null;
private final String baseuri;
private final String baseAuthUri;
private final String tenant;
private final String key;
private final String secret;
private final String apikey;
private final URI executable;
private static final Gson gson = new Gson();

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

try {
fileURI = extract(zipFile, fileName);
log.info("Location of the jar file: " + fileURI);
log.info("Location of the jar file: {}",fileURI) ;
} finally {
zipFile.close();
}
Expand Down Expand Up @@ -156,34 +156,49 @@ private static void close(final Closeable stream) {
}
}

public CxScan cxScanShow(String id) throws IOException, InterruptedException {
log.info("Initialized scan retrieval for id: " + id);
public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedException {
log.info("Initialized scan retrieval for id: {}" , id);
List<String> commands = initialCommands();
commands.add("scan");
commands.add("show");
commands.add("--scan-id");
commands.add(id);
CxScan scanObject = runExecutionCommands(commands);
if (scanObject != null)
CxCommandOutput scanObject = runExecutionCommands(commands);
if (scanObject.getScanObjectList() != null && scanObject.getScanObjectList().size() == 1)
log.info("Scan retrieved");
else
log.info("Did not receive the scan");

return scanObject;
}

private CxScan runExecutionCommands(List<String> commands) throws IOException, InterruptedException {
private CxCommandOutput runExecutionCommands(List<String> commands) throws IOException, InterruptedException {
log.info("Process submitting to the executor");
ExecutionService exec = new ExecutionService();
BufferedReader br = exec.executeCommand(commands);
Process process = exec.executeCommand(commands);
String line;
CxScan scanObject = null;
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
CxCommandOutput cxCommandOutput = new CxCommandOutput();
while ((line = br.readLine()) != null) {
log.info(line);
if (isJSONValid(line, CxScan.class))
if (isJSONValid(line, CxScan.class)) {
scanObject = transformToCxScanObject(line);
List<CxScan> scanList = new ArrayList<>();
scanList.add(scanObject);
cxCommandOutput.setScanObjectList(scanList);
}
}
br.close();
if(!process.isAlive()) {
cxCommandOutput.setExitCode(process.exitValue());
log.info("Exit code from AST-CLI: {}", process.exitValue());
}
log.info("Process returned from the executor");
return scanObject;
process.destroy();
return cxCommandOutput;
}

private CxScan transformToCxScanObject(String line) {
Expand All @@ -199,7 +214,7 @@ private CxScan transformToCxScanObject(String line) {
}

public List<String> initialCommandsCommon() {
List<String> commands = new ArrayList<String>();
List<String> commands = new ArrayList<>();
commands.add(executable.getPath());
addAuthCredentials(commands);

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

public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
public CxCommandOutput cxAstScanList() throws IOException, InterruptedException {
log.info("Initialized scan list retrieval");
List<String> commands = initialCommands();
commands.add("scan");
commands.add("list");

ExecutionService exec = new ExecutionService();
BufferedReader br = exec.executeCommand(commands);
Process process = exec.executeCommand(commands);
String line;
List<CxScan> list = new ArrayList<>();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (isJSONValid(line, List.class) && !line.isEmpty())
list = transformToCxScanList(line);
}
br.close();
CxCommandOutput cxCommandOutput = new CxCommandOutput();
cxCommandOutput.setScanObjectList(list);
cxCommandOutput.setExitCode(process.exitValue());
if (list != null && !list.isEmpty())
log.info("Retrieved scan list with size: " + list.size());
log.info("Retrieved scan list with size: {}" , list.size());
else
log.info("Not able to retrieve scan list");

return list;
return cxCommandOutput;
}

public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException, InterruptedException {
public CxCommandOutput cxScanCreate(Map<CxParamType, String> params) throws IOException, InterruptedException {
log.info("Initialized scan creation");
List<String> commands = initialCommands();
commands.add("scan");
Expand Down Expand Up @@ -291,6 +312,8 @@ public CxScan cxScanCreate(Map<CxParamType, String> params) throws IOException,
return runExecutionCommands(commands);
}



private void addIndividualParams(List<String> commands, String value) {
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(value);
while (m.find())
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/checkmarx/ast/CxCommandOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.checkmarx.ast;


import lombok.*;


import java.util.List;

@Getter
@Setter
public class CxCommandOutput {
private int exitCode;
private List<CxScan> scanObjectList;

}
12 changes: 3 additions & 9 deletions src/main/java/com/checkmarx/ast/ExecutionService.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package com.checkmarx.ast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class ExecutionService {
public BufferedReader executeCommand(List<String> commands) throws IOException {
public Process executeCommand(List<String> commands) throws IOException {
ProcessBuilder lmBuilder = new ProcessBuilder(commands);
lmBuilder.redirectErrorStream(true);
final Process lmProcess = lmBuilder.start();
InputStream is = lmProcess.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
return br;
return lmBuilder.start();
}


public Integer executeCommandSync(List<String> commands) throws IOException, InterruptedException {
ProcessBuilder lmBuilder = new ProcessBuilder(commands);
lmBuilder.redirectErrorStream(true);
Expand Down
Binary file modified src/main/resources/cx-linux
Binary file not shown.
Binary file modified src/main/resources/cx-mac
Binary file not shown.
Binary file modified src/main/resources/cx.exe
Binary file not shown.
30 changes: 14 additions & 16 deletions src/test/java/com/checkmarx/ast/CxAuthTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

@RunWith(JUnit4.class)
public class CxAuthTest {
Expand Down Expand Up @@ -47,7 +46,7 @@ public void init() throws IOException, URISyntaxException {
@NotNull
private Map<CxParamType, String> createParams() {
Map<CxParamType, String> params = new HashMap<>();
params.put(CxParamType.PROJECT_NAME, "TestCaseWrapper");
params.put(CxParamType.PROJECT_NAME, "JavaWrapperTestCases");
params.put(CxParamType.SCAN_TYPES, "sast");
params.put(CxParamType.S, ".");
params.put(CxParamType.FILTER, "*.java");
Expand All @@ -57,50 +56,49 @@ private Map<CxParamType, String> createParams() {

@Test
public void cxScanShow() throws InterruptedException, IOException {
List<CxScan> scanList = auth.cxAstScanList();

assertTrue(scanList.get(0) instanceof CxScan);
CxCommandOutput scanList = auth.cxAstScanList(); //scan ID
assertNotNull(scanList.getScanObjectList().get(0));
}

@Test
public void cxAstAuthValidate() throws IOException, InterruptedException {
Integer validate = auth.cxAuthValidate();

assertEquals(VALID_RETURN_CODE, validate.intValue());
}

@Test
public void cxAstScanList() throws IOException, InterruptedException {
List<CxScan> scanList = auth.cxAstScanList();

assertTrue(scanList.size() > 0);
CxCommandOutput scanList = auth.cxAstScanList();
assertTrue(scanList.getScanObjectList().size() > 0);
}

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

CxScan scanResult = auth.cxScanCreate(params);
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(COMPLETED));
CxCommandOutput scanResult = auth.cxScanCreate(params);
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(COMPLETED));
}

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

CxScan scanResult = auth.cxScanCreate(params);
CxCommandOutput scanResult = auth.cxScanCreate(params);

assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(FAILED));
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(FAILED));
}


@Test
public void cxScanCreationSuccess() throws InterruptedException, IOException {
Map<CxParamType, String> params = createParams();
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
//params.put(CxParamType.ADDITIONAL_PARAMETERS,"--nowait");

CxScan scanResult = auth.cxScanCreate(params);
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(COMPLETED));
CxCommandOutput scanResult = auth.cxScanCreate(params);
assertTrue(auth.cxScanShow(scanResult.getScanObjectList().get(0).getID()).getScanObjectList().get(0).getStatus().equalsIgnoreCase(COMPLETED));
}
}

0 comments on commit fdffab3

Please sign in to comment.