use of com.blackducksoftware.integration.hub.rest.RestConnection 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.rest.RestConnection in project hub-alert by blackducksoftware.
the class LoginActionsTestIT method testIsUserValidFailIT.
@Test
public void testIsUserValidFailIT() throws IntegrationException, IOException {
final LoginRestModel loginRestModel = mockLoginRestModel.createRestModel();
final GlobalProperties globalProperties = new TestGlobalProperties();
final HubServerConfigBuilder serverConfigBuilder = new HubServerConfigBuilder();
serverConfigBuilder.setLogger(new Slf4jIntLogger(logger));
serverConfigBuilder.setHubUrl(globalProperties.getHubUrl());
serverConfigBuilder.setAlwaysTrustServerCertificate(globalProperties.getHubTrustCertificate());
serverConfigBuilder.setTimeout(globalProperties.getHubTimeout());
serverConfigBuilder.setPassword(loginRestModel.getHubPassword());
serverConfigBuilder.setUsername(loginRestModel.getHubUsername());
final LoginActions loginActions = new LoginActions(globalProperties);
final RestConnection restConnection = loginActions.createRestConnection(serverConfigBuilder);
final boolean roleValid = loginActions.isUserRoleValid("broken", restConnection);
assertFalse(roleValid);
}
use of com.blackducksoftware.integration.hub.rest.RestConnection in project hub-alert by blackducksoftware.
the class GlobalHubConfigActionsTest method testChannelTestConfig.
@Test
@Override
public void testChannelTestConfig() throws Exception {
final MockGlobalHubRestModel mockUtils = new MockGlobalHubRestModel();
final RestConnection mockedRestConnection = Mockito.mock(RestConnection.class);
final GlobalHubRepositoryWrapper mockedGlobalRepository = Mockito.mock(GlobalHubRepositoryWrapper.class);
final TestGlobalProperties globalProperties = new TestGlobalProperties(mockedGlobalRepository);
GlobalHubConfigActions configActions = new GlobalHubConfigActions(mockedGlobalRepository, globalProperties, new ObjectTransformer());
configActions = Mockito.spy(configActions);
Mockito.doAnswer(new Answer<RestConnection>() {
@Override
public RestConnection answer(final InvocationOnMock invocation) throws Throwable {
return mockedRestConnection;
}
}).when(configActions).createRestConnection(Mockito.any(HubServerConfigBuilder.class));
Mockito.doNothing().when(configActions).validateHubConfiguration(Mockito.any(HubServerConfigBuilder.class));
configActions.testConfig(mockUtils.createGlobalRestModel());
Mockito.verify(mockedRestConnection, Mockito.times(1)).connect();
Mockito.reset(mockedRestConnection);
final GlobalHubConfigRestModel restModel = mockUtils.createGlobalRestModel();
final String result = configActions.channelTestConfig(restModel);
assertEquals("Successfully connected to the Hub.", result);
Mockito.verify(mockedRestConnection, Mockito.times(1)).connect();
}
use of com.blackducksoftware.integration.hub.rest.RestConnection in project hub-alert by blackducksoftware.
the class ChannelRequestHelperTest method testCreateMessageRequest.
@Test
public void testCreateMessageRequest() throws Exception {
final Request request = createRequest();
final RestConnection restConnection = Mockito.mock(RestConnection.class);
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
final Request returnedRequest = channelRequestHelper.createPostMessageRequest("https://google.com", null, "{}");
assertEquals(request.getUri(), returnedRequest.getUri());
assertEquals(request.getMethod(), returnedRequest.getMethod());
assertEquals(request.getMimeType(), returnedRequest.getMimeType());
assertEquals(request.getQueryParameters(), returnedRequest.getQueryParameters());
assertEquals(request.getAdditionalHeaders(), returnedRequest.getAdditionalHeaders());
assertEquals(request.getBodyEncoding(), returnedRequest.getBodyEncoding());
assertEquals(request.getBodyContent().getBodyContent(), returnedRequest.getBodyContent().getBodyContent());
}
use of com.blackducksoftware.integration.hub.rest.RestConnection in project hub-alert by blackducksoftware.
the class RestDistributionChannel method sendMessage.
@Override
public void sendMessage(final E event, final C config) throws Exception {
final RestConnection restConnection = channelRestConnectionFactory.createUnauthenticatedRestConnection(getApiUrl());
final ChannelRequestHelper channelRequestHelper = new ChannelRequestHelper(restConnection);
final Request request = createRequest(channelRequestHelper, config, event.getProjectData());
channelRequestHelper.sendMessageRequest(request, event.getTopic());
}
Aggregations