Search in sources :

Example 1 with UsersResourceApi

use of com.cloudera.api.swagger.UsersResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityService method setupMonitoringUser.

@Override
public void setupMonitoringUser() throws CloudbreakException {
    Cluster cluster = stack.getCluster();
    String user = cluster.getCloudbreakAmbariUser();
    String password = cluster.getCloudbreakAmbariPassword();
    try {
        ApiClient client = getClient(stack.getGatewayPort(), user, password, clientConfig);
        UsersResourceApi usersResourceApi = clouderaManagerApiFactory.getUserResourceApi(client);
        String monitoringUser = cluster.getCloudbreakClusterManagerMonitoringUser();
        String monitoringPassword = cluster.getCloudbreakClusterManagerMonitoringPassword();
        ApiUser2List userList = usersResourceApi.readUsers2("SUMMARY");
        Optional<ApiUser2> mUser = userList.getItems().stream().filter(apiUser2 -> apiUser2.getName().equals(monitoringUser)).findFirst();
        if (mUser.isPresent()) {
            LOGGER.info("Monitoring user '{}' already exists. Skipping user generation", monitoringUser);
        } else {
            List<ApiAuthRoleRef> authRoles = new ArrayList<>();
            ApiAuthRoleRef apiAuthRoleRef = new ApiAuthRoleRef();
            apiAuthRoleRef.setName("ROLE_ADMIN");
            authRoles.add(apiAuthRoleRef);
            createNewUser(usersResourceApi, authRoles, monitoringUser, monitoringPassword, userList);
        }
    } catch (ApiException | ClouderaManagerClientInitException e) {
        throw new CloudbreakException("Can't replace admin password due to: " + e.getMessage());
    }
}
Also used : UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) ApiUser2List(com.cloudera.api.swagger.model.ApiUser2List) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) KeyPair(java.security.KeyPair) ApiBatchRequestElement(com.cloudera.api.swagger.model.ApiBatchRequestElement) ApiCommand(com.cloudera.api.swagger.model.ApiCommand) ClusterClientInitException(com.sequenceiq.cloudbreak.cluster.service.ClusterClientInitException) ApiClient(com.cloudera.api.swagger.client.ApiClient) LoggerFactory(org.slf4j.LoggerFactory) ToolsResourceApi(com.cloudera.api.swagger.ToolsResourceApi) ApiHostList(com.cloudera.api.swagger.model.ApiHostList) ClouderaManagerClientInitException(com.sequenceiq.cloudbreak.cm.client.ClouderaManagerClientInitException) StringUtils(org.apache.commons.lang3.StringUtils) ApiAuthRoleRef(com.cloudera.api.swagger.model.ApiAuthRoleRef) Scope(org.springframework.context.annotation.Scope) ApiException(com.cloudera.api.swagger.client.ApiException) ClouderaManagerPollingServiceProvider(com.sequenceiq.cloudbreak.cm.polling.ClouderaManagerPollingServiceProvider) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) BigDecimal(java.math.BigDecimal) ClouderaManagerApiClientProvider(com.sequenceiq.cloudbreak.cm.client.ClouderaManagerApiClientProvider) UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) BatchResourceApi(com.cloudera.api.swagger.BatchResourceApi) Service(org.springframework.stereotype.Service) LdapView(com.sequenceiq.cloudbreak.dto.LdapView) ApiBatchRequest(com.cloudera.api.swagger.model.ApiBatchRequest) Retryable(org.springframework.retry.annotation.Retryable) Logger(org.slf4j.Logger) VirtualGroupRequest(com.sequenceiq.cloudbreak.auth.altus.VirtualGroupRequest) HostsResourceApi(com.cloudera.api.swagger.HostsResourceApi) ApiBatchResponse(com.cloudera.api.swagger.model.ApiBatchResponse) ApiUser2List(com.cloudera.api.swagger.model.ApiUser2List) ExtendedPollingResult(com.sequenceiq.cloudbreak.polling.ExtendedPollingResult) URLUtils(com.sequenceiq.cloudbreak.util.URLUtils) ClusterSecurityService(com.sequenceiq.cloudbreak.cluster.api.ClusterSecurityService) ApiGenerateHostCertsArguments(com.cloudera.api.swagger.model.ApiGenerateHostCertsArguments) HttpClientConfig(com.sequenceiq.cloudbreak.client.HttpClientConfig) Collectors(java.util.stream.Collectors) ApiUser2(com.cloudera.api.swagger.model.ApiUser2) Json(com.sequenceiq.cloudbreak.common.json.Json) List(java.util.List) HTTPMethod(com.cloudera.api.swagger.model.HTTPMethod) PkiUtil(com.sequenceiq.cloudbreak.certificate.PkiUtil) DatalakeDto(com.sequenceiq.cloudbreak.dto.datalake.DatalakeDto) Optional(java.util.Optional) CancellationException(com.sequenceiq.cloudbreak.cloud.scheduler.CancellationException) ClouderaManagerApiFactory(com.sequenceiq.cloudbreak.cm.client.retry.ClouderaManagerApiFactory) ArrayList(java.util.ArrayList) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) ClouderaManagerClientInitException(com.sequenceiq.cloudbreak.cm.client.ClouderaManagerClientInitException) ApiClient(com.cloudera.api.swagger.client.ApiClient) ApiAuthRoleRef(com.cloudera.api.swagger.model.ApiAuthRoleRef) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) ApiUser2(com.cloudera.api.swagger.model.ApiUser2) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 2 with UsersResourceApi

use of com.cloudera.api.swagger.UsersResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityService method changeOriginalCredentialsAndCreateCloudbreakUser.

@Override
public void changeOriginalCredentialsAndCreateCloudbreakUser(boolean ldapConfigured) throws CloudbreakException {
    LOGGER.debug("change original admin user and create cloudbreak user");
    try {
        ApiClient client = createApiClient();
        UsersResourceApi usersResourceApi = clouderaManagerApiFactory.getUserResourceApi(client);
        ApiUser2List userList = usersResourceApi.readUsers2("SUMMARY");
        ApiUser2 oldAdminUser = getOldAdminUser(userList).orElseThrow(() -> new CloudbreakException("Can't find original admin user"));
        Cluster cluster = stack.getCluster();
        createNewUser(usersResourceApi, oldAdminUser.getAuthRoles(), cluster.getCloudbreakAmbariUser(), cluster.getCloudbreakAmbariPassword(), userList);
        createNewUser(usersResourceApi, oldAdminUser.getAuthRoles(), cluster.getDpAmbariUser(), cluster.getDpAmbariPassword(), userList);
        if (ADMIN_USER.equals(cluster.getUserName())) {
            oldAdminUser.setPassword(cluster.getPassword());
            usersResourceApi.updateUser2(oldAdminUser.getName(), oldAdminUser);
        } else if (cluster.getUserName() != null) {
            createUserSuppliedCMUser(userList, oldAdminUser, cluster);
        }
        removeDefaultAdminUser(ldapConfigured, Optional.ofNullable(cluster.getUserName()));
    } catch (ApiException | ClusterClientInitException | ClouderaManagerClientInitException e) {
        LOGGER.info("Can't replace original admin user due to: ", e);
        throw new CloudbreakException("Can't replace original admin user due to: " + e.getMessage());
    }
}
Also used : UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) ApiUser2List(com.cloudera.api.swagger.model.ApiUser2List) ClusterClientInitException(com.sequenceiq.cloudbreak.cluster.service.ClusterClientInitException) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) ClouderaManagerClientInitException(com.sequenceiq.cloudbreak.cm.client.ClouderaManagerClientInitException) ApiUser2(com.cloudera.api.swagger.model.ApiUser2) ApiClient(com.cloudera.api.swagger.client.ApiClient) ApiException(com.cloudera.api.swagger.client.ApiException)

Example 3 with UsersResourceApi

use of com.cloudera.api.swagger.UsersResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityServiceTest method testChangeOriginalCredentialsAndCreateCloudbreakUserWhenLdapIsConfiguredAndAdminUserIsProvided.

@Test
public void testChangeOriginalCredentialsAndCreateCloudbreakUserWhenLdapIsConfiguredAndAdminUserIsProvided() throws CloudbreakException, ApiException, ClouderaManagerClientInitException {
    initTestInput(ADMIN);
    UsersResourceApi usersResourceApi = mock(UsersResourceApi.class);
    ToolsResourceApi toolsResourceApi = mock(ToolsResourceApi.class);
    ApiUser2List oldUserList = createApiUser2List();
    when(clouderaManagerApiClientProvider.getDefaultClient(GATEWAY_PORT, clientConfig, ClouderaManagerApiClientProvider.API_V_31)).thenReturn(apiClient);
    when(clouderaManagerApiFactory.getUserResourceApi(any())).thenReturn(usersResourceApi);
    when(usersResourceApi.readUsers2("SUMMARY")).thenReturn(oldUserList);
    when(clouderaManagerApiFactory.getToolsResourceApi(any())).thenReturn(toolsResourceApi);
    when(toolsResourceApi.echo("TEST")).thenReturn(new ApiEcho());
    underTest.changeOriginalCredentialsAndCreateCloudbreakUser(LDAP_ENABLED);
    verify(clouderaManagerApiFactory).getUserResourceApi(apiClient);
    verify(usersResourceApi).readUsers2("SUMMARY");
    ArgumentCaptor<ApiUser2List> argumentCaptor = ArgumentCaptor.forClass(ApiUser2List.class);
    verify(usersResourceApi, times(2)).createUsers2(argumentCaptor.capture());
    List<ApiUser2List> createdUsers = argumentCaptor.getAllValues();
    assertEquals(stack.getCluster().getCloudbreakAmbariUser(), createdUsers.get(0).getItems().get(0).getName());
    assertEquals(stack.getCluster().getCloudbreakAmbariPassword(), createdUsers.get(0).getItems().get(0).getPassword());
    assertEquals(stack.getCluster().getDpAmbariUser(), createdUsers.get(1).getItems().get(0).getName());
    assertEquals(stack.getCluster().getDpAmbariPassword(), createdUsers.get(1).getItems().get(0).getPassword());
    verify(usersResourceApi).updateUser2(oldUserList.getItems().get(0).getName(), oldUserList.getItems().get(0));
}
Also used : UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) ApiUser2List(com.cloudera.api.swagger.model.ApiUser2List) ApiEcho(com.cloudera.api.swagger.model.ApiEcho) ToolsResourceApi(com.cloudera.api.swagger.ToolsResourceApi) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with UsersResourceApi

use of com.cloudera.api.swagger.UsersResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityServiceTest method testChangeOriginalCredentialsAndCreateCloudbreakUserWhenLdapIsConfiguredAndTheGivenUserIsNotAdmin.

@Test
public void testChangeOriginalCredentialsAndCreateCloudbreakUserWhenLdapIsConfiguredAndTheGivenUserIsNotAdmin() throws CloudbreakException, ApiException, ClouderaManagerClientInitException {
    initTestInput("ambariUser");
    UsersResourceApi usersResourceApi = mock(UsersResourceApi.class);
    UsersResourceApi newUsersResourceApi = mock(UsersResourceApi.class);
    ToolsResourceApi toolsResourceApi = mock(ToolsResourceApi.class);
    ApiClient newApiClient = mock(ApiClient.class);
    ApiUser2List oldUserList = createApiUser2List();
    when(clouderaManagerApiClientProvider.getDefaultClient(GATEWAY_PORT, clientConfig, ClouderaManagerApiClientProvider.API_V_31)).thenReturn(apiClient);
    when(clouderaManagerApiFactory.getUserResourceApi(apiClient)).thenReturn(usersResourceApi);
    when(usersResourceApi.readUsers2("SUMMARY")).thenReturn(oldUserList);
    when(clouderaManagerApiFactory.getToolsResourceApi(any())).thenReturn(toolsResourceApi);
    when(toolsResourceApi.echo("TEST")).thenReturn(new ApiEcho());
    when(clouderaManagerApiClientProvider.getClouderaManagerClient(clientConfig, GATEWAY_PORT, stack.getCluster().getCloudbreakAmbariUser(), stack.getCluster().getCloudbreakAmbariPassword(), ClouderaManagerApiClientProvider.API_V_31)).thenReturn(newApiClient);
    when(clouderaManagerApiFactory.getUserResourceApi(newApiClient)).thenReturn(newUsersResourceApi);
    underTest.changeOriginalCredentialsAndCreateCloudbreakUser(LDAP_ENABLED);
    verify(clouderaManagerApiClientProvider).getDefaultClient(GATEWAY_PORT, clientConfig, ClouderaManagerApiClientProvider.API_V_31);
    verify(usersResourceApi).readUsers2("SUMMARY");
    verify(clouderaManagerApiClientProvider, times(2)).getClouderaManagerClient(clientConfig, GATEWAY_PORT, stack.getCluster().getCloudbreakAmbariUser(), stack.getCluster().getCloudbreakAmbariPassword(), ClouderaManagerApiClientProvider.API_V_31);
    ArgumentCaptor<ApiUser2List> createUserCaptor = ArgumentCaptor.forClass(ApiUser2List.class);
    verify(usersResourceApi, times(2)).createUsers2(createUserCaptor.capture());
    List<ApiUser2List> createdUsers = createUserCaptor.getAllValues();
    assertEquals(stack.getCluster().getCloudbreakAmbariUser(), createdUsers.get(0).getItems().get(0).getName());
    assertEquals(stack.getCluster().getCloudbreakAmbariPassword(), createdUsers.get(0).getItems().get(0).getPassword());
    assertEquals(stack.getCluster().getDpAmbariUser(), createdUsers.get(1).getItems().get(0).getName());
    assertEquals(stack.getCluster().getDpAmbariPassword(), createdUsers.get(1).getItems().get(0).getPassword());
    ArgumentCaptor<ApiUser2List> createNewUserCaptor = ArgumentCaptor.forClass(ApiUser2List.class);
    verify(newUsersResourceApi).createUsers2(createNewUserCaptor.capture());
    List<ApiUser2List> createdNewUser = createNewUserCaptor.getAllValues();
    assertEquals(stack.getCluster().getUserName(), createdNewUser.get(0).getItems().get(0).getName());
    assertEquals(stack.getCluster().getPassword(), createdNewUser.get(0).getItems().get(0).getPassword());
    verify(newUsersResourceApi).deleteUser2(ADMIN);
}
Also used : UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) ApiUser2List(com.cloudera.api.swagger.model.ApiUser2List) ApiEcho(com.cloudera.api.swagger.model.ApiEcho) ToolsResourceApi(com.cloudera.api.swagger.ToolsResourceApi) ApiClient(com.cloudera.api.swagger.client.ApiClient) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with UsersResourceApi

use of com.cloudera.api.swagger.UsersResourceApi in project cloudbreak by hortonworks.

the class ClouderaManagerSecurityServiceTest method testChangeOriginalCredentialsAndCreateCloudbreakUserWhenUsersAlreadyCreated.

@Test
public void testChangeOriginalCredentialsAndCreateCloudbreakUserWhenUsersAlreadyCreated() throws CloudbreakException, ApiException, ClouderaManagerClientInitException {
    initTestInput("ambariUser");
    UsersResourceApi usersResourceApi = mock(UsersResourceApi.class);
    UsersResourceApi newUsersResourceApi = mock(UsersResourceApi.class);
    ApiClient newApiClient = mock(ApiClient.class);
    Cluster cluster = stack.getCluster();
    setUpClientCreation(cluster);
    when(clouderaManagerApiFactory.getUserResourceApi(apiClient)).thenReturn(usersResourceApi);
    setUpUsersAlreadyCreated(usersResourceApi, cluster);
    setUpApiClientCredentialAlreadyChanged();
    when(clouderaManagerApiClientProvider.getClouderaManagerClient(clientConfig, GATEWAY_PORT, cluster.getCloudbreakAmbariUser(), cluster.getCloudbreakAmbariPassword(), ClouderaManagerApiClientProvider.API_V_31)).thenReturn(newApiClient);
    when(clouderaManagerApiFactory.getUserResourceApi(newApiClient)).thenReturn(newUsersResourceApi);
    underTest.changeOriginalCredentialsAndCreateCloudbreakUser(LDAP_DISABLED);
    verifyClientCreation(cluster);
    verify(usersResourceApi).readUsers2("SUMMARY");
    verifyNoUsersCreated(usersResourceApi, newUsersResourceApi);
    verifyNoMoreInteractions(clouderaManagerApiClientProvider);
}
Also used : UsersResourceApi(com.cloudera.api.swagger.UsersResourceApi) Cluster(com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster) ApiClient(com.cloudera.api.swagger.client.ApiClient) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

UsersResourceApi (com.cloudera.api.swagger.UsersResourceApi)10 ApiClient (com.cloudera.api.swagger.client.ApiClient)9 ApiUser2List (com.cloudera.api.swagger.model.ApiUser2List)7 ToolsResourceApi (com.cloudera.api.swagger.ToolsResourceApi)6 ApiException (com.cloudera.api.swagger.client.ApiException)5 ClouderaManagerClientInitException (com.sequenceiq.cloudbreak.cm.client.ClouderaManagerClientInitException)5 Cluster (com.sequenceiq.cloudbreak.domain.stack.cluster.Cluster)5 ApiUser2 (com.cloudera.api.swagger.model.ApiUser2)4 ClusterClientInitException (com.sequenceiq.cloudbreak.cluster.service.ClusterClientInitException)4 CloudbreakException (com.sequenceiq.cloudbreak.service.CloudbreakException)4 Test (org.junit.jupiter.api.Test)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 BatchResourceApi (com.cloudera.api.swagger.BatchResourceApi)3 HostsResourceApi (com.cloudera.api.swagger.HostsResourceApi)3 ApiAuthRoleRef (com.cloudera.api.swagger.model.ApiAuthRoleRef)3 ApiBatchRequest (com.cloudera.api.swagger.model.ApiBatchRequest)3 ApiBatchRequestElement (com.cloudera.api.swagger.model.ApiBatchRequestElement)3 ApiBatchResponse (com.cloudera.api.swagger.model.ApiBatchResponse)3 ApiCommand (com.cloudera.api.swagger.model.ApiCommand)3 ApiEcho (com.cloudera.api.swagger.model.ApiEcho)3