Search in sources :

Example 46 with IcConnectorObject

use of eu.bcvsolutions.idm.ic.api.IcConnectorObject in project CzechIdMng by bcvsolutions.

the class RoleCatalogueSynchronizationExecutor method processTreeSync.

/**
 * Execute sync for catalogue and given accounts.
 *
 * @param context
 * @param accountsMap
 */
private void processTreeSync(SynchronizationContext context, Map<String, IcConnectorObject> accountsMap) {
    AbstractSysSyncConfigDto config = context.getConfig();
    SystemEntityType entityType = context.getEntityType();
    SysSystemDto system = context.getSystem();
    List<SysSystemAttributeMappingDto> mappedAttributes = context.getMappedAttributes();
    SysSyncLogDto log = context.getLog();
    List<SysSyncActionLogDto> actionsLog = context.getActionLogs();
    AttributeMapping tokenAttribute = context.getTokenAttribute();
    // Find UID/PARENT/CODE attribute
    SysSystemAttributeMappingDto uidAttribute = attributeHandlingService.getUidAttribute(mappedAttributes, system);
    SysSystemAttributeMappingDto parentAttribute = getAttributeByIdmProperty(PARENT_FIELD, mappedAttributes);
    SysSystemAttributeMappingDto codeAttribute = getAttributeByIdmProperty(CODE_FIELD, mappedAttributes);
    if (parentAttribute == null) {
        LOG.warn("Parent attribute is not specified! Role catalogue will not be recomputed.");
    }
    if (codeAttribute == null) {
        LOG.warn("Code attribute is not specified!");
    }
    // Find all roots
    Collection<String> roots = findRoots(parentAttribute, accountsMap, config, context);
    if (roots.isEmpty()) {
        log.addToLog("No roots to synchronization found!");
    } else {
        log.addToLog(MessageFormat.format("We found [{0}] roots: [{1}]", roots.size(), roots));
    }
    if (parentAttribute == null) {
        // just alias all accounts as roots and process
        roots.addAll(accountsMap.keySet());
    }
    Set<String> accountsUseInTreeList = new HashSet<>(roots.size());
    for (String root : roots) {
        accountsUseInTreeList.add(root);
        IcConnectorObject account = accountsMap.get(root);
        SynchronizationContext itemContext = cloneItemContext(context);
        // 
        itemContext.addUid(// 
        root).addIcObject(// 
        account).addAccount(// 
        null).addTokenAttribute(// 
        tokenAttribute).addGeneratedUid(// 
        null);
        boolean result = handleIcObject(itemContext);
        if (!result) {
            return;
        }
        if (parentAttribute != null) {
            Object uidValueParent = this.getValueByMappedAttribute(uidAttribute, account.getAttributes(), context);
            processChildren(parentAttribute, uidValueParent, uidAttribute, accountsMap, accountsUseInTreeList, itemContext, roots);
        }
    }
    if (config.isReconciliation()) {
        // We do reconciliation (find missing account)
        startReconciliation(entityType, accountsUseInTreeList, config, system, log, actionsLog);
    }
}
Also used : SysSystemAttributeMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto) SystemEntityType(eu.bcvsolutions.idm.acc.domain.SystemEntityType) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) SysSyncActionLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto) AbstractSysSyncConfigDto(eu.bcvsolutions.idm.acc.dto.AbstractSysSyncConfigDto) SynchronizationContext(eu.bcvsolutions.idm.acc.domain.SynchronizationContext) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) AttributeMapping(eu.bcvsolutions.idm.acc.domain.AttributeMapping) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) SysSyncLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncLogDto) HashSet(java.util.HashSet)

Example 47 with IcConnectorObject

use of eu.bcvsolutions.idm.ic.api.IcConnectorObject in project CzechIdMng by bcvsolutions.

the class RoleCatalogueSynchronizationExecutor method findRoots.

/**
 * Find all roots for this catalogue tree (uses groovy script for root definition)
 *
 * @param parentAttribute
 * @param accountsMap
 * @param config
 * @return
 */
private Collection<String> findRoots(SysSystemAttributeMappingDto parentAttribute, Map<String, IcConnectorObject> accountsMap, AbstractSysSyncConfigDto config, SynchronizationContext context) {
    Set<String> roots = Sets.newHashSet();
    if (parentAttribute == null) {
        return roots;
    }
    accountsMap.forEach((uid, account) -> {
        if (StringUtils.hasLength(config.getRootsFilterScript())) {
            Map<String, Object> variables = new HashMap<>();
            variables.put("account", account);
            List<Class<?>> allowTypes = new ArrayList<>();
            allowTypes.add(IcAttributeImpl.class);
            allowTypes.add(IcAttribute.class);
            allowTypes.add(IcLoginAttributeImpl.class);
            Object isRoot = groovyScriptService.evaluate(config.getRootsFilterScript(), variables, allowTypes);
            if (isRoot != null && !(isRoot instanceof Boolean)) {
                throw new ProvisioningException(AccResultCode.SYNCHRONIZATION_TREE_ROOT_FILTER_VALUE_WRONG_TYPE, ImmutableMap.of("type", isRoot.getClass().getName()));
            }
            if ((Boolean) isRoot) {
                roots.add(uid);
            }
        } else {
            // Default search root strategy: If parent is null or an empty string, then it is a root node.
            // IdM is able to cope only with null parent of the root node. Therefore empty string value is changed to null.
            Object parentValue = super.getValueByMappedAttribute(parentAttribute, account.getAttributes(), context);
            if (parentValue == null) {
                roots.add(uid);
            } else if (StringUtils.isEmpty(parentValue)) {
                SysSchemaAttributeDto schemaAttribute = DtoUtils.getEmbedded(parentAttribute, SysSystemAttributeMapping_.schemaAttribute.getName(), SysSchemaAttributeDto.class);
                IcAttribute attribute = account.getAttributeByName(schemaAttribute.getName());
                if (attribute instanceof IcAttributeImpl) {
                    ((IcAttributeImpl) attribute).setValues(null);
                }
                roots.add(uid);
            }
        }
    });
    return roots;
}
Also used : HashMap(java.util.HashMap) SysSchemaAttributeDto(eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto) ArrayList(java.util.ArrayList) IcAttributeImpl(eu.bcvsolutions.idm.ic.impl.IcAttributeImpl) IcAttribute(eu.bcvsolutions.idm.ic.api.IcAttribute) ProvisioningException(eu.bcvsolutions.idm.acc.exception.ProvisioningException) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) IcObjectClass(eu.bcvsolutions.idm.ic.api.IcObjectClass)

Example 48 with IcConnectorObject

use of eu.bcvsolutions.idm.ic.api.IcConnectorObject in project CzechIdMng by bcvsolutions.

the class RoleCatalogueSynchronizationExecutor method process.

@Override
public AbstractSysSyncConfigDto process(UUID synchronizationConfigId) {
    // Clear cache
    cacheManager.evictCache(CACHE_NAME);
    // Validate and create basic context
    SynchronizationContext context = this.validate(synchronizationConfigId);
    AbstractSysSyncConfigDto config = context.getConfig();
    SysSystemDto system = context.getSystem();
    IcConnectorConfiguration connectorConfig = context.getConnectorConfig();
    SysSystemMappingDto systemMapping = systemMappingService.get(context.getConfig().getSystemMapping());
    SysSchemaObjectClassDto schemaObjectClassDto = schemaObjectClassService.get(systemMapping.getObjectClass());
    IcObjectClass objectClass = new IcObjectClassImpl(schemaObjectClassDto.getObjectClassName());
    // Load last token
    Object lastToken = config.isReconciliation() ? null : config.getToken();
    // Create basic synchronization log
    SysSyncLogDto log = new SysSyncLogDto();
    log.setSynchronizationConfig(config.getId());
    log.setStarted(ZonedDateTime.now());
    log.setRunning(true);
    log.setToken(lastToken != null ? lastToken.toString() : null);
    log.addToLog(MessageFormat.format("Synchronization was started in {0}.", log.getStarted()));
    // List of all accounts with full IC object (used in catalogue sync)
    Map<String, IcConnectorObject> accountsMap = new HashMap<>();
    longRunningTaskExecutor.setCounter(0L);
    try {
        log = synchronizationLogService.save(log);
        List<SysSyncActionLogDto> actionsLog = new ArrayList<>();
        // Add logs to context
        context.addLog(log).addActionLogs(actionsLog);
        if (config.getTokenAttribute() == null && !config.isReconciliation()) {
            throw new ProvisioningException(AccResultCode.SYNCHRONIZATION_TOKEN_ATTRIBUTE_NOT_FOUND);
        }
        TreeResultsHandler resultHandler = new TreeResultsHandler(accountsMap);
        // We have to search all data for tree
        IcFilter filter = null;
        log.addToLog(MessageFormat.format("Start search with filter {0}.", "NONE"));
        log = synchronizationLogService.save(log);
        connectorFacade.search(systemService.getConnectorInstance(system), connectorConfig, objectClass, filter, resultHandler);
        // Execute sync for this catalogue and searched accounts
        processTreeSync(context, accountsMap);
        log = context.getLog();
        // Sync is correctly ends if wasn't cancelled
        if (log.isRunning()) {
            log = syncCorrectlyEnded(log, context);
        }
        // 
        synchronizationConfigService.save(config);
    } catch (Exception e) {
        String message = "Error during synchronization";
        log.addToLog(message);
        log.setContainsError(true);
        log.addToLog(Throwables.getStackTraceAsString(e));
        LOG.error(message, e);
    } finally {
        log.setRunning(false);
        log.setEnded(ZonedDateTime.now());
        log = synchronizationLogService.save(log);
        // 
        longRunningTaskExecutor.setCount(longRunningTaskExecutor.getCounter());
        longRunningTaskExecutor.updateState();
        // Clear cache
        cacheManager.evictCache(CACHE_NAME);
    }
    return config;
}
Also used : IcConnectorConfiguration(eu.bcvsolutions.idm.ic.api.IcConnectorConfiguration) IcObjectClassImpl(eu.bcvsolutions.idm.ic.impl.IcObjectClassImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SysSystemMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) ProvisioningException(eu.bcvsolutions.idm.acc.exception.ProvisioningException) SysSyncActionLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto) AbstractSysSyncConfigDto(eu.bcvsolutions.idm.acc.dto.AbstractSysSyncConfigDto) SynchronizationContext(eu.bcvsolutions.idm.acc.domain.SynchronizationContext) IcObjectClass(eu.bcvsolutions.idm.ic.api.IcObjectClass) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) ProvisioningException(eu.bcvsolutions.idm.acc.exception.ProvisioningException) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) SysSchemaObjectClassDto(eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto) SysSyncLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncLogDto) IcFilter(eu.bcvsolutions.idm.ic.filter.api.IcFilter)

Example 49 with IcConnectorObject

use of eu.bcvsolutions.idm.ic.api.IcConnectorObject in project CzechIdMng by bcvsolutions.

the class TreeSynchronizationExecutor method processChildren.

/**
 * Process recursively tree children
 *
 * @param parentAttribute
 * @param uidValueParent
 * @param uidAttribute
 * @param accountsMap
 * @param accountsUseInTreeList
 * @param context
 */
private void processChildren(SysSystemAttributeMappingDto parentAttribute, Object uidValueParent, SysSystemAttributeMappingDto uidAttribute, Map<String, IcConnectorObject> accountsMap, Set<String> accountsUseInTreeList, SynchronizationContext context, Collection<String> roots) {
    accountsMap.forEach((uid, account) -> {
        if (roots.contains(uid)) {
            return;
        }
        Object parentValue = super.getValueByMappedAttribute(parentAttribute, account.getAttributes(), context);
        if (parentValue != null && parentValue.equals(uidValueParent)) {
            // Account is use in tree
            accountsUseInTreeList.add(uid);
            // Do provisioning for this account
            SynchronizationContext itemContext = cloneItemContext(context);
            // 
            itemContext.addUid(// 
            uid).addIcObject(// 
            account).addAccount(// 
            null).addGeneratedUid(// 
            null);
            boolean resultChild = handleIcObject(itemContext);
            if (!resultChild) {
                return;
            }
            Object uidValueParentChilde = super.getValueByMappedAttribute(uidAttribute, account.getAttributes(), context);
            processChildren(parentAttribute, uidValueParentChilde, uidAttribute, accountsMap, accountsUseInTreeList, itemContext, roots);
        }
    });
}
Also used : SynchronizationContext(eu.bcvsolutions.idm.acc.domain.SynchronizationContext) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject)

Example 50 with IcConnectorObject

use of eu.bcvsolutions.idm.ic.api.IcConnectorObject in project CzechIdMng by bcvsolutions.

the class TreeSynchronizationExecutor method process.

@Override
public AbstractSysSyncConfigDto process(UUID synchronizationConfigId) {
    // Clear cache
    cacheManager.evictCache(CACHE_NAME);
    // Validate and create basic context
    SynchronizationContext context = this.validate(synchronizationConfigId);
    AbstractSysSyncConfigDto config = context.getConfig();
    SysSystemDto system = context.getSystem();
    IcConnectorConfiguration connectorConfig = context.getConnectorConfig();
    SysSystemMappingDto systemMapping = systemMappingService.get(context.getConfig().getSystemMapping());
    SysSchemaObjectClassDto schemaObjectClassDto = schemaObjectClassService.get(systemMapping.getObjectClass());
    IcObjectClass objectClass = new IcObjectClassImpl(schemaObjectClassDto.getObjectClassName());
    // Load last token
    Object lastToken = config.isReconciliation() ? null : config.getToken();
    // Create basic synchronization log
    SysSyncLogDto log = new SysSyncLogDto();
    log.setSynchronizationConfig(config.getId());
    log.setStarted(ZonedDateTime.now());
    log.setRunning(true);
    log.setToken(lastToken != null ? lastToken.toString() : null);
    log.addToLog(MessageFormat.format("Synchronization was started in {0}.", log.getStarted()));
    // List of all accounts with full IC object (used in tree sync)
    Map<String, IcConnectorObject> accountsMap = new HashMap<>();
    longRunningTaskExecutor.setCounter(0L);
    try {
        log = synchronizationLogService.save(log);
        List<SysSyncActionLogDto> actionsLog = new ArrayList<>();
        // Add logs to context
        context.addLog(log).addActionLogs(actionsLog);
        if (config.getTokenAttribute() == null && !config.isReconciliation()) {
            throw new ProvisioningException(AccResultCode.SYNCHRONIZATION_TOKEN_ATTRIBUTE_NOT_FOUND);
        }
        TreeResultsHandler resultHandler = new TreeResultsHandler(accountsMap);
        // We have to search all data for tree
        IcFilter filter = null;
        log.addToLog(MessageFormat.format("Start search with filter {0}.", "NONE"));
        log = synchronizationLogService.save(log);
        connectorFacade.search(systemService.getConnectorInstance(system), connectorConfig, objectClass, filter, resultHandler);
        // Execute sync for this tree and searched accounts
        processTreeSync(context, accountsMap);
        log = context.getLog();
        // Sync is correctly ends if wasn't cancelled
        if (log.isRunning()) {
            log = syncCorrectlyEnded(log, context);
        }
        // 
        synchronizationConfigService.save(config);
    } catch (Exception e) {
        String message = "Error during synchronization";
        log.addToLog(message);
        log.setContainsError(true);
        log.addToLog(Throwables.getStackTraceAsString(e));
        LOG.error(message, e);
    } finally {
        log.setRunning(false);
        log.setEnded(ZonedDateTime.now());
        log = synchronizationLogService.save(log);
        // 
        longRunningTaskExecutor.setCount(longRunningTaskExecutor.getCounter());
        longRunningTaskExecutor.updateState();
        // Clear cache
        cacheManager.evictCache(CACHE_NAME);
    }
    return config;
}
Also used : IcConnectorConfiguration(eu.bcvsolutions.idm.ic.api.IcConnectorConfiguration) IcObjectClassImpl(eu.bcvsolutions.idm.ic.impl.IcObjectClassImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SysSystemMappingDto(eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto) SysSystemDto(eu.bcvsolutions.idm.acc.dto.SysSystemDto) ProvisioningException(eu.bcvsolutions.idm.acc.exception.ProvisioningException) SysSyncActionLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto) AbstractSysSyncConfigDto(eu.bcvsolutions.idm.acc.dto.AbstractSysSyncConfigDto) SynchronizationContext(eu.bcvsolutions.idm.acc.domain.SynchronizationContext) IcObjectClass(eu.bcvsolutions.idm.ic.api.IcObjectClass) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) ProvisioningException(eu.bcvsolutions.idm.acc.exception.ProvisioningException) IcConnectorObject(eu.bcvsolutions.idm.ic.api.IcConnectorObject) SysSchemaObjectClassDto(eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto) SysSyncLogDto(eu.bcvsolutions.idm.acc.dto.SysSyncLogDto) IcFilter(eu.bcvsolutions.idm.ic.filter.api.IcFilter)

Aggregations

IcConnectorObject (eu.bcvsolutions.idm.ic.api.IcConnectorObject)79 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)45 IcAttribute (eu.bcvsolutions.idm.ic.api.IcAttribute)36 IcObjectClass (eu.bcvsolutions.idm.ic.api.IcObjectClass)30 SysProvisioningOperationDto (eu.bcvsolutions.idm.acc.dto.SysProvisioningOperationDto)28 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)28 Test (org.junit.Test)28 IcConnectorObjectImpl (eu.bcvsolutions.idm.ic.impl.IcConnectorObjectImpl)27 IcObjectClassImpl (eu.bcvsolutions.idm.ic.impl.IcObjectClassImpl)26 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)23 ProvisioningAttributeDto (eu.bcvsolutions.idm.acc.dto.ProvisioningAttributeDto)20 SysSystemAttributeMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemAttributeMappingDto)19 SysSystemMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto)19 ProvisioningContext (eu.bcvsolutions.idm.acc.domain.ProvisioningContext)18 SysSchemaAttributeDto (eu.bcvsolutions.idm.acc.dto.SysSchemaAttributeDto)17 SysSystemEntityDto (eu.bcvsolutions.idm.acc.dto.SysSystemEntityDto)17 IcAttributeImpl (eu.bcvsolutions.idm.ic.impl.IcAttributeImpl)17 SysSchemaObjectClassDto (eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto)15 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)14 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)13