Search in sources :

Example 11 with CxConfig

use of com.checkmarx.sdk.dto.sast.CxConfig in project cx-flow by checkmarx-ltd.

the class ScaConfigAsCodeSteps method getRepoCxConfig.

@When("target repo contains a configuration file")
public void getRepoCxConfig() {
    CxConfig cxConfigOverride = gitHubService.getCxConfigOverride(scanRequest);
    configOverrider.overrideScanRequestProperties(cxConfigOverride, scanRequest);
}
Also used : CxConfig(com.checkmarx.sdk.dto.sast.CxConfig) When(io.cucumber.java.en.When)

Example 12 with CxConfig

use of com.checkmarx.sdk.dto.sast.CxConfig in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.

the class ScanUtilsTest method testGetConfigAsCode.

@Test
public void testGetConfigAsCode() {
    File file = new File(getClass().getClassLoader().getResource("CxConfig.json").getFile());
    CxConfig config = ScanUtils.getConfigAsCode(file);
    assertNotNull(config);
    assertTrue(config.getActive());
    assertEquals("/a/b/c", config.getTeam());
}
Also used : CxConfig(com.checkmarx.sdk.dto.sast.CxConfig) File(java.io.File) Test(org.junit.Test)

Example 13 with CxConfig

use of com.checkmarx.sdk.dto.sast.CxConfig in project cx-flow by checkmarx-ltd.

the class GitLabController method mergeRequest.

/**
 * Merge Request event webhook submitted.
 */
@PostMapping(value = { "/{product}", "/" }, headers = MERGE)
public ResponseEntity<EventResponse> mergeRequest(@RequestBody MergeEvent body, @RequestHeader(value = TOKEN_HEADER) String token, @PathVariable(value = "product", required = false) String product, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing GitLab MERGE request");
    controllerRequest = ensureNotNull(controllerRequest);
    validateGitLabRequest(token, controllerRequest);
    try {
        ObjectAttributes objectAttributes = body.getObjectAttributes();
        if (!objectAttributes.getState().equalsIgnoreCase("opened") || isWIP(body)) {
            log.info("Merge requested not processed.  Status was not opened , or was WIP ({})", objectAttributes.getState());
            return ResponseEntity.status(HttpStatus.OK).body(EventResponse.builder().message("No processing occurred for updates to Merge Request").success(true).build());
        }
        String app = body.getRepository().getName();
        if (StringUtils.isNotEmpty(controllerRequest.getApplication())) {
            app = controllerRequest.getApplication();
        }
        BugTracker.Type bugType = BugTracker.Type.GITLABMERGE;
        if (StringUtils.isNotEmpty(controllerRequest.getBug())) {
            bugType = ScanUtils.getBugTypeEnum(controllerRequest.getBug(), flowProperties.getBugTrackerImpl());
        }
        if (controllerRequest.getAppOnly() != null) {
            flowProperties.setTrackApplicationOnly(controllerRequest.getAppOnly());
        }
        if (ScanUtils.empty(product)) {
            product = ScanRequest.Product.CX.getProduct();
        }
        ScanRequest.Product p = ScanRequest.Product.valueOf(product.toUpperCase(Locale.ROOT));
        String currentBranch = objectAttributes.getSourceBranch();
        String targetBranch = objectAttributes.getTargetBranch();
        String defaultBranch = objectAttributes.getTarget().getDefaultBranch();
        List<String> branches = getBranches(controllerRequest, flowProperties);
        BugTracker bt = ScanUtils.getBugTracker(controllerRequest.getAssignee(), bugType, jiraProperties, controllerRequest.getBug());
        FilterConfiguration filter = filterFactory.getFilter(controllerRequest, flowProperties);
        Project proj = body.getProject();
        String gitUrl = proj.getGitHttpUrl();
        log.info("Using url: {}", gitUrl);
        String configToken = scmConfigOverrider.determineConfigToken(properties, controllerRequest.getScmInstance());
        String gitAuthUrl = gitAuthUrlGenerator.addCredToUrl(ScanRequest.Repository.GITLAB, gitUrl, configToken);
        ScanRequest request = ScanRequest.builder().id(String.valueOf(proj.getId())).application(app).product(p).project(controllerRequest.getProject()).team(controllerRequest.getTeam()).namespace(proj.getNamespace().replace(" ", "_")).repoName(proj.getName()).repoUrl(proj.getGitHttpUrl()).repoUrlWithAuth(gitAuthUrl).repoType(ScanRequest.Repository.GITLAB).branch(currentBranch).defaultBranch(defaultBranch).mergeTargetBranch(targetBranch).refs(Constants.CX_BRANCH_PREFIX.concat(currentBranch)).email(null).incremental(controllerRequest.getIncremental()).scanPreset(controllerRequest.getPreset()).excludeFolders(controllerRequest.getExcludeFolders()).excludeFiles(controllerRequest.getExcludeFiles()).bugTracker(bt).filter(filter).organizationId(getOrganizationId(proj)).gitUrl(gitUrl).hash(objectAttributes.getLastCommit().getId()).build();
        setMergeEndPointUri(objectAttributes, proj, request);
        setScmInstance(controllerRequest, request);
        if (proj.getId() != null) {
            request.setRepoProjectId(proj.getId());
        }
        /*Check for Config as code (cx.config) and override*/
        CxConfig cxConfig = gitLabService.getCxConfigOverride(request);
        request = configOverrider.overrideScanRequestProperties(cxConfig, request);
        request.putAdditionalMetadata(HTMLHelper.WEB_HOOK_PAYLOAD, body.toString());
        request.putAdditionalMetadata(FlowConstants.MERGE_ID, objectAttributes.getIid().toString());
        request.putAdditionalMetadata(FlowConstants.MERGE_TITLE, objectAttributes.getTitle());
        request.setId(uid);
        if (helperService.isBranch2Scan(request, branches)) {
            flowService.initiateAutomation(request);
        }
    } catch (IllegalArgumentException e) {
        return getBadRequestMessage(e, controllerRequest, product);
    }
    return getSuccessMessage();
}
Also used : ScanRequest(com.checkmarx.flow.dto.ScanRequest) FilterConfiguration(com.checkmarx.sdk.dto.filtering.FilterConfiguration) CxConfig(com.checkmarx.sdk.dto.sast.CxConfig) BugTracker(com.checkmarx.flow.dto.BugTracker)

Example 14 with CxConfig

use of com.checkmarx.sdk.dto.sast.CxConfig in project cx-flow by checkmarx-ltd.

the class GitLabController method pushRequest.

/**
 * Push Request event webhook submitted.
 */
@PostMapping(value = { "/{product}", "/" }, headers = PUSH)
public ResponseEntity<EventResponse> pushRequest(@RequestBody PushEvent body, @RequestHeader(value = TOKEN_HEADER) String token, @PathVariable(value = "product", required = false) String product, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    controllerRequest = ensureNotNull(controllerRequest);
    validateGitLabRequest(token, controllerRequest);
    try {
        String app;
        if (body != null && body.getRepository() != null) {
            app = body.getRepository().getName();
        } else {
            throw new IllegalArgumentException("Request body or request repository cannot be null");
        }
        if (StringUtils.isNotEmpty(controllerRequest.getApplication())) {
            app = controllerRequest.getApplication();
        }
        // set the default bug tracker as per yml
        setBugTracker(flowProperties, controllerRequest);
        BugTracker.Type bugType = ScanUtils.getBugTypeEnum(controllerRequest.getBug(), flowProperties.getBugTrackerImpl());
        if (controllerRequest.getAppOnly() != null) {
            flowProperties.setTrackApplicationOnly(controllerRequest.getAppOnly());
        }
        if (ScanUtils.empty(product)) {
            product = ScanRequest.Product.CX.getProduct();
        }
        ScanRequest.Product p = ScanRequest.Product.valueOf(product.toUpperCase(Locale.ROOT));
        // extract branch from ref (refs/heads/master -> master)
        String currentBranch = ScanUtils.getBranchFromRef(body.getRef());
        List<String> branches = getBranches(controllerRequest, flowProperties);
        BugTracker bt = ScanUtils.getBugTracker(controllerRequest.getAssignee(), bugType, jiraProperties, controllerRequest.getBug());
        FilterConfiguration filter = filterFactory.getFilter(controllerRequest, flowProperties);
        Project proj = body.getProject();
        String gitUrl = proj.getGitHttpUrl();
        log.debug("Using url: {}", gitUrl);
        String configToken = scmConfigOverrider.determineConfigToken(properties, controllerRequest.getScmInstance());
        String gitAuthUrl = gitAuthUrlGenerator.addCredToUrl(ScanRequest.Repository.GITLAB, gitUrl, configToken);
        ScanRequest request = ScanRequest.builder().id(String.valueOf(body.getProjectId())).application(app).product(p).project(controllerRequest.getProject()).team(controllerRequest.getTeam()).namespace(proj.getNamespace().replace(" ", "_")).repoName(proj.getName()).repoUrl(proj.getGitHttpUrl()).repoUrlWithAuth(gitAuthUrl).repoType(ScanRequest.Repository.GITLAB).branch(currentBranch).refs(body.getRef()).incremental(controllerRequest.getIncremental()).scanPreset(controllerRequest.getPreset()).excludeFolders(controllerRequest.getExcludeFolders()).excludeFiles(controllerRequest.getExcludeFiles()).bugTracker(bt).filter(filter).organizationId(getOrganizationId(proj)).gitUrl(gitUrl).hash(body.getAfter()).build();
        /*Determine emails*/
        List<String> emails = new ArrayList<>();
        String commitEndpoint = null;
        commitEndpoint = setUserEmail(body, bugType, proj, request, emails, commitEndpoint);
        request.setMergeNoteUri(commitEndpoint);
        request.setEmail(emails);
        setScmInstance(controllerRequest, request);
        if (StringUtils.isNotEmpty(controllerRequest.getPreset())) {
            request.setScanPreset(controllerRequest.getPreset());
            request.setScanPresetOverride(true);
        }
        if (proj.getId() != null) {
            request.setRepoProjectId(proj.getId());
        }
        /*Check for Config as code (cx.config) and override*/
        CxConfig cxConfig = gitLabService.getCxConfigOverride(request);
        request = configOverrider.overrideScanRequestProperties(cxConfig, request);
        request.putAdditionalMetadata(HTMLHelper.WEB_HOOK_PAYLOAD, body.toString());
        request.setId(uid);
        if (helperService.isBranch2Scan(request, branches)) {
            flowService.initiateAutomation(request);
        }
    } catch (IllegalArgumentException e) {
        return getBadRequestMessage(e, controllerRequest, product);
    }
    return getSuccessMessage();
}
Also used : ScanRequest(com.checkmarx.flow.dto.ScanRequest) FilterConfiguration(com.checkmarx.sdk.dto.filtering.FilterConfiguration) ArrayList(java.util.ArrayList) CxConfig(com.checkmarx.sdk.dto.sast.CxConfig) BugTracker(com.checkmarx.flow.dto.BugTracker)

Example 15 with CxConfig

use of com.checkmarx.sdk.dto.sast.CxConfig in project cx-flow by checkmarx-ltd.

the class CxFlowRunner method scanLocalPath.

private void scanLocalPath(ScanRequest request, String path) throws ExitThrowable {
    if (ScanUtils.empty(request.getProject())) {
        log.error("Please provide --cx-project to define the project in Checkmarx");
        exit(ExitCode.ARGUMENT_NOT_PROVIDED);
    }
    CxConfig cxConfig = getCxConfigOverride(path, "cx.config");
    request = configOverrider.overrideScanRequestProperties(cxConfig, request);
    // A lambda rquires a final or effectively final parameter
    ScanRequest finalRequest = request;
    ScanResults scanResults = runOnActiveScanners(scanner -> scanner.scanCli(finalRequest, "cxFullScan", new File(path)));
    processResults(request, scanResults);
}
Also used : ScanResults(com.checkmarx.sdk.dto.ScanResults) CxConfig(com.checkmarx.sdk.dto.sast.CxConfig) File(java.io.File)

Aggregations

CxConfig (com.checkmarx.sdk.dto.sast.CxConfig)22 ScanRequest (com.checkmarx.flow.dto.ScanRequest)11 BugTracker (com.checkmarx.flow.dto.BugTracker)6 File (java.io.File)6 Test (org.junit.Test)6 FilterConfiguration (com.checkmarx.sdk.dto.filtering.FilterConfiguration)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 IOException (java.io.IOException)4 MachinaRuntimeException (com.checkmarx.flow.exception.MachinaRuntimeException)3 JSONObject (org.json.JSONObject)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 IfProfileValue (org.springframework.test.annotation.IfProfileValue)2 CxProperties (com.checkmarx.sdk.config.CxProperties)1 ScanResults (com.checkmarx.sdk.dto.ScanResults)1 When (io.cucumber.java.en.When)1 ArrayList (java.util.ArrayList)1 HttpHeaders (org.springframework.http.HttpHeaders)1