DataStore implements DataCacheProvider, RolesProvider { void processGroup(Group group) { if (group.getGroupMembers() == null) { group.setGroupMembers(new ArrayList<>()); } List<GroupMember> originalMembers = groupMemberCache.getIfPresent(group.getName()); List<GroupMember> curMembers = originalMembers == null ? new ArrayList<>() : new ArrayList<>(originalMembers); List<GroupMember> delMembers = new ArrayList<>(curMembers); List<GroupMember> newMembers = new ArrayList<>(group.getGroupMembers()); List<GroupMember> updMembers = new ArrayList<>(group.getGroupMembers()); AuthzHelper.removeGroupMembers(newMembers, curMembers); AuthzHelper.removeGroupMembers(delMembers, group.getGroupMembers()); AuthzHelper.removeGroupMembers(updMembers, newMembers); groupMemberCache.put(group.getName(), group.getGroupMembers()); long currentTime = System.currentTimeMillis(); for (GroupMember member : updMembers) { List<GroupMember> groupMembers = principalGroupCache.getIfPresent(member.getMemberName()); if (groupMembers == null) { if (AuthzHelper.shouldSkipGroupMember(member, currentTime)) { continue; } groupMembers = new ArrayList<>(); groupMembers.add(member); principalGroupCache.put(member.getMemberName(), groupMembers); } else { if (AuthzHelper.shouldSkipGroupMember(member, currentTime)) { groupMembers.removeIf(item -> item.getGroupName().equalsIgnoreCase(group.getName())); } else { for (GroupMember mbr : groupMembers) { if (mbr.getGroupName().equalsIgnoreCase(group.getName())) { mbr.setExpiration(member.getExpiration()); mbr.setSystemDisabled(member.getSystemDisabled()); mbr.setActive(member.getActive()); mbr.setApproved(member.getApproved()); break; } } } } } for (GroupMember member : newMembers) { if (AuthzHelper.shouldSkipGroupMember(member, currentTime)) { continue; } List<GroupMember> groupMembers = principalGroupCache.getIfPresent(member.getMemberName()); if (groupMembers == null) { groupMembers = new ArrayList<>(); principalGroupCache.put(member.getMemberName(), groupMembers); } groupMembers.add(member); } processGroupDeletedMembers(group.getName(), delMembers); } DataStore(ChangeLogStore clogStore, CloudStore cloudStore); JWKList getZtsJWKList(Boolean rfc); void init(); boolean processDomain(SignedDomain signedDomain, boolean saveInStore); boolean processDomainDeletes(); void processDomainChecks(); boolean processDomainUpdates(); DomainData getDomainData(String name); void addDomainToCache(String name, DataCache dataCache); @Override DataCache getDataCache(String domainName); List<GroupMember> getGroupMembers(final String groupName); void getAccessibleRoles(DataCache data, String domainName, String identity,
            String[] requestedRoleList, Set<String> accessibleRoles, boolean keepFullName); String getPublicKey(String domain, String service, String keyId); HostServices getHostServices(String host); CloudStore getCloudStore(); void setCloudStore(CloudStore cloudStore); Cache<String, DataCache> getCacheStore(); Map<String, String> getPublicKeyCache(); @Override List<Role> getRolesByDomain(String domain);  }
@Test public void testProcessGroup() { ChangeLogStore clogStore = new MockZMSFileChangeLogStore("/tmp/zts_server_unit_tests/zts_root", pkey, "0"); DataStore store = new DataStore(clogStore, null); assertNull(store.principalGroupCache.getIfPresent("user.user1")); assertNull(store.groupMemberCache.getIfPresent("coretech:group.dev-team")); Group group = new Group().setName("coretech:group.dev-team"); store.processGroup(group); assertTrue(store.groupMemberCache.getIfPresent("coretech:group.dev-team").isEmpty()); assertNull(store.principalGroupCache.getIfPresent("user.user1")); List<GroupMember> members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team")); members.add(new GroupMember().setMemberName("user.user2") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team")); group.setGroupMembers(members); store.processGroup(group); group = new Group().setName("coretech:group.pe-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.pe-team")); members.add(new GroupMember().setMemberName("coretech.api") .setPrincipalType(Principal.Type.SERVICE.getValue()) .setGroupName("coretech:group.pe-team")); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.dev-team"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user2")); members = store.groupMemberCache.getIfPresent("coretech:group.pe-team"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "coretech.api")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("user.user2"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); members = store.principalGroupCache.getIfPresent("coretech.api"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); group = new Group().setName("coretech:group.dev-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team")); members.add(new GroupMember().setMemberName("user.user3") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team")); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.dev-team"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user3")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("user.user2"); assertNotNull(members); assertTrue(members.isEmpty()); members = store.principalGroupCache.getIfPresent("user.user3"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); group = new Group().setName("coretech:group.dev-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(System.currentTimeMillis() + 100000))); members.add(new GroupMember().setMemberName("user.user3") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team")); members.add(new GroupMember().setMemberName("user.user4") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setSystemDisabled(1)); members.add(new GroupMember().setMemberName("user.user5") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(1000))); members.add(new GroupMember().setMemberName("user.user6") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(1000))); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.dev-team"); assertNotNull(members); assertEquals(members.size(), 5); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user3")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user4")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user5")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user6")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("user.user3"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertNull(store.principalGroupCache.getIfPresent("user.user4")); assertNull(store.principalGroupCache.getIfPresent("user.user5")); assertNull(store.principalGroupCache.getIfPresent("user.user6")); group = new Group().setName("coretech:group.dev-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(System.currentTimeMillis() + 100000))); members.add(new GroupMember().setMemberName("user.user3") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(1000))); members.add(new GroupMember().setMemberName("user.user4") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setSystemDisabled(0)); members.add(new GroupMember().setMemberName("user.user5") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(1000))); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.dev-team"); assertNotNull(members); assertEquals(members.size(), 4); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user3")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user4")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user5")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("user.user3"); assertNotNull(members); assertTrue(members.isEmpty()); members = store.principalGroupCache.getIfPresent("user.user4"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertNull(store.principalGroupCache.getIfPresent("user.user5")); assertNull(store.principalGroupCache.getIfPresent("user.user6")); group = new Group().setName("coretech:group.dev-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(System.currentTimeMillis() + 100000))); members.add(new GroupMember().setMemberName("user.user3") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(1000))); members.add(new GroupMember().setMemberName("user.user4") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setSystemDisabled(0)); members.add(new GroupMember().setMemberName("user.user5") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.dev-team") .setExpiration(Timestamp.fromMillis(System.currentTimeMillis() + 100000))); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.dev-team"); assertNotNull(members); assertEquals(members.size(), 4); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user3")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user4")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user5")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("user.user3"); assertNotNull(members); assertTrue(members.isEmpty()); members = store.principalGroupCache.getIfPresent("user.user4"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); members = store.principalGroupCache.getIfPresent("user.user5"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); group = new Group().setName("coretech:group.pe-team"); members = new ArrayList<>(); members.add(new GroupMember().setMemberName("user.user1") .setPrincipalType(Principal.Type.USER.getValue()) .setGroupName("coretech:group.pe-team")); members.add(new GroupMember().setMemberName("coretech.api") .setPrincipalType(Principal.Type.SERVICE.getValue()) .setGroupName("coretech:group.pe-team")); group.setGroupMembers(members); store.processGroup(group); members = store.groupMemberCache.getIfPresent("coretech:group.pe-team"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "user.user1")); assertTrue(ZTSTestUtils.verifyGroupMemberName(members, "coretech.api")); members = store.principalGroupCache.getIfPresent("user.user1"); assertNotNull(members); assertEquals(members.size(), 2); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.dev-team")); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); members = store.principalGroupCache.getIfPresent("coretech.api"); assertNotNull(members); assertEquals(members.size(), 1); assertTrue(ZTSTestUtils.verifyGroupMemberGroup(members, "coretech:group.pe-team")); }