Search in sources :

Example 11 with MachinaException

use of com.checkmarx.flow.exception.MachinaException in project cx-flow by checkmarx-ltd.

the class ResultsService method processScanResultsAsync.

@Async("scanRequest")
public CompletableFuture<ScanResults> processScanResultsAsync(ScanRequest request, Integer projectId, Integer scanId, String osaScanId, FilterConfiguration filterConfiguration) throws MachinaException {
    try {
        CompletableFuture<ScanResults> future = new CompletableFuture<>();
        // TODO async these, and join and merge after
        ScanResults results = cxScannerService.getScannerClient().getReportContentByScanId(scanId, filterConfiguration);
        logGetResultsJsonLogger(request, scanId, results);
        results = getOSAScan(request, projectId, osaScanId, filterConfiguration, results);
        sendEmailNotification(request, results);
        processResults(request, results, new ScanDetails(projectId, scanId, osaScanId));
        logScanDetails(request, projectId, results);
        future.complete(results);
        return future;
    } catch (Exception e) {
        log.error("Error occurred while processing results.", e);
        CompletableFuture<ScanResults> x = new CompletableFuture<>();
        x.completeExceptionally(e);
        return x;
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ScanResults(com.checkmarx.sdk.dto.ScanResults) ScanDetails(com.checkmarx.flow.dto.ScanDetails) InvalidCredentialsException(com.checkmarx.flow.exception.InvalidCredentialsException) CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) RestClientException(com.atlassian.jira.rest.client.api.RestClientException) JiraClientException(com.checkmarx.flow.exception.JiraClientException) JiraClientRunTimeException(com.checkmarx.flow.exception.JiraClientRunTimeException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) MachinaException(com.checkmarx.flow.exception.MachinaException) Async(org.springframework.scheduling.annotation.Async)

Example 12 with MachinaException

use of com.checkmarx.flow.exception.MachinaException in project cx-flow by checkmarx-ltd.

the class ResultsService method getCxFields.

private void getCxFields(ScanRequest request, ScanResults results) throws MachinaException {
    try {
        /*Are cx fields required?*/
        if (!requiresCxCustomFields(request.getBugTracker().getFields())) {
            return;
        }
        /*if so, then get them and add them to the request object*/
        if (!ScanUtils.empty(results.getProjectId()) && !results.getProjectId().equals(Constants.UNKNOWN)) {
            CxProject project = cxScannerService.getScannerClient().getProject(Integer.parseInt(results.getProjectId()));
            Map<String, String> fields = new HashMap<>();
            for (CxProject.CustomField field : project.getCustomFields()) {
                if (!ScanUtils.empty(field.getName()) && !ScanUtils.empty(field.getValue())) {
                    fields.put(field.getName(), field.getValue());
                }
            }
            if (!fields.isEmpty()) {
                request.setCxFields(fields);
                if (!ScanUtils.empty(cxScannerService.getProperties().getJiraProjectField())) {
                    String jiraProject = fields.get(cxScannerService.getProperties().getJiraProjectField());
                    if (!ScanUtils.empty(jiraProject)) {
                        request.getBugTracker().setProjectKey(jiraProject);
                    }
                }
                if (!ScanUtils.empty(cxScannerService.getProperties().getJiraIssuetypeField())) {
                    String jiraIssuetype = fields.get(cxScannerService.getProperties().getJiraIssuetypeField());
                    if (!ScanUtils.empty(jiraIssuetype)) {
                        request.getBugTracker().setIssueType(jiraIssuetype);
                    }
                }
            }
        }
    } catch (InvalidCredentialsException e) {
        log.warn("Error retrieving Checkmarx Project details for {}, no custom fields will be available", results.getProjectId(), e);
        throw new MachinaException("Error logging into Checkmarx");
    }
}
Also used : HashMap(java.util.HashMap) InvalidCredentialsException(com.checkmarx.flow.exception.InvalidCredentialsException) MachinaException(com.checkmarx.flow.exception.MachinaException) CxProject(com.checkmarx.sdk.dto.cx.CxProject)

Example 13 with MachinaException

use of com.checkmarx.flow.exception.MachinaException in project cx-flow by checkmarx-ltd.

the class ThresholdsSteps method processScanResultsInCxFlow.

private void processScanResultsInCxFlow(boolean isGitHub) {
    try {
        ScanRequest scanRequest = createScanRequest(isGitHub);
        CompletableFuture<ScanResults> task = resultsService.processScanResultsAsync(scanRequest, 0, 0, null, null);
        task.get(1, TimeUnit.MINUTES);
    } catch (MachinaException | InterruptedException | ExecutionException | TimeoutException e) {
        String message = "Error processing scan results.";
        log.error(message, e);
        Assert.fail(message);
        Thread.currentThread().interrupt();
    }
}
Also used : ScanRequest(com.checkmarx.flow.dto.ScanRequest) ScanResults(com.checkmarx.sdk.dto.ScanResults) MachinaException(com.checkmarx.flow.exception.MachinaException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with MachinaException

use of com.checkmarx.flow.exception.MachinaException in project cx-flow by checkmarx-ltd.

the class ServiceNowTracker method init.

@Override
public void init(ScanRequest request, ScanResults results) throws MachinaException {
    log.info("Initializing Service Now Tracker");
    if (ScanUtils.empty(request.getNamespace()) || ScanUtils.empty(request.getRepoName()) || ScanUtils.empty(request.getBranch())) {
        throw new MachinaException("Namespace / RepoName / Branch are required");
    }
    if (ScanUtils.empty(properties.getApiUrl())) {
        throw new MachinaException("Service Now API Url must be provided in property config");
    }
    if (ScanUtils.empty(properties.getUsername()) || ScanUtils.empty(properties.getPassword())) {
        throw new MachinaException("Service Now API Rest Call requires username and password");
    }
    restOperations = new RestTemplateBuilder().basicAuthentication(properties.getUsername(), properties.getPassword()).build();
    createSeviceNowTags(request);
}
Also used : RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) MachinaException(com.checkmarx.flow.exception.MachinaException)

Example 15 with MachinaException

use of com.checkmarx.flow.exception.MachinaException in project cx-flow by checkmarx-ltd.

the class WebPostIssueTracker method complete.

@Override
public void complete(ScanRequest request, ScanResults results) throws MachinaException {
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        if (request != null && results != null) {
            mapper.writeValue(new File(request.getFilename()).getCanonicalFile(), results);
            String resultUrl = request.getAdditionalMetadata("result_url");
            String filename = request.getFilename();
            if (ScanUtils.anyEmpty(resultUrl, filename)) {
                log.error("result_url | temporary file was massing from the ScanRequest metadata");
                throw new MachinaException();
            }
            File resultFile = new File(filename);
            log.info("Saving file {} to signed web url", filename);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<byte[]> entity = new HttpEntity<>(Files.readAllBytes(Paths.get(resultFile.getCanonicalPath())), headers);
            URI uri = new URI(resultUrl);
            restTemplate.put(uri, entity);
            log.info("Save successful");
        } else {
            log.error("No request or results provided");
            throw new MachinaException();
        }
    } catch (IOException e) {
        log.error("Issue occurred while writing file {}", request.getFilename(), e);
        throw new MachinaException();
    } catch (URISyntaxException e) {
        log.error("Error occurred: {}", ExceptionUtils.getMessage(e), e);
        throw new MachinaException();
    } catch (HttpClientErrorException e) {
        log.error("HttpClientErrorException occurred: {}", ExceptionUtils.getMessage(e), e);
        throw new MachinaException();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) MachinaException(com.checkmarx.flow.exception.MachinaException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URI(java.net.URI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

MachinaException (com.checkmarx.flow.exception.MachinaException)30 ScanResults (com.checkmarx.sdk.dto.ScanResults)18 ScanRequest (com.checkmarx.flow.dto.ScanRequest)11 CheckmarxException (com.checkmarx.sdk.exception.CheckmarxException)8 Issue (com.checkmarx.flow.dto.Issue)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)6 IOException (java.io.IOException)5 ExecutionException (java.util.concurrent.ExecutionException)5 TimeoutException (java.util.concurrent.TimeoutException)5 MachinaRuntimeException (com.checkmarx.flow.exception.MachinaRuntimeException)4 File (java.io.File)4 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 When (io.cucumber.java.en.When)3 URI (java.net.URI)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Incident (com.checkmarx.flow.dto.servicenow.Incident)2 InvalidCredentialsException (com.checkmarx.flow.exception.InvalidCredentialsException)2 CxProject (com.checkmarx.sdk.dto.cx.CxProject)2