use of com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus in project hub-alert by blackducksoftware.
the class ValidationResponseModel method fromStatusCollection.
public static ValidationResponseModel fromStatusCollection(String message, Collection<AlertFieldStatus> fieldStatuses) {
Map<String, AlertFieldStatus> fieldNameToStatus = new HashMap<>();
for (AlertFieldStatus fieldStatus : fieldStatuses) {
String fieldName = fieldStatus.getFieldName();
AlertFieldStatus existingStatus = fieldNameToStatus.get(fieldName);
if (null != existingStatus) {
if (existingStatus.getSeverity().equals(fieldStatus.getSeverity())) {
String combinedMessage = String.format("%s, %s", existingStatus.getFieldMessage(), fieldStatus.getFieldMessage());
fieldNameToStatus.put(fieldName, new AlertFieldStatus(fieldName, fieldStatus.getSeverity(), combinedMessage));
} else if (FieldStatusSeverity.WARNING.equals(fieldStatus.getSeverity())) {
continue;
}
}
fieldNameToStatus.put(fieldName, fieldStatus);
}
return new ValidationResponseModel(message, fieldNameToStatus);
}
use of com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus in project hub-alert by blackducksoftware.
the class AuthenticationFieldModelTestAction method performLdapTest.
private void performLdapTest(FieldModel fieldModel, FieldUtility registeredFieldValues) throws IntegrationException {
logger.info("LDAP enabled testing LDAP authentication.");
String userName = fieldModel.getFieldValue(AuthenticationDescriptor.TEST_FIELD_KEY_USERNAME).orElse("");
Optional<LdapAuthenticationProvider> ldapProvider = ldapManager.createAuthProvider(registeredFieldValues);
String errorMessage = String.format("Ldap Authentication test failed for the test user %s. Please check the LDAP configuration.", userName);
List<AlertFieldStatus> errors = new ArrayList<>();
if (!ldapProvider.isPresent()) {
errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_LDAP_ENABLED, errorMessage));
} else {
try {
Authentication pendingAuthentication = new UsernamePasswordAuthenticationToken(userName, fieldModel.getFieldValue(AuthenticationDescriptor.TEST_FIELD_KEY_PASSWORD).orElse(""));
Authentication authentication = ldapProvider.get().authenticate(pendingAuthentication);
if (!authentication.isAuthenticated()) {
errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_LDAP_ENABLED, errorMessage));
}
authentication.setAuthenticated(false);
} catch (Exception ex) {
logger.error("Exception occurred testing LDAP authentication", ex);
String exceptionMessage = ex.getMessage();
if (StringUtils.isNotBlank(exceptionMessage)) {
errorMessage = String.format("%s Additional details: %s", errorMessage, exceptionMessage);
}
errors.add(AlertFieldStatus.error(AuthenticationDescriptor.KEY_LDAP_ENABLED, errorMessage));
}
}
if (!errors.isEmpty()) {
throw new AlertFieldException(errors);
}
}
use of com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus in project hub-alert by blackducksoftware.
the class CertificateActions method validateCertificateFields.
private List<AlertFieldStatus> validateCertificateFields(CertificateModel certificateModel) {
CustomCertificateModel convertedModel = convertToDatabaseModel(certificateModel);
List<AlertFieldStatus> fieldErrors = new ArrayList<>();
if (StringUtils.isBlank(certificateModel.getAlias())) {
fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, "Alias cannot be empty."));
} else {
List<CustomCertificateModel> duplicateCertificates = certificateAccessor.getCertificates().stream().filter(certificate -> certificate.getAlias().equals(certificateModel.getAlias())).collect(Collectors.toList());
if (duplicateCertificates.size() > 1) {
fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
} else if (duplicateCertificates.size() == 1) {
boolean sameConfig = convertedModel.getNullableId() != null && duplicateCertificates.get(0).getNullableId().equals(convertedModel.getNullableId());
if (!sameConfig) {
fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
}
}
}
if (StringUtils.isBlank(certificateModel.getCertificateContent())) {
fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, "Certificate content cannot be empty."));
} else {
try {
trustStoreService.validateCertificateContent(convertedModel);
} catch (AlertException ex) {
logger.error(ex.getMessage(), ex);
fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, String.format("Certificate content not valid: %s", ex.getMessage())));
}
}
return fieldErrors;
}
use of com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus in project hub-alert by blackducksoftware.
the class CertificateActions method validateWithoutChecks.
@Override
protected ValidationActionResponse validateWithoutChecks(CertificateModel resource) {
ValidationResponseModel responseModel;
if (StringUtils.isNotBlank(resource.getId()) && !NumberUtils.isCreatable(resource.getId())) {
responseModel = ValidationResponseModel.generalError("Invalid resource id");
return new ValidationActionResponse(HttpStatus.BAD_REQUEST, responseModel);
}
List<AlertFieldStatus> fieldErrors = validateCertificateFields(resource);
if (fieldErrors.isEmpty()) {
responseModel = ValidationResponseModel.success("The certificate configuration is valid");
return new ValidationActionResponse(HttpStatus.OK, responseModel);
}
responseModel = ValidationResponseModel.fromStatusCollection("There were problems with the certificate configuration", fieldErrors);
return new ValidationActionResponse(HttpStatus.BAD_REQUEST, responseModel);
}
use of com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus in project hub-alert by blackducksoftware.
the class AlertFieldStatusTest method getFieldNameTest.
@Test
public void getFieldNameTest() {
AlertFieldStatus alertFieldStatusError = AlertFieldStatus.error(FIELD_NAME, FIELD_ERROR_MESSAGE);
AlertFieldStatus alertFieldStatusWarning = AlertFieldStatus.warning(FIELD_NAME, FIELD_WARNING_MESSAGE);
assertEquals(FIELD_NAME, alertFieldStatusError.getFieldName());
assertEquals(FIELD_NAME, alertFieldStatusWarning.getFieldName());
}
Aggregations