Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI Execution from processing results twice #352

Merged
merged 1 commit into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions src/main/java/com/checkmarx/flow/CxFlowRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import static com.checkmarx.flow.exception.ExitThrowable.exit;

@Component
Expand Down Expand Up @@ -452,7 +451,6 @@ private String getOptionValues(ApplicationArguments arg, String option){
}

private void cxScan(ScanRequest request, String gitUrl, String gitAuthUrl, String branch, ScanRequest.Repository repoType) throws ExitThrowable {
ScanDetails sastScanDetails = null;
ScanResults sastScanResults = null;
ScanResults scaScanResults = null;
log.info("Initiating scan using Checkmarx git clone");
Expand All @@ -465,16 +463,16 @@ private void cxScan(ScanRequest request, String gitUrl, String gitAuthUrl, Strin

if(flowProperties.getEnabledVulnerabilityScanners() == null ||
flowProperties.getEnabledVulnerabilityScanners().contains(SAST_SCANNER)) {
sastScanDetails = sastScanner.cxFullScan(request);
sastScanResults = sastScanner.cxFullScan(request);
}
if(flowProperties.getEnabledVulnerabilityScanners().contains(SCA_SCANNER)) {
scaScanResults = scaScanner.scan(request);
}
handleScanResults(request, sastScanDetails, sastScanResults, scaScanResults);
ScanResults scanResults = resultsService.joinResults(sastScanResults, scaScanResults);
processResults(request, scanResults);
}

private void cxScan(ScanRequest request, String path) throws ExitThrowable {
ScanDetails sastScanDetails = null;
ScanResults sastScanResults = null;
ScanResults scaScanResults = null;
if(ScanUtils.empty(request.getProject())){
Expand All @@ -483,24 +481,10 @@ private void cxScan(ScanRequest request, String path) throws ExitThrowable {
}
if(flowProperties.getEnabledVulnerabilityScanners() == null ||
flowProperties.getEnabledVulnerabilityScanners().contains(SAST_SCANNER)) {
sastScanDetails = sastScanner.cxFullScan(request, path);
sastScanResults = sastScanner.cxFullScan(request, path);
}
if(flowProperties.getEnabledVulnerabilityScanners().contains(SCA_SCANNER)) {
scaScanResults = scaScanner.cxFullScan(request, path);
}
handleScanResults(request, sastScanDetails, sastScanResults, scaScanResults);
}

private void handleScanResults(ScanRequest request, ScanDetails sastScanDetails, ScanResults sastScanResults, ScanResults scaScanResults) throws ExitThrowable {
if(sastScanDetails != null){
if (sastScanDetails.getResults().isCompletedExceptionally()) {
log.error("An error occurred while executing process");
} else {
if (log.isInfoEnabled()) {
log.info("Finished processing the request");
}
}
sastScanResults = sastScanDetails.getResults().join();
scaScanResults = scaScanner.scan(request, path);
}
ScanResults scanResults = resultsService.joinResults(sastScanResults, scaScanResults);
processResults(request, scanResults);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/checkmarx/flow/service/SCAScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public ScanResults scan(ScanRequest scanRequest) {
return result;
}

public ScanResults cxFullScan(ScanRequest scanRequest, String path) throws ExitThrowable {
ScanResults result = null;
public ScanResults scan(ScanRequest scanRequest, String path) throws ExitThrowable {
ScanResults result;
log.info("--------------------- Initiating new {} scan ---------------------", SCAN_TYPE);
SCAResults internalResults = new SCAResults();

Expand All @@ -73,6 +73,7 @@ public ScanResults cxFullScan(ScanRequest scanRequest, String path) throws ExitT
internalResults = scaClient.scanLocalSource(internalScaParams);
logRequest(scanRequest, internalResults.getScanId(), OperationResult.successful());
result = toScanResults(internalResults);

log.debug("Deleting temp file {}", f.getPath());
Files.deleteIfExists(Paths.get(cxZipFile));

Expand All @@ -86,7 +87,6 @@ public ScanResults cxFullScan(ScanRequest scanRequest, String path) throws ExitT
return result;
}


private void logRequest(ScanRequest request, String scanId, OperationResult scanCreationResult) {
ScanReport report = new ScanReport(scanId, request,request.getRepoUrl(), scanCreationResult, ScanReport.SCA);
report.log();
Expand Down
33 changes: 12 additions & 21 deletions src/main/java/com/checkmarx/flow/service/SastScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import static com.checkmarx.flow.exception.ExitThrowable.exit;
import static com.checkmarx.sdk.config.Constants.UNKNOWN;
import static com.checkmarx.sdk.config.Constants.UNKNOWN_INT;
Expand Down Expand Up @@ -142,15 +140,6 @@ public boolean isEnabled() {
return result;
}

public CompletableFuture<ScanResults> executeCxScanFlow(ScanRequest request, File cxFile) throws MachinaException {
ScanDetails details = executeCxScan(request, cxFile);
if (details.processResults()) {
return resultsService.processScanResultsAsync(request, details.getProjectId(), details.getScanId(), details.getOsaScanId(), request.getFilter());
} else {
return details.getResults();
}
}

public ScanDetails executeCxScan(ScanRequest request, File cxFile) throws MachinaException {

String osaScanId;
Expand Down Expand Up @@ -197,8 +186,8 @@ public ScanDetails executeCxScan(ScanRequest request, File cxFile) throws Machin
return scanDetails;
}

public ScanDetails cxFullScan(ScanRequest request, String path) throws ExitThrowable {
ScanDetails cliScanDetails = null;
public ScanResults cxFullScan(ScanRequest request, String path) throws ExitThrowable {
ScanResults results = null;
try {
String effectiveProjectName = projectNameGenerator.determineProjectName(request);
request.setProject(effectiveProjectName);
Expand All @@ -210,30 +199,32 @@ public ScanDetails cxFullScan(ScanRequest request, String path) throws ExitThrow
log.debug("free space {}", f.getFreeSpace());
log.debug("total space {}", f.getTotalSpace());
log.debug(f.getAbsolutePath());
cliScanDetails = new ScanDetails(UNKNOWN_INT, UNKNOWN_INT, executeCxScanFlow(request, f), true);
ScanDetails details = executeCxScan(request, f);
results = cxService.getReportContentByScanId(details.getScanId(), request.getFilter());
log.debug("Deleting temp file {}", f.getPath());
Files.deleteIfExists(Paths.get(cxZipFile));
} catch (IOException e) {
log.error("Error occurred while attempting to zip path {}", path, e);
exit(3);
} catch (MachinaException e) {
} catch (MachinaException | CheckmarxException e) {
log.error("Error occurred", e);
exit(3);
}
return cliScanDetails;
return results;
}

public ScanDetails cxFullScan(ScanRequest request) throws ExitThrowable {
ScanDetails cliScanDetails = null;
public ScanResults cxFullScan(ScanRequest request) throws ExitThrowable {
ScanResults results = null;
try {
String effectiveProjectName = projectNameGenerator.determineProjectName(request);
request.setProject(effectiveProjectName);
cliScanDetails = new ScanDetails(UNKNOWN_INT, UNKNOWN_INT, executeCxScanFlow(request, null), true);
} catch (MachinaException e) {
ScanDetails details = executeCxScan(request, null);
results = cxService.getReportContentByScanId(details.getScanId(), request.getFilter());
} catch (MachinaException | CheckmarxException e) {
log.error("Error occurred", e);
exit(3);
}
return cliScanDetails;
return results;
}

public void cxParseResults(ScanRequest request, File file) throws ExitThrowable {
Expand Down