Search in sources :

Example 1 with AlertFieldStatus

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);
}
Also used : HashMap(java.util.HashMap) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)

Example 2 with AlertFieldStatus

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);
    }
}
Also used : Authentication(org.springframework.security.core.Authentication) ArrayList(java.util.ArrayList) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) LdapAuthenticationProvider(org.springframework.security.ldap.authentication.LdapAuthenticationProvider) AlertFieldException(com.synopsys.integration.alert.common.exception.AlertFieldException) IntegrationException(com.synopsys.integration.exception.IntegrationException)

Example 3 with AlertFieldStatus

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;
}
Also used : AbstractResourceActions(com.synopsys.integration.alert.common.action.api.AbstractResourceActions) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) AlertTrustStoreManager(com.synopsys.integration.alert.component.certificates.AlertTrustStoreManager) IntegrationEscapeUtil(com.synopsys.integration.util.IntegrationEscapeUtil) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Logger(org.slf4j.Logger) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) CertificatesDescriptor(com.synopsys.integration.alert.component.certificates.CertificatesDescriptor) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) CertificatesDescriptorKey(com.synopsys.integration.alert.component.certificates.CertificatesDescriptorKey) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) CustomCertificateAccessor(com.synopsys.integration.alert.common.persistence.accessor.CustomCertificateAccessor) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 4 with AlertFieldStatus

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);
}
Also used : ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)

Example 5 with AlertFieldStatus

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());
}
Also used : AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) Test(org.junit.jupiter.api.Test)

Aggregations

AlertFieldStatus (com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)50 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)23 Test (org.junit.jupiter.api.Test)23 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)11 ArrayList (java.util.ArrayList)11 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)10 FieldValueModel (com.synopsys.integration.alert.common.rest.model.FieldValueModel)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 Component (org.springframework.stereotype.Component)9 AlertFieldException (com.synopsys.integration.alert.common.exception.AlertFieldException)8 EmailGlobalConfigModel (com.synopsys.integration.alert.service.email.model.EmailGlobalConfigModel)8 IntegrationException (com.synopsys.integration.exception.IntegrationException)8 List (java.util.List)8 Optional (java.util.Optional)8 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)7 ConfigurationModelConfigurationAccessor (com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor)7 JobFieldModel (com.synopsys.integration.alert.common.rest.model.JobFieldModel)7 FieldModelTestAction (com.synopsys.integration.alert.common.action.FieldModelTestAction)6