PolicyUtil { public static Map<String, Map<AtlasResourceTypes, List<String>>> createPermissionMap(List<PolicyDef> policyDefList, AtlasActionTypes permissionType, SimpleAtlasAuthorizer.AtlasAccessorTypes principalType) { if (isDebugEnabled) { LOG.debug("==> PolicyUtil createPermissionMap\nCreating Permission Map for :: {} & {}", permissionType, principalType); } Map<String, Map<AtlasResourceTypes, List<String>>> userReadMap = new HashMap<>(); for (PolicyDef policyDef : policyDefList) { if (LOG.isDebugEnabled()) { LOG.debug("Processing policy def : {}", policyDef); } Map<String, List<AtlasActionTypes>> principalMap = principalType.equals(SimpleAtlasAuthorizer.AtlasAccessorTypes.USER) ? policyDef.getUsers() : policyDef .getGroups(); for (Entry<String, List<AtlasActionTypes>> e : principalMap.entrySet()) { if (!e.getValue().contains(permissionType)) { continue; } String username = e.getKey(); Map<AtlasResourceTypes, List<String>> userResourceList = userReadMap.get(username); if (userResourceList == null) { if (isDebugEnabled) { LOG.debug("Resource list not found for {}, creating it", username); } userResourceList = new HashMap<>(); } for (Entry<AtlasResourceTypes, List<String>> resourceTypeMap : policyDef.getResources().entrySet()) { AtlasResourceTypes type = resourceTypeMap.getKey(); List<String> resourceList = userResourceList.get(type); if (resourceList == null) { resourceList = new ArrayList<>(); resourceList.addAll(resourceTypeMap.getValue()); } else { resourceList.removeAll(resourceTypeMap.getValue()); resourceList.addAll(resourceTypeMap.getValue()); } userResourceList.put(type, resourceList); } userReadMap.put(username, userResourceList); if (LOG.isDebugEnabled()) { LOG.debug("userReadMap {}", userReadMap); } } } if (isDebugEnabled) { LOG.debug("Returning Map for {} :: {}", principalType, userReadMap); LOG.debug("<== PolicyUtil createPermissionMap"); } return userReadMap; }  static Map<String, Map<AtlasResourceTypes, List<String>>> createPermissionMap(List<PolicyDef> policyDefList,
        AtlasActionTypes permissionType, SimpleAtlasAuthorizer.AtlasAccessorTypes principalType);  }
@Test public void testCreatePermissionMap() { HashMap<AtlasResourceTypes, List<String>> resourceMap = new HashMap<>(); List<String> resource1List = new ArrayList<>(); resource1List.add("*abc"); resourceMap.put(AtlasResourceTypes.ENTITY, resource1List); List<String> resource2List = new ArrayList<>(); resource2List.add("*xyz"); resourceMap.put(AtlasResourceTypes.OPERATION, resource2List); List<String> resource3List = new ArrayList<>(); resource3List.add("PII"); resourceMap.put(AtlasResourceTypes.TYPE, resource3List); Map<String, HashMap<AtlasResourceTypes, List<String>>> permissionMap = new HashMap<>(); permissionMap.put("grp1", resourceMap); List<String> policies = new ArrayList<>(); policies.add("hivePolicy;;usr1:r,usr2:rw;;grp1:rwu,grp2:u;;entity:*abc,operation:*xyz,type:PII"); List<PolicyDef> policyDefList = new PolicyParser().parsePolicies(policies); Map<String, Map<AtlasResourceTypes, List<String>>> createdPermissionMap = new PolicyUtil().createPermissionMap(policyDefList, AtlasActionTypes.READ, SimpleAtlasAuthorizer.AtlasAccessorTypes.GROUP); assertEquals(permissionMap, createdPermissionMap); }
@Test public void testMergeCreatePermissionMap() { HashMap<AtlasResourceTypes, List<String>> resourceMap = new HashMap<>(); List<String> resource1List = new ArrayList<>(); resource1List.add("*abc"); resourceMap.put(AtlasResourceTypes.ENTITY, resource1List); List<String> resource2List = new ArrayList<>(); resource2List.add("*x"); resource2List.add("*xyz"); resourceMap.put(AtlasResourceTypes.OPERATION, resource2List); List<String> resource3List = new ArrayList<>(); resource3List.add("PII"); resourceMap.put(AtlasResourceTypes.TYPE, resource3List); Map<String, HashMap<AtlasResourceTypes, List<String>>> permissionMap = new HashMap<>(); permissionMap.put("grp1", resourceMap); List<String> policies = new ArrayList<>(); policies.add("hivePolicys;;;;grp1:rwu;;entity:*abc,operation:*xyz,operation:*x"); policies.add("hivePolicy;;;;grp1:rwu;;entity:*abc,operation:*xyz"); policies.add("hivePolicy;;usr1:r,usr2:rw;;grp1:rwu;;entity:*abc,operation:*xyz"); policies.add("hivePolicy;;usr1:r,usr2:rw;;grp1:rwu,grp2:u;;entity:*abc,operation:*xyz,type:PII"); List<PolicyDef> policyDefList = new PolicyParser().parsePolicies(policies); Map<String, Map<AtlasResourceTypes, List<String>>> createdPermissionMap = new PolicyUtil().createPermissionMap(policyDefList, AtlasActionTypes.READ, SimpleAtlasAuthorizer.AtlasAccessorTypes.GROUP); assertEquals(permissionMap, createdPermissionMap); }