Search in sources :

Example 16 with PullActions

use of org.apache.syncope.core.provisioning.api.pushpull.PullActions in project syncope by apache.

the class AbstractPullResultHandler method update.

protected List<ProvisioningReport> update(final SyncDelta delta, final List<String> anyKeys, final Provision provision) throws JobExecutionException {
    if (!profile.getTask().isPerformUpdate()) {
        LOG.debug("PullTask not configured for update");
        finalize(MatchingRule.toEventName(MatchingRule.UPDATE), Result.SUCCESS, null, null, delta);
        return Collections.<ProvisioningReport>emptyList();
    }
    LOG.debug("About to update {}", anyKeys);
    List<ProvisioningReport> results = new ArrayList<>();
    for (String key : anyKeys) {
        LOG.debug("About to update {}", key);
        ProvisioningReport result = new ProvisioningReport();
        result.setOperation(ResourceOperation.UPDATE);
        result.setAnyType(provision.getAnyType().getKey());
        result.setStatus(ProvisioningReport.Status.SUCCESS);
        result.setKey(key);
        AnyTO before = getAnyTO(key);
        if (before == null) {
            result.setStatus(ProvisioningReport.Status.FAILURE);
            result.setMessage(String.format("Any '%s(%s)' not found", provision.getAnyType().getKey(), key));
        } else {
            result.setName(getName(before));
        }
        if (!profile.isDryRun()) {
            Result resultStatus;
            Object output;
            AnyPatch effectivePatch = null;
            if (before == null) {
                resultStatus = Result.FAILURE;
                output = null;
            } else {
                AnyPatch anyPatch = null;
                try {
                    anyPatch = connObjectUtils.getAnyPatch(before.getKey(), delta.getObject(), before, profile.getTask(), provision, getAnyUtils());
                    for (PullActions action : profile.getActions()) {
                        action.beforeUpdate(profile, delta, before, anyPatch);
                    }
                    effectivePatch = doUpdate(before, anyPatch, delta, result);
                    AnyTO updated = AnyOperations.patch(before, effectivePatch);
                    for (PullActions action : profile.getActions()) {
                        action.after(profile, delta, updated, result);
                    }
                    output = updated;
                    resultStatus = Result.SUCCESS;
                    result.setName(getName(updated));
                    LOG.debug("{} {} successfully updated", provision.getAnyType().getKey(), key);
                } catch (PropagationException e) {
                    // A propagation failure doesn't imply a pull failure.
                    // The propagation exception status will be reported into the propagation task execution.
                    LOG.error("Could not propagate {} {}", provision.getAnyType().getKey(), delta.getUid().getUidValue(), e);
                    output = e;
                    resultStatus = Result.FAILURE;
                } catch (Exception e) {
                    throwIgnoreProvisionException(delta, e);
                    result.setStatus(ProvisioningReport.Status.FAILURE);
                    result.setMessage(ExceptionUtils.getRootCauseMessage(e));
                    LOG.error("Could not update {} {}", provision.getAnyType().getKey(), delta.getUid().getUidValue(), e);
                    output = e;
                    resultStatus = Result.FAILURE;
                    if (profile.getTask().isRemediation()) {
                        Remediation entity = entityFactory.newEntity(Remediation.class);
                        entity.setAnyType(provision.getAnyType());
                        entity.setOperation(ResourceOperation.UPDATE);
                        entity.setPayload(anyPatch);
                        entity.setError(result.getMessage());
                        entity.setInstant(new Date());
                        entity.setRemoteName(delta.getObject().getName().getNameValue());
                        entity.setPullTask(profile.getTask());
                        remediationDAO.save(entity);
                    }
                }
            }
            finalize(MatchingRule.toEventName(MatchingRule.UPDATE), resultStatus, before, output, delta, effectivePatch);
        }
        results.add(result);
    }
    return results;
}
Also used : AnyTO(org.apache.syncope.common.lib.to.AnyTO) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) ArrayList(java.util.ArrayList) ProvisioningReport(org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) IgnoreProvisionException(org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) JobExecutionException(org.quartz.JobExecutionException) Date(java.util.Date) Result(org.apache.syncope.common.lib.types.AuditElements.Result) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) AnyPatch(org.apache.syncope.common.lib.patch.AnyPatch) Remediation(org.apache.syncope.core.persistence.api.entity.Remediation)

Example 17 with PullActions

use of org.apache.syncope.core.provisioning.api.pushpull.PullActions in project syncope by apache.

the class AbstractPullResultHandler method doHandle.

/**
 * Look into SyncDelta and take necessary profile.getActions() (create / update / delete) on any object(s).
 *
 * @param delta returned by the underlying profile.getConnector()
 * @param provision provisioning info
 * @throws JobExecutionException in case of pull failure.
 */
protected void doHandle(final SyncDelta delta, final Provision provision) throws JobExecutionException {
    AnyUtils anyUtils = getAnyUtils();
    LOG.debug("Process {} for {} as {}", delta.getDeltaType(), delta.getUid().getUidValue(), delta.getObject().getObjectClass());
    SyncDelta processed = delta;
    for (PullActions action : profile.getActions()) {
        processed = action.preprocess(profile, processed);
    }
    LOG.debug("Transformed {} for {} as {}", processed.getDeltaType(), processed.getUid().getUidValue(), processed.getObject().getObjectClass());
    try {
        List<String> anyKeys = pullUtils.match(processed.getObject(), provision, anyUtils);
        LOG.debug("Match(es) found for {} as {}: {}", processed.getUid().getUidValue(), processed.getObject().getObjectClass(), anyKeys);
        if (anyKeys.size() > 1) {
            switch(profile.getResAct()) {
                case IGNORE:
                    throw new IllegalStateException("More than one match " + anyKeys);
                case FIRSTMATCH:
                    anyKeys = anyKeys.subList(0, 1);
                    break;
                case LASTMATCH:
                    anyKeys = anyKeys.subList(anyKeys.size() - 1, anyKeys.size());
                    break;
                default:
            }
        }
        if (SyncDeltaType.CREATE_OR_UPDATE == processed.getDeltaType()) {
            if (anyKeys.isEmpty()) {
                switch(profile.getTask().getUnmatchingRule()) {
                    case ASSIGN:
                        profile.getResults().addAll(assign(processed, provision, anyUtils));
                        break;
                    case PROVISION:
                        profile.getResults().addAll(provision(processed, provision, anyUtils));
                        break;
                    case IGNORE:
                        profile.getResults().addAll(ignore(processed, null, provision, false));
                        break;
                    default:
                }
            } else {
                // update VirAttrCache
                for (VirSchema virSchema : virSchemaDAO.findByProvision(provision)) {
                    Attribute attr = processed.getObject().getAttributeByName(virSchema.getExtAttrName());
                    for (String anyKey : anyKeys) {
                        if (attr == null) {
                            virAttrCache.expire(provision.getAnyType().getKey(), anyKey, virSchema.getKey());
                        } else {
                            VirAttrCacheValue cacheValue = new VirAttrCacheValue();
                            cacheValue.setValues(attr.getValue());
                            virAttrCache.put(provision.getAnyType().getKey(), anyKey, virSchema.getKey(), cacheValue);
                        }
                    }
                }
                switch(profile.getTask().getMatchingRule()) {
                    case UPDATE:
                        profile.getResults().addAll(update(processed, anyKeys, provision));
                        break;
                    case DEPROVISION:
                        profile.getResults().addAll(deprovision(processed, anyKeys, provision, false));
                        break;
                    case UNASSIGN:
                        profile.getResults().addAll(deprovision(processed, anyKeys, provision, true));
                        break;
                    case LINK:
                        profile.getResults().addAll(link(processed, anyKeys, provision, false));
                        break;
                    case UNLINK:
                        profile.getResults().addAll(link(processed, anyKeys, provision, true));
                        break;
                    case IGNORE:
                        profile.getResults().addAll(ignore(processed, anyKeys, provision, true));
                        break;
                    default:
                }
            }
        } else if (SyncDeltaType.DELETE == processed.getDeltaType()) {
            if (anyKeys.isEmpty()) {
                finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, processed);
                LOG.debug("No match found for deletion");
            } else {
                profile.getResults().addAll(delete(processed, anyKeys, provision));
            }
        }
    } catch (IllegalStateException | IllegalArgumentException e) {
        LOG.warn(e.getMessage());
    }
}
Also used : SyncDelta(org.identityconnectors.framework.common.objects.SyncDelta) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) Attribute(org.identityconnectors.framework.common.objects.Attribute) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 18 with PullActions

use of org.apache.syncope.core.provisioning.api.pushpull.PullActions in project syncope by apache.

the class AbstractPullResultHandler method delete.

protected List<ProvisioningReport> delete(final SyncDelta delta, final List<String> anyKeys, final Provision provision) throws JobExecutionException {
    if (!profile.getTask().isPerformDelete()) {
        LOG.debug("PullTask not configured for delete");
        finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, delta);
        return Collections.<ProvisioningReport>emptyList();
    }
    LOG.debug("About to delete {}", anyKeys);
    List<ProvisioningReport> results = new ArrayList<>();
    for (String key : anyKeys) {
        Object output;
        Result resultStatus = Result.FAILURE;
        ProvisioningReport result = new ProvisioningReport();
        try {
            AnyTO before = getAnyTO(key);
            result.setKey(key);
            result.setName(getName(before));
            result.setOperation(ResourceOperation.DELETE);
            result.setAnyType(provision.getAnyType().getKey());
            result.setStatus(ProvisioningReport.Status.SUCCESS);
            if (!profile.isDryRun()) {
                for (PullActions action : profile.getActions()) {
                    action.beforeDelete(profile, delta, before);
                }
                try {
                    doDelete(provision.getAnyType().getKind(), key);
                    output = null;
                    resultStatus = Result.SUCCESS;
                    for (PullActions action : profile.getActions()) {
                        action.after(profile, delta, before, result);
                    }
                } catch (Exception e) {
                    throwIgnoreProvisionException(delta, e);
                    result.setStatus(ProvisioningReport.Status.FAILURE);
                    result.setMessage(ExceptionUtils.getRootCauseMessage(e));
                    LOG.error("Could not delete {} {}", provision.getAnyType().getKey(), key, e);
                    output = e;
                    if (profile.getTask().isRemediation()) {
                        Remediation entity = entityFactory.newEntity(Remediation.class);
                        entity.setAnyType(provision.getAnyType());
                        entity.setOperation(ResourceOperation.DELETE);
                        entity.setPayload(key);
                        entity.setError(result.getMessage());
                        entity.setInstant(new Date());
                        entity.setRemoteName(delta.getObject().getName().getNameValue());
                        entity.setPullTask(profile.getTask());
                        remediationDAO.save(entity);
                    }
                }
                finalize(ResourceOperation.DELETE.name().toLowerCase(), resultStatus, before, output, delta);
            }
            results.add(result);
        } catch (NotFoundException e) {
            LOG.error("Could not find {} {}", provision.getAnyType().getKey(), key, e);
        } catch (DelegatedAdministrationException e) {
            LOG.error("Not allowed to read {} {}", provision.getAnyType().getKey(), key, e);
        } catch (Exception e) {
            LOG.error("Could not delete {} {}", provision.getAnyType().getKey(), key, e);
        }
    }
    return results;
}
Also used : AnyTO(org.apache.syncope.common.lib.to.AnyTO) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) ArrayList(java.util.ArrayList) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) ProvisioningReport(org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport) DelegatedAdministrationException(org.apache.syncope.core.spring.security.DelegatedAdministrationException) IgnoreProvisionException(org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) JobExecutionException(org.quartz.JobExecutionException) Date(java.util.Date) Result(org.apache.syncope.common.lib.types.AuditElements.Result) Remediation(org.apache.syncope.core.persistence.api.entity.Remediation)

Example 19 with PullActions

use of org.apache.syncope.core.provisioning.api.pushpull.PullActions in project syncope by apache.

the class TaskTest method savePullTask.

@Test
public void savePullTask() {
    ExternalResource resource = resourceDAO.find("ws-target-resource-1");
    assertNotNull(resource);
    AnyTemplatePullTask template = entityFactory.newEntity(AnyTemplatePullTask.class);
    template.set(new UserTO());
    PullTask task = entityFactory.newEntity(PullTask.class);
    task.setName("savePullTask");
    task.setDescription("PullTask description");
    task.setActive(true);
    task.setPullMode(PullMode.FULL_RECONCILIATION);
    task.add(template);
    task.setCronExpression("BLA BLA");
    task.setMatchingRule(MatchingRule.UPDATE);
    task.setUnmatchingRule(UnmatchingRule.PROVISION);
    // this save() fails because of an invalid Cron Expression
    InvalidEntityException exception = null;
    try {
        taskDAO.save(task);
    } catch (InvalidEntityException e) {
        exception = e;
    }
    assertNotNull(exception);
    task.setCronExpression(null);
    // this save() fails because a PullTask requires a target resource
    exception = null;
    try {
        taskDAO.save(task);
    } catch (InvalidEntityException e) {
        exception = e;
    }
    assertNotNull(exception);
    task.setResource(resource);
    Implementation pullActions = entityFactory.newEntity(Implementation.class);
    pullActions.setKey("PullActions" + UUID.randomUUID().toString());
    pullActions.setEngine(ImplementationEngine.JAVA);
    pullActions.setType(ImplementationType.PULL_ACTIONS);
    pullActions.setBody(PullActions.class.getName());
    pullActions = implementationDAO.save(pullActions);
    task.add(pullActions);
    // this save() finally works
    task = taskDAO.save(task);
    assertNotNull(task);
    PullTask actual = taskDAO.find(task.getKey());
    assertEquals(task, actual);
}
Also used : UserTO(org.apache.syncope.common.lib.to.UserTO) PullTask(org.apache.syncope.core.persistence.api.entity.task.PullTask) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) AnyTemplatePullTask(org.apache.syncope.core.persistence.api.entity.task.AnyTemplatePullTask) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Test(org.junit.jupiter.api.Test) AbstractTest(org.apache.syncope.core.persistence.jpa.AbstractTest)

Aggregations

PullActions (org.apache.syncope.core.provisioning.api.pushpull.PullActions)19 ProvisioningReport (org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport)12 JobExecutionException (org.quartz.JobExecutionException)11 Result (org.apache.syncope.common.lib.types.AuditElements.Result)10 PropagationException (org.apache.syncope.core.provisioning.api.propagation.PropagationException)10 IgnoreProvisionException (org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException)10 DelegatedAdministrationException (org.apache.syncope.core.spring.security.DelegatedAdministrationException)10 ArrayList (java.util.ArrayList)9 AnyTO (org.apache.syncope.common.lib.to.AnyTO)7 RealmTO (org.apache.syncope.common.lib.to.RealmTO)7 NotFoundException (org.apache.syncope.core.persistence.api.dao.NotFoundException)6 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)5 Realm (org.apache.syncope.core.persistence.api.entity.Realm)5 PropagationByResource (org.apache.syncope.core.provisioning.api.PropagationByResource)5 Date (java.util.Date)3 AnyPatch (org.apache.syncope.common.lib.patch.AnyPatch)3 PropagationTaskTO (org.apache.syncope.common.lib.to.PropagationTaskTO)3 Remediation (org.apache.syncope.core.persistence.api.entity.Remediation)3 PullTask (org.apache.syncope.core.persistence.api.entity.task.PullTask)3 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)2