Search in sources :

Example 16 with Provision

use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.

the class AbstractPropagationTaskExecutor method getRemoteObject.

/**
 * Get remote object for given task.
 *
 * @param connector connector facade proxy.
 * @param task current propagation task.
 * @param provision provision
 * @param latest 'FALSE' to retrieve object using old connObjectKey if not null.
 * @return remote connector object.
 */
protected ConnectorObject getRemoteObject(final PropagationTask task, final Connector connector, final Provision provision, final boolean latest) {
    String connObjectKey = latest || task.getOldConnObjectKey() == null ? task.getConnObjectKey() : task.getOldConnObjectKey();
    Set<MappingItem> linkingMappingItems = virSchemaDAO.findByProvision(provision).stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet());
    ConnectorObject obj = null;
    Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
    if (connObjectKeyItem.isPresent()) {
        try {
            obj = connector.getObject(new ObjectClass(task.getObjectClassName()), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey), MappingUtils.buildOperationOptions(new IteratorChain<>(MappingUtils.getPropagationItems(provision.getMapping().getItems()).iterator(), linkingMappingItems.iterator())));
            for (MappingItem item : linkingMappingItems) {
                Attribute attr = obj.getAttributeByName(item.getExtAttrName());
                if (attr == null) {
                    virAttrCache.expire(task.getAnyType(), task.getEntityKey(), item.getIntAttrName());
                } else {
                    VirAttrCacheValue cacheValue = new VirAttrCacheValue();
                    cacheValue.setValues(attr.getValue());
                    virAttrCache.put(task.getAnyType(), task.getEntityKey(), item.getIntAttrName(), cacheValue);
                }
            }
        } catch (TimeoutException toe) {
            LOG.debug("Request timeout", toe);
            throw toe;
        } catch (RuntimeException ignore) {
            LOG.debug("While resolving {}", connObjectKey, ignore);
        }
    }
    return obj;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditElements(org.apache.syncope.common.lib.types.AuditElements) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) NotificationManager(org.apache.syncope.core.provisioning.api.notification.NotificationManager) StringUtils(org.apache.commons.lang3.StringUtils) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) Attribute(org.identityconnectors.framework.common.objects.Attribute) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) Map(java.util.Map) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ExceptionUtils2(org.apache.syncope.core.provisioning.api.utils.ExceptionUtils2) ExecTO(org.apache.syncope.common.lib.to.ExecTO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) Collection(java.util.Collection) Set(java.util.Set) PropagationActions(org.apache.syncope.core.provisioning.api.propagation.PropagationActions) Collectors(java.util.stream.Collectors) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ImplementationManager(org.apache.syncope.core.spring.ImplementationManager) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Connector(org.apache.syncope.core.provisioning.api.Connector) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) AuditManager(org.apache.syncope.core.provisioning.api.AuditManager) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) ConnectorFactory(org.apache.syncope.core.provisioning.api.ConnectorFactory) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) Optional(java.util.Optional) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) POJOHelper(org.apache.syncope.core.provisioning.api.serialization.POJOHelper) PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) ConnectorObjectBuilder(org.identityconnectors.framework.common.objects.ConnectorObjectBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException) ArrayList(java.util.ArrayList) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) HashSet(java.util.HashSet) Result(org.apache.syncope.common.lib.types.AuditElements.Result) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException) Logger(org.slf4j.Logger) Uid(org.identityconnectors.framework.common.objects.Uid) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) IteratorChain(org.apache.syncope.common.lib.collections.IteratorChain) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) Name(org.identityconnectors.framework.common.objects.Name) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) Collections(java.util.Collections) TraceLevel(org.apache.syncope.common.lib.types.TraceLevel) Transactional(org.springframework.transaction.annotation.Transactional) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) Attribute(org.identityconnectors.framework.common.objects.Attribute) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException)

Example 17 with Provision

use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.

the class ResourceTest method emptyMapping.

@Test
public void emptyMapping() {
    ExternalResource ldap = resourceDAO.find("resource-ldap");
    assertNotNull(ldap);
    assertNotNull(ldap.getProvision(anyTypeDAO.findUser()).get().getMapping());
    assertNotNull(ldap.getProvision(anyTypeDAO.findGroup()).get().getMapping());
    // need to avoid any class not defined in this Maven module
    ldap.getPropagationActions().clear();
    List<? extends MappingItem> items = ldap.getProvision(anyTypeDAO.findGroup()).get().getMapping().getItems();
    assertNotNull(items);
    assertFalse(items.isEmpty());
    List<String> itemKeys = items.stream().map(Entity::getKey).collect(Collectors.toList());
    Provision groupProvision = ldap.getProvision(anyTypeDAO.findGroup()).get();
    virSchemaDAO.findByProvision(groupProvision).forEach(schema -> virSchemaDAO.delete(schema.getKey()));
    ldap.getProvisions().remove(groupProvision);
    resourceDAO.save(ldap);
    resourceDAO.flush();
    itemKeys.forEach(itemKey -> assertNull(entityManager().find(JPAMappingItem.class, itemKey)));
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 18 with Provision

use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.

the class ResourceTest method save.

@Test
public void save() {
    ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
    resource.setKey("ws-target-resource-save");
    // specify the connector
    ConnInstance connector = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
    assertNotNull(connector);
    resource.setConnector(connector);
    Provision provision = entityFactory.newEntity(Provision.class);
    provision.setAnyType(anyTypeDAO.findUser());
    provision.setObjectClass(ObjectClass.ACCOUNT);
    provision.setResource(resource);
    resource.add(provision);
    Mapping mapping = entityFactory.newEntity(Mapping.class);
    mapping.setProvision(provision);
    provision.setMapping(mapping);
    // specify mappings
    for (int i = 0; i < 3; i++) {
        MappingItem item = entityFactory.newEntity(MappingItem.class);
        item.setExtAttrName("test" + i);
        item.setIntAttrName("nonexistent" + i);
        item.setMandatoryCondition("false");
        item.setPurpose(MappingPurpose.PULL);
        mapping.add(item);
        item.setMapping(mapping);
    }
    MappingItem connObjectKey = entityFactory.newEntity(MappingItem.class);
    connObjectKey.setExtAttrName("username");
    connObjectKey.setIntAttrName("username");
    connObjectKey.setPurpose(MappingPurpose.PROPAGATION);
    mapping.setConnObjectKeyItem(connObjectKey);
    connObjectKey.setMapping(mapping);
    // map a derived attribute
    MappingItem derived = entityFactory.newEntity(MappingItem.class);
    derived.setConnObjectKey(false);
    derived.setExtAttrName("fullname");
    derived.setIntAttrName("cn");
    derived.setPurpose(MappingPurpose.PROPAGATION);
    mapping.add(derived);
    derived.setMapping(mapping);
    // save the resource
    ExternalResource actual = resourceDAO.save(resource);
    assertNotNull(actual);
    assertNotNull(actual.getProvision(anyTypeDAO.findUser()).get().getMapping());
    resourceDAO.flush();
    resourceDAO.detach(actual);
    connInstanceDAO.detach(connector);
    // assign the new resource to an user
    User user = userDAO.findByUsername("rossini");
    assertNotNull(user);
    user.add(actual);
    resourceDAO.flush();
    // retrieve resource
    resource = resourceDAO.find(actual.getKey());
    assertNotNull(resource);
    resourceDAO.refresh(resource);
    // check connector
    connector = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
    assertNotNull(connector);
    assertNotNull(connector.getResources());
    assertNotNull(resource.getConnector());
    assertTrue(resource.getConnector().equals(connector));
    // check mappings
    List<? extends MappingItem> items = resource.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems();
    assertNotNull(items);
    assertEquals(5, items.size());
    // check user
    user = userDAO.findByUsername("rossini");
    assertNotNull(user);
    assertNotNull(user.getResources());
    assertTrue(user.getResources().contains(actual));
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) JPAMappingItem(org.apache.syncope.core.persistence.jpa.entity.resource.JPAMappingItem) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) User(org.apache.syncope.core.persistence.api.entity.user.User) Mapping(org.apache.syncope.core.persistence.api.entity.resource.Mapping) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) ConnInstance(org.apache.syncope.core.persistence.api.entity.ConnInstance) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Example 19 with Provision

use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.

the class AbstractPullResultHandler method handle.

@Override
public boolean handle(final SyncDelta delta) {
    Provision provision = null;
    try {
        provision = profile.getTask().getResource().getProvision(delta.getObject().getObjectClass()).orElse(null);
        if (provision == null) {
            throw new JobExecutionException("No provision found on " + profile.getTask().getResource() + " for " + delta.getObject().getObjectClass());
        }
        doHandle(delta, provision);
        executor.reportHandled(delta.getObjectClass(), delta.getObject().getName());
        LOG.debug("Successfully handled {}", delta);
        if (profile.getTask().getPullMode() != PullMode.INCREMENTAL) {
            if (executor.wasInterruptRequested()) {
                LOG.debug("Pull interrupted");
                executor.setInterrupted();
                return false;
            }
            return true;
        }
        boolean shouldContinue;
        synchronized (this) {
            shouldContinue = latestResult == Result.SUCCESS;
            this.latestResult = null;
        }
        if (shouldContinue) {
            executor.setLatestSyncToken(delta.getObjectClass(), delta.getToken());
        }
        if (executor.wasInterruptRequested()) {
            LOG.debug("Pull interrupted");
            executor.setInterrupted();
            return false;
        }
        return shouldContinue;
    } catch (IgnoreProvisionException e) {
        ProvisioningReport ignoreResult = new ProvisioningReport();
        ignoreResult.setOperation(ResourceOperation.NONE);
        ignoreResult.setAnyType(provision == null ? getAnyUtils().getAnyTypeKind().name() : provision.getAnyType().getKey());
        ignoreResult.setStatus(ProvisioningReport.Status.IGNORE);
        ignoreResult.setKey(null);
        ignoreResult.setName(delta.getObject().getName().getNameValue());
        profile.getResults().add(ignoreResult);
        LOG.warn("Ignoring during pull", e);
        executor.setLatestSyncToken(delta.getObjectClass(), delta.getToken());
        executor.reportHandled(delta.getObjectClass(), delta.getObject().getName());
        return true;
    } catch (JobExecutionException e) {
        LOG.error("Pull failed", e);
        return false;
    }
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) JobExecutionException(org.quartz.JobExecutionException) IgnoreProvisionException(org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException) ProvisioningReport(org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport)

Example 20 with Provision

use of org.apache.syncope.core.persistence.api.entity.resource.Provision in project syncope by apache.

the class PushJobDelegate method doExecuteProvisioning.

@Override
protected String doExecuteProvisioning(final PushTask pushTask, final Connector connector, final boolean dryRun) throws JobExecutionException {
    LOG.debug("Executing push on {}", pushTask.getResource());
    List<PushActions> actions = new ArrayList<>();
    pushTask.getActions().forEach(impl -> {
        try {
            actions.add(ImplementationManager.build(impl));
        } catch (Exception e) {
            LOG.warn("While building {}", impl, e);
        }
    });
    profile = new ProvisioningProfile<>(connector, pushTask);
    profile.getActions().addAll(actions);
    profile.setDryRun(dryRun);
    profile.setResAct(null);
    if (!profile.isDryRun()) {
        for (PushActions action : actions) {
            action.beforeAll(profile);
        }
    }
    status.set("Initialization completed");
    // First realms...
    if (pushTask.getResource().getOrgUnit() != null) {
        status.set("Pushing realms");
        rhandler = buildRealmHandler();
        for (Realm realm : realmDAO.findDescendants(profile.getTask().getSourceRealm())) {
            // Never push the root realm
            if (realm.getParent() != null) {
                try {
                    rhandler.handle(realm.getKey());
                    reportHandled(SyncopeConstants.REALM_ANYTYPE, realm.getName());
                } catch (Exception e) {
                    LOG.warn("Failure pushing '{}' on '{}'", realm, pushTask.getResource(), e);
                    throw new JobExecutionException("While pushing " + realm + " on " + pushTask.getResource(), e);
                }
            }
        }
    }
    // ...then provisions for any types
    ahandler = buildAnyObjectHandler();
    uhandler = buildUserHandler();
    ghandler = buildGroupHandler();
    for (Provision provision : pushTask.getResource().getProvisions()) {
        if (provision.getMapping() != null) {
            status.set("Pushing " + provision.getAnyType().getKey());
            AnyDAO<?> anyDAO = getAnyDAO(provision.getAnyType().getKind());
            SyncopePushResultHandler handler;
            switch(provision.getAnyType().getKind()) {
                case USER:
                    handler = uhandler;
                    break;
                case GROUP:
                    handler = ghandler;
                    break;
                case ANY_OBJECT:
                default:
                    handler = ahandler;
            }
            Optional<? extends PushTaskAnyFilter> anyFilter = pushTask.getFilter(provision.getAnyType());
            String filter = anyFilter.isPresent() ? anyFilter.get().getFIQLCond() : null;
            SearchCond cond = StringUtils.isBlank(filter) ? anyDAO.getAllMatchingCond() : SearchCondConverter.convert(filter);
            int count = searchDAO.count(Collections.singleton(profile.getTask().getSourceRealm().getFullPath()), cond, provision.getAnyType().getKind());
            for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1 && !interrupt; page++) {
                List<? extends Any<?>> anys = searchDAO.search(Collections.singleton(profile.getTask().getSourceRealm().getFullPath()), cond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), provision.getAnyType().getKind());
                doHandle(anys, handler, pushTask.getResource());
            }
        }
    }
    if (!profile.isDryRun() && !interrupt) {
        for (PushActions action : actions) {
            action.afterAll(profile);
        }
    }
    if (interrupt) {
        interrupted = true;
    }
    status.set("Push done");
    String result = createReport(profile.getResults(), pushTask.getResource(), dryRun);
    LOG.debug("Push result: {}", result);
    return result;
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) ArrayList(java.util.ArrayList) JobExecutionException(org.quartz.JobExecutionException) SyncopePushResultHandler(org.apache.syncope.core.provisioning.api.pushpull.SyncopePushResultHandler) JobExecutionException(org.quartz.JobExecutionException) PushActions(org.apache.syncope.core.provisioning.api.pushpull.PushActions) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Realm(org.apache.syncope.core.persistence.api.entity.Realm)

Aggregations

Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)30 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)20 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)19 List (java.util.List)13 Set (java.util.Set)12 Autowired (org.springframework.beans.factory.annotation.Autowired)12 UserDAO (org.apache.syncope.core.persistence.api.dao.UserDAO)11 ArrayList (java.util.ArrayList)10 StringUtils (org.apache.commons.lang3.StringUtils)10 Attribute (org.identityconnectors.framework.common.objects.Attribute)10 HashSet (java.util.HashSet)9 GroupDAO (org.apache.syncope.core.persistence.api.dao.GroupDAO)9 VirSchemaDAO (org.apache.syncope.core.persistence.api.dao.VirSchemaDAO)9 OrgUnit (org.apache.syncope.core.persistence.api.entity.resource.OrgUnit)9 Connector (org.apache.syncope.core.provisioning.api.Connector)9 Test (org.junit.jupiter.api.Test)9 Transactional (org.springframework.transaction.annotation.Transactional)9 Collections (java.util.Collections)8 Collectors (java.util.stream.Collectors)8 IteratorChain (org.apache.syncope.common.lib.collections.IteratorChain)8