Search in sources :

Example 11 with MachinaRuntimeException

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

the class ResultsService method handleCustomIssueTracker.

private void handleCustomIssueTracker(ScanRequest request, ScanResults results) throws MachinaException {
    try {
        log.info("Issue tracking is custom bean implementation");
        issueService.process(results, request);
    } catch (HttpClientErrorException e) {
        if (e.getRawStatusCode() == HttpStatus.UNAUTHORIZED.value()) {
            throw new MachinaRuntimeException("Token is invalid. Please make sure your custom tokens are correct.\n" + e.getMessage());
        } else {
            throw e;
        }
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException)

Example 12 with MachinaRuntimeException

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

the class BitbucketServerController method handleDeleteEvent.

public ResponseEntity<EventResponse> handleDeleteEvent(String body, String uid, PushEvent event, String signature, String product, ControllerRequest controllerRequest) {
    log.info("Processing BitBucket DELETE branch request");
    if (flowProperties == null) {
        log.error("Properties have null values");
        throw new MachinaRuntimeException();
    }
    verifyHmacSignature(body, signature);
    String application = event.getRepository().getName();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    BitbucketServerEventHandler handler = BitbucketServerDeleteHandler.builder().controllerRequest(controllerRequest).branchNameForDelete(event.getChanges().get(INDEX_FROM_CHANGES).getRefId()).fromProjectKey(event.getRepository().getProject().getKey()).repositoryName(event.getRepository().getName()).product(product).application(application).webhookPayload(body).configProvider(this).build();
    return handler.execute(uid);
}
Also used : MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler)

Example 13 with MachinaRuntimeException

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

the class PostWebhookController method pushRequest.

@PostMapping(value = { ROOT + "/{product}", ROOT }, headers = PUSH)
public ResponseEntity<EventResponse> pushRequest(@RequestBody String body, @PathVariable(value = "product", required = false) String product, @RequestHeader(value = AUTH_HEADER, required = false) String credentials, @RequestParam(value = TOKEN_PARAM, required = false) String token, ControllerRequest controllerRequest) {
    String uid = helperService.getShortUid();
    MDC.put(FlowConstants.MAIN_MDC_ENTRY, uid);
    log.info("Processing BitBucket(Post Web Hook) PUSH request");
    validateCredentials(credentials, token);
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    BitbucketPushEvent event;
    try {
        event = mapper.readValue(body, BitbucketPushEvent.class);
    } catch (IOException e) {
        throw new MachinaRuntimeException(e);
    }
    String application = event.getRepository().getSlug();
    if (!ScanUtils.empty(controllerRequest.getApplication())) {
        application = controllerRequest.getApplication();
    }
    if (ScanUtils.empty(product)) {
        product = ScanRequest.Product.CX.getProduct();
    }
    if (event.getPush().getChanges() == null || event.getPush().getChanges().length == 0) {
        log.warn("Empty commit, nothing to handle.");
        return getEmptyCommitMessage();
    }
    BitbucketPushChange change = event.getPush().getChanges()[CHANGE_INDEX];
    if (change.isClosed() && change.getNewState() == null) {
        return handleDelete(event, controllerRequest, product, application, body, uid);
    }
    BitbucketServerEventHandler handler = BitbucketServerPushHandler.builder().controllerRequest(controllerRequest).toSlug(event.getRepository().getSlug()).repositoryName(event.getRepository().getSlug()).fromSlug(event.getRepository().getSlug()).branchFromRef(!change.isCreated() ? change.getOldState().getName() : change.getNewState().getName()).toHash(change.getNewState().getTarget().getHash()).email(event.getActor().getEmailAddress()).fromProjectKey(event.getRepository().getProject().getKey()).toProjectKey(event.getRepository().getProject().getKey()).refId(change.getNewState().getName()).browseUrl(event.getRepository().getLinks().get(BROWSE_LINK_NAME).get(BROWSE_URL_INDEX).getHref()).webhookPayload(body).configProvider(this).product(product).application(application).build();
    return handler.execute(uid);
}
Also used : MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) BitbucketServerEventHandler(com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler) BitbucketPushEvent(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushEvent) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BitbucketPushChange(com.checkmarx.flow.dto.bitbucketserver.plugin.postwebhook.BitbucketPushChange) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 14 with MachinaRuntimeException

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

the class ServiceNowTracker method closeIssue.

@Override
public void closeIssue(Issue issue, ScanRequest request) throws MachinaException {
    log.info("Executing closeIssue Service Now API call");
    Incident incident = getCloseIncident();
    try {
        String query = String.format("%s%s/%s", properties.getApiUrl(), INCIDENTS, issue.getId());
        log.debug("ServiceNow Close Issues URL: {}", query);
        restOperations.put(query, incident);
    } catch (HttpClientErrorException e) {
        log.error("Error closing issue.Details are:" + e.getMessage());
        throw new MachinaRuntimeException(e);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) MachinaRuntimeException(com.checkmarx.flow.exception.MachinaRuntimeException) Incident(com.checkmarx.flow.dto.servicenow.Incident)

Example 15 with MachinaRuntimeException

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

the class ServiceNowTracker method getIncidentByIDConvertToIssue.

/**
 * Find Incident By Sys ID and convert into Issue
 * @return Issue object.
 */
private Optional<Issue> getIncidentByIDConvertToIssue(String sysId) {
    log.debug("Executing getIncidentByIDConvertToIssue");
    try {
        String apiRequest = String.format("%s%s?sys_id=%s", properties.getApiUrl(), INCIDENTS, sysId);
        Optional<Result> res = Optional.ofNullable(restOperations.getForObject(apiRequest, Result.class));
        if (res.isPresent()) {
            return res.get().getIncidents().stream().map(i -> this.mapToIssue(i)).findFirst();
        }
    } catch (RestClientException e) {
        log.error("Error occurred while fetching ServiceNow Issue");
        log.error(ExceptionUtils.getStackTrace(e));
        throw new MachinaRuntimeException();
    }
    return Optional.empty();
}
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) Result(com.checkmarx.flow.dto.servicenow.Result)

Aggregations

MachinaRuntimeException (com.checkmarx.flow.exception.MachinaRuntimeException)23 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 IOException (java.io.IOException)8 Issue (com.checkmarx.flow.dto.Issue)5 ScanRequest (com.checkmarx.flow.dto.ScanRequest)5 Incident (com.checkmarx.flow.dto.servicenow.Incident)5 BitbucketServerEventHandler (com.checkmarx.flow.handlers.bitbucket.server.BitbucketServerEventHandler)5 BugTracker (com.checkmarx.flow.dto.BugTracker)4 MachinaException (com.checkmarx.flow.exception.MachinaException)4 ScanResults (com.checkmarx.sdk.dto.ScanResults)3 CxConfig (com.checkmarx.sdk.dto.sast.CxConfig)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 URI (java.net.URI)3 List (java.util.List)3 FlowProperties (com.checkmarx.flow.config.FlowProperties)2 ServiceNowProperties (com.checkmarx.flow.config.ServiceNowProperties)2 Result (com.checkmarx.flow.dto.servicenow.Result)2 HTMLHelper (com.checkmarx.flow.utils.HTMLHelper)2 ScanUtils (com.checkmarx.flow.utils.ScanUtils)2