Search in sources :

Example 1 with MachinaException

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

the class IssueService method process.

public void process(ScanResults results, ScanRequest request) throws MachinaException {
    Map<String, ScanResults.XIssue> xMap;
    Map<String, Issue> iMap;
    List<String> newIssues = new ArrayList<>();
    List<String> updatedIssues = new ArrayList<>();
    List<String> closedIssues = new ArrayList<>();
    BugTracker bugTracker = request.getBugTracker();
    String customBean = bugTracker.getCustomBean();
    if (!bugTracker.getType().equals(BugTracker.Type.CUSTOM) && !ScanUtils.empty(customBean)) {
        throw new MachinaException("A valid custom bean must be used here.");
    }
    try {
        IssueTracker tracker = (IssueTracker) context.getBean(customBean);
        tracker.init(request, results);
        String fpLabel = tracker.getFalsePositiveLabel();
        codeBashingService.createLessonsMap();
        log.info("Processing Issues with custom bean {}", customBean);
        List<Issue> issues = tracker.getIssues(request);
        if (issues == null) {
            issues = Collections.emptyList();
        }
        xMap = this.getXIssueMap(tracker, results, request);
        iMap = this.getIssueMap(tracker, issues, request);
        for (Map.Entry<String, ScanResults.XIssue> xIssue : xMap.entrySet()) {
            try {
                String fileUrl;
                ScanResults.XIssue currentIssue = xIssue.getValue();
                codeBashingService.addCodebashingUrlToIssue(currentIssue);
                /*Issue already exists -> update and comment*/
                if (iMap.containsKey(xIssue.getKey())) {
                    Issue i = iMap.get(xIssue.getKey());
                    if (xIssue.getValue().isAllFalsePositive()) {
                        // All issues are false positive, so issue should be closed
                        Issue fpIssue;
                        log.debug("All issues are false positives");
                        if (properties.isListFalsePositives()) {
                            // Update the ticket if flag is set
                            log.debug("Issue is being updated to reflect false positive references.  Updating issue with key {}", xIssue.getKey());
                            tracker.updateIssue(i, currentIssue, request);
                        }
                        if (tracker.isIssueOpened(i, request)) {
                            /*Close the issue if in an open state*/
                            log.info("Closing issue with key {}", i.getId());
                            tracker.closeIssue(i, request);
                            closedIssues.add(i.getId());
                        }
                    } else if (!i.getLabels().contains(fpLabel)) {
                        /*Ignore any with label indicating false positive*/
                        log.info("Issue still exists.  Updating issue with key {}", xIssue.getKey());
                        fileUrl = ScanUtils.getFileUrl(request, currentIssue.getFilename());
                        currentIssue.setGitUrl(fileUrl);
                        Issue updatedIssue = tracker.updateIssue(i, currentIssue, request);
                        if (updatedIssue != null) {
                            updatedIssues.add(updatedIssue.getId());
                            log.debug("Update completed for issue #{}", updatedIssue.getId());
                        }
                    } else {
                        log.info("Skipping issue marked as false positive with key {}", xIssue.getKey());
                    }
                } else {
                    /*Create the new issue*/
                    if (!xIssue.getValue().isAllFalsePositive()) {
                        fileUrl = ScanUtils.getFileUrl(request, currentIssue.getFilename());
                        xIssue.getValue().setGitUrl(fileUrl);
                        log.info("Creating new issue with key {}", xIssue.getKey());
                        Issue newIssue = tracker.createIssue(xIssue.getValue(), request);
                        if (newIssue != null) {
                            newIssues.add(newIssue.getId());
                            log.info("New issue created. #{}", newIssue.getId());
                        }
                    }
                }
            } catch (HttpClientErrorException e) {
                log.error("Error occurred while processing issue with key {}", xIssue.getKey(), e);
            }
        }
        /*Check if an issue exists in GitLab but not within results and close if not*/
        for (Map.Entry<String, Issue> issueMap : iMap.entrySet()) {
            String key = issueMap.getKey();
            Issue issue = issueMap.getValue();
            try {
                if (!xMap.containsKey(key) && tracker.isIssueOpened(issue, request)) {
                    /*Close the issue*/
                    tracker.closeIssue(issue, request);
                    closedIssues.add(issue.getId());
                    log.info("Closing issue #{} with key {}", issue.getId(), key);
                }
            } catch (HttpClientErrorException e) {
                log.error("Error occurred while processing issue with key {}", key, e);
            }
        }
        Map<String, List<String>> issuesMap = new HashMap<>();
        issuesMap.put("new", newIssues);
        issuesMap.put("updated", updatedIssues);
        issuesMap.put("closed", closedIssues);
        tracker.complete(request, results);
    } catch (BeansException e) {
        log.error("Specified bug tracker bean was not found or properly loaded.", e);
        throw new MachinaRuntimeException();
    } catch (ClassCastException e) {
        log.error("Bean must implement the IssueTracker Interface", e);
        throw new MachinaRuntimeException();
    }
}
Also used : Issue(com.checkmarx.flow.dto.Issue) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) IssueTracker(com.checkmarx.flow.custom.IssueTracker) ScanResults(com.checkmarx.sdk.dto.ScanResults) BugTracker(com.checkmarx.flow.dto.BugTracker) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) MachinaException(com.checkmarx.flow.exception.MachinaException) BeansException(org.springframework.beans.BeansException)

Example 2 with MachinaException

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

the class AbstractVulnerabilityScanner method executeCxScan.

public ScanDetails executeCxScan(ScanRequest request, File cxFile) throws MachinaException {
    String osaScanId;
    Integer scanId = null;
    Integer projectId;
    try {
        /*Check if team is provided*/
        String ownerId = getScanRequestConverter().determineTeamAndOwnerID(request);
        log.debug("Auto profiling is enabled");
        projectId = getScanRequestConverter().determinePresetAndProjectId(request, ownerId);
        CxScanParams params = getScanRequestConverter().prepareScanParamsObject(request, cxFile, ownerId, projectId);
        scanId = getScannerClient().createScan(params, getComment(request));
        osaScanId = createOsaScan(request, projectId);
        if (osaScanId != null) {
            logRequest(request, osaScanId, cxFile, OperationResult.successful());
        }
    } catch (GitHubRepoUnavailableException e) {
        // an error stack trace in the log.
        return new ScanDetails(UNKNOWN_INT, UNKNOWN_INT, new CompletableFuture<>(), false);
    } catch (CheckmarxException | GitAPIException e) {
        String extendedMessage = treatFailure(request, cxFile, scanId, e);
        throw new MachinaException("Checkmarx Error Occurred: " + extendedMessage);
    }
    logRequest(request, scanId, cxFile, OperationResult.successful());
    this.scanDetails = new ScanDetails(projectId, scanId, osaScanId);
    return scanDetails;
}
Also used : CxScanParams(com.checkmarx.sdk.dto.cx.CxScanParams) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CompletableFuture(java.util.concurrent.CompletableFuture) MachinaException(com.checkmarx.flow.exception.MachinaException) CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) GitHubRepoUnavailableException(com.checkmarx.flow.exception.GitHubRepoUnavailableException)

Example 3 with MachinaException

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

the class ADOIssueTracker method getIssues.

/**
 * Get all issues for a Azure repository
 *
 * @return List of Azure Issues
 * @ full name (owner/repo format)
 */
@Override
public List<Issue> getIssues(ScanRequest request) throws MachinaException {
    log.info("Executing getIssues Azure API call");
    List<Issue> issues = new ArrayList<>();
    String projectName = calculateProjectName(request);
    URI endpoint = getSearchEndpoint(projectName, request);
    String issueBody = request.getAdditionalMetadata(Constants.ADO_ISSUE_BODY_KEY);
    String wiq;
    // if there are project and namespace in properties on the ado section, they will be used for the URL
    if (!StringUtils.isEmpty(properties.getProjectName())) {
        if (!StringUtils.isEmpty(properties.getNamespace())) {
            wiq = String.format(WIQ_PROJECT_NAME_AND_NAMESPACE, getNamespaceTag(properties.getNamespace()));
        } else {
            wiq = String.format(WIQ_PROJECT_NAME_AND_NAMESPACE, getNamespaceTag(request.getNamespace()));
        }
    } else /*Namespace/Repo/Branch provided*/
    if (!flowProperties.isTrackApplicationOnly() && !ScanUtils.empty(request.getNamespace()) && !ScanUtils.empty(request.getRepoName()) && !ScanUtils.empty(request.getBranch())) {
        wiq = String.format(WIQ_REPO_BRANCH, request.getProduct().getProduct(), properties.getOwnerTagPrefix(), request.getNamespace(), properties.getRepoTagPrefix(), request.getRepoName(), properties.getBranchLabelPrefix(), request.getBranch());
    } else /*Only application provided*/
    if (!ScanUtils.empty(request.getApplication())) {
        wiq = String.format(WIQ_APP, request.getProduct().getProduct(), properties.getAppTagPrefix(), request.getApplication());
    } else {
        log.error("Application must be set at minimum");
        throw new MachinaException("Application must be set at minimum");
    }
    log.debug(wiq);
    JSONObject wiqJson = new JSONObject();
    wiqJson.put("query", wiq);
    HttpEntity<String> httpEntity = new HttpEntity<>(wiqJson.toString(), ADOUtils.createAuthHeaders(scmConfigOverrider.determineConfigToken(properties, request.getScmInstance())));
    ResponseEntity<String> response = restTemplate.exchange(endpoint, HttpMethod.POST, httpEntity, String.class);
    if (response.getBody() == null)
        return issues;
    JSONObject json = new JSONObject(response.getBody());
    JSONArray workItems = json.getJSONArray("workItems");
    if (workItems.length() < 1)
        return issues;
    for (int i = 0; i < workItems.length(); i++) {
        JSONObject workItem = workItems.getJSONObject(i);
        String workItemUri = workItem.getString("url");
        Issue wi = getIssue(workItemUri, issueBody, request);
        if (wi != null) {
            issues.add(wi);
        }
    }
    return issues;
}
Also used : Issue(com.checkmarx.flow.dto.Issue) JSONObject(org.json.JSONObject) MachinaException(com.checkmarx.flow.exception.MachinaException) JSONArray(org.json.JSONArray) URI(java.net.URI)

Example 4 with MachinaException

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

the class ServiceNowTracker method updateIssue.

@Override
public Issue updateIssue(Issue issue, ScanResults.XIssue resultIssue, ScanRequest request) throws MachinaException {
    log.info("Executing updateIssue Service Now API call");
    Incident incident = updateIncidentFromIssue(issue, resultIssue);
    try {
        String query = String.format("%s%s/%s", properties.getApiUrl(), INCIDENTS, issue.getId());
        this.addComment(incident, resultIssue.getLink(), "Issue still exists.");
        restOperations.put(query, incident);
        return getIssues(request).stream().findFirst().orElseThrow(() -> new MachinaException("Incident record hasn't been found."));
    } catch (HttpClientErrorException e) {
        log.error("Error updating issue.");
        throw new MachinaRuntimeException();
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) MachinaException(com.checkmarx.flow.exception.MachinaException) Incident(com.checkmarx.flow.dto.servicenow.Incident)

Example 5 with MachinaException

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

the class ServiceNowTracker method getIssues.

/**
 * Get Incidents/Issues from Service Now.
 * @param request
 * @return issues collection of data.
 * @throws MachinaException
 */
@Override
public List<Issue> getIssues(ScanRequest request) throws MachinaException {
    log.debug("Executing getIssues Service Now API call");
    try {
        String apiRequest = createServiceNowRequest(request);
        URI apiRequestUri = URI.create(apiRequest);
        Optional<Result> res = Optional.ofNullable(restOperations.getForObject(apiRequestUri, Result.class));
        if (res.isPresent()) {
            List results = res.get().getIncidents().stream().map(i -> mapToIssue(i)).collect(Collectors.toList());
            log.debug("Found {} issues in ServiceNow for this project.", results != null ? results.size() : 0);
            return results;
        }
    } catch (RestClientException e) {
        log.error("Error occurred while fetching ServiceNow Issues");
        log.error(ExceptionUtils.getStackTrace(e));
        throw new MachinaRuntimeException();
    }
    return Lists.newArrayList();
}
Also used : Issue(com.checkmarx.flow.dto.Issue) ScanRequest(com.checkmarx.flow.dto.ScanRequest) ScanResults(com.checkmarx.sdk.dto.ScanResults) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) FlowProperties(com.checkmarx.flow.config.FlowProperties) HTMLHelper(com.checkmarx.flow.utils.HTMLHelper) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) Locale(java.util.Locale) URI(java.net.URI) RestClientException(org.springframework.web.client.RestClientException) Result(com.checkmarx.flow.dto.servicenow.Result) Logger(org.slf4j.Logger) RestOperations(org.springframework.web.client.RestOperations) RestTemplateBuilder(org.springframework.boot.web.client.RestTemplateBuilder) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ServiceNowProperties(com.checkmarx.flow.config.ServiceNowProperties) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) URLEncoder(java.net.URLEncoder) List(java.util.List) Incident(com.checkmarx.flow.dto.servicenow.Incident) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) ScanUtils(com.checkmarx.flow.utils.ScanUtils) Optional(java.util.Optional) MachinaException(com.checkmarx.flow.exception.MachinaException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONArray(org.json.JSONArray) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) RestClientException(org.springframework.web.client.RestClientException) List(java.util.List) URI(java.net.URI) Result(com.checkmarx.flow.dto.servicenow.Result)

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