Search in sources :

Example 41 with Operation

use of com.sequenceiq.freeipa.entity.Operation in project cloudbreak by hortonworks.

the class UserV1Controller method setPassword.

@Override
@CustomPermissionCheck
public SyncOperationStatus setPassword(SetPasswordRequest request) {
    String userCrn = checkActorCrn();
    String accountId = ThreadBasedUserCrnProvider.getAccountId();
    LOGGER.debug("setPassword() requested for user {} in account {}", userCrn, accountId);
    Operation setPasswordOperation = passwordService.setPasswordWithCustomPermissionCheck(accountId, userCrn, request.getPassword(), nullToEmpty(request.getEnvironments()), AuthorizationResourceAction.DESCRIBE_ENVIRONMENT);
    return checkOperationRejected(operationToSyncOperationStatus.convert(setPasswordOperation));
}
Also used : Operation(com.sequenceiq.freeipa.entity.Operation) CustomPermissionCheck(com.sequenceiq.authorization.annotation.CustomPermissionCheck)

Example 42 with Operation

use of com.sequenceiq.freeipa.entity.Operation in project cloudbreak by hortonworks.

the class UserSyncServiceTest method createRunningOperation.

private Operation createRunningOperation() {
    Operation operation = new Operation();
    operation.setOperationId(UUID.randomUUID().toString());
    operation.setAccountId(ACCOUNT_ID);
    operation.setStatus(OperationState.RUNNING);
    operation.setOperationType(OperationType.USER_SYNC);
    return operation;
}
Also used : Operation(com.sequenceiq.freeipa.entity.Operation)

Example 43 with Operation

use of com.sequenceiq.freeipa.entity.Operation in project cloudbreak by hortonworks.

the class UserSyncServiceTest method testSyncUsersWithTimeoutCheckTaskRunnable.

@Test
public void testSyncUsersWithTimeoutCheckTaskRunnable() {
    Stack stack = mock(Stack.class);
    when(stack.getEnvironmentCrn()).thenReturn(ENV_CRN);
    when(stackService.getMultipleByEnvironmentCrnOrChildEnvironmantCrnAndAccountId(Set.of(), ACCOUNT_ID)).thenReturn(List.of(stack));
    Operation operation = createRunningOperation();
    when(operationService.startOperation(anyString(), any(OperationType.class), anyCollection(), anyCollection())).thenReturn(operation);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(2, Runnable.class);
        runnable.run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(operation.getOperationId()), eq(ACCOUNT_ID), any(Runnable.class));
    when(regionAwareInternalCrnGenerator.getInternalCrnForServiceAsString()).thenReturn(INTERNAL_ACTOR);
    when(regionAwareInternalCrnGeneratorFactory.iam()).thenReturn(regionAwareInternalCrnGenerator);
    UserSyncStatus userSyncStatus = new UserSyncStatus();
    when(userSyncStatusService.getOrCreateForStack(stack)).thenReturn(userSyncStatus);
    when(entitlementService.usersyncCredentialsUpdateOptimizationEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    when(entitlementService.isFmsToFreeipaBatchCallEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    when(entitlementService.isUserSyncThreadTimeoutEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    ReflectionTestUtils.setField(underTest, "operationTimeout", TIMEOUT);
    Future<?> usersyncTask = mock(Future.class);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(0, Runnable.class);
        assertEquals(operation.getOperationId(), MDCBuilder.getMdcContextMap().get(LoggerContextKey.OPERATION_ID.toString()));
        assertEquals(INTERNAL_ACTOR, ThreadBasedUserCrnProvider.getUserCrn());
        runnable.run();
        return usersyncTask;
    }).when(asyncTaskExecutor).submit(any(Runnable.class));
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(0, Runnable.class);
        runnable.run();
        return null;
    }).when(scheduledExecutorService).schedule(any(Runnable.class), eq(TIMEOUT), eq(TimeUnit.MILLISECONDS));
    Operation result = underTest.synchronizeUsers(ACCOUNT_ID, ACTOR_CRN, Set.of(), Set.of(), Set.of(), WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    assertEquals(operation, result);
    ArgumentCaptor<UserSyncRequestFilter> requestFilterCaptor = ArgumentCaptor.forClass(UserSyncRequestFilter.class);
    verify(userSyncRequestValidator).validateParameters(eq(ACCOUNT_ID), eq(ACTOR_CRN), eq(Set.of()), requestFilterCaptor.capture());
    UserSyncRequestFilter requestFilter = requestFilterCaptor.getValue();
    assertTrue(requestFilter.getUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getMachineUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getDeletedWorkloadUser().isEmpty());
    assertEquals(operation, userSyncStatus.getLastStartedFullSync());
    verify(userSyncStatusService).save(userSyncStatus);
    ArgumentCaptor<UserSyncOptions> syncOptionsCaptor = ArgumentCaptor.forClass(UserSyncOptions.class);
    verify(userSyncForEnvService).synchronizeUsers(eq(operation.getOperationId()), eq(ACCOUNT_ID), eq(List.of(stack)), eq(requestFilter), syncOptionsCaptor.capture(), anyLong());
    UserSyncOptions userSyncOptions = syncOptionsCaptor.getValue();
    assertTrue(userSyncOptions.isFullSync());
    assertTrue(userSyncOptions.isCredentialsUpdateOptimizationEnabled());
    assertTrue(userSyncOptions.isFmsToFreeIpaBatchCallEnabled());
    verify(usersyncTask).cancel(true);
    verify(usersyncTask, never()).cancel(false);
    verify(usersyncTask).isCancelled();
    verify(usersyncTask).isDone();
    verify(operationService).timeout(operation.getOperationId(), ACCOUNT_ID);
}
Also used : UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) UserSyncStatus(com.sequenceiq.freeipa.entity.UserSyncStatus) Operation(com.sequenceiq.freeipa.entity.Operation) OperationType(com.sequenceiq.freeipa.api.v1.operation.model.OperationType) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 44 with Operation

use of com.sequenceiq.freeipa.entity.Operation in project cloudbreak by hortonworks.

the class UserSyncServiceTest method testSyncUsers.

@Test
public void testSyncUsers() {
    Stack stack = mock(Stack.class);
    when(stack.getEnvironmentCrn()).thenReturn(ENV_CRN);
    when(stackService.getMultipleByEnvironmentCrnOrChildEnvironmantCrnAndAccountId(Set.of(), ACCOUNT_ID)).thenReturn(List.of(stack));
    Operation operation = createRunningOperation();
    when(operationService.startOperation(anyString(), any(OperationType.class), anyCollection(), anyCollection())).thenReturn(operation);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(2, Runnable.class);
        runnable.run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(operation.getOperationId()), eq(ACCOUNT_ID), any(Runnable.class));
    when(regionAwareInternalCrnGenerator.getInternalCrnForServiceAsString()).thenReturn(INTERNAL_ACTOR);
    when(regionAwareInternalCrnGeneratorFactory.iam()).thenReturn(regionAwareInternalCrnGenerator);
    UserSyncStatus userSyncStatus = new UserSyncStatus();
    when(userSyncStatusService.getOrCreateForStack(stack)).thenReturn(userSyncStatus);
    when(entitlementService.usersyncCredentialsUpdateOptimizationEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    when(entitlementService.isFmsToFreeipaBatchCallEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(0, Runnable.class);
        assertEquals(operation.getOperationId(), MDCBuilder.getMdcContextMap().get(LoggerContextKey.OPERATION_ID.toString()));
        assertEquals(INTERNAL_ACTOR, ThreadBasedUserCrnProvider.getUserCrn());
        runnable.run();
        return mock(Future.class);
    }).when(asyncTaskExecutor).submit(any(Runnable.class));
    Operation result = underTest.synchronizeUsers(ACCOUNT_ID, ACTOR_CRN, Set.of(), Set.of(), Set.of(), WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    assertEquals(operation, result);
    ArgumentCaptor<UserSyncRequestFilter> requestFilterCaptor = ArgumentCaptor.forClass(UserSyncRequestFilter.class);
    verify(userSyncRequestValidator).validateParameters(eq(ACCOUNT_ID), eq(ACTOR_CRN), eq(Set.of()), requestFilterCaptor.capture());
    UserSyncRequestFilter requestFilter = requestFilterCaptor.getValue();
    assertTrue(requestFilter.getUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getMachineUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getDeletedWorkloadUser().isEmpty());
    assertEquals(operation, userSyncStatus.getLastStartedFullSync());
    verify(userSyncStatusService).save(userSyncStatus);
    ArgumentCaptor<UserSyncOptions> syncOptionsCaptor = ArgumentCaptor.forClass(UserSyncOptions.class);
    verify(userSyncForEnvService).synchronizeUsers(eq(operation.getOperationId()), eq(ACCOUNT_ID), eq(List.of(stack)), eq(requestFilter), syncOptionsCaptor.capture(), anyLong());
    UserSyncOptions userSyncOptions = syncOptionsCaptor.getValue();
    assertTrue(userSyncOptions.isFullSync());
    assertTrue(userSyncOptions.isCredentialsUpdateOptimizationEnabled());
    assertTrue(userSyncOptions.isFmsToFreeIpaBatchCallEnabled());
}
Also used : UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) UserSyncStatus(com.sequenceiq.freeipa.entity.UserSyncStatus) Operation(com.sequenceiq.freeipa.entity.Operation) OperationType(com.sequenceiq.freeipa.api.v1.operation.model.OperationType) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Example 45 with Operation

use of com.sequenceiq.freeipa.entity.Operation in project cloudbreak by hortonworks.

the class UserSyncServiceTest method testSyncUsersWithTimeoutCheckTaskFinished.

@Test
public void testSyncUsersWithTimeoutCheckTaskFinished() {
    Stack stack = mock(Stack.class);
    when(stack.getEnvironmentCrn()).thenReturn(ENV_CRN);
    when(stackService.getMultipleByEnvironmentCrnOrChildEnvironmantCrnAndAccountId(Set.of(), ACCOUNT_ID)).thenReturn(List.of(stack));
    Operation operation = createRunningOperation();
    when(operationService.startOperation(anyString(), any(OperationType.class), anyCollection(), anyCollection())).thenReturn(operation);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(2, Runnable.class);
        runnable.run();
        return null;
    }).when(operationService).tryWithOperationCleanup(eq(operation.getOperationId()), eq(ACCOUNT_ID), any(Runnable.class));
    when(regionAwareInternalCrnGenerator.getInternalCrnForServiceAsString()).thenReturn(INTERNAL_ACTOR);
    when(regionAwareInternalCrnGeneratorFactory.iam()).thenReturn(regionAwareInternalCrnGenerator);
    UserSyncStatus userSyncStatus = new UserSyncStatus();
    when(userSyncStatusService.getOrCreateForStack(stack)).thenReturn(userSyncStatus);
    when(entitlementService.usersyncCredentialsUpdateOptimizationEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    when(entitlementService.isFmsToFreeipaBatchCallEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    when(entitlementService.isUserSyncThreadTimeoutEnabled(ACCOUNT_ID)).thenReturn(Boolean.TRUE);
    ReflectionTestUtils.setField(underTest, "operationTimeout", TIMEOUT);
    Future<?> usersyncTask = mock(Future.class);
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(0, Runnable.class);
        assertEquals(operation.getOperationId(), MDCBuilder.getMdcContextMap().get(LoggerContextKey.OPERATION_ID.toString()));
        assertEquals(INTERNAL_ACTOR, ThreadBasedUserCrnProvider.getUserCrn());
        runnable.run();
        return usersyncTask;
    }).when(asyncTaskExecutor).submit(any(Runnable.class));
    doAnswer(inv -> {
        Runnable runnable = inv.getArgument(0, Runnable.class);
        runnable.run();
        return null;
    }).when(scheduledExecutorService).schedule(any(Runnable.class), eq(TIMEOUT), eq(TimeUnit.MILLISECONDS));
    when(usersyncTask.isDone()).thenReturn(Boolean.TRUE);
    Operation result = underTest.synchronizeUsers(ACCOUNT_ID, ACTOR_CRN, Set.of(), Set.of(), Set.of(), WorkloadCredentialsUpdateType.UPDATE_IF_CHANGED);
    assertEquals(operation, result);
    ArgumentCaptor<UserSyncRequestFilter> requestFilterCaptor = ArgumentCaptor.forClass(UserSyncRequestFilter.class);
    verify(userSyncRequestValidator).validateParameters(eq(ACCOUNT_ID), eq(ACTOR_CRN), eq(Set.of()), requestFilterCaptor.capture());
    UserSyncRequestFilter requestFilter = requestFilterCaptor.getValue();
    assertTrue(requestFilter.getUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getMachineUserCrnFilter().isEmpty());
    assertTrue(requestFilter.getDeletedWorkloadUser().isEmpty());
    assertEquals(operation, userSyncStatus.getLastStartedFullSync());
    verify(userSyncStatusService).save(userSyncStatus);
    ArgumentCaptor<UserSyncOptions> syncOptionsCaptor = ArgumentCaptor.forClass(UserSyncOptions.class);
    verify(userSyncForEnvService).synchronizeUsers(eq(operation.getOperationId()), eq(ACCOUNT_ID), eq(List.of(stack)), eq(requestFilter), syncOptionsCaptor.capture(), anyLong());
    UserSyncOptions userSyncOptions = syncOptionsCaptor.getValue();
    assertTrue(userSyncOptions.isFullSync());
    assertTrue(userSyncOptions.isCredentialsUpdateOptimizationEnabled());
    assertTrue(userSyncOptions.isFmsToFreeIpaBatchCallEnabled());
    verify(usersyncTask, never()).cancel(true);
    verify(usersyncTask, never()).cancel(false);
    verify(usersyncTask).isCancelled();
    verify(usersyncTask).isDone();
    verify(operationService, never()).timeout(anyString(), anyString());
}
Also used : UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) UserSyncStatus(com.sequenceiq.freeipa.entity.UserSyncStatus) Operation(com.sequenceiq.freeipa.entity.Operation) OperationType(com.sequenceiq.freeipa.api.v1.operation.model.OperationType) Stack(com.sequenceiq.freeipa.entity.Stack) Test(org.junit.jupiter.api.Test)

Aggregations

Operation (com.sequenceiq.freeipa.entity.Operation)88 Test (org.junit.jupiter.api.Test)55 Stack (com.sequenceiq.freeipa.entity.Stack)23 UserSyncStatus (com.sequenceiq.freeipa.entity.UserSyncStatus)18 SyncOperationStatus (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SyncOperationStatus)15 OperationToSyncOperationStatus (com.sequenceiq.freeipa.converter.freeipa.user.OperationToSyncOperationStatus)12 AcceptResult (com.sequenceiq.freeipa.service.freeipa.user.AcceptResult)11 UserSyncRequestFilter (com.sequenceiq.freeipa.service.freeipa.user.UserSyncRequestFilter)10 InstanceMetaData (com.sequenceiq.freeipa.entity.InstanceMetaData)9 FlowIdentifier (com.sequenceiq.flow.api.model.FlowIdentifier)8 EnvironmentUserSyncState (com.sequenceiq.freeipa.api.v1.freeipa.user.model.EnvironmentUserSyncState)7 OperationType (com.sequenceiq.freeipa.api.v1.operation.model.OperationType)7 OperationStatus (com.sequenceiq.freeipa.api.v1.operation.model.OperationStatus)6 UserSyncOptions (com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Acceptable (com.sequenceiq.cloudbreak.common.event.Acceptable)5 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)5 FreeIpaUpgradeResponse (com.sequenceiq.freeipa.api.v1.freeipa.upgrade.model.FreeIpaUpgradeResponse)4 SynchronizeAllUsersRequest (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SynchronizeAllUsersRequest)4 CustomPermissionCheck (com.sequenceiq.authorization.annotation.CustomPermissionCheck)3