use of com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig in project sechub by mercedes-benz.
the class CheckmarxProjectSupport method createProject.
private CheckmarxSessionData createProject(CheckmarxContext context) throws AdapterException {
CheckmarxAdapterConfig config = context.getConfig();
String projectName = config.getProjectId();
String teamId = config.getTeamIdForNewProjects();
Map<String, String> json = new TreeMap<>();
json.put("name", projectName);
json.put("owningTeam", teamId);
json.put("isPublic", "false");
String url = context.getAPIURL("projects");
String jsonAsString = context.json().toJSON(json);
RestOperations restTemplate = context.getRestOperations();
// https://checkmarx.atlassian.net/wiki/spaces/KC/pages/222265747/Create+Project+with+Default+Configuration+-+POST+projects
// https://checkmarx.atlassian.net/wiki/spaces/KC/pages/814285654/Swagger+Examples+v8.8.0+-+v2
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.set("Content-Type", "application/json;v=2.0");
HttpEntity<String> request = new HttpEntity<>(jsonAsString, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
CheckmarxSessionData sessionData = extractProjectFromJsonWithProjectCreationData(projectName, context.json(), response.getBody());
updatePresetIdAndEngineConfigurationIfNecessary(context, sessionData);
return sessionData;
}
use of com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig in project sechub by mercedes-benz.
the class CheckmarxProjectSupport method ensureProjectExists.
public void ensureProjectExists(CheckmarxContext context) throws AdapterException {
CheckmarxAdapterConfig config = context.getConfig();
String projectName = config.getProjectId();
String teamId = config.getTeamIdForNewProjects();
Map<String, String> map = new LinkedHashMap<>();
map.put("projectName", projectName);
map.put("teamId", teamId);
String url = context.getAPIURL("projects", map);
RestOperations restTemplate = context.getRestOperations();
// CxRestAPI/projects?projectName=myProject&teamId=00000000-1111-1111-b111-989c9070eb11
try {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
context.setSessionData(extractFirstProjectFromJsonWithProjectArray(context.json(), response.getBody()));
context.setNewProject(false);
return;
} catch (HttpStatusCodeException e) {
if (e.getRawStatusCode() != 404) {
/* only 404 - not found is accepted */
throw context.asAdapterException("Unexpected HTTP status error", e);
}
}
/* 404 error - okay, lets create */
context.setSessionData(createProject(context));
context.setNewProject(true);
}
use of com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig 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);
}
use of com.mercedesbenz.sechub.adapter.checkmarx.CheckmarxAdapterConfig in project sechub by mercedes-benz.
the class CheckmarxUploadSupport method uploadZippedSourceCode.
// https://checkmarx.atlassian.net/wiki/spaces/KC/pages/223313947/Upload+Source+Code+Zip+File+-+POST+projects+id+sourceCode+attachments
// POST /projects/{id}/sourceCode/attachments and upload the zipped source code
// https://www.baeldung.com/spring-rest-template-multipart-upload
public void uploadZippedSourceCode(CheckmarxContext context) throws AdapterException {
CheckmarxAdapterConfig config = context.getConfig();
Resource sourceCodeFile = fetchResource(context, config);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("zippedSource", sourceCodeFile);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
String url = context.getAPIURL("projects/" + context.getSessionData().getProjectId() + "/sourceCode/attachments");
RestOperations restTemplate = context.getRestOperations();
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
if (!result.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
throw context.asAdapterException("Response HTTP status not as expected: " + result.getStatusCode());
}
}
Aggregations