AmbariLdapDataPopulator { protected void refreshGroupMembers(LdapBatchDto batchInfo, LdapGroupDto group, Map<String, User> internalUsers, Map<String, Group> internalGroupsMap, Set<String> groupMemberAttributes, boolean recursive) throws AmbariException { Set<String> externalMembers = new HashSet<String>(); if (groupMemberAttributes == null) { groupMemberAttributes = new HashSet<String>(); } for (String memberAttributeValue : group.getMemberAttributes()) { LdapUserDto groupMember = getLdapUserByMemberAttr(memberAttributeValue); if (groupMember != null) { externalMembers.add(groupMember.getUserName()); } else { if (recursive && !groupMemberAttributes.contains(memberAttributeValue)) { LdapGroupDto subGroup = getLdapGroupByMemberAttr(memberAttributeValue); if (subGroup != null) { groupMemberAttributes.add(memberAttributeValue); addLdapGroup(batchInfo, internalGroupsMap, subGroup.getGroupName()); refreshGroupMembers(batchInfo, subGroup, internalUsers, internalGroupsMap, groupMemberAttributes, true); } } } } String groupName = group.getGroupName(); final Map<String, User> internalMembers = getInternalMembers(groupName); for (String externalMember : externalMembers) { if (internalUsers.containsKey(externalMember)) { final User user = internalUsers.get(externalMember); if (user == null) { if (!internalMembers.containsKey(externalMember)) { batchInfo.getMembershipToAdd().add(new LdapUserGroupMemberDto(groupName, externalMember)); } continue; } if (!user.isLdapUser()) { if (Configuration.LdapUsernameCollisionHandlingBehavior.SKIP == configuration.getLdapSyncCollisionHandlingBehavior()) { LOG.info("User '{}' skipped because it is local user", externalMember); batchInfo.getUsersSkipped().add(externalMember); continue; } else { batchInfo.getUsersToBecomeLdap().add(externalMember); } } if (!internalMembers.containsKey(externalMember)) { batchInfo.getMembershipToAdd().add(new LdapUserGroupMemberDto(groupName, externalMember)); } internalMembers.remove(externalMember); } else { batchInfo.getUsersToBeCreated().add(externalMember); batchInfo.getMembershipToAdd().add(new LdapUserGroupMemberDto(groupName, externalMember)); } } for (Entry<String, User> userToBeUnsynced : internalMembers.entrySet()) { final User user = userToBeUnsynced.getValue(); batchInfo.getMembershipToRemove().add(new LdapUserGroupMemberDto(groupName, user.getUserName())); } } @Inject  AmbariLdapDataPopulator(Configuration configuration, Users users); boolean isLdapEnabled(); LdapSyncDto getLdapSyncInfo(); LdapBatchDto synchronizeAllLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeAllLdapUsers(LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapGroups(Set<String> groups, LdapBatchDto batchInfo); LdapBatchDto synchronizeLdapUsers(Set<String> users, LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapGroups(LdapBatchDto batchInfo); LdapBatchDto synchronizeExistingLdapUsers(LdapBatchDto batchInfo);  }
@Test public void testRefreshGroupMembers() throws Exception { User user1 = createNiceMock(User.class); User user2 = createNiceMock(User.class); User user3 = createNiceMock(User.class); User user4 = createNiceMock(User.class); expect(user1.getUserName()).andReturn("user1").anyTimes(); expect(user2.getUserName()).andReturn("user2").anyTimes(); expect(user3.getUserName()).andReturn("user3").anyTimes(); expect(user4.getUserName()).andReturn("user4").anyTimes(); expect(user1.isLdapUser()).andReturn(false).anyTimes(); expect(user2.isLdapUser()).andReturn(true).anyTimes(); expect(user3.isLdapUser()).andReturn(true).anyTimes(); expect(user4.isLdapUser()).andReturn(false).anyTimes(); Group group1 = createNiceMock(Group.class); Group group2 = createNiceMock(Group.class); expect(group1.isLdapGroup()).andReturn(true).anyTimes(); expect(group2.isLdapGroup()).andReturn(true).anyTimes(); expect(group1.getGroupName()).andReturn("group1").anyTimes(); expect(group2.getGroupName()).andReturn("group2").anyTimes(); Configuration configuration = createNiceMock(Configuration.class); Users users = createNiceMock(Users.class); LdapTemplate ldapTemplate = createNiceMock(LdapTemplate.class); LdapServerProperties ldapServerProperties = createNiceMock(LdapServerProperties.class); expect(ldapServerProperties.getGroupNamingAttr()).andReturn("cn").anyTimes(); expect(ldapServerProperties.getUsernameAttribute()).andReturn("uid").anyTimes(); replay(ldapTemplate, ldapServerProperties, users, configuration); replay(user1, user2, user3, user4); replay(group1, group2); AmbariLdapDataPopulatorTestInstance populator = createMockBuilder(AmbariLdapDataPopulatorTestInstance.class) .addMockedMethod("getLdapUserByMemberAttr") .addMockedMethod("getLdapGroupByMemberAttr") .addMockedMethod("getInternalMembers") .withConstructor(configuration, users) .createNiceMock(); LdapGroupDto externalGroup = createNiceMock(LdapGroupDto.class); expect(externalGroup.getGroupName()).andReturn("group1").anyTimes(); expect(externalGroup.getMemberAttributes()).andReturn(createSet("user1", "user2", "user4", "user6")).anyTimes(); replay(externalGroup); Map<String, User> internalMembers = new HashMap<String, User>(); internalMembers.put("user1", user1); internalMembers.put("user3", user3); internalMembers.put("user4", user4); LdapBatchDto batchInfo = new LdapBatchDto(); LdapUserDto externalUser1 = createNiceMock(LdapUserDto.class); LdapUserDto externalUser2 = createNiceMock(LdapUserDto.class); LdapUserDto externalUser3 = createNiceMock(LdapUserDto.class); LdapUserDto externalUser4 = createNiceMock(LdapUserDto.class); expect(externalUser1.getUserName()).andReturn("user1").anyTimes(); expect(externalUser2.getUserName()).andReturn("user2").anyTimes(); expect(externalUser3.getUserName()).andReturn("user4").anyTimes(); expect(externalUser4.getUserName()).andReturn("user6").anyTimes(); replay(externalUser1, externalUser2, externalUser3, externalUser4); expect(populator.getLdapUserByMemberAttr("user1")).andReturn(externalUser1).anyTimes(); expect(populator.getLdapUserByMemberAttr("user2")).andReturn(externalUser2).anyTimes(); expect(populator.getLdapUserByMemberAttr("user4")).andReturn(null).anyTimes(); expect(populator.getLdapGroupByMemberAttr("user4")).andReturn(externalGroup).anyTimes(); expect(populator.getLdapUserByMemberAttr("user6")).andReturn(externalUser4).anyTimes(); expect(populator.getInternalMembers("group1")).andReturn(internalMembers).anyTimes(); replay(populator); populator.setLdapTemplate(ldapTemplate); populator.setLdapServerProperties(ldapServerProperties); Map<String, User> internalUsers = new HashMap<String, User>(); internalUsers.putAll(internalMembers); internalUsers.put("user2", user2); Map<String, Group> internalGroups = new HashMap<String, Group>(); internalGroups.put("group2", group2); populator.refreshGroupMembers(batchInfo, externalGroup, internalUsers, internalGroups, null, true); Set<String> groupMembersToAdd = new HashSet<String>(); for (LdapUserGroupMemberDto ldapUserGroupMemberDto : batchInfo.getMembershipToAdd()) { groupMembersToAdd.add(ldapUserGroupMemberDto.getUserName()); } assertEquals(3, groupMembersToAdd.size()); assertTrue(groupMembersToAdd.contains("user2")); assertTrue(groupMembersToAdd.contains("user6")); Set<String> groupMembersToRemove = new HashSet<String>(); for (LdapUserGroupMemberDto ldapUserGroupMemberDto : batchInfo.getMembershipToRemove()) { groupMembersToRemove.add(ldapUserGroupMemberDto.getUserName()); } assertEquals(2, groupMembersToRemove.size()); assertTrue(groupMembersToRemove.contains("user3")); assertEquals(1, batchInfo.getUsersToBeCreated().size()); assertTrue(batchInfo.getUsersToBeCreated().contains("user6")); assertEquals(1, batchInfo.getUsersToBecomeLdap().size()); assertTrue(batchInfo.getUsersToBecomeLdap().contains("user1")); assertTrue(!batchInfo.getUsersToBecomeLdap().contains("user4")); assertTrue(batchInfo.getGroupsToBecomeLdap().isEmpty()); assertEquals(1, batchInfo.getGroupsToBeCreated().size()); assertTrue(batchInfo.getGroupsToBeRemoved().isEmpty()); assertTrue(batchInfo.getUsersToBeRemoved().isEmpty()); verify(populator.loadLdapTemplate(), populator); }