Search in sources :

Example 1 with AnyUtils

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

the class JPAAnyUtilsFactory method getInstance.

@Override
public AnyUtils getInstance(final AnyTypeKind anyTypeKind) {
    AnyUtils instance;
    synchronized (instances) {
        instance = instances.get(anyTypeKind);
        if (instance == null) {
            instance = new JPAAnyUtils(anyTypeKind);
            ApplicationContextProvider.getBeanFactory().autowireBean(instance);
            instances.put(anyTypeKind, instance);
        }
    }
    return instance;
}
Also used : AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 2 with AnyUtils

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

the class AzurePropagationActions method after.

@Transactional
@Override
public void after(final PropagationTask task, final TaskExec execution, final ConnectorObject afterObj) {
    if (task.getOperation() == ResourceOperation.DELETE || task.getOperation() == ResourceOperation.NONE) {
        return;
    }
    if (AnyTypeKind.USER.equals(task.getAnyTypeKind())) {
        User user = userDAO.find(task.getEntityKey());
        if (user == null) {
            LOG.error("Could not find user {}, skipping", task.getEntityKey());
        } else {
            boolean modified = false;
            AnyUtils anyUtils = anyUtilsFactory.getInstance(user);
            // Azure User ID
            PlainSchema azureId = plainSchemaDAO.find(getAzureIdSchema());
            if (azureId == null) {
                LOG.error("Could not find schema {}, skipping", getAzureIdSchema());
            } else {
                // set back the __UID__ received by Azure
                UPlainAttr attr = user.getPlainAttr(getAzureIdSchema()).orElse(null);
                if (attr == null) {
                    attr = entityFactory.newEntity(UPlainAttr.class);
                    attr.setSchema(azureId);
                    attr.setOwner(user);
                    user.add(attr);
                    try {
                        attr.add(afterObj.getUid().getUidValue(), anyUtils);
                        modified = true;
                    } catch (InvalidPlainAttrValueException e) {
                        LOG.error("Invalid value for attribute {}: {}", azureId.getKey(), afterObj.getUid().getUidValue(), e);
                    }
                } else {
                    LOG.debug("User {} has already {} assigned: {}", user, azureId.getKey(), attr.getValuesAsStrings());
                }
            }
            if (modified) {
                userDAO.save(user);
            }
        }
    } else if (AnyTypeKind.GROUP.equals(task.getAnyTypeKind())) {
        Group group = groupDAO.find(task.getEntityKey());
        if (group == null) {
            LOG.error("Could not find group {}, skipping", task.getEntityKey());
        } else {
            boolean modified = false;
            AnyUtils anyUtils = anyUtilsFactory.getInstance(group);
            // Azure Group ID
            PlainSchema azureId = plainSchemaDAO.find(getAzureGroupIdSchema());
            if (azureId == null) {
                LOG.error("Could not find schema {}, skipping", getAzureGroupIdSchema());
            } else {
                // set back the __UID__ received by Azure
                GPlainAttr attr = group.getPlainAttr(getAzureGroupIdSchema()).orElse(null);
                if (attr == null) {
                    attr = entityFactory.newEntity(GPlainAttr.class);
                    attr.setSchema(azureId);
                    attr.setOwner(group);
                    group.add(attr);
                    try {
                        attr.add(afterObj.getUid().getUidValue(), anyUtils);
                        modified = true;
                    } catch (InvalidPlainAttrValueException e) {
                        LOG.error("Invalid value for attribute {}: {}", azureId.getKey(), afterObj.getUid().getUidValue(), e);
                    }
                } else {
                    LOG.debug("Group {} has already {} assigned: {}", group, azureId.getKey(), attr.getValuesAsStrings());
                }
            }
            if (modified) {
                groupDAO.save(group);
            }
        }
    }
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) GPlainAttr(org.apache.syncope.core.persistence.api.entity.group.GPlainAttr) User(org.apache.syncope.core.persistence.api.entity.user.User) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) UPlainAttr(org.apache.syncope.core.persistence.api.entity.user.UPlainAttr) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) InvalidPlainAttrValueException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidPlainAttrValueException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with AnyUtils

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

the class AbstractPushResultHandler method doHandle.

protected void doHandle(final Any<?> any) throws JobExecutionException {
    AnyUtils anyUtils = anyUtilsFactory.getInstance(any);
    ProvisioningReport result = new ProvisioningReport();
    profile.getResults().add(result);
    result.setKey(any.getKey());
    result.setAnyType(any.getType().getKey());
    result.setName(getName(any));
    Boolean enabled = any instanceof User && profile.getTask().isSyncStatus() ? ((User) any).isSuspended() ? Boolean.FALSE : Boolean.TRUE : null;
    LOG.debug("Propagating {} with key {} towards {}", anyUtils.getAnyTypeKind(), any.getKey(), profile.getTask().getResource());
    Object output = null;
    Result resultStatus = null;
    // Try to read remote object BEFORE any actual operation
    Optional<? extends Provision> provision = profile.getTask().getResource().getProvision(any.getType());
    Optional<MappingItem> connObjectKey = MappingUtils.getConnObjectKeyItem(provision.get());
    Optional<String> connObjecKeyValue = mappingManager.getConnObjectKeyValue(any, provision.get());
    ConnectorObject beforeObj = null;
    if (connObjectKey.isPresent() && connObjecKeyValue.isPresent()) {
        beforeObj = getRemoteObject(provision.get().getObjectClass(), connObjectKey.get().getExtAttrName(), connObjecKeyValue.get(), provision.get().getMapping().getItems().iterator());
    } else {
        LOG.debug("ConnObjectKeyItem {} or its value {} are null", connObjectKey, connObjecKeyValue);
    }
    Boolean status = profile.getTask().isSyncStatus() ? enabled : null;
    if (profile.isDryRun()) {
        if (beforeObj == null) {
            result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule()));
        } else {
            result.setOperation(toResourceOperation(profile.getTask().getMatchingRule()));
        }
        result.setStatus(ProvisioningReport.Status.SUCCESS);
    } else {
        String operation = beforeObj == null ? UnmatchingRule.toEventName(profile.getTask().getUnmatchingRule()) : MatchingRule.toEventName(profile.getTask().getMatchingRule());
        boolean notificationsAvailable = notificationManager.notificationsAvailable(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation);
        boolean auditRequested = auditManager.auditRequested(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation);
        try {
            if (beforeObj == null) {
                result.setOperation(toResourceOperation(profile.getTask().getUnmatchingRule()));
                switch(profile.getTask().getUnmatchingRule()) {
                    case ASSIGN:
                        for (PushActions action : profile.getActions()) {
                            action.beforeAssign(profile, any);
                        }
                        if (!profile.getTask().isPerformCreate()) {
                            LOG.debug("PushTask not configured for create");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            assign(any, status, result);
                        }
                        break;
                    case PROVISION:
                        for (PushActions action : profile.getActions()) {
                            action.beforeProvision(profile, any);
                        }
                        if (!profile.getTask().isPerformCreate()) {
                            LOG.debug("PushTask not configured for create");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            provision(any, status, result);
                        }
                        break;
                    case UNLINK:
                        for (PushActions action : profile.getActions()) {
                            action.beforeUnlink(profile, any);
                        }
                        if (!profile.getTask().isPerformUpdate()) {
                            LOG.debug("PushTask not configured for update");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            link(any, true, result);
                        }
                        break;
                    case IGNORE:
                        LOG.debug("Ignored any: {}", any);
                        result.setStatus(ProvisioningReport.Status.IGNORE);
                        break;
                    default:
                }
            } else {
                result.setOperation(toResourceOperation(profile.getTask().getMatchingRule()));
                switch(profile.getTask().getMatchingRule()) {
                    case UPDATE:
                        for (PushActions action : profile.getActions()) {
                            action.beforeUpdate(profile, any);
                        }
                        if (!profile.getTask().isPerformUpdate()) {
                            LOG.debug("PushTask not configured for update");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            update(any, result);
                        }
                        break;
                    case DEPROVISION:
                        for (PushActions action : profile.getActions()) {
                            action.beforeDeprovision(profile, any);
                        }
                        if (!profile.getTask().isPerformDelete()) {
                            LOG.debug("PushTask not configured for delete");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            deprovision(any, result);
                        }
                        break;
                    case UNASSIGN:
                        for (PushActions action : profile.getActions()) {
                            action.beforeUnassign(profile, any);
                        }
                        if (!profile.getTask().isPerformDelete()) {
                            LOG.debug("PushTask not configured for delete");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            unassign(any, result);
                        }
                        break;
                    case LINK:
                        for (PushActions action : profile.getActions()) {
                            action.beforeLink(profile, any);
                        }
                        if (!profile.getTask().isPerformUpdate()) {
                            LOG.debug("PushTask not configured for update");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            link(any, false, result);
                        }
                        break;
                    case UNLINK:
                        for (PushActions action : profile.getActions()) {
                            action.beforeUnlink(profile, any);
                        }
                        if (!profile.getTask().isPerformUpdate()) {
                            LOG.debug("PushTask not configured for update");
                            result.setStatus(ProvisioningReport.Status.IGNORE);
                        } else {
                            link(any, true, result);
                        }
                        break;
                    case IGNORE:
                        LOG.debug("Ignored any: {}", any);
                        result.setStatus(ProvisioningReport.Status.IGNORE);
                        break;
                    default:
                }
            }
            for (PushActions action : profile.getActions()) {
                action.after(profile, any, result);
            }
            if (result.getStatus() == null) {
                result.setStatus(ProvisioningReport.Status.SUCCESS);
            }
            resultStatus = AuditElements.Result.SUCCESS;
            if (connObjectKey.isPresent() && connObjecKeyValue.isPresent()) {
                output = getRemoteObject(provision.get().getObjectClass(), connObjectKey.get().getExtAttrName(), connObjecKeyValue.get(), provision.get().getMapping().getItems().iterator());
            }
        } catch (IgnoreProvisionException e) {
            throw e;
        } catch (Exception e) {
            result.setStatus(ProvisioningReport.Status.FAILURE);
            result.setMessage(ExceptionUtils.getRootCauseMessage(e));
            resultStatus = AuditElements.Result.FAILURE;
            output = e;
            LOG.warn("Error pushing {} towards {}", any, profile.getTask().getResource(), e);
            for (PushActions action : profile.getActions()) {
                action.onError(profile, any, result, e);
            }
            throw new JobExecutionException(e);
        } finally {
            if (notificationsAvailable || auditRequested) {
                Map<String, Object> jobMap = new HashMap<>();
                jobMap.put(AfterHandlingEvent.JOBMAP_KEY, new AfterHandlingEvent(AuditElements.EventCategoryType.PUSH, any.getType().getKind().name().toLowerCase(), profile.getTask().getResource().getKey(), operation, resultStatus, beforeObj, output, any));
                AfterHandlingJob.schedule(scheduler, jobMap);
            }
        }
    }
}
Also used : MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) User(org.apache.syncope.core.persistence.api.entity.user.User) HashMap(java.util.HashMap) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) AfterHandlingEvent(org.apache.syncope.core.provisioning.api.event.AfterHandlingEvent) ProvisioningReport(org.apache.syncope.core.provisioning.api.pushpull.ProvisioningReport) IgnoreProvisionException(org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException) IgnoreProvisionException(org.apache.syncope.core.provisioning.api.pushpull.IgnoreProvisionException) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException) JobExecutionException(org.quartz.JobExecutionException) Result(org.apache.syncope.common.lib.types.AuditElements.Result) JobExecutionException(org.quartz.JobExecutionException) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) PushActions(org.apache.syncope.core.provisioning.api.pushpull.PushActions) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 4 with AnyUtils

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

the class PullUtils method match.

public Optional<String> match(final AnyType anyType, final String name, final ExternalResource resource, final Connector connector) {
    Optional<? extends Provision> provision = resource.getProvision(anyType);
    if (!provision.isPresent()) {
        return Optional.empty();
    }
    Optional<String> result = Optional.empty();
    AnyUtils anyUtils = anyUtilsFactory.getInstance(anyType.getKind());
    final List<ConnectorObject> found = new ArrayList<>();
    connector.search(provision.get().getObjectClass(), new EqualsFilter(new Name(name)), obj -> found.add(obj), MappingUtils.buildOperationOptions(MappingUtils.getPullItems(provision.get().getMapping().getItems()).iterator()));
    if (found.isEmpty()) {
        LOG.debug("No {} found on {} with __NAME__ {}", provision.get().getObjectClass(), resource, name);
    } else {
        if (found.size() > 1) {
            LOG.warn("More than one {} found on {} with __NAME__ {} - taking first only", provision.get().getObjectClass(), resource, name);
        }
        ConnectorObject connObj = found.iterator().next();
        try {
            List<String> anyKeys = match(connObj, provision.get(), anyUtils);
            if (anyKeys.isEmpty()) {
                LOG.debug("No matching {} found for {}, aborting", anyUtils.getAnyTypeKind(), connObj);
            } else {
                if (anyKeys.size() > 1) {
                    LOG.warn("More than one {} found {} - taking first only", anyUtils.getAnyTypeKind(), anyKeys);
                }
                result = Optional.ofNullable(anyKeys.iterator().next());
            }
        } catch (IllegalArgumentException e) {
            LOG.warn(e.getMessage());
        }
    }
    return result;
}
Also used : ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) ArrayList(java.util.ArrayList) EqualsFilter(org.identityconnectors.framework.common.objects.filter.EqualsFilter) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) IntAttrName(org.apache.syncope.core.provisioning.api.IntAttrName) Name(org.identityconnectors.framework.common.objects.Name)

Example 5 with AnyUtils

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

the class ReconciliationReportlet method doExtract.

private void doExtract(final ContentHandler handler, final List<? extends Any<?>> anys) throws SAXException, ReportException {
    final Set<Missing> missing = new HashSet<>();
    final Set<Misaligned> misaligned = new HashSet<>();
    for (Any<?> any : anys) {
        missing.clear();
        misaligned.clear();
        AnyUtils anyUtils = anyUtilsFactory.getInstance(any);
        anyUtils.getAllResources(any).forEach(resource -> {
            Provision provision = resource.getProvision(any.getType()).orElse(null);
            Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
            final String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, provision).get() : StringUtils.EMPTY;
            if (provision != null && connObjectKeyItem.isPresent() && StringUtils.isNotBlank(connObjectKeyValue)) {
                // 1. read from the underlying connector
                Connector connector = connFactory.getConnector(resource);
                ConnectorObject connectorObject = connector.getObject(provision.getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(provision.getMapping().getItems().iterator()));
                if (connectorObject == null) {
                    // 2. not found on resource?
                    LOG.error("Object {} with class {} not found on resource {}", connObjectKeyValue, provision.getObjectClass(), resource);
                    missing.add(new Missing(resource.getKey(), connObjectKeyValue));
                } else {
                    // 3. found but misaligned?
                    Pair<String, Set<Attribute>> preparedAttrs = mappingManager.prepareAttrs(any, null, false, null, provision);
                    preparedAttrs.getRight().add(AttributeBuilder.build(Uid.NAME, preparedAttrs.getLeft()));
                    preparedAttrs.getRight().add(AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), preparedAttrs.getLeft()));
                    final Map<String, Set<Object>> syncopeAttrs = new HashMap<>();
                    preparedAttrs.getRight().forEach(attr -> {
                        syncopeAttrs.put(attr.getName(), getValues(attr));
                    });
                    final Map<String, Set<Object>> resourceAttrs = new HashMap<>();
                    connectorObject.getAttributes().stream().filter(attr -> (!OperationalAttributes.PASSWORD_NAME.equals(attr.getName()) && !OperationalAttributes.ENABLE_NAME.equals(attr.getName()))).forEachOrdered(attr -> {
                        resourceAttrs.put(attr.getName(), getValues(attr));
                    });
                    syncopeAttrs.keySet().stream().filter(syncopeAttr -> !resourceAttrs.containsKey(syncopeAttr)).forEach(name -> {
                        misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, name, syncopeAttrs.get(name), Collections.emptySet()));
                    });
                    resourceAttrs.forEach((key, values) -> {
                        if (syncopeAttrs.containsKey(key)) {
                            if (!Objects.equals(syncopeAttrs.get(key), values)) {
                                misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, syncopeAttrs.get(key), values));
                            }
                        } else {
                            misaligned.add(new Misaligned(resource.getKey(), connObjectKeyValue, key, Collections.emptySet(), values));
                        }
                    });
                }
            }
        });
        if (!missing.isEmpty() || !misaligned.isEmpty()) {
            doExtract(handler, any, missing, misaligned);
        }
    }
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) Feature(org.apache.syncope.common.lib.report.ReconciliationReportletConf.Feature) FormatUtils(org.apache.syncope.core.provisioning.api.utils.FormatUtils) AnyTypeCond(org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) Attribute(org.identityconnectors.framework.common.objects.Attribute) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) OperationalAttributes(org.identityconnectors.framework.common.objects.OperationalAttributes) AttributesImpl(org.xml.sax.helpers.AttributesImpl) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) Set(java.util.Set) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) Objects(java.util.Objects) Connector(org.apache.syncope.core.provisioning.api.Connector) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) Base64(java.util.Base64) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) ReconciliationReportletConf(org.apache.syncope.common.lib.report.ReconciliationReportletConf) ConnectorFactory(org.apache.syncope.core.provisioning.api.ConnectorFactory) SAXException(org.xml.sax.SAXException) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) ReportletConfClass(org.apache.syncope.core.persistence.api.dao.ReportletConfClass) AnySearchDAO(org.apache.syncope.core.persistence.api.dao.AnySearchDAO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) ReportletConf(org.apache.syncope.common.lib.report.ReportletConf) MappingManager(org.apache.syncope.core.provisioning.api.MappingManager) SearchCondConverter(org.apache.syncope.core.persistence.api.search.SearchCondConverter) ContentHandler(org.xml.sax.ContentHandler) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Uid(org.identityconnectors.framework.common.objects.Uid) User(org.apache.syncope.core.persistence.api.entity.user.User) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) AnyDAO(org.apache.syncope.core.persistence.api.dao.AnyDAO) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) Any(org.apache.syncope.core.persistence.api.entity.Any) Connector(org.apache.syncope.core.provisioning.api.Connector) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) HashSet(java.util.HashSet)

Aggregations

AnyUtils (org.apache.syncope.core.persistence.api.entity.AnyUtils)25 PlainSchema (org.apache.syncope.core.persistence.api.entity.PlainSchema)15 Realm (org.apache.syncope.core.persistence.api.entity.Realm)11 List (java.util.List)10 StringUtils (org.apache.commons.lang3.StringUtils)10 User (org.apache.syncope.core.persistence.api.entity.user.User)10 Autowired (org.springframework.beans.factory.annotation.Autowired)10 Collections (java.util.Collections)9 Set (java.util.Set)9 SyncopeClientCompositeException (org.apache.syncope.common.lib.SyncopeClientCompositeException)9 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)9 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)9 Transactional (org.springframework.transaction.annotation.Transactional)9 HashMap (java.util.HashMap)8 Group (org.apache.syncope.core.persistence.api.entity.group.Group)8 Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Optional (java.util.Optional)7 UserDAO (org.apache.syncope.core.persistence.api.dao.UserDAO)7