use of com.checkmarx.sdk.ShardManager.ShardSession in project cx-flow by checkmarx-ltd.
the class ScanRequestConverter method setShardPropertiesIfExists.
public void setShardPropertiesIfExists(ScanRequest request, String fullTeamName) {
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
shard.setTeam(fullTeamName);
shard.setProject(request.getProject());
}
}
use of com.checkmarx.sdk.ShardManager.ShardSession in project cx-flow by checkmarx-ltd.
the class GitHubService method startBlockMerge.
public void startBlockMerge(ScanRequest request, String url) {
if (properties.isBlockMerge()) {
final String PULL_REQUEST_STATUS = "pending";
// When Shard Manager is enabled overide the PULL url to link to the correct shard.
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
try {
String teamId = cxService.getTeamId(request.getTeam());
List<CxProject> projects = cxService.getProjects(teamId);
String projectID = "0";
// String projName = request.getRepoName() + "-" + request.getBranch();
for (CxProject project : projects) {
if (project.getName().equals(request.getProject())) {
projectID = project.getId().toString();
}
}
url = shard.getUrl() + "/cxwebclient/portal#/projectState/" + projectID + "/Summary";
} catch (CheckmarxException e) {
log.error(URL_INVALID);
}
}
HttpEntity<?> httpEntity = new HttpEntity<>(getJSONStatus(PULL_REQUEST_STATUS, url, "Checkmarx Scan Initiated").toString(), createAuthHeaders(request));
String statusApiUrl = request.getAdditionalMetadata(STATUSES_URL_KEY);
if (ScanUtils.empty(statusApiUrl)) {
log.error(STATUSES_URL_NOT_PROVIDED);
return;
}
log.debug("Setting pull request status to '{}': {}", PULL_REQUEST_STATUS, statusApiUrl);
String logErrorMessage = String.format("failed to set pull request status to %s", PULL_REQUEST_STATUS);
statusExchange(request, httpEntity, statusApiUrl, logErrorMessage);
}
}
use of com.checkmarx.sdk.ShardManager.ShardSession in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.
the class CxLegacyService method getWSCallback.
private WebServiceMessageCallback getWSCallback(String callbackUri, String token) {
String curToken;
if (properties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
curToken = shard.getSoapToken();
} else {
curToken = token;
}
return message -> {
SoapMessage soapMessage = (SoapMessage) message;
soapMessage.setSoapAction(callbackUri);
TransportContext context = TransportContextHolder.getTransportContext();
HttpUrlConnection connection = (HttpUrlConnection) context.getConnection();
try {
if (!ScanUtils.empty(curToken) && properties.getVersion() >= 9.0) {
connection.addRequestHeader(HttpHeaders.AUTHORIZATION, "Bearer ".concat(token));
}
} catch (IOException e) {
log.warn("Problem adding SOAP WS http header: {}", ExceptionUtils.getStackTrace(e));
}
};
}
use of com.checkmarx.sdk.ShardManager.ShardSession in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.
the class CxAuthService method getSoapAuthToken.
/**
* Get Auth Token specific to SOAP API Calls
*/
@Override
public String getSoapAuthToken(String username, String password) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
username = shard.getUsername();
password = shard.getPassword();
}
map.add("username", username);
map.add("password", password);
map.add("grant_type", "password");
map.add("scope", cxProperties.getSoapScope());
map.add("client_id", cxProperties.getSoapClientId());
if (!ScanUtils.empty(cxProperties.getSoapClientSecret())) {
map.add("client_secret", cxProperties.getSoapClientSecret());
}
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map, headers);
try {
// get the access token
log.info("Logging into Checkmarx for SOAP token {}", cxProperties.getUrl().concat(LOGIN));
CxAuthResponse response = restTemplate.postForObject(cxProperties.getUrl().concat(LOGIN), requestEntity, CxAuthResponse.class);
if (response == null) {
throw new InvalidCredentialsException();
}
soapToken = response.getAccessToken();
// expire 500 seconds early
soapTokenExpires = LocalDateTime.now().plusSeconds(response.getExpiresIn() - 500);
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
shard.setSoapToken(soapToken);
shard.setSoapTokenExpires(soapTokenExpires);
}
} catch (NullPointerException | HttpStatusCodeException e) {
log.error("Error occurred white obtaining Access Token. Possibly incorrect credentials");
log.error(ExceptionUtils.getStackTrace(e));
throw new InvalidCredentialsException();
}
return soapToken;
}
use of com.checkmarx.sdk.ShardManager.ShardSession in project checkmarx-spring-boot-java-sdk by checkmarx-ltd.
the class CxAuthService method isTokenExpired.
private boolean isTokenExpired() {
//
// / If sharding enabled then use Shards token
//
LocalDateTime curTokenExpires = tokenExpires;
if (cxProperties.getEnableShardManager()) {
ShardSession shard = sessionTracker.getShardSession();
curTokenExpires = shard.getTokenExpires();
}
if (curTokenExpires == null) {
return true;
}
return LocalDateTime.now().isAfter(curTokenExpires);
}
Aggregations