use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.
the class AzureBoardsCustomFunctionAction method createActionResponse.
@Override
public ActionResponse<OAuthEndpointResponse> createActionResponse(FieldModel fieldModel, HttpServletContentWrapper servletContentWrapper) {
try {
Optional<FieldModel> savedFieldModel = saveIfValid(fieldModel);
if (!savedFieldModel.isPresent()) {
return createErrorResponse("The configuration is invalid. Please test the configuration.");
}
FieldUtility fieldUtility = createFieldAccessor(savedFieldModel.get());
Optional<String> clientId = fieldUtility.getString(AzureBoardsDescriptor.KEY_CLIENT_ID);
if (!clientId.isPresent()) {
return createErrorResponse("App ID not found.");
}
Optional<String> alertServerUrl = alertWebServerUrlManager.getServerUrl();
if (!alertServerUrl.isPresent()) {
return createErrorResponse("Could not determine the alert server url for the callback.");
}
String requestKey = oAuthRequestValidator.generateRequestKey();
// since we have only one OAuth channel now remove any other requests.
// if we have more OAuth clients then the "remove requests" will have to be removed from here.
// beginning authentication process create the request id at the start.
oAuthRequestValidator.removeRequestsOlderThan5MinutesAgo();
oAuthRequestValidator.addAuthorizationRequest(requestKey);
logger.info("OAuth authorization request created: {}", requestKey);
String authUrl = createAuthURL(clientId.get(), requestKey);
logger.debug("Authenticating Azure OAuth URL: {}", authUrl);
return new ActionResponse<>(HttpStatus.OK, new OAuthEndpointResponse(isAuthenticated(fieldUtility), authUrl, "Authenticating..."));
} catch (Exception ex) {
logger.error("Error activating Azure Boards", ex);
return createErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "Error activating azure oauth.");
}
}
use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.
the class AzureOAuthCallbackController method oauthCallback.
@GetMapping
public ResponseEntity<String> oauthCallback(HttpServletRequest request) {
logger.debug("Azure OAuth callback method called");
if (!authorizationManager.hasExecutePermission(ConfigContextEnum.GLOBAL.name(), ChannelKeys.AZURE_BOARDS.getUniversalKey())) {
logger.debug("Azure OAuth callback user does not have permission to call the controller.");
return responseFactory.createForbiddenResponse();
}
String state = request.getParameter("state");
String oAuthRequestId = oAuthRequestValidator.parseRequestIdString(state);
try {
String requestURI = request.getRequestURI();
String requestQueryString = request.getQueryString();
logger.debug("Request URI {}?{}", requestURI, requestQueryString);
String authorizationCode = request.getParameter("code");
if (!oAuthRequestValidator.hasRequestKey(state)) {
logger.info("OAuth request with id {}: not found.", oAuthRequestId);
} else {
logger.info("OAuth request with id {}: Processing...", oAuthRequestId);
oAuthRequestValidator.removeAuthorizationRequest(state);
FieldUtility fieldUtility = createFieldAccessor();
if (fieldUtility.getFields().isEmpty()) {
logger.error("OAuth request with id {}: Azure oauth callback: Channel global configuration missing", oAuthRequestId);
} else {
if (StringUtils.isBlank(authorizationCode)) {
logger.error("OAuth request with id {}: Azure oauth callback: Authorization code isn't valid. Stop processing", oAuthRequestId);
} else {
String oAuthRedirectUri = azureRedirectUrlCreator.createOAuthRedirectUri();
AzureBoardsProperties properties = AzureBoardsProperties.fromFieldAccessor(azureBoardsCredentialDataStoreFactory, oAuthRedirectUri, fieldUtility);
testOAuthConnection(properties, authorizationCode, oAuthRequestId);
}
}
}
} catch (Exception ex) {
// catch any exceptions so the redirect back to the UI happens and doesn't display the URL with the authorization code to the user.
logger.error("OAuth request with id {}: Azure OAuth callback error occurred", oAuthRequestId, ex);
}
// redirect back to the global channel configuration URL in the Alert UI.
return responseFactory.createFoundRedirectResponse(azureRedirectUrlCreator.createUIRedirectLocation());
}
use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.
the class JobConfigActions method testWithoutChecks.
@Override
protected ValidationActionResponse testWithoutChecks(JobFieldModel resource) {
ValidationResponseModel responseModel;
String jobIdString = resource.getJobId();
UUID jobId = Optional.ofNullable(jobIdString).filter(StringUtils::isNotBlank).map(UUID::fromString).orElse(null);
try {
Collection<FieldModel> otherJobModels = new LinkedList<>();
FieldModel channelFieldModel = getChannelFieldModelAndPopulateOtherJobModels(resource, otherJobModels);
if (null != channelFieldModel) {
String channelDescriptorName = channelFieldModel.getDescriptorName();
Map<String, ConfigurationFieldModel> fields = createFieldsMap(channelFieldModel, otherJobModels);
// The custom message fields are not written to the database or defined fields in the database. Need to manually add them.
// TODO Create a mechanism to create the field accessor with a combination of fields in the database and fields that are not.
Optional<ConfigurationFieldModel> topicField = convertFieldToConfigurationField(channelFieldModel, FieldModelTestAction.KEY_CUSTOM_TOPIC);
Optional<ConfigurationFieldModel> messageField = convertFieldToConfigurationField(channelFieldModel, FieldModelTestAction.KEY_CUSTOM_MESSAGE);
topicField.ifPresent(model -> fields.put(FieldModelTestAction.KEY_CUSTOM_TOPIC, model));
messageField.ifPresent(model -> fields.put(FieldModelTestAction.KEY_CUSTOM_MESSAGE, model));
MessageResult providerTestResult = testProviderConfig(new FieldUtility(fields), jobIdString, channelFieldModel);
if (providerTestResult.hasErrors()) {
responseModel = ValidationResponseModel.fromStatusCollection(providerTestResult.getStatusMessage(), providerTestResult.getFieldStatuses());
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
List<BlackDuckProjectDetailsModel> projectFilterDetails = Optional.ofNullable(resource.getConfiguredProviderProjects()).orElse(List.of()).stream().map(jobProject -> new BlackDuckProjectDetailsModel(jobProject.getName(), jobProject.getHref())).collect(Collectors.toList());
DistributionJobModel testJobModel = distributionJobModelExtractor.convertToJobModel(jobId, fields, DateUtils.createCurrentDateTimestamp(), null, projectFilterDetails);
DistributionChannelTestAction distributionChannelTestAction = channelTestActionMap.findRequiredAction(channelDescriptorName);
MessageResult testActionResult = distributionChannelTestAction.testConfig(testJobModel, testJobModel.getName(), topicField.flatMap(ConfigurationFieldModel::getFieldValue).orElse(null), messageField.flatMap(ConfigurationFieldModel::getFieldValue).orElse(null));
List<AlertFieldStatus> resultFieldStatuses = testActionResult.getFieldStatuses();
List<AlertFieldStatus> allStatuses = Stream.concat(resultFieldStatuses.stream(), providerTestResult.fieldWarnings().stream()).collect(Collectors.toList());
responseModel = ValidationResponseModel.fromStatusCollection(testActionResult.getStatusMessage(), allStatuses);
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
responseModel = ValidationResponseModel.generalError("No field model of type channel was was sent to test.");
return new ValidationActionResponse(HttpStatus.BAD_REQUEST, responseModel);
} catch (IntegrationRestException e) {
logger.error(e.getMessage(), e);
return ValidationActionResponse.createResponseFromIntegrationRestException(e);
} catch (AlertFieldException e) {
logger.error("Test Error with field Errors", e);
responseModel = ValidationResponseModel.fromStatusCollection(e.getMessage(), e.getFieldErrors());
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (IntegrationException e) {
// TODO this is not necessarily a PKIX
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (Exception e) {
logger.error(e.getMessage(), e);
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
}
use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.
the class ConfigActions method testWithoutChecks.
@Override
protected ValidationActionResponse testWithoutChecks(FieldModel resource) {
Optional<FieldModelTestAction> testActionOptional = descriptorProcessor.retrieveTestAction(resource);
ValidationResponseModel responseModel;
if (testActionOptional.isPresent()) {
try {
FieldModel upToDateFieldModel = fieldModelProcessor.createCustomMessageFieldModel(resource);
FieldUtility fieldUtility = modelConverter.convertToFieldAccessor(upToDateFieldModel);
FieldModelTestAction fieldModelTestAction = testActionOptional.get();
// TODO return the message from the result of testAction.testConfig(...)
fieldModelTestAction.testConfig(upToDateFieldModel.getId(), upToDateFieldModel, fieldUtility);
responseModel = ValidationResponseModel.success("Successfully sent test message.");
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (IntegrationRestException e) {
logger.error(e.getMessage(), e);
return ValidationActionResponse.createResponseFromIntegrationRestException(e);
} catch (AlertFieldException e) {
logger.error("Test Error with field Errors", e);
responseModel = ValidationResponseModel.fromStatusCollection(e.getMessage(), e.getFieldErrors());
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (IntegrationException e) {
// FIXME there are definitely other possibilities than this
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
} catch (Exception e) {
logger.error(e.getMessage(), e);
responseModel = pkixErrorResponseFactory.createSSLExceptionResponse(e).orElse(ValidationResponseModel.generalError(e.getMessage()));
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
}
String descriptorName = resource.getDescriptorName();
responseModel = ValidationResponseModel.generalError("Test functionality not implemented for " + descriptorName);
return new ValidationActionResponse(HttpStatus.NOT_IMPLEMENTED, responseModel);
}
use of com.synopsys.integration.alert.common.persistence.accessor.FieldUtility in project hub-alert by blackducksoftware.
the class PolicyNotificationFilterCustomFunctionAction method createBlackDuckProperties.
private Optional<BlackDuckProperties> createBlackDuckProperties(FieldModel fieldModel) {
FieldUtility fieldUtility = fieldModelConverter.convertToFieldAccessor(fieldModel);
Long providerConfigId = fieldUtility.getLong(ProviderDescriptor.KEY_PROVIDER_CONFIG_ID).orElse(null);
if (null == providerConfigId) {
return Optional.empty();
}
return configurationModelConfigurationAccessor.getConfigurationById(providerConfigId).map(blackDuckPropertiesFactory::createProperties);
}
Aggregations