Search in sources :

Example 6 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalHubConfigActionsTest method testValidConfig.

@Test
@Override
public void testValidConfig() throws Exception {
    final GlobalHubConfigActions configActions = new GlobalHubConfigActions(null, null, null);
    final String url = "https://www.google.com/";
    final String user = "User";
    final String password = "Password";
    HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
    serverConfigBuilder.setHubUrl(url);
    serverConfigBuilder.setUsername(user);
    serverConfigBuilder.setUsername(password);
    try {
        configActions.validateHubConfiguration(serverConfigBuilder);
        fail();
    } catch (final AlertFieldException e) {
        assertNotNull(e);
        assertEquals("There were issues with the configuration.", e.getMessage());
        assertTrue(!e.getFieldErrors().isEmpty());
    }
    final HubServerConfigValidator validator = Mockito.mock(HubServerConfigValidator.class);
    serverConfigBuilder = Mockito.spy(serverConfigBuilder);
    Mockito.when(serverConfigBuilder.createValidator()).thenReturn(validator);
    Mockito.when(validator.assertValid()).thenReturn(new ValidationResults());
    try {
        configActions.validateHubConfiguration(serverConfigBuilder);
    } catch (final AlertFieldException e) {
        fail();
    }
}
Also used : ValidationResults(com.blackducksoftware.integration.validator.ValidationResults) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) GlobalHubConfigActions(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions) HubServerConfigValidator(com.blackducksoftware.integration.hub.configuration.HubServerConfigValidator) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 7 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class DistributionConfigActions method validateConfig.

@Override
public String validateConfig(final R restModel) throws AlertFieldException {
    final Map<String, String> fieldErrors = new HashMap<>();
    if (restModel.getName() != null) {
        final List<CommonDistributionConfigEntity> configuredEntities = commonDistributionRepository.findAll();
        for (final CommonDistributionConfigEntity entity : configuredEntities) {
            final boolean areIdsEqual = entity.getId().toString().equals(restModel.getId());
            final boolean areNamesEqual = entity.getName().trim().equalsIgnoreCase((restModel.getName().trim()));
            if (!areIdsEqual && areNamesEqual) {
                fieldErrors.put("name", "A distribution configuration with this name already exists.");
                break;
            }
        }
    } else {
        fieldErrors.put("name", "Name cannot be null.");
    }
    if (StringUtils.isNotBlank(restModel.getId()) && !StringUtils.isNumeric(restModel.getId())) {
        fieldErrors.put("id", "Not an Integer.");
    }
    if (StringUtils.isNotBlank(restModel.getDistributionConfigId()) && !StringUtils.isNumeric(restModel.getDistributionConfigId())) {
        fieldErrors.put("distributionConfigId", "Not an Integer.");
    }
    if (StringUtils.isNotBlank(restModel.getFilterByProject()) && !isBoolean(restModel.getFilterByProject())) {
        fieldErrors.put("filterByProject", "Not a Boolean.");
    }
    if (!fieldErrors.isEmpty()) {
        throw new AlertFieldException(fieldErrors);
    }
    return "Valid";
}
Also used : CommonDistributionConfigEntity(com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity) HashMap(java.util.HashMap) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)

Example 8 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class GlobalHubConfigActions method validateHubConfiguration.

public void validateHubConfiguration(final HubServerConfigBuilder hubServerConfigBuilder) throws AlertFieldException {
    final AbstractValidator validator = hubServerConfigBuilder.createValidator();
    final ValidationResults results = validator.assertValid();
    if (!results.getResultMap().isEmpty()) {
        final Map<String, String> fieldErrors = new HashMap<>();
        for (final Entry<FieldEnum, Set<ValidationResult>> result : results.getResultMap().entrySet()) {
            final Set<ValidationResult> validationResult = result.getValue();
            final List<String> errors = new ArrayList<>();
            for (final ValidationResult currentValidationResult : validationResult) {
                errors.add(currentValidationResult.getMessage());
            }
            fieldErrors.put(result.getKey().getKey(), StringUtils.join(errors, " , "));
        }
        throw new AlertFieldException("There were issues with the configuration.", fieldErrors);
    }
}
Also used : Set(java.util.Set) AbstractValidator(com.blackducksoftware.integration.validator.AbstractValidator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) ValidationResult(com.blackducksoftware.integration.validator.ValidationResult) FieldEnum(com.blackducksoftware.integration.validator.FieldEnum) ValidationResults(com.blackducksoftware.integration.validator.ValidationResults)

Example 9 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class LoginActions method authenticateUser.

public boolean authenticateUser(final LoginRestModel loginRestModel, final IntLogger logger) throws IntegrationException {
    final HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
    serverConfigBuilder.setLogger(logger);
    serverConfigBuilder.setHubUrl(globalProperties.getHubUrl());
    serverConfigBuilder.setTimeout(HubServerConfigBuilder.DEFAULT_TIMEOUT_SECONDS);
    if (globalProperties.getHubTrustCertificate() != null) {
        serverConfigBuilder.setAlwaysTrustServerCertificate(globalProperties.getHubTrustCertificate());
    }
    serverConfigBuilder.setProxyHost(globalProperties.getHubProxyHost());
    serverConfigBuilder.setProxyPort(globalProperties.getHubProxyPort());
    serverConfigBuilder.setProxyUsername(globalProperties.getHubProxyUsername());
    serverConfigBuilder.setProxyPassword(globalProperties.getHubProxyPassword());
    serverConfigBuilder.setPassword(loginRestModel.getHubPassword());
    serverConfigBuilder.setUsername(loginRestModel.getHubUsername());
    try {
        validateHubConfiguration(serverConfigBuilder);
        final RestConnection restConnection = createRestConnection(serverConfigBuilder);
        restConnection.connect();
        logger.info("Connected");
        final boolean isValidLoginUser = isUserRoleValid(loginRestModel.getHubUsername(), restConnection);
        if (isValidLoginUser) {
            final Authentication authentication = new UsernamePasswordAuthenticationToken(loginRestModel.getHubUsername(), loginRestModel.getHubPassword(), Arrays.asList(new SimpleGrantedAuthority("ROLE_ADMIN")));
            SecurityContextHolder.getContext().setAuthentication(authentication);
            return authentication.isAuthenticated();
        }
    } catch (final AlertFieldException afex) {
        logger.error("Error establishing connection", afex);
        final Map<String, String> fieldErrorMap = afex.getFieldErrors();
        fieldErrorMap.keySet().forEach(key -> {
            final String value = fieldErrorMap.get(key);
            logger.error(String.format("Field Error %s - %s", key, value));
        });
        logger.info("User not authenticated");
        return false;
    } catch (final IntegrationException ex) {
        logger.error("Error establishing connection", ex);
        logger.info("User not authenticated");
        return false;
    }
    logger.info("User role not authenticated");
    return false;
}
Also used : AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) Arrays(java.util.Arrays) IntLogger(com.blackducksoftware.integration.log.IntLogger) FieldEnum(com.blackducksoftware.integration.validator.FieldEnum) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) HubServicesFactory(com.blackducksoftware.integration.hub.service.HubServicesFactory) Map(java.util.Map) GlobalProperties(com.blackducksoftware.integration.hub.alert.config.GlobalProperties) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) UserGroupService(com.blackducksoftware.integration.hub.service.UserGroupService) RoleAssignmentView(com.blackducksoftware.integration.hub.api.generated.view.RoleAssignmentView) ValidationResults(com.blackducksoftware.integration.validator.ValidationResults) Set(java.util.Set) AbstractValidator(com.blackducksoftware.integration.validator.AbstractValidator) List(java.util.List) Component(org.springframework.stereotype.Component) LoginRestModel(com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) Entry(java.util.Map.Entry) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) ValidationResult(com.blackducksoftware.integration.validator.ValidationResult) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HubServerConfig(com.blackducksoftware.integration.hub.configuration.HubServerConfig) Authentication(org.springframework.security.core.Authentication) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) IntegrationException(com.blackducksoftware.integration.exception.IntegrationException) Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with AlertFieldException

use of com.blackducksoftware.integration.hub.alert.exception.AlertFieldException in project hub-alert by blackducksoftware.

the class LoginActions method validateHubConfiguration.

public void validateHubConfiguration(final HubServerConfigBuilder hubServerConfigBuilder) throws AlertFieldException {
    final AbstractValidator validator = hubServerConfigBuilder.createValidator();
    final ValidationResults results = validator.assertValid();
    if (!results.getResultMap().isEmpty()) {
        final Map<String, String> fieldErrors = new HashMap<>();
        for (final Entry<FieldEnum, Set<ValidationResult>> result : results.getResultMap().entrySet()) {
            final Set<ValidationResult> validationResult = result.getValue();
            final List<String> errors = new ArrayList<>();
            for (final ValidationResult currentValidationResult : validationResult) {
                errors.add(currentValidationResult.getMessage());
            }
            final String key = result.getKey().getKey();
            final String errorMessage = StringUtils.join(errors, " , ");
            fieldErrors.put(key, errorMessage);
        }
        throw new AlertFieldException("There were issues with the configuration.", fieldErrors);
    }
}
Also used : Set(java.util.Set) AbstractValidator(com.blackducksoftware.integration.validator.AbstractValidator) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AlertFieldException(com.blackducksoftware.integration.hub.alert.exception.AlertFieldException) ValidationResult(com.blackducksoftware.integration.validator.ValidationResult) FieldEnum(com.blackducksoftware.integration.validator.FieldEnum) ValidationResults(com.blackducksoftware.integration.validator.ValidationResults)

Aggregations

AlertFieldException (com.blackducksoftware.integration.hub.alert.exception.AlertFieldException)17 Test (org.junit.Test)8 CommonDistributionConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.CommonDistributionConfigEntity)5 CommonDistributionRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.CommonDistributionRepositoryWrapper)4 CommonDistributionConfigActions (com.blackducksoftware.integration.hub.alert.web.actions.distribution.CommonDistributionConfigActions)4 CommonDistributionConfigRestModel (com.blackducksoftware.integration.hub.alert.web.model.distribution.CommonDistributionConfigRestModel)4 ValidationResults (com.blackducksoftware.integration.validator.ValidationResults)4 HashMap (java.util.HashMap)4 GlobalActionsTest (com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)3 ResponseBodyBuilder (com.blackducksoftware.integration.hub.alert.web.model.ResponseBodyBuilder)3 AbstractValidator (com.blackducksoftware.integration.validator.AbstractValidator)3 FieldEnum (com.blackducksoftware.integration.validator.FieldEnum)3 ValidationResult (com.blackducksoftware.integration.validator.ValidationResult)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 AlertException (com.blackducksoftware.integration.hub.alert.exception.AlertException)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)2 IntLogger (com.blackducksoftware.integration.log.IntLogger)2 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)1