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();
}
}
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";
}
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);
}
}
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;
}
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);
}
}
Aggregations