Search in sources :

Example 6 with SecHubExecutionException

use of com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException in project sechub by mercedes-benz.

the class CreateScanReportService method createReport.

/**
 * Creates a report based on product results. There is no security check because
 * its only called internally from system.
 *
 * @param context
 * @return report, never <code>null</code>
 * @throws ScanReportException
 */
public ScanReport createReport(SecHubExecutionContext context) throws ScanReportException {
    notNull(context, "Context may not be null!");
    UUID sechubJobUUID = context.getSechubJobUUID();
    if (sechubJobUUID == null) {
        throw new ScanReportException("Cannot create a report for Job UUID:null");
    }
    LOG.info("Creating report for {}, will delete former reports if existing", traceLogID(sechubJobUUID));
    /* we allow only one report for one job */
    scanReportTransactionService.deleteAllReportsForSecHubJobUUIDinOwnTransaction(sechubJobUUID);
    /*
         * create report - project id in configuration was set on job creation time and
         * is always correct/valid and will differ between api parameter and config..!
         */
    ScanReport scanReport = new ScanReport(sechubJobUUID, context.getConfiguration().getProjectId());
    scanReport.setStarted(LocalDateTime.now());
    /* execute report products */
    try {
        reportProductExecutionService.executeProductsAndStoreResults(context);
    } catch (SecHubExecutionException e) {
        throw new ScanReportException("Report product execution failed", e);
    }
    /* transform */
    ReportTransformationResult reportTransformerResult;
    try {
        reportTransformerResult = reportTransformerService.createResult(context);
        scanReport.setResultType(ScanReportResultType.MODEL);
        scanReport.setResult(reportTransformerResult.toJSON());
    } catch (Exception e) {
        throw new ScanReportException("Was not able to build sechub result", e);
    }
    /* create and set the traffic light */
    TrafficLight trafficLight = trafficLightCalculator.calculateTrafficLight(reportTransformerResult);
    scanReport.setTrafficLight(trafficLight);
    /* update time stamp */
    scanReport.setEnded(LocalDateTime.now());
    /* persist */
    return reportRepository.save(scanReport);
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) ReportTransformationResult(com.mercedesbenz.sechub.domain.scan.ReportTransformationResult) TrafficLight(com.mercedesbenz.sechub.commons.model.TrafficLight) UUID(java.util.UUID) SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException)

Example 7 with SecHubExecutionException

use of com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException in project sechub by mercedes-benz.

the class CheckmarxBadRequestConsultantTest method nested_http_bad_request_400_exception_wrapped_in_runtime_and_sechubexecution_exception_returns_retry_proposal_with_3_retries_and_2000_millis_to_wait.

@Test
public void nested_http_bad_request_400_exception_wrapped_in_runtime_and_sechubexecution_exception_returns_retry_proposal_with_3_retries_and_2000_millis_to_wait() {
    /* prepare */
    when(context.getCurrentError()).thenReturn(new SecHubExecutionException("se1", new RuntimeException(new HttpClientErrorException(HttpStatus.BAD_REQUEST))));
    /* execute */
    ResilienceProposal proposal = consultantToTest.consultFor(context);
    /* test */
    assertNotNull(proposal);
    assertTrue(proposal instanceof RetryResilienceProposal);
    RetryResilienceProposal rrp = (RetryResilienceProposal) proposal;
    assertEquals(3, rrp.getMaximumAmountOfRetries());
    assertEquals(2000, rrp.getMillisecondsToWaitBeforeRetry());
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) RetryResilienceProposal(com.mercedesbenz.sechub.sharedkernel.resilience.RetryResilienceProposal) ResilienceProposal(com.mercedesbenz.sechub.sharedkernel.resilience.ResilienceProposal) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) RetryResilienceProposal(com.mercedesbenz.sechub.sharedkernel.resilience.RetryResilienceProposal) Test(org.junit.Test)

Example 8 with SecHubExecutionException

use of com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException in project sechub by mercedes-benz.

the class AbstractProductExecutionServiceTest method sechub_execution_error_on_execution_shall_not_break_the_build_but_safe_fallbackresult.

@Test
public void sechub_execution_error_on_execution_shall_not_break_the_build_but_safe_fallbackresult() throws Exception {
    /* prepare */
    ArgumentCaptor<ProductResult> productResultCaptor = ArgumentCaptor.forClass(ProductResult.class);
    SecHubExecutionException exception = new SecHubExecutionException("an-error occurred on execution, but this should not break at all!");
    when(executor.execute(context, productExecutorContext)).thenThrow(exception);
    /* execute */
    serviceToTest.runOnAllAvailableExecutors(executors, context, traceLogID);
    /* test */
    verify(productResultRepository).findProductResults(sechubJobUUID, config1);
    verify(productExecutorContext).persist(productResultCaptor.capture());
    ProductResult captured = productResultCaptor.getValue();
    assertEquals(USED_PRODUCT_IDENTIFIER, captured.getProductIdentifier());
    assertEquals("", captured.getResult());
    ArgumentCaptor<String> stringCaptor = ArgumentCaptor.forClass(String.class);
    verify(logger).error(stringCaptor.capture(), eq(USED_PRODUCT_IDENTIFIER), eq(traceLogID), eq(exception));
    assertTrue(stringCaptor.getValue().startsWith("Product executor failed"));
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) Test(org.junit.Test)

Example 9 with SecHubExecutionException

use of com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException in project sechub by mercedes-benz.

the class ScanJobExecutor method execute.

public void execute() throws SecHubExecutionException {
    String jobUUID = context.getTraceLogId().getPlainId();
    CanceableScanJobRunnable canceableJobRunner = new CanceableScanJobRunnable(jobUUID);
    Thread canceableJobThread = new Thread(canceableJobRunner, "SecHub-exec-" + jobUUID + "-" + progress.getId());
    canceableJobRunner.executorThread = canceableJobThread;
    UUID sechubJobUUID = context.getSechubJobUUID();
    try {
        canceableJobThread.start();
        this.scanService.scanJobListener.started(sechubJobUUID, canceableJobRunner);
        /* wait for job runnable - except when canceled */
        while (canceableJobThread.isAlive()) {
            try {
                LOG.debug("will wait max {} milliseconds before cancel checks - job thread is:{}", millisecondsToWaitBeforeCancelCheck, canceableJobThread.getName());
                /* we simply join scan thread until we do next cancel check */
                canceableJobThread.join(millisecondsToWaitBeforeCancelCheck);
                if (progress.isCanceled()) {
                    handleCanceled(canceableJobRunner, sechubJobUUID);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        SecHubExecutionException exception = canceableJobRunner.exception;
        handleErrors(exception);
    } catch (Exception e) {
        /* should never happen, because all handled by runnable, but... */
        handleErrors(new SecHubExecutionException("Scan failed - but not handled by runnable.", e));
    } finally {
        this.scanService.scanJobListener.ended(sechubJobUUID);
    }
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) UUID(java.util.UUID) SecHubExecutionAbandonedException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionAbandonedException) SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException)

Example 10 with SecHubExecutionException

use of com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException in project sechub by mercedes-benz.

the class ScanService method startScan.

@IsSendingSyncMessageAnswer(value = MessageID.SCAN_DONE, answeringTo = MessageID.START_SCAN, branchName = "success")
@IsSendingSyncMessageAnswer(value = MessageID.SCAN_FAILED, answeringTo = MessageID.START_SCAN, branchName = "failure")
@IsSendingSyncMessageAnswer(value = MessageID.SCAN_ABANDONDED, answeringTo = MessageID.START_SCAN, branchName = "failure")
DomainMessageSynchronousResult startScan(DomainMessage request) {
    SecHubExecutionContext context = null;
    try {
        context = createExecutionContext(request);
        executeScan(context, request);
        ScanReport report = reportService.createReport(context);
        DomainMessageSynchronousResult response = new DomainMessageSynchronousResult(MessageID.SCAN_DONE);
        response.set(REPORT_TRAFFIC_LIGHT, report.getTrafficLightAsString());
        return response;
    } catch (ScanReportException e) {
        LOG.error("Execution was possible, but report failed." + traceLogID(request), e);
        return new DomainMessageSynchronousResult(MessageID.SCAN_FAILED, e);
    } catch (SecHubExecutionAbandonedException e) {
        LOG.info("Execution abandoned on scan {} - message: {}", traceLogID(request), e.getMessage());
        return new DomainMessageSynchronousResult(MessageID.SCAN_ABANDONDED, e);
    } catch (SecHubExecutionException e) {
        LOG.error("Execution problems on scan." + traceLogID(request), e);
        return new DomainMessageSynchronousResult(MessageID.SCAN_FAILED, e);
    } catch (Exception e) {
        LOG.error("Was not able to start scan." + traceLogID(request), e);
        return new DomainMessageSynchronousResult(MessageID.SCAN_FAILED, e);
    } finally {
        if (context == null) {
            LOG.warn("No sechub execution context available, so cannot check state or cleanup storage");
        } else {
            if (!context.isAbandonded()) {
                cleanupStorage(context);
            }
        }
    }
}
Also used : SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) DomainMessageSynchronousResult(com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageSynchronousResult) SecHubExecutionAbandonedException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionAbandonedException) SecHubExecutionContext(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionContext) ScanReport(com.mercedesbenz.sechub.domain.scan.report.ScanReport) ScanReportException(com.mercedesbenz.sechub.domain.scan.report.ScanReportException) JSONConverterException(com.mercedesbenz.sechub.commons.model.JSONConverterException) ScanReportException(com.mercedesbenz.sechub.domain.scan.report.ScanReportException) SecHubExecutionAbandonedException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionAbandonedException) IOException(java.io.IOException) SecHubExecutionException(com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException) IsSendingSyncMessageAnswer(com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingSyncMessageAnswer)

Aggregations

SecHubExecutionException (com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionException)14 Test (org.junit.Test)6 SecHubExecutionAbandonedException (com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionAbandonedException)3 DomainMessageSynchronousResult (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageSynchronousResult)3 UUID (java.util.UUID)3 JSONConverterException (com.mercedesbenz.sechub.commons.model.JSONConverterException)2 ProductResult (com.mercedesbenz.sechub.domain.scan.product.ProductResult)2 ScanReportException (com.mercedesbenz.sechub.domain.scan.report.ScanReportException)2 UUIDTraceLogID (com.mercedesbenz.sechub.sharedkernel.UUIDTraceLogID)2 SecHubConfiguration (com.mercedesbenz.sechub.sharedkernel.configuration.SecHubConfiguration)2 SecHubExecutionContext (com.mercedesbenz.sechub.sharedkernel.execution.SecHubExecutionContext)2 DomainMessage (com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage)2 IOException (java.io.IOException)2 AdapterException (com.mercedesbenz.sechub.adapter.AdapterException)1 AdapterLogId (com.mercedesbenz.sechub.adapter.AdapterLogId)1 TrafficLight (com.mercedesbenz.sechub.commons.model.TrafficLight)1 NetworkTargetInfo (com.mercedesbenz.sechub.domain.scan.NetworkTargetRegistry.NetworkTargetInfo)1 ReportTransformationResult (com.mercedesbenz.sechub.domain.scan.ReportTransformationResult)1 TargetRegistry (com.mercedesbenz.sechub.domain.scan.TargetRegistry)1 ProductExecutorCallback (com.mercedesbenz.sechub.domain.scan.product.ProductExecutorCallback)1