Skip to content

Commit f132a73

Browse files
Feature/results update (#14)
* update version * update cli to 2.0.0-rc.22 * update tests to use galactica tenant
1 parent 8e1f081 commit f132a73

File tree

7 files changed

+55
-34
lines changed

7 files changed

+55
-34
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ jobs:
3131
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
3232
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
3333
CX_BASE_URI: ${{ secrets.BASE_URI }}
34+
CX_TENANT: ${{ secrets.TENANT }}
3435
PATH_TO_EXECUTABLE: /tmp/cx-linux
3536
run: mvn -B test --file pom.xml

.github/workflows/maven-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ jobs:
3434
CX_CLIENT_ID: ${{ secrets.CLIENT_ID}}
3535
CX_CLIENT_SECRET: ${{ secrets.CLIENT_SECRET}}
3636
CX_BASE_URI : ${{ secrets.BASE_URI }}
37+
CX_TENANT: ${{ secrets.TENANT }}
3738
PATH_TO_EXECUTABLE: /tmp/cx-linux
3839
GITHUB_TOKEN: ${{ github.token }}

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

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@
88
import com.fasterxml.jackson.core.type.TypeReference;
99
import com.fasterxml.jackson.databind.ObjectMapper;
1010
import org.apache.commons.lang3.StringUtils;
11+
import org.apache.commons.lang3.reflect.FieldUtils;
1112
import org.slf4j.Logger;
1213
import org.slf4j.LoggerFactory;
1314

1415
import java.io.*;
1516
import java.net.URI;
1617
import java.net.URISyntaxException;
1718
import java.net.URL;
19+
import java.nio.charset.StandardCharsets;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
22+
import java.nio.file.Paths;
1823
import java.security.CodeSource;
1924
import java.security.ProtectionDomain;
2025
import java.util.ArrayList;
@@ -185,43 +190,57 @@ public CxCommandOutput cxScanShow(String id) throws IOException, InterruptedExce
185190
return scanObject;
186191
}
187192

188-
public String cxGetResultsSummary(String scanID, String formatType, String target)
193+
public String cxGetResultsSummary(String scanID) throws IOException {
194+
return runResultExecutionCommands(scanID, "summaryHTML", ".html");
195+
}
196+
197+
public String cxGetResultsList(String scanID) throws IOException {
198+
return runResultExecutionCommands(scanID, "json", ".json");
199+
}
200+
201+
public void cxGetResults(String resultType, String scanID, String fileName, String target)
189202
throws IOException {
190-
List<String> commands = initialCommandsCommon();
191-
commands.add("result");
192-
commands.add("summary");
193-
if (scanID.isEmpty()) {
203+
204+
List<String> commands = buildResultCommand(resultType, scanID, fileName, target);
205+
206+
runResultExecutionCommands(commands);
207+
}
208+
209+
private List<String> buildResultCommand(String resultType, String scanId, String outputName, String outputTarget) {
210+
if (scanId.isEmpty()) {
194211
throw new CxException("Please provide the scan id ");
195212
}
196-
commands.add("--scan-id");
197-
commands.add(scanID);
198-
if (!formatType.isEmpty()) {
199-
commands.add("--format");
200-
commands.add(formatType);
201-
}
202-
if (!target.isEmpty()) {
203-
commands.add("--target");
204-
commands.add(target);
205-
}
206-
return runResultExecutionCommands(commands);
207-
}
208213

209-
public String cxGetResultsList(String scanID, String formatType)
210-
throws IOException {
211214
List<String> commands = initialCommandsCommon();
212215
commands.add("result");
213-
commands.add("list");
214-
if (scanID.isEmpty()) {
215-
throw new CxException("Please provide the scan id ");
216-
}
217216
commands.add("--scan-id");
218-
commands.add(scanID);
219-
if (!formatType.isEmpty()) {
220-
commands.add("--format");
221-
commands.add(formatType);
217+
commands.add(scanId);
218+
commands.add("--report-format");
219+
commands.add(resultType);
220+
221+
if (StringUtils.isNotBlank(outputName)) {
222+
commands.add("--output-name");
223+
commands.add(outputName);
224+
}
225+
226+
if (StringUtils.isNotBlank(outputTarget)) {
227+
commands.add("--output-path");
228+
commands.add(outputTarget);
222229
}
223-
224-
return runResultExecutionCommands(commands);
230+
231+
return commands;
232+
}
233+
234+
235+
236+
private String runResultExecutionCommands(String scanId, String resultType, String extension) throws IOException {
237+
Path tempDir = Files.createTempDirectory("cx");
238+
String fileName = Long.toString(System.nanoTime());
239+
List<String> commands = buildResultCommand(resultType, scanId, fileName, tempDir.toAbsolutePath().toString());
240+
runResultExecutionCommands(commands);
241+
242+
File outputFile = new File(tempDir.toAbsolutePath().toString(), fileName + extension);
243+
return new String(Files.readAllBytes(Paths.get(outputFile.getAbsolutePath())), StandardCharsets.UTF_8);
225244
}
226245

227246
private String runResultExecutionCommands(List<String> commands) throws IOException {

src/main/resources/cx-linux

-52 KB
Binary file not shown.

src/main/resources/cx-mac

-4.28 KB
Binary file not shown.

src/main/resources/cx.exe

-2.5 KB
Binary file not shown.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,24 @@ public void cxScanCreationSuccess() throws InterruptedException, IOException {
112112
public void cxGenerateHTMLResults() throws InterruptedException, IOException {
113113
CxCommandOutput scanList = auth.cxAstScanList();
114114
String id = scanList.getScanObjectList().get(0).getID();
115-
String filePath = System.getProperty("user.dir") + "/index.html";
116-
auth.cxGetResultsSummary( id, "", filePath);
117-
assertTrue(new File(filePath).length() > 0);
115+
String filePath = System.getProperty("user.dir");
116+
auth.cxGetResults("summaryHTML", id, "index", filePath);
117+
assertTrue(new File(filePath + "/index.html").length() > 0);
118118
}
119119

120120
@Test
121121
public void cxGetResultsSummaryString() throws InterruptedException, IOException {
122122
CxCommandOutput scanList = auth.cxAstScanList();
123123
String id = scanList.getScanObjectList().get(0).getID();
124-
String op = auth.cxGetResultsSummary(id,"","");
124+
String op = auth.cxGetResultsSummary(id);
125125
assertTrue(op.length() > 0);
126126
}
127127

128128
@Test
129129
public void cxGetResultsListString() throws InterruptedException, IOException {
130130
CxCommandOutput scanList = auth.cxAstScanList();
131131
String id = scanList.getScanObjectList().get(0).getID();
132-
String op = auth.cxGetResultsList(id,"json");
132+
String op = auth.cxGetResultsList(id);
133133
assertTrue(op.length() > 0);
134134
}
135135
}

0 commit comments

Comments
 (0)