Skip to content

Commit

Permalink
Merge pull request #57 from checkmarx-ltd/handle-exceptions
Browse files Browse the repository at this point in the history
Made different scanners loosely coupled and run independently
  • Loading branch information
ghannamz authored Oct 12, 2020
2 parents 2119211 + 475e1d6 commit f3d984a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.cx.plugin</groupId>
<artifactId>CxConsolePlugin</artifactId>
<version>2020.4.3</version>
<version>2020.4.4</version>
<packaging>jar</packaging>

<repositories>
Expand Down Expand Up @@ -133,7 +133,7 @@
<dependency>
<groupId>com.checkmarx</groupId>
<artifactId>cx-client-common</artifactId>
<version>2020.4.94</version>
<version>2020.4.96</version>
</dependency>
<!-- those dependencies were implemented in the common, to solve a log4j warning -->
<!-- <dependency>
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/com/cx/plugin/cli/CxConsoleLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.cx.plugin.cli.utils.ErrorParsingHelper;
import com.cx.restclient.CxClientDelegator;
import com.cx.restclient.configuration.CxScanConfig;
import com.cx.restclient.dto.Results;
import com.cx.restclient.dto.ScanResults;
import com.cx.restclient.dto.ScannerType;
import com.cx.restclient.dto.scansummary.ScanSummary;
import com.cx.restclient.exception.CxClientException;
import com.google.common.io.Files;
Expand All @@ -27,7 +29,10 @@
import org.slf4j.impl.Log4jLoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.cx.plugin.cli.constants.Parameters.*;
import static com.cx.plugin.cli.errorsconstants.ErrorMessages.INVALID_COMMAND_COUNT;
Expand Down Expand Up @@ -94,6 +99,7 @@ private static String[] overrideProperties(String[] args) {

private static int execute(Command command, CommandLine commandLine)
throws CLIParsingException, IOException, CxClientException, InterruptedException {
List<ScanResults> results = new ArrayList<>();
int exitCode = Errors.SCAN_SUCCEEDED.getCode();
CxConfigHelper.printConfig(commandLine);

Expand All @@ -104,7 +110,8 @@ private static int execute(Command command, CommandLine commandLine)
CxSastConnectionProvider connectionProvider = new CxSastConnectionProvider(cxScanConfig, logger);

CxClientDelegator clientDelegator = new CxClientDelegator(cxScanConfig, logger);
clientDelegator.init();
ScanResults initScanResults = clientDelegator.init();
results.add(initScanResults);

if (command.equals(Command.TEST_CONNECTION)) {
if (cxScanConfig.getAstScaConfig() != null) {
Expand Down Expand Up @@ -146,13 +153,14 @@ private static int execute(Command command, CommandLine commandLine)
}
}

clientDelegator.initiateScan();

ScanResults createScanResults = clientDelegator.initiateScan();
results.add(createScanResults);

if (cxScanConfig.getSynchronous()) {
final ScanResults scanResults = clientDelegator.waitForScanResults();
results.add(scanResults);

getScanResultExceptionIfExists(scanResults);
getScanResultExceptionIfExists(results);

ScanSummary scanSummary = new ScanSummary(
cxScanConfig,
Expand All @@ -165,21 +173,24 @@ private static int execute(Command command, CommandLine commandLine)
log.info(scanSummaryString);
exitCode = ErrorParsingHelper.getErrorType(scanSummary).getCode();
}
} else {
getScanResultExceptionIfExists(results);
}

return exitCode;
}

private static void getScanResultExceptionIfExists(ScanResults scanResults) {
if (scanResults != null && scanResults.getSastResults() != null && scanResults.getSastResults().getWaitException() != null) {
throw new CxClientException(scanResults.getSastResults().getWaitException());
}
if (scanResults != null && scanResults.getOsaResults() != null && scanResults.getOsaResults().getWaitException() != null) {
throw new CxClientException(scanResults.getOsaResults().getWaitException());
}
if (scanResults != null && scanResults.getScaResults() != null && scanResults.getScaResults().getWaitException() != null) {
throw new CxClientException(scanResults.getScaResults().getWaitException());
}
private static void getScanResultExceptionIfExists(List<ScanResults> scanResults) {
scanResults.forEach(scanResult -> {
if (scanResult != null) {
Map<ScannerType, Results> resultsMap = scanResult.getResults();
for (Results value : resultsMap.values()) {
if (value != null && value.getException() != null) {
throw value.getException();
}
}
}
});
}

private static CommandLine getCommandLine(String[] args) throws ParseException {
Expand Down

0 comments on commit f3d984a

Please sign in to comment.