Search in sources :

Example 16 with AdapterMetaData

use of com.mercedesbenz.sechub.adapter.AdapterMetaData in project sechub by mercedes-benz.

the class CheckmarxScanReportSupport method triggerNewReport.

// https://checkmarx.atlassian.net/wiki/spaces/KC/pages/223379587/Register+Scan+Report+-+POST+reports+sastScan
void triggerNewReport(CheckmarxOAuthSupport oauthSupport, CheckmarxAdapterContext context) throws AdapterException {
    oauthSupport.refreshBearerTokenWhenNecessary(context);
    long scanId = context.getScanId();
    String traceId = context.getTraceID();
    AdapterMetaData metaData = context.getRuntimeContext().getMetaData();
    Long reportIdLong = metaData.getValueLong(CheckmarxMetaDataID.KEY_REPORT_ID);
    long reportId = -1;
    if (reportIdLong == null) {
        LOG.info("Trigger new report in queue. Trace Id: {}", traceId);
        Map<String, Object> json = new TreeMap<>();
        json.put("reportType", "XML");
        json.put("scanId", scanId);
        String url = context.getAPIURL("reports/sastScan");
        String jsonAsString = context.json().toJSON(json);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(jsonAsString, headers);
        LOG.debug("Sending request for new report generation for scan Id: {}. Trace Id: {}.", scanId, traceId);
        RestOperations restTemplate = context.getRestOperations();
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        if (!result.getStatusCode().equals(HttpStatus.ACCEPTED)) {
            throw context.asAdapterException("Response HTTP status not as expected: " + result.getStatusCode(), null);
        }
        String body = result.getBody();
        reportId = context.json().fetch("reportId", body).asLong();
        metaData.setValue(CheckmarxMetaDataID.KEY_REPORT_ID, reportId);
        LOG.debug("The report generation request was successful. Received new report Id {} for scan Id {}. Trace Id: {}", reportId, scanId, traceId);
        context.getRuntimeContext().getCallback().persist(metaData);
    } else {
        /* just reuse existing data */
        reportId = reportIdLong.longValue();
        LOG.info("Reuse existing reportId: {} for {}", reportId, traceId);
    }
    context.setReportId(reportId);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) TreeMap(java.util.TreeMap) RestOperations(org.springframework.web.client.RestOperations) AdapterMetaData(com.mercedesbenz.sechub.adapter.AdapterMetaData)

Example 17 with AdapterMetaData

use of com.mercedesbenz.sechub.adapter.AdapterMetaData in project sechub by mercedes-benz.

the class CheckmarxScanSupport method triggerNewEntryInQueue.

// https://checkmarx.atlassian.net/wiki/spaces/KC/pages/814121878/Swagger+Examples+v8.8.0+-+v1
private void triggerNewEntryInQueue(CheckmarxOAuthSupport oauthSupport, CheckmarxContext context) throws AdapterException {
    oauthSupport.refreshBearerTokenWhenNecessary(context);
    AdapterMetaData metaData = context.getRuntimeContext().getMetaData();
    Long scanIdLong = metaData.getValueLong(CheckmarxMetaDataID.KEY_SCAN_ID);
    long scanId = -1;
    if (scanIdLong == null) {
        LOG.info("Trigger new scan entry in checkmarx queue");
        CheckmarxAdapterConfig config = context.getConfig();
        long projectId = context.getSessionData().getProjectId();
        Map<String, Object> json = new TreeMap<>();
        json.put("projectId", projectId);
        json.put("isIncremental", context.isIncrementalScan());
        json.put("isPublic", false);
        json.put("forceScan", false);
        json.put("comment", "sechub job:" + config.getTraceID());
        String url = context.getAPIURL("sast/scans");
        String jsonAsString = context.json().toJSON(json);
        HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Type", "application/json;v=1.0");
        HttpEntity<String> request = new HttpEntity<>(jsonAsString, headers);
        RestOperations restTemplate = context.getRestOperations();
        ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        if (!result.getStatusCode().equals(HttpStatus.CREATED)) {
            throw context.asAdapterException("Response HTTP status not as expected: " + result.getStatusCode(), null);
        }
        String body = result.getBody();
        scanId = context.json().fetch("id", body).asLong();
        metaData.setValue(CheckmarxMetaDataID.KEY_SCAN_ID, scanId);
        context.getRuntimeContext().getCallback().persist(metaData);
    } else {
        /* just reuse existing data */
        scanId = scanIdLong.longValue();
        LOG.info("Reuse existing scanId:{}, for :{}", scanId, context.getTraceID());
    }
    context.getSessionData().setScanId(scanId);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) TreeMap(java.util.TreeMap) CheckmarxAdapterConfig(com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig) RestOperations(org.springframework.web.client.RestOperations) AdapterMetaData(com.mercedesbenz.sechub.adapter.AdapterMetaData)

Example 18 with AdapterMetaData

use of com.mercedesbenz.sechub.adapter.AdapterMetaData in project sechub by mercedes-benz.

the class NetsparkerAdapterV1 method createNewScanAndFetchId.

private void createNewScanAndFetchId(NetsparkerContext context) throws AdapterException {
    NetsparkerAdapterConfig config = context.getConfig();
    String traceID = config.getTraceID();
    AdapterMetaData metaData = context.getRuntimeContext().getMetaData();
    metaData.setValue(NetsparkerMetaDataID.KEY_TARGET_URI, "" + context.getConfig().getTargetURI());
    String jsonAsString = buildJsonForCreateNewScan(context.json(), config);
    LOG.debug("{} request body will contain json:'{}'", traceID, jsonAsString);
    HttpEntity<String> request = new HttpEntity<>(jsonAsString);
    String apiUrl = createAPIURL(APICALL_CREATE_NEW_SCAN, config);
    try {
        LOG.debug("{} calling api url '{}'", traceID, apiUrl);
        ResponseEntity<String> response = context.getRestOperations().postForEntity(apiUrl, request, String.class);
        if (!CREATED.equals(response.getStatusCode())) {
            throw new NetsparkerRESTFailureException(response.getStatusCode(), response.getBody());
        }
        context.setProductContextId(extractIDFromScanResult(response.getBody(), context));
        LOG.debug("{} created new scan and got netsparker ID '{}'", traceID, context.getProductContextId());
    } catch (HttpClientErrorException e) {
        throw new NetsparkerRESTFailureException(e.getStatusCode(), e.getResponseBodyAsString());
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) AdapterMetaData(com.mercedesbenz.sechub.adapter.AdapterMetaData)

Example 19 with AdapterMetaData

use of com.mercedesbenz.sechub.adapter.AdapterMetaData in project sechub by mercedes-benz.

the class CheckmarxAdapterV1 method uploadSourceCodeAndStartScan.

private void uploadSourceCodeAndStartScan(CheckmarxOAuthSupport oauthSupport, CheckmarxContext context) throws AdapterException {
    AdapterMetaData metaData = context.getRuntimeContext().getMetaData();
    if (!metaData.hasValue(CheckmarxMetaDataID.KEY_FILEUPLOAD_DONE, true)) {
        /* upload source code */
        oauthSupport.refreshBearerTokenWhenNecessary(context);
        CheckmarxUploadSupport uploadSupport = new CheckmarxUploadSupport();
        uploadSupport.uploadZippedSourceCode(context);
        /* after this - mark file upload done, so on a restart we don't need this */
        metaData.setValue(CheckmarxMetaDataID.KEY_FILEUPLOAD_DONE, true);
        context.getRuntimeContext().getCallback().persist(metaData);
    } else {
        LOG.info("Reuse existing upload for:{}", context.getTraceID());
    }
    /* start scan */
    CheckmarxScanSupport scanSupport = new CheckmarxScanSupport();
    scanSupport.startNewScan(oauthSupport, context);
}
Also used : CheckmarxScanSupport(com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxScanSupport) CheckmarxUploadSupport(com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxUploadSupport) AdapterMetaData(com.mercedesbenz.sechub.adapter.AdapterMetaData)

Example 20 with AdapterMetaData

use of com.mercedesbenz.sechub.adapter.AdapterMetaData in project sechub by mercedes-benz.

the class ProductExecutorContextTest method useFirstFormerResultHavingMetaData_finds_product_by_metadata.

@Test
public void useFirstFormerResultHavingMetaData_finds_product_by_metadata() {
    /* prepare */
    ProductResult result1 = new ProductResult();
    ProductResult result2 = new ProductResult();
    ProductResult result3 = new ProductResult();
    formerResults.add(result1);
    formerResults.add(result2);
    formerResults.add(null);
    formerResults.add(result3);
    AdapterMetaData metaData1 = new AdapterMetaData();
    metaData1.setValue("test.key", "xyz");
    AdapterMetaData metaData2 = new AdapterMetaData();
    metaData2.setValue("test.key", "abc");
    result1.setMetaData(converter.convertToJSONOrNull(metaData1));
    result2.setMetaData(null);
    result3.setMetaData(converter.convertToJSONOrNull(metaData2));
    contextToTest.useFirstFormerResult();
    // by constructor...
    verify(callback, times(1)).setCurrentProductResult(result3);
    /* execute */
    contextToTest.useFirstFormerResultHavingMetaData("test.key", "abc");
    /* test */
    // by last call
    verify(callback, times(2)).setCurrentProductResult(result3);
}
Also used : AdapterMetaData(com.mercedesbenz.sechub.adapter.AdapterMetaData) Test(org.junit.Test)

Aggregations

AdapterMetaData (com.mercedesbenz.sechub.adapter.AdapterMetaData)26 Test (org.junit.Test)10 UUID (java.util.UUID)8 File (java.io.File)5 AssertFullScanData (com.mercedesbenz.sechub.integrationtest.api.AssertFullScanData)4 CheckmarxAdapterConfig (com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig)3 ProductResult (com.mercedesbenz.sechub.domain.scan.product.ProductResult)3 ExecutionResult (com.mercedesbenz.sechub.integrationtest.internal.SecHubClientExecutor.ExecutionResult)3 MetaDataInspection (com.mercedesbenz.sechub.sharedkernel.metadata.MetaDataInspection)3 JobStorage (com.mercedesbenz.sechub.storage.core.JobStorage)3 InputStream (java.io.InputStream)3 HttpEntity (org.springframework.http.HttpEntity)3 AdapterMetaDataCallback (com.mercedesbenz.sechub.adapter.AdapterMetaDataCallback)2 TreeMap (java.util.TreeMap)2 HttpHeaders (org.springframework.http.HttpHeaders)2 RestOperations (org.springframework.web.client.RestOperations)2 SecHubTimeUnitData (com.mercedesbenz.sechub.adapter.SecHubTimeUnitData)1 CheckmarxScanSupport (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxScanSupport)1 CheckmarxUploadSupport (com.mercedesbenz.sechub.adapter.checkmarx.support.CheckmarxUploadSupport)1 NetsparkerConfigBuilder (com.mercedesbenz.sechub.adapter.netsparker.NetsparkerConfig.NetsparkerConfigBuilder)1