Search in sources :

Example 11 with RestConnection

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;
}
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 12 with RestConnection

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);
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) LoginRestModel(com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel) MockLoginRestModel(com.blackducksoftware.integration.hub.alert.mock.model.MockLoginRestModel) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) GlobalProperties(com.blackducksoftware.integration.hub.alert.config.GlobalProperties) Slf4jIntLogger(com.blackducksoftware.integration.log.Slf4jIntLogger) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) Test(org.junit.Test) HubConnectionTest(com.blackducksoftware.integration.test.annotation.HubConnectionTest)

Example 13 with RestConnection

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();
}
Also used : GlobalHubConfigActions(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions) GlobalHubConfigRestModel(com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigRestModel) RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ObjectTransformer(com.blackducksoftware.integration.hub.alert.web.ObjectTransformer) HubServerConfigBuilder(com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder) MockGlobalHubRestModel(com.blackducksoftware.integration.hub.alert.hub.mock.MockGlobalHubRestModel) GlobalHubRepositoryWrapper(com.blackducksoftware.integration.hub.alert.datasource.entity.repository.global.GlobalHubRepositoryWrapper) TestGlobalProperties(com.blackducksoftware.integration.hub.alert.TestGlobalProperties) Test(org.junit.Test) GlobalActionsTest(com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)

Example 14 with RestConnection

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());
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) Request(com.blackducksoftware.integration.hub.request.Request) Test(org.junit.Test)

Example 15 with RestConnection

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());
}
Also used : RestConnection(com.blackducksoftware.integration.hub.rest.RestConnection) Request(com.blackducksoftware.integration.hub.request.Request)

Aggregations

RestConnection (com.blackducksoftware.integration.hub.rest.RestConnection)16 Test (org.junit.Test)10 HubServerConfigBuilder (com.blackducksoftware.integration.hub.configuration.HubServerConfigBuilder)6 IntegrationException (com.blackducksoftware.integration.exception.IntegrationException)5 TestGlobalProperties (com.blackducksoftware.integration.hub.alert.TestGlobalProperties)5 Request (com.blackducksoftware.integration.hub.request.Request)5 ChannelRestConnectionFactory (com.blackducksoftware.integration.hub.alert.channel.rest.ChannelRestConnectionFactory)3 GlobalHubConfigActions (com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigActions)3 GlobalActionsTest (com.blackducksoftware.integration.hub.alert.web.actions.global.GlobalActionsTest)3 HubServerConfig (com.blackducksoftware.integration.hub.configuration.HubServerConfig)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 GlobalProperties (com.blackducksoftware.integration.hub.alert.config.GlobalProperties)2 GlobalHubConfigEntity (com.blackducksoftware.integration.hub.alert.datasource.entity.global.GlobalHubConfigEntity)2 GlobalHubRepositoryWrapper (com.blackducksoftware.integration.hub.alert.datasource.entity.repository.global.GlobalHubRepositoryWrapper)2 GlobalHubConfigRestModel (com.blackducksoftware.integration.hub.alert.hub.controller.global.GlobalHubConfigRestModel)2 ObjectTransformer (com.blackducksoftware.integration.hub.alert.web.ObjectTransformer)2 LoginRestModel (com.blackducksoftware.integration.hub.alert.web.model.LoginRestModel)2 ProxyInfo (com.blackducksoftware.integration.hub.proxy.ProxyInfo)2 UriCombiner (com.blackducksoftware.integration.hub.rest.UriCombiner)2 HubServicesFactory (com.blackducksoftware.integration.hub.service.HubServicesFactory)2