Skip to content

Commit 5f26bcd

Browse files
Feature/auth validate (#6)
* add auth validate command
1 parent 0dc4e57 commit 5f26bcd

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private CxScan transformToCxScanObject(String line) {
198198
return scanObject;
199199
}
200200

201-
public List<String> initialCommands() {
201+
public List<String> initialCommandsCommon() {
202202
List<String> commands = new ArrayList<String>();
203203
commands.add(executable.getPath());
204204
addAuthCredentials(commands);
@@ -216,12 +216,27 @@ public List<String> initialCommands() {
216216
commands.add(this.baseAuthUri);
217217
}
218218

219+
return commands;
220+
}
221+
222+
public List<String> initialCommands() {
223+
List<String> commands = initialCommandsCommon();
219224
commands.add("--format");
220225
commands.add("json");
221226

222227
return commands;
223228
}
224229

230+
public Integer cxAuthValidate() throws IOException, InterruptedException {
231+
log.info("Initialize auth validate command");
232+
List<String> commands = initialCommandsCommon();
233+
commands.add("auth");
234+
commands.add("validate");
235+
236+
ExecutionService executionService = new ExecutionService();
237+
return executionService.executeCommandSync(commands);
238+
}
239+
225240
public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
226241
log.info("Initialized scan list retrieval");
227242
List<String> commands = initialCommands();
@@ -241,6 +256,7 @@ public List<CxScan> cxAstScanList() throws IOException, InterruptedException {
241256
log.info("Retrieved scan list with size: " + list.size());
242257
else
243258
log.info("Not able to retrieve scan list");
259+
244260
return list;
245261
}
246262

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ public BufferedReader executeCommand(List<String> commands) throws IOException {
1616
BufferedReader br = new BufferedReader(isr);
1717
return br;
1818
}
19+
20+
public Integer executeCommandSync(List<String> commands) throws IOException, InterruptedException {
21+
ProcessBuilder lmBuilder = new ProcessBuilder(commands);
22+
lmBuilder.redirectErrorStream(true);
23+
final Process process = lmBuilder.start();
24+
25+
return process.waitFor();
26+
}
1927
}
Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,97 @@
11
package com.checkmarx.ast;
22

3+
import org.jetbrains.annotations.NotNull;
34
import org.junit.Before;
45
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.JUnit4;
58
import org.slf4j.Logger;
69
import org.slf4j.LoggerFactory;
710

811
import java.io.IOException;
912
import java.net.URISyntaxException;
10-
import java.util.ArrayList;
1113
import java.util.HashMap;
1214
import java.util.List;
1315
import java.util.Map;
1416

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

20+
@RunWith(JUnit4.class)
1721
public class CxAuthTest {
18-
CxAuth auth = null;
19-
private Logger log = LoggerFactory.getLogger(CxAuthTest.class.getName());
20-
CxScanConfig config = new CxScanConfig();
21-
List<CxScan> scanList = new ArrayList<>();
22-
Map<CxParamType, String> params = new HashMap<>();
23-
Map<String, String> environmentVariables = System.getenv();
22+
private static final Logger log = LoggerFactory.getLogger(CxAuthTest.class.getName());
23+
24+
private static final int VALID_RETURN_CODE = 0;
25+
private static final String FAILED = "failed";
26+
private static final String COMPLETED = "completed";
27+
28+
private CxAuth auth;
2429

2530
@Before
2631
public void init() throws IOException, URISyntaxException {
27-
if (environmentVariables.containsKey("CX_CLIENT_ID")) {
28-
config.setClientId(environmentVariables.get("CX_CLIENT_ID"));
29-
}
30-
if (environmentVariables.containsKey("CX_CLIENT_SECRET")) {
31-
config.setClientSecret(environmentVariables.get("CX_CLIENT_SECRET"));
32-
}
33-
if (environmentVariables.containsKey("CX_APIKEY")) {
34-
config.setApiKey(environmentVariables.get("CX_APIKEY"));
35-
}
36-
if (environmentVariables.containsKey("CX_BASE_URI")) {
37-
config.setBaseUri(environmentVariables.get("CX_BASE_URI"));
38-
}
39-
if (environmentVariables.containsKey("CX_BASE_AUTH_URI")) {
40-
config.setBaseAuthUri(environmentVariables.get("CX_BASE_AUTH_URI"));
41-
}
42-
if (environmentVariables.containsKey("CX_TENANT")) {
43-
config.setTenant(environmentVariables.get("CX_TENANT"));
44-
}
45-
if (environmentVariables.containsKey("PATH_TO_EXECUTABLE")) {
46-
config.setPathToExecutable(environmentVariables.get("PATH_TO_EXECUTABLE"));
47-
}
32+
log.info("Init test");
4833

34+
Map<String, String> environmentVariables = System.getenv();
35+
CxScanConfig config = new CxScanConfig();
36+
config.setClientId(environmentVariables.getOrDefault("CX_CLIENT_ID", null));
37+
config.setClientSecret(environmentVariables.getOrDefault("CX_CLIENT_SECRET", null));
38+
config.setApiKey(environmentVariables.getOrDefault("CX_APIKEY", null));
39+
config.setBaseUri(environmentVariables.getOrDefault("CX_BASE_URI", null));
40+
config.setBaseAuthUri(environmentVariables.getOrDefault("CX_BASE_AUTH_URI", null));
41+
config.setTenant(environmentVariables.getOrDefault("CX_TENANT", null));
42+
config.setPathToExecutable(environmentVariables.getOrDefault("PATH_TO_EXECUTABLE", null));
43+
44+
auth = new CxAuth(config, log);
45+
}
46+
47+
@NotNull
48+
private Map<CxParamType, String> createParams() {
49+
Map<CxParamType, String> params = new HashMap<>();
4950
params.put(CxParamType.PROJECT_NAME, "TestCaseWrapper");
5051
params.put(CxParamType.SCAN_TYPES, "sast");
5152
params.put(CxParamType.S, ".");
5253
params.put(CxParamType.FILTER, "*.java");
53-
auth = new CxAuth(config, log);
54+
55+
return params;
5456
}
5557

5658
@Test
59+
public void cxScanShow() throws InterruptedException, IOException {
60+
List<CxScan> scanList = auth.cxAstScanList();
61+
62+
assertTrue(scanList.get(0) instanceof CxScan);
63+
}
5764

58-
public void cxScanShow() throws InterruptedException, IOException, URISyntaxException {
59-
init();
60-
if (scanList == null || scanList.size() == 0) {
61-
cxAstScanList();
62-
}
63-
if (scanList.size() > 0) {
64-
assertTrue(scanList.get(0) instanceof CxScan);
65-
}
65+
@Test
66+
public void cxAstAuthValidate() throws IOException, InterruptedException {
67+
Integer validate = auth.cxAuthValidate();
6668

69+
assertEquals(VALID_RETURN_CODE, validate.intValue());
6770
}
6871

6972
@Test
70-
public void cxAstScanList() throws IOException, InterruptedException, URISyntaxException {
71-
init();
72-
scanList = auth.cxAstScanList();
73+
public void cxAstScanList() throws IOException, InterruptedException {
74+
List<CxScan> scanList = auth.cxAstScanList();
75+
7376
assertTrue(scanList.size() > 0);
7477
}
7578

7679
@Test
77-
public void cxScanCreationWrongPreset() throws InterruptedException, IOException, URISyntaxException {
78-
init();
80+
public void cxScanCreationWrongPreset() throws InterruptedException, IOException {
81+
Map<CxParamType, String> params = createParams();
7982
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default Jay");
83+
8084
CxScan scanResult = auth.cxScanCreate(params);
81-
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase("failed"));
8285

86+
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(FAILED));
8387
}
8488

8589
@Test
86-
public void cxScanCreationSuccess() throws InterruptedException, IOException, URISyntaxException {
87-
init();
90+
public void cxScanCreationSuccess() throws InterruptedException, IOException {
91+
Map<CxParamType, String> params = createParams();
8892
params.put(CxParamType.SAST_PRESET_NAME, "Checkmarx Default");
93+
8994
CxScan scanResult = auth.cxScanCreate(params);
90-
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase("completed"));
95+
assertTrue(auth.cxScanShow(scanResult.getID()).getStatus().equalsIgnoreCase(COMPLETED));
9196
}
9297
}

0 commit comments

Comments
 (0)