Search in sources :

Example 6 with SyncStatusDetail

use of com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail in project cloudbreak by hortonworks.

the class UserSyncForEnvService method synchronizeUsers.

public void synchronizeUsers(String operationId, String accountId, List<Stack> stacks, UserSyncRequestFilter userSyncFilter, UserSyncOptions options, long startTime) {
    operationService.tryWithOperationCleanup(operationId, accountId, () -> {
        Set<String> environmentCrns = stacks.stream().map(Stack::getEnvironmentCrn).collect(Collectors.toSet());
        UserSyncLogEvent logUserSyncEvent = options.isFullSync() ? FULL_USER_SYNC : PARTIAL_USER_SYNC;
        LOGGER.info("Starting {} for environments {} with operationId {} ...", logUserSyncEvent, environmentCrns, operationId);
        if (options.isFullSync()) {
            umsVirtualGroupCreateService.createVirtualGroups(accountId, stacks);
        }
        Map<String, Future<SyncStatusDetail>> statusFutures = startAsyncSyncsForStacks(operationId, accountId, stacks, userSyncFilter, options, environmentCrns);
        List<SuccessDetails> success = new ArrayList<>();
        List<FailureDetails> failure = new ArrayList<>();
        statusFutures.forEach((envCrn, statusFuture) -> {
            try {
                SyncStatusDetail statusDetail = waitForSyncStatusDetailResult(startTime, statusFuture, accountId);
                switch(statusDetail.getStatus()) {
                    case COMPLETED:
                        success.add(new SuccessDetails(envCrn));
                        break;
                    case FAILED:
                        failure.add(createFailureDetails(envCrn, statusDetail.getDetails(), statusDetail.getWarnings()));
                        break;
                    default:
                        failure.add(createFailureDetails(envCrn, "Unexpected status: " + statusDetail.getStatus(), statusDetail.getWarnings()));
                        break;
                }
            } catch (TimeoutException e) {
                LOGGER.warn("Sync timed out for env: {}", envCrn, e);
                statusFuture.cancel(true);
                failure.add(new FailureDetails(envCrn, "Timed out"));
            } catch (InterruptedException | ExecutionException e) {
                LOGGER.error("Sync is interrupted for env: {}", envCrn, e);
                failure.add(new FailureDetails(envCrn, e.getLocalizedMessage()));
            }
        });
        operationService.completeOperation(accountId, operationId, success, failure);
        LOGGER.info("Finished {} for environments {} with operationId {}.", logUserSyncEvent, environmentCrns, operationId);
    });
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) ArrayList(java.util.ArrayList) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 7 with SyncStatusDetail

use of com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail in project cloudbreak by hortonworks.

the class UserSyncForEnvServiceTest method testSyncUserDelete.

@Test
public void testSyncUserDelete() {
    Stack stack1 = mock(Stack.class);
    when(stack1.getEnvironmentCrn()).thenReturn(ENV_CRN);
    Stack stack2 = mock(Stack.class);
    when(stack2.getEnvironmentCrn()).thenReturn(ENV_CRN_2);
    UserSyncRequestFilter userSyncFilter = new UserSyncRequestFilter(Set.of(), Set.of(), Optional.of("deleteMe"));
    UserSyncOptions options = new UserSyncOptions(true, true, WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    doAnswer(inv -> {
        inv.getArgument(2, Runnable.class).run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(OPERATION_ID), eq(ACCOUNT_ID), any(Runnable.class));
    when(asyncTaskExecutor.submit(any(Callable.class))).thenAnswer(inv -> {
        SyncStatusDetail result = (SyncStatusDetail) inv.getArgument(0, Callable.class).call();
        Future future = mock(Future.class);
        when(future.get()).thenReturn(result);
        return future;
    });
    when(userSyncForStackService.synchronizeStackForDeleteUser(stack1, "deleteMe")).thenReturn(new SyncStatusDetail(ENV_CRN, SynchronizationStatus.COMPLETED, "", ImmutableMultimap.of()));
    when(userSyncForStackService.synchronizeStackForDeleteUser(stack2, "deleteMe")).thenReturn(new SyncStatusDetail(ENV_CRN_2, SynchronizationStatus.COMPLETED, "", ImmutableMultimap.of()));
    underTest.synchronizeUsers(OPERATION_ID, ACCOUNT_ID, List.of(stack1, stack2), userSyncFilter, options, System.currentTimeMillis());
    verifyNoInteractions(userSyncStatusService);
    ArgumentCaptor<Collection> successCaptor = ArgumentCaptor.forClass(Collection.class);
    ArgumentCaptor<Collection> failureCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(operationService).completeOperation(eq(ACCOUNT_ID), eq(OPERATION_ID), successCaptor.capture(), failureCaptor.capture());
    assertTrue(failureCaptor.getValue().isEmpty());
    assertTrue(successCaptor.getValue().contains(new SuccessDetails(ENV_CRN)));
    assertTrue(successCaptor.getValue().contains(new SuccessDetails(ENV_CRN_2)));
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) Future(java.util.concurrent.Future) Collection(java.util.Collection) Callable(java.util.concurrent.Callable) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 8 with SyncStatusDetail

use of com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail in project cloudbreak by hortonworks.

the class UserSyncForEnvServiceTest method testSyncUsersFailures.

@Test
public void testSyncUsersFailures() {
    Stack stack1 = mock(Stack.class);
    when(stack1.getEnvironmentCrn()).thenReturn(ENV_CRN);
    Stack stack2 = mock(Stack.class);
    when(stack2.getEnvironmentCrn()).thenReturn(ENV_CRN_2);
    UserSyncRequestFilter userSyncFilter = new UserSyncRequestFilter(Set.of(), Set.of(), Optional.empty());
    UserSyncOptions options = new UserSyncOptions(true, true, WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    doAnswer(inv -> {
        inv.getArgument(2, Runnable.class).run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(OPERATION_ID), eq(ACCOUNT_ID), any(Runnable.class));
    UmsUsersState umsUsersState1 = mock(UmsUsersState.class);
    UmsUsersState umsUsersState2 = mock(UmsUsersState.class);
    when(umsUsersStateProviderDispatcher.getEnvToUmsUsersStateMap(eq(ACCOUNT_ID), eq(Set.of(ENV_CRN, ENV_CRN_2)), eq(Set.of()), eq(Set.of()), any())).thenReturn(Map.of(ENV_CRN, umsUsersState1, ENV_CRN_2, umsUsersState2));
    when(asyncTaskExecutor.submit(any(Callable.class))).thenAnswer(inv -> {
        SyncStatusDetail result = (SyncStatusDetail) inv.getArgument(0, Callable.class).call();
        Future future = mock(Future.class);
        when(future.get()).thenReturn(result);
        return future;
    });
    when(umsEventGenerationIdsProvider.getEventGenerationIds(eq(ACCOUNT_ID), any(Optional.class))).thenReturn(new UmsEventGenerationIds());
    when(userSyncForStackService.synchronizeStack(stack1, umsUsersState1, options)).thenReturn(new SyncStatusDetail(ENV_CRN, SynchronizationStatus.FAILED, "fial1", ImmutableMultimap.of(ENV_CRN, "failed1")));
    when(userSyncForStackService.synchronizeStack(stack2, umsUsersState2, options)).thenReturn(new SyncStatusDetail(ENV_CRN_2, SynchronizationStatus.REJECTED, "fial2", ImmutableMultimap.of(ENV_CRN_2, "failed2")));
    underTest.synchronizeUsers(OPERATION_ID, ACCOUNT_ID, List.of(stack1, stack2), userSyncFilter, options, System.currentTimeMillis());
    verifyNoInteractions(userSyncStatusService);
    ArgumentCaptor<Collection> successCaptor = ArgumentCaptor.forClass(Collection.class);
    ArgumentCaptor<Collection> failureCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(operationService).completeOperation(eq(ACCOUNT_ID), eq(OPERATION_ID), successCaptor.capture(), failureCaptor.capture());
    assertTrue(successCaptor.getValue().isEmpty());
    List<FailureDetails> failures = (List<FailureDetails>) failureCaptor.getValue();
    assertThat(failures, allOf(hasItem(allOf(hasProperty("environment", is(ENV_CRN)), hasProperty("message", is("fial1")), hasProperty("additionalDetails", hasEntry(ENV_CRN, "failed1")))), hasItem(allOf(hasProperty("environment", is(ENV_CRN_2)), hasProperty("message", is("Unexpected status: REJECTED")), hasProperty("additionalDetails", hasEntry(ENV_CRN_2, "failed2"))))));
}
Also used : Optional(java.util.Optional) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) Callable(java.util.concurrent.Callable) Stack(com.sequenceiq.freeipa.entity.Stack) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) Future(java.util.concurrent.Future) Collection(java.util.Collection) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 9 with SyncStatusDetail

use of com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail in project cloudbreak by hortonworks.

the class UserSyncForEnvServiceTest method testSyncUsersDoesntTimeout.

@Test
public void testSyncUsersDoesntTimeout() {
    ReflectionTestUtils.setField(underTest, "operationTimeout", 0L);
    Stack stack1 = mock(Stack.class);
    when(stack1.getEnvironmentCrn()).thenReturn(ENV_CRN);
    UserSyncRequestFilter userSyncFilter = new UserSyncRequestFilter(Set.of(), Set.of(), Optional.empty());
    UserSyncOptions options = new UserSyncOptions(true, true, WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    doAnswer(inv -> {
        inv.getArgument(2, Runnable.class).run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(OPERATION_ID), eq(ACCOUNT_ID), any(Runnable.class));
    UmsUsersState umsUsersState1 = mock(UmsUsersState.class);
    when(umsUsersStateProviderDispatcher.getEnvToUmsUsersStateMap(eq(ACCOUNT_ID), eq(Set.of(ENV_CRN)), eq(Set.of()), eq(Set.of()), any())).thenReturn(Map.of(ENV_CRN, umsUsersState1));
    when(userSyncForStackService.synchronizeStack(stack1, umsUsersState1, options)).thenReturn(new SyncStatusDetail(ENV_CRN, SynchronizationStatus.COMPLETED, "", ImmutableMultimap.of()));
    Future<SyncStatusDetail> future = mock(Future.class);
    when(asyncTaskExecutor.submit(any(Callable.class))).thenAnswer(inv -> {
        SyncStatusDetail result = (SyncStatusDetail) inv.getArgument(0, Callable.class).call();
        when(future.get(anyLong(), eq(TimeUnit.MILLISECONDS))).thenReturn(result);
        return future;
    });
    when(umsEventGenerationIdsProvider.getEventGenerationIds(eq(ACCOUNT_ID), any(Optional.class))).thenReturn(new UmsEventGenerationIds());
    when(userSyncStatusService.getOrCreateForStack(stack1)).thenReturn(new UserSyncStatus());
    when(entitlementService.isUserSyncThreadTimeoutEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    underTest.synchronizeUsers(OPERATION_ID, ACCOUNT_ID, List.of(stack1), userSyncFilter, options, System.currentTimeMillis());
    ArgumentCaptor<Collection> successCaptor = ArgumentCaptor.forClass(Collection.class);
    ArgumentCaptor<Collection> failureCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(operationService).completeOperation(eq(ACCOUNT_ID), eq(OPERATION_ID), successCaptor.capture(), failureCaptor.capture());
    verify(future, never()).cancel(true);
    verify(future, never()).cancel(false);
    assertTrue(failureCaptor.getValue().isEmpty());
    assertTrue(successCaptor.getValue().contains(new SuccessDetails(ENV_CRN)));
}
Also used : SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) Optional(java.util.Optional) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) UserSyncStatus(com.sequenceiq.freeipa.entity.UserSyncStatus) Callable(java.util.concurrent.Callable) Stack(com.sequenceiq.freeipa.entity.Stack) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 10 with SyncStatusDetail

use of com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail in project cloudbreak by hortonworks.

the class UserSyncForStackServiceTest method testSynchronizeStackSuccessFullAtFirst.

@Test
public void testSynchronizeStackSuccessFullAtFirst() throws Exception {
    UmsUsersState umsUsersState = mock(UmsUsersState.class);
    UserSyncOptions options = new UserSyncOptions(true, false, WorkloadCredentialsUpdateType.FORCE_UPDATE);
    UsersState usersState = mock(UsersState.class);
    when(usersState.getUsers()).thenReturn(ImmutableSet.of());
    when(usersState.getGroups()).thenReturn(ImmutableSet.of());
    when(freeIpaUsersStateProvider.getUsersState(FREE_IPA_CLIENT)).thenReturn(usersState);
    UsersStateDifference usersStateDifference = mock(UsersStateDifference.class);
    when(userStateDifferenceCalculator.fromUmsAndIpaUsersStates(umsUsersState, usersState, options)).thenReturn(usersStateDifference);
    when(entitlementService.cloudIdentityMappingEnabled(ACCOUNT)).thenReturn(TRUE);
    when(entitlementService.isEnvironmentPrivilegedUserEnabled(ACCOUNT)).thenReturn(TRUE);
    SyncStatusDetail result = underTest.synchronizeStack(STACK, umsUsersState, options);
    verify(freeIpaUsersStateProvider, never()).getFilteredFreeIpaState(any(), any());
    verify(stateApplier).applyDifference(eq(umsUsersState), eq(ENV_CRN), any(), eq(usersStateDifference), eq(options), eq(FREE_IPA_CLIENT));
    verifyNoMoreInteractions(stateApplier);
    verify(cloudIdentitySyncService).syncCloudIdentities(eq(STACK), eq(umsUsersState), any());
    verify(sudoRuleService).setupSudoRule(STACK, FREE_IPA_CLIENT);
    assertEquals(ENV_CRN, result.getEnvironmentCrn());
    assertEquals(COMPLETED, result.getStatus());
    assertTrue(result.getWarnings().isEmpty());
}
Also used : SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) UsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UsersState) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) UsersStateDifference(com.sequenceiq.freeipa.service.freeipa.user.model.UsersStateDifference) Test(org.junit.jupiter.api.Test)

Aggregations

SyncStatusDetail (com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail)18 UmsUsersState (com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState)13 Test (org.junit.jupiter.api.Test)13 UserSyncOptions (com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions)11 Stack (com.sequenceiq.freeipa.entity.Stack)7 UsersState (com.sequenceiq.freeipa.service.freeipa.user.model.UsersState)7 SuccessDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails)6 UsersStateDifference (com.sequenceiq.freeipa.service.freeipa.user.model.UsersStateDifference)6 Collection (java.util.Collection)6 Future (java.util.concurrent.Future)6 UserSyncStatus (com.sequenceiq.freeipa.entity.UserSyncStatus)5 UmsEventGenerationIds (com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds)5 FailureDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails)4 FreeIpaClientException (com.sequenceiq.freeipa.client.FreeIpaClientException)4 Callable (java.util.concurrent.Callable)4 TimeoutException (java.util.concurrent.TimeoutException)4 Json (com.sequenceiq.cloudbreak.common.json.Json)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 ExecutionException (java.util.concurrent.ExecutionException)3