Search in sources :

Example 26 with UmsUsersState

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

the class UserSyncForEnvService method startAsyncSyncsForStacks.

private Map<String, Future<SyncStatusDetail>> startAsyncSyncsForStacks(String operationId, String accountId, List<Stack> stacks, UserSyncRequestFilter userSyncFilter, UserSyncOptions options, Set<String> environmentCrns) {
    if (userSyncFilter.getDeletedWorkloadUser().isEmpty()) {
        UserSyncLogEvent logRetrieveUmsEvent = options.isFullSync() ? RETRIEVE_FULL_UMS_STATE : RETRIEVE_PARTIAL_UMS_STATE;
        LOGGER.debug("Starting {} for environments {} ...", logRetrieveUmsEvent, environmentCrns);
        Map<String, UmsUsersState> envToUmsStateMap = umsUsersStateProviderDispatcher.getEnvToUmsUsersStateMap(accountId, environmentCrns, userSyncFilter.getUserCrnFilter(), userSyncFilter.getMachineUserCrnFilter(), MDCUtils.getRequestId());
        LOGGER.debug("Finished {}.", logRetrieveUmsEvent);
        UmsEventGenerationIds umsEventGenerationIds = options.isFullSync() ? umsEventGenerationIdsProvider.getEventGenerationIds(accountId, MDCUtils.getRequestId()) : null;
        return stacks.stream().collect(Collectors.toMap(Stack::getEnvironmentCrn, stack -> asyncSynchronizeStack(stack, envToUmsStateMap.get(stack.getEnvironmentCrn()), umsEventGenerationIds, options, operationId, accountId)));
    } else {
        String deletedWorkloadUser = userSyncFilter.getDeletedWorkloadUser().get();
        return stacks.stream().collect(Collectors.toMap(Stack::getEnvironmentCrn, stack -> asyncSynchronizeStackForDeleteUser(stack, deletedWorkloadUser)));
    }
}
Also used : EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) PARTIAL_USER_SYNC(com.sequenceiq.freeipa.service.freeipa.user.UserSyncLogEvent.PARTIAL_USER_SYNC) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Multimap(com.google.common.collect.Multimap) UmsUsersStateProviderDispatcher(com.sequenceiq.freeipa.service.freeipa.user.ums.UmsUsersStateProviderDispatcher) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) Future(java.util.concurrent.Future) Service(org.springframework.stereotype.Service) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) UsersyncConfig(com.sequenceiq.freeipa.configuration.UsersyncConfig) RETRIEVE_FULL_UMS_STATE(com.sequenceiq.freeipa.service.freeipa.user.UserSyncLogEvent.RETRIEVE_FULL_UMS_STATE) UmsEventGenerationIdsProvider(com.sequenceiq.freeipa.service.freeipa.user.ums.UmsEventGenerationIdsProvider) SynchronizationStatus(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SynchronizationStatus) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) ExecutorService(java.util.concurrent.ExecutorService) Stack(com.sequenceiq.freeipa.entity.Stack) Logger(org.slf4j.Logger) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) UserSyncOptions(com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) Set(java.util.Set) UserSyncStatus(com.sequenceiq.freeipa.entity.UserSyncStatus) FULL_USER_SYNC(com.sequenceiq.freeipa.service.freeipa.user.UserSyncLogEvent.FULL_USER_SYNC) Collectors(java.util.stream.Collectors) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) OperationService(com.sequenceiq.freeipa.service.operation.OperationService) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Json(com.sequenceiq.cloudbreak.common.json.Json) RETRIEVE_PARTIAL_UMS_STATE(com.sequenceiq.freeipa.service.freeipa.user.UserSyncLogEvent.RETRIEVE_PARTIAL_UMS_STATE) List(java.util.List) MDCUtils(com.sequenceiq.cloudbreak.logger.MDCUtils) SuccessDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState)

Example 27 with UmsUsersState

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

the class ActorHandler method handleActor.

public void handleActor(EnvironmentAccessRights environmentAccessRights, FmsUser fmsUser, String actorCrn, Supplier<Collection<String>> groupCrnMembershipSupplier, Supplier<Collection<String>> wagMembershipSupplier, Supplier<WorkloadCredential> workloadCredentialSupplier, List<UserManagementProto.CloudIdentity> cloudIdentityList) {
    if (environmentAccessRights.hasEnvironmentAccessRight()) {
        String workloadUsername = fmsUser.getName();
        // Retrieve all information from UMS before modifying to the UmsUsersState or UsersState. This is so that
        // we don't partially modify the state if the member has been deleted after we started the sync
        Collection<String> groupCrnsForMember = groupCrnMembershipSupplier.get();
        Collection<String> workloadAdministrationGroupsForMember = wagMembershipSupplier.get();
        WorkloadCredential workloadCredential = workloadCredentialSupplier.get();
        groupCrnsForMember.forEach(gcrn -> {
            FmsGroup group = crnToFmsGroup.get(gcrn);
            // the group and group membership will be updated on the next sync
            if (group != null) {
                usersStateBuilder.addMemberToGroup(group.getName(), workloadUsername);
            } else {
                LOGGER.warn("{} is a member of unexpected group {}. Group must have been added after UMS state calculation started", workloadUsername, gcrn);
            }
        });
        workloadAdministrationGroupsForMember.stream().filter(wagName -> !wagNamesForOtherEnvironments.contains(wagName)).forEach(wagName -> {
            usersStateBuilder.addGroup(fmsGroupConverter.nameToGroup(wagName));
            usersStateBuilder.addMemberToGroup(wagName, workloadUsername);
        });
        addMemberToInternalTrackingGroup(usersStateBuilder, workloadUsername);
        if (environmentAccessRights.hasAdminFreeIpaRight()) {
            usersStateBuilder.addMemberToGroup(UserSyncConstants.ADMINS_GROUP, workloadUsername);
        }
        umsUsersStateBuilder.addWorkloadCredentials(workloadUsername, workloadCredential);
        umsUsersStateBuilder.addUserCloudIdentities(workloadUsername, cloudIdentityList);
        usersStateBuilder.addUserMetadata(workloadUsername, new UserMetadata(actorCrn, workloadCredential.getVersion()));
        usersStateBuilder.addUser(fmsUser);
    }
}
Also used : FmsGroup(com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) Logger(org.slf4j.Logger) Collection(java.util.Collection) WorkloadCredential(com.sequenceiq.freeipa.service.freeipa.user.model.WorkloadCredential) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) FmsGroupConverter(com.sequenceiq.freeipa.service.freeipa.user.conversion.FmsGroupConverter) FmsUser(com.sequenceiq.freeipa.service.freeipa.user.model.FmsUser) Supplier(java.util.function.Supplier) List(java.util.List) UserManagementProto(com.cloudera.thunderhead.service.usermanagement.UserManagementProto) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) UsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UsersState) UserMetadata(com.sequenceiq.freeipa.service.freeipa.user.model.UserMetadata) EnvironmentAccessRights(com.sequenceiq.freeipa.service.freeipa.user.model.EnvironmentAccessRights) UserSyncConstants(com.sequenceiq.freeipa.service.freeipa.user.UserSyncConstants) FmsGroup(com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup) UserMetadata(com.sequenceiq.freeipa.service.freeipa.user.model.UserMetadata) WorkloadCredential(com.sequenceiq.freeipa.service.freeipa.user.model.WorkloadCredential)

Example 28 with UmsUsersState

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

the class UserSyncServiceTest method testFilteredSyncRetrievesFilteredIpaState.

@Test
void testFilteredSyncRetrievesFilteredIpaState() throws Exception {
    UmsUsersState umsUsersState = mock(UmsUsersState.class);
    ImmutableSet<String> workloadUsers = mock(ImmutableSet.class);
    when(umsUsersState.getRequestedWorkloadUsernames()).thenReturn(workloadUsers);
    underTest.getIpaUserState(freeIpaClient, umsUsersState, false);
    verify(freeIpaUsersStateProvider).getFilteredFreeIpaState(any(), eq(workloadUsers));
}
Also used : UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 29 with UmsUsersState

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

the class UserSyncService method internalSynchronizeUsers.

private void internalSynchronizeUsers(String operationId, String accountId, List<Stack> stacks, UserSyncRequestFilter userSyncFilter, UserSyncOptions options) {
    tryWithOperationCleanup(operationId, accountId, () -> {
        Set<String> environmentCrns = stacks.stream().map(Stack::getEnvironmentCrn).collect(Collectors.toSet());
        Optional<String> requestId = MDCUtils.getRequestId();
        UmsEventGenerationIds umsEventGenerationIds = options.isFullSync() ? umsEventGenerationIdsProvider.getEventGenerationIds(accountId, requestId) : null;
        LogEvent logUserSyncEvent = options.isFullSync() ? LogEvent.FULL_USER_SYNC : LogEvent.PARTIAL_USER_SYNC;
        LOGGER.info("Starting {} for environments {} with operationId {} ...", logUserSyncEvent, environmentCrns, operationId);
        Map<String, Future<SyncStatusDetail>> statusFutures;
        if (userSyncFilter.getDeletedWorkloadUser().isEmpty()) {
            LogEvent logRetrieveUmsEvent = options.isFullSync() ? LogEvent.RETRIEVE_FULL_UMS_STATE : LogEvent.RETRIEVE_PARTIAL_UMS_STATE;
            LOGGER.debug("Starting {} for environments {} ...", logRetrieveUmsEvent, environmentCrns);
            Map<String, UmsUsersState> envToUmsStateMap = umsUsersStateProviderDispatcher.getEnvToUmsUsersStateMap(accountId, environmentCrns, userSyncFilter.getUserCrnFilter(), userSyncFilter.getMachineUserCrnFilter(), requestId);
            LOGGER.debug("Finished {}.", logRetrieveUmsEvent);
            statusFutures = stacks.stream().collect(Collectors.toMap(Stack::getEnvironmentCrn, stack -> asyncSynchronizeStack(stack, envToUmsStateMap.get(stack.getEnvironmentCrn()), umsEventGenerationIds, options, operationId, accountId)));
        } else {
            String deletedWorkloadUser = userSyncFilter.getDeletedWorkloadUser().get();
            statusFutures = stacks.stream().collect(Collectors.toMap(Stack::getEnvironmentCrn, stack -> asyncSynchronizeStackForDeleteUser(stack, deletedWorkloadUser)));
        }
        List<SuccessDetails> success = new ArrayList<>();
        List<FailureDetails> failure = new ArrayList<>();
        statusFutures.forEach((envCrn, statusFuture) -> {
            try {
                SyncStatusDetail statusDetail = statusFuture.get();
                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 (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) UmsUsersState(com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState) ArrayList(java.util.ArrayList) FailureDetails(com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails) Stack(com.sequenceiq.freeipa.entity.Stack) UmsEventGenerationIds(com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds) SyncStatusDetail(com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

Example 30 with UmsUsersState

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

the class UserSyncService method compareUmsAndFreeIpa.

private UsersStateDifference compareUmsAndFreeIpa(UmsUsersState umsUsersState, UserSyncOptions options, FreeIpaClient freeIpaClient) throws FreeIpaClientException {
    LogEvent logEvent = options.isFullSync() ? LogEvent.RETRIEVE_FULL_IPA_STATE : LogEvent.RETRIEVE_PARTIAL_IPA_STATE;
    LOGGER.debug("Starting {} ...", logEvent);
    UsersState ipaUsersState = getIpaUserState(freeIpaClient, umsUsersState, options.isFullSync());
    LOGGER.debug("Finished {}, found {} users and {} groups.", logEvent, ipaUsersState.getUsers().size(), ipaUsersState.getGroups().size());
    LOGGER.debug("Starting {} ...", LogEvent.CALCULATE_UMS_IPA_DIFFERENCE);
    UsersStateDifference usersStateDifference = UsersStateDifference.fromUmsAndIpaUsersStates(umsUsersState, ipaUsersState, options);
    LOGGER.debug("Finished {}.", LogEvent.CALCULATE_UMS_IPA_DIFFERENCE);
    return usersStateDifference;
}
Also used : 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)

Aggregations

UmsUsersState (com.sequenceiq.freeipa.service.freeipa.user.model.UmsUsersState)30 Test (org.junit.jupiter.api.Test)23 UsersState (com.sequenceiq.freeipa.service.freeipa.user.model.UsersState)19 UserSyncOptions (com.sequenceiq.freeipa.service.freeipa.user.model.UserSyncOptions)14 SyncStatusDetail (com.sequenceiq.freeipa.service.freeipa.user.model.SyncStatusDetail)11 UsersStateDifference (com.sequenceiq.freeipa.service.freeipa.user.model.UsersStateDifference)9 Stack (com.sequenceiq.freeipa.entity.Stack)8 UmsEventGenerationIds (com.sequenceiq.freeipa.service.freeipa.user.model.UmsEventGenerationIds)7 Collection (java.util.Collection)7 FailureDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.FailureDetails)5 List (java.util.List)5 Optional (java.util.Optional)5 Callable (java.util.concurrent.Callable)5 Future (java.util.concurrent.Future)5 SuccessDetails (com.sequenceiq.freeipa.api.v1.freeipa.user.model.SuccessDetails)4 FmsGroup (com.sequenceiq.freeipa.service.freeipa.user.model.FmsGroup)4 FmsUser (com.sequenceiq.freeipa.service.freeipa.user.model.FmsUser)4 Set (java.util.Set)4 TimeoutException (java.util.concurrent.TimeoutException)4 FreeIpaClientException (com.sequenceiq.freeipa.client.FreeIpaClientException)3