Search in sources :

Example 41 with CheckmarxException

use of com.checkmarx.sdk.exception.CheckmarxException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxService method setProjectRepositoryDetails.

/**
 * Set Repository details for a project
 */
public void setProjectRepositoryDetails(Integer projectId, String gitUrl, String branch, CxScanParams params) throws CheckmarxException {
    String sshKey = getSshKey(params);
    CxProjectSource projectSource;
    if (sshKey.length() > 0) {
        projectSource = CxProjectSource.builder().url(createGitURL(gitUrl)).privateKey(sshKey).branch(branch).build();
    } else {
        projectSource = CxProjectSource.builder().url(gitUrl).branch(branch).build();
    }
    log.debug("branch {}", branch);
    log.debug("project {}", projectId);
    HttpEntity<CxProjectSource> requestEntity = new HttpEntity<>(projectSource, authClient.createAuthHeaders());
    try {
        log.info("Updating Source details for project Id {}", projectId);
        restTemplate.exchange(cxProperties.getUrl().concat(PROJECT_SOURCE), HttpMethod.POST, requestEntity, String.class, projectId);
    } catch (HttpStatusCodeException e) {
        log.error("Error occurred while updating Project source info for project {}.", projectId);
        log.debug(ExceptionUtils.getStackTrace(e));
        throw new CheckmarxException("Error occurred while adding source details to project.  Please ensure GIT is defined within Checkmarx");
    }
}
Also used : CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException)

Example 42 with CheckmarxException

use of com.checkmarx.sdk.exception.CheckmarxException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxService method getLdapRoleMapId.

@Override
public Integer getLdapRoleMapId(Integer ldapServerId, Integer roleId, String ldapGroupDn) throws CheckmarxException {
    if (cxProperties.getVersion() < 9.0) {
        throw new CheckmarxException(ONLY_SUPPORTED_IN_90_PLUS);
    }
    try {
        HttpEntity requestEntity = new HttpEntity<>(authClient.createAuthHeaders());
        ResponseEntity<String> response = restTemplate.exchange(cxProperties.getUrl().concat(ROLE_LDAP_MAPPINGS), HttpMethod.GET, requestEntity, String.class, ldapServerId);
        JSONArray objs = new JSONArray(response.getBody());
        for (int i = 0; i < objs.length(); i++) {
            JSONObject obj = objs.getJSONObject(i);
            String cn = obj.getString("ldapGroupDn");
            if (roleId.equals(obj.getInt("roleId")) && cn.equals(ldapGroupDn)) {
                return obj.getInt("id");
            }
        }
        log.info("No mapping found for {} with Server id {}", ldapGroupDn, ldapServerId);
    } catch (HttpStatusCodeException e) {
        log.error("Error occurred while retrieving ldap server mappings, http error {}", e.getStatusCode());
        log.error(ExceptionUtils.getStackTrace(e));
    }
    return UNKNOWN_INT;
}
Also used : JSONObject(org.json.JSONObject) CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) JSONArray(org.json.JSONArray) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException)

Example 43 with CheckmarxException

use of com.checkmarx.sdk.exception.CheckmarxException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxService method createScanAndReport.

/**
 * @param params attributes used to define the project
 */
@Override
public CxXMLResultsType createScanAndReport(CxScanParams params, String comment) throws CheckmarxException {
    Integer scanId = createScan(params, comment);
    waitForScanCompletion(scanId);
    try {
        Integer reportId = createScanReport(scanId);
        waitForReportCreateOrFail(reportId);
        Thread.sleep(1000);
        return getXmlReportContent(reportId);
    } catch (InterruptedException e) {
        log.error(ExceptionUtils.getStackTrace(e));
        Thread.currentThread().interrupt();
        throw new CheckmarxException(INTERRUPTED_EXCEPTION_MESSAGE);
    }
}
Also used : CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException)

Example 44 with CheckmarxException

use of com.checkmarx.sdk.exception.CheckmarxException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxService method waitForScanCompletion.

/**
 * Wait for a for a scan with a given scan Id to complete with a finished or failure state
 *
 * @param scanId
 * @throws CheckmarxException
 */
public void waitForScanCompletion(Integer scanId) throws CheckmarxException {
    Integer status = getScanStatus(scanId);
    long timer = 0;
    long queueTimer = 0;
    try {
        while (!status.equals(CxService.SCAN_STATUS_FINISHED) && !status.equals(CxService.SCAN_STATUS_CANCELED) && !status.equals(CxService.SCAN_STATUS_FAILED)) {
            Thread.sleep(cxProperties.getScanPolling());
            status = getScanStatus(scanId);
            timer += cxProperties.getScanPolling();
            // Scan Queuing Timeout = '0' and Scan Queuing = true would be waiting forever with the scan in the queue
            if (cxProperties.getScanQueuing() && status.equals(CxService.SCAN_STATUS_QUEUED)) {
                queueTimer += cxProperties.getScanPolling();
                if (cxProperties.getScanQueuingTimeout() != 0 && queueTimer >= (cxProperties.getScanQueuingTimeout() * 60000)) {
                    log.error("Scan queued time exceded. {} minutes ", cxProperties.getScanQueuingTimeout());
                    throw new CheckmarxException("Timeout exceeded for Scan Queuing.");
                }
            }
            if (timer >= (cxProperties.getScanTimeout() * 60000)) {
                log.error("Scan timeout exceeded.  {} minutes", cxProperties.getScanTimeout());
                throw new CheckmarxException("Timeout exceeded during scan");
            }
        }
        if (status.equals(CxService.SCAN_STATUS_FAILED) || status.equals(CxService.SCAN_STATUS_CANCELED)) {
            throw new CheckmarxException("Scan was cancelled or failed");
        }
    } catch (InterruptedException e) {
        throw new CheckmarxException("Thread interrupted");
    } catch (HttpStatusCodeException e) {
        throw new CheckmarxException("HTTP Error".concat(ExceptionUtils.getRootCauseMessage(e)));
    }
}
Also used : CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException)

Example 45 with CheckmarxException

use of com.checkmarx.sdk.exception.CheckmarxException in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class CxService method uploadProjectSource.

/**
 * Upload file (zip of source) for a project
 */
public void uploadProjectSource(Integer projectId, File file) throws CheckmarxException {
    HttpHeaders headers = authClient.createAuthHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    FileSystemResource value = new FileSystemResource(file);
    map.add("zippedSource", value);
    HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
    try {
        log.info("Updating Source details for project Id {}", projectId);
        restTemplate.exchange(cxProperties.getUrl().concat(PROJECT_SOURCE_FILE), HttpMethod.POST, requestEntity, String.class, projectId);
    } catch (HttpStatusCodeException e) {
        log.error(ExceptionUtils.getStackTrace(e));
        log.error("Error occurred while uploading Project source for project id {}.", projectId);
        throw new CheckmarxException("Error occurred while uploading source");
    }
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) CheckmarxException(com.checkmarx.sdk.exception.CheckmarxException) JSONObject(org.json.JSONObject) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) FileSystemResource(org.springframework.core.io.FileSystemResource)

Aggregations

CheckmarxException (com.checkmarx.sdk.exception.CheckmarxException)62 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)23 ScanResults (com.checkmarx.sdk.dto.ScanResults)11 HttpEntity (org.springframework.http.HttpEntity)10 MachinaException (com.checkmarx.flow.exception.MachinaException)8 Test (org.junit.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 File (java.io.File)6 JSONObject (org.json.JSONObject)6 CxProject (com.checkmarx.sdk.dto.cx.CxProject)5 CxScanParams (com.checkmarx.sdk.dto.cx.CxScanParams)4 IOException (java.io.IOException)4 JAXBContext (javax.xml.bind.JAXBContext)4 JAXBException (javax.xml.bind.JAXBException)4 Unmarshaller (javax.xml.bind.Unmarshaller)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 SoapActionCallback (org.springframework.ws.soap.client.core.SoapActionCallback)3 ScanReport (com.checkmarx.flow.dto.report.ScanReport)2