You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@syncope.apache.org by md...@apache.org on 2019/05/31 13:27:35 UTC

[syncope] branch 2_0_X updated: [SYNCOPE-1473] conservative membership policy management

This is an automated email from the ASF dual-hosted git repository.

mdisabatino pushed a commit to branch 2_0_X
in repository https://gitbox.apache.org/repos/asf/syncope.git


The following commit(s) were added to refs/heads/2_0_X by this push:
     new 7e3d76c  [SYNCOPE-1473] conservative membership policy management
7e3d76c is described below

commit 7e3d76ca42369bb56598663c9721248a5e5b1f53
Author: Marco Di Sabatino Di Diodoro <ma...@tirasa.net>
AuthorDate: Fri May 31 15:25:22 2019 +0200

    [SYNCOPE-1473] conservative membership policy management
---
 .../LDAPMembershipPropagationActions.java          |  59 ++++++++---
 .../syncope/fit/core/PropagationTaskITCase.java    | 118 +++++++++++++++++++++
 2 files changed, 164 insertions(+), 13 deletions(-)

diff --git a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/propagation/LDAPMembershipPropagationActions.java b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/propagation/LDAPMembershipPropagationActions.java
index c392db2..86ec4a2 100644
--- a/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/propagation/LDAPMembershipPropagationActions.java
+++ b/core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/propagation/LDAPMembershipPropagationActions.java
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.TreeSet;
 import org.apache.commons.jexl3.JexlContext;
 import org.apache.commons.jexl3.MapContext;
 import org.apache.commons.lang3.StringUtils;
@@ -33,6 +34,7 @@ import org.apache.syncope.core.persistence.api.entity.user.User;
 import org.apache.syncope.core.provisioning.java.jexl.JexlUtils;
 import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
 import org.apache.syncope.core.persistence.api.dao.GroupDAO;
+import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
 import org.apache.syncope.core.persistence.api.entity.resource.Provision;
 import org.identityconnectors.framework.common.objects.Attribute;
 import org.identityconnectors.framework.common.objects.AttributeBuilder;
@@ -87,33 +89,45 @@ public class LDAPMembershipPropagationActions extends DefaultPropagationActions
                 for (String groupKey : userDAO.findAllGroupKeys(user)) {
                     Group group = groupDAO.find(groupKey);
                     if (group != null && groupDAO.findAllResourceKeys(groupKey).contains(task.getResource().getKey())) {
-                        LOG.debug("Evaluating connObjectLink for {}", group);
-
-                        JexlContext jexlContext = new MapContext();
-                        JexlUtils.addFieldsToContext(group, jexlContext);
-                        JexlUtils.addPlainAttrsToContext(group.getPlainAttrs(), jexlContext);
-                        JexlUtils.addDerAttrsToContext(group, jexlContext);
-
-                        String groupConnObjectLinkLink =
-                                JexlUtils.evaluate(provision.getMapping().getConnObjectLink(), jexlContext);
-                        LOG.debug("ConnObjectLink for {} is '{}'", group, groupConnObjectLinkLink);
-                        if (StringUtils.isNotBlank(groupConnObjectLinkLink)) {
-                            groupConnObjectLinks.add(groupConnObjectLinkLink);
+
+                        String groupConnObjectLink = evaluateGroupConnObjectLink(
+                                provision.getMapping().getConnObjectLink(), group);
+
+                        LOG.debug("ConnObjectLink for {} is '{}'", group, groupConnObjectLink);
+                        if (StringUtils.isNotBlank(groupConnObjectLink)) {
+                            groupConnObjectLinks.add(groupConnObjectLink);
                         }
                     }
                 }
                 LOG.debug("Group connObjectLinks to propagate for membership: {}", groupConnObjectLinks);
 
                 Set<Attribute> attributes = new HashSet<>(task.getAttributes());
-
                 Set<String> groups = new HashSet<>(groupConnObjectLinks);
                 Attribute ldapGroups = AttributeUtil.find(getGroupMembershipAttrName(), attributes);
+
                 if (ldapGroups != null) {
                     for (Object obj : ldapGroups.getValue()) {
                         groups.add(obj.toString());
                     }
                     attributes.remove(ldapGroups);
+
+                    if (beforeObj != null && beforeObj.getAttributeByName(getGroupMembershipAttrName()) != null) {
+                        Set<String> connObjectLinks = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+                        buildManagedGroupConnObjectLinks(
+                                provision.getResource(),
+                                provision.getMapping().getConnObjectLink(),
+                                connObjectLinks);
+
+                        Attribute beforeLdapGroups = beforeObj.getAttributeByName(getGroupMembershipAttrName());
+                        LOG.debug("Memberships not managed by Syncope: {}", beforeLdapGroups);
+                        for (Object value : beforeLdapGroups.getValue()) {
+                            if (!connObjectLinks.contains(String.valueOf(value))) {
+                                groups.add(String.valueOf(value));
+                            }
+                        }
+                    }
                 }
+                LOG.debug("Add ldapGroups to attributes: {}" + groups);
                 attributes.add(AttributeBuilder.build(getGroupMembershipAttrName(), groups));
 
                 task.setAttributes(attributes);
@@ -122,4 +136,23 @@ public class LDAPMembershipPropagationActions extends DefaultPropagationActions
             LOG.debug("Not about user, or group mapping missing for resource: not doing anything");
         }
     }
+
+    private String evaluateGroupConnObjectLink(final String connObjectLinkTemplate, final Group group) {
+        LOG.debug("Evaluating connObjectLink for {}", group);
+
+        JexlContext jexlContext = new MapContext();
+        JexlUtils.addFieldsToContext(group, jexlContext);
+        JexlUtils.addPlainAttrsToContext(group.getPlainAttrs(), jexlContext);
+        JexlUtils.addDerAttrsToContext(group, jexlContext);
+
+        return JexlUtils.evaluate(connObjectLinkTemplate, jexlContext);
+    }
+
+    private void buildManagedGroupConnObjectLinks(final ExternalResource externalResource,
+            final String connObjectLinkTemplate, final Set<String> connObjectLinks) {
+        List<Group> managedGroups = groupDAO.findByResource(externalResource);
+        for (Group group : managedGroups) {
+            connObjectLinks.add(evaluateGroupConnObjectLink(connObjectLinkTemplate, group));
+        }
+    }
 }
diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PropagationTaskITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PropagationTaskITCase.java
index 5692667..b15af41 100644
--- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PropagationTaskITCase.java
+++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/PropagationTaskITCase.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -38,7 +39,10 @@ import org.apache.commons.collections4.IterableUtils;
 import org.apache.commons.collections4.Predicate;
 import org.apache.commons.lang3.SerializationUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.syncope.common.lib.SyncopeClientException;
 import org.apache.syncope.common.lib.patch.AttrPatch;
+import org.apache.syncope.common.lib.patch.DeassociationPatch;
+import org.apache.syncope.common.lib.patch.MembershipPatch;
 import org.apache.syncope.common.lib.patch.UserPatch;
 import org.apache.syncope.common.lib.to.TaskTO;
 import org.apache.syncope.common.lib.to.AnyObjectTO;
@@ -48,13 +52,20 @@ import org.apache.syncope.common.lib.to.ConnObjectTO;
 import org.apache.syncope.common.lib.to.PagedResult;
 import org.apache.syncope.common.lib.to.PropagationTaskTO;
 import org.apache.syncope.common.lib.to.ExecTO;
+import org.apache.syncope.common.lib.to.GroupTO;
 import org.apache.syncope.common.lib.to.ItemTO;
+import org.apache.syncope.common.lib.to.MembershipTO;
+import org.apache.syncope.common.lib.to.PlainSchemaTO;
 import org.apache.syncope.common.lib.to.ProvisionTO;
 import org.apache.syncope.common.lib.to.ResourceTO;
 import org.apache.syncope.common.lib.to.UserTO;
 import org.apache.syncope.common.lib.types.AnyTypeKind;
+import org.apache.syncope.common.lib.types.AttrSchemaType;
+import org.apache.syncope.common.lib.types.ClientExceptionType;
 import org.apache.syncope.common.lib.types.MappingPurpose;
 import org.apache.syncope.common.lib.types.PatchOperation;
+import org.apache.syncope.common.lib.types.ResourceDeassociationAction;
+import org.apache.syncope.common.lib.types.SchemaType;
 import org.apache.syncope.common.lib.types.TaskType;
 import org.apache.syncope.common.rest.api.beans.ExecuteQuery;
 import org.apache.syncope.common.rest.api.beans.ExecQuery;
@@ -400,4 +411,111 @@ public class PropagationTaskITCase extends AbstractTaskITCase {
             }
         }
     }
+
+    @Test
+    public void issueSYNCOPE1473() throws ParseException {
+        // create a new group schema
+        PlainSchemaTO schemaTO = new PlainSchemaTO();
+        schemaTO.setKey("ldapGroups" + getUUIDString());
+        schemaTO.setType(AttrSchemaType.String);
+        schemaTO.setMultivalue(true);
+        schemaTO.setReadonly(true);
+        schemaTO.setAnyTypeClass("minimal user");
+
+        schemaTO = createSchema(SchemaType.PLAIN, schemaTO);
+        assertNotNull(schemaTO);
+
+        ResourceTO ldap = resourceService.read(RESOURCE_NAME_LDAP);
+        try {
+            // 1. clone the LDAP resource and add some sensible mappings
+            ProvisionTO provisionGroup = SerializationUtils.clone(ldap.getProvision(AnyTypeKind.GROUP.name()));
+            assertNotNull(provisionGroup);
+            provisionGroup.getVirSchemas().clear();
+            
+            ProvisionTO provisionUser = SerializationUtils.clone(ldap.getProvision(AnyTypeKind.USER.name()));
+            assertNotNull(provisionUser);
+            CollectionUtils.filterInverse(provisionUser.getMapping().getItems(), new Predicate<ItemTO>() {
+
+                @Override
+                public boolean evaluate(final ItemTO item) {
+                    return "mail".equals(item.getExtAttrName());
+                }
+            });
+            provisionUser.getVirSchemas().clear();
+
+            ItemTO ldapGroups = new ItemTO();
+            ldapGroups.setPurpose(MappingPurpose.PROPAGATION);
+            ldapGroups.setIntAttrName(schemaTO.getKey());
+            ldapGroups.setExtAttrName("ldapGroups");
+            provisionUser.getMapping().add(ldapGroups);
+
+            ldap.getProvisions().clear();
+            ldap.getProvisions().add(provisionUser);
+            ldap.getProvisions().add(provisionGroup);
+            ldap.setKey(RESOURCE_NAME_LDAP + "1473" + getUUIDString());
+            resourceService.create(ldap);
+
+            // 1. create group with the new resource assigned
+            GroupTO groupTO = new GroupTO();
+            groupTO.setName("SYNCOPEGROUP1473-" + getUUIDString());
+            groupTO.setRealm("/");
+            groupTO.getResources().add(ldap.getKey());
+
+            groupTO = createGroup(groupTO).getEntity();
+            assertNotNull(groupTO);
+
+            // 2. create user with the new resource assigned
+            UserTO userTO = UserITCase.getUniqueSampleTO("syncope1473@syncope.apache.org");
+            userTO.getResources().clear();
+            userTO.getResources().add(ldap.getKey());
+            userTO.getMemberships().add(new MembershipTO.Builder().group(groupTO.getKey()).build());
+
+            userTO = createUser(userTO).getEntity();
+            assertNotNull(userTO);
+
+            // 3. check attributes prepared for propagation
+            PagedResult<PropagationTaskTO> tasks = taskService.search(new TaskQuery.Builder(TaskType.PROPAGATION).
+                    resource(userTO.getResources().iterator().next()).
+                    anyTypeKind(AnyTypeKind.USER).entityKey(userTO.getKey()).build());
+            assertEquals(1, tasks.getSize());
+
+            groupService.deassociate(new DeassociationPatch.Builder().key(groupTO.getKey()).
+                    action(ResourceDeassociationAction.UNLINK).resource(ldap.getKey()).build());
+            groupService.delete(groupTO.getKey());
+
+            GroupTO newGroupTO = new GroupTO();
+            newGroupTO.setName("NEWSYNCOPEGROUP1473-" + getUUIDString());
+            newGroupTO.setRealm("/");
+            newGroupTO.getResources().add(ldap.getKey());
+
+            newGroupTO = createGroup(newGroupTO).getEntity();
+            assertNotNull(newGroupTO);
+
+            UserPatch userPatch = new UserPatch();
+            userPatch.setKey(userTO.getKey());
+            userPatch.getMemberships().add(
+                    new MembershipPatch.Builder().group(
+                            newGroupTO.getKey()).operation(PatchOperation.ADD_REPLACE).build());
+            userService.update(userPatch);
+
+            Set<Attribute> propagationAttrs = new HashSet<>();
+            if (StringUtils.isNotBlank(tasks.getResult().get(0).getAttributes())) {
+                propagationAttrs.addAll(Arrays.asList(
+                        POJOHelper.deserialize(tasks.getResult().get(0).getAttributes(), Attribute[].class)));
+            }
+
+            ConnObjectTO connObject =
+                    resourceService.readConnObject(ldap.getKey(), AnyTypeKind.USER.name(), userTO.getKey());
+            assertNotNull(connObject);
+            assertNotNull(connObject.getAttr("ldapGroups"));
+            assertTrue(connObject.getAttr("ldapGroups").getValues().size() == 2);
+
+        } finally {
+            try {
+                resourceService.delete(ldap.getKey());
+            } catch (Exception ignore) {
+                // ignore
+            }
+        }
+    }
 }