Repository: ranger Updated Branches: refs/heads/master ca9b5f512 -> 478f5ffc7
RANGER-1832: Export REST API should return exact matching results if polResource param is provided Project: http://git-wip-us.apache.org/repos/asf/ranger/repo Commit: http://git-wip-us.apache.org/repos/asf/ranger/commit/478f5ffc Tree: http://git-wip-us.apache.org/repos/asf/ranger/tree/478f5ffc Diff: http://git-wip-us.apache.org/repos/asf/ranger/diff/478f5ffc Branch: refs/heads/master Commit: 478f5ffc737b4c238db67e844e88fd73c61dc5da Parents: ca9b5f5 Author: pradeep <[email protected]> Authored: Fri Oct 13 13:45:20 2017 +0530 Committer: pradeep <[email protected]> Committed: Fri Oct 13 16:58:14 2017 +0530 ---------------------------------------------------------------------- .../org/apache/ranger/common/ServiceUtil.java | 54 ++++++++++++++++++++ .../org/apache/ranger/rest/ServiceREST.java | 1 + 2 files changed, 55 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ranger/blob/478f5ffc/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java ---------------------------------------------------------------------- diff --git a/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java b/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java index 6ad56d2..e82d33d 100644 --- a/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java +++ b/security-admin/src/main/java/org/apache/ranger/common/ServiceUtil.java @@ -1561,5 +1561,59 @@ public class ServiceUtil { return assetType; } + + public List<RangerPolicy> getMatchingPoliciesForResource(HttpServletRequest request, + List<RangerPolicy> policyLists) { + List<RangerPolicy> policies = new ArrayList<RangerPolicy>(); + if (request != null) { + String resource = request.getParameter(SearchFilter.POL_RESOURCE); + String serviceType = request.getParameter(SearchFilter.SERVICE_TYPE); + if (!StringUtil.isEmpty(resource) && !StringUtil.isEmpty(serviceType)) { + List<String> resourceList = null; + Map<String, RangerPolicy.RangerPolicyResource> rangerPolicyResourceMap = null; + RangerPolicy.RangerPolicyResource rangerPolicyResource = null; + for (RangerPolicy rangerPolicy : policyLists) { + if (rangerPolicy != null) { + rangerPolicyResourceMap = rangerPolicy.getResources(); + if (rangerPolicyResourceMap != null) { + if (rangerPolicyResourceMap.containsKey("path")) { + rangerPolicyResource = rangerPolicyResourceMap.get("path"); + if (rangerPolicyResource != null) { + resourceList = rangerPolicyResource.getValues(); + if (CollectionUtils.isNotEmpty(resourceList) && resourceList.size() == 1) { + String resourcePath = resourceList.get(0); + if (!StringUtil.isEmpty(resourcePath)) { + if (resourcePath.equals(resource) + || resourcePath.startsWith(resource + "/")) { + policies.add(rangerPolicy); + } + } + } + } + } else if (rangerPolicyResourceMap.containsKey("database")) { + rangerPolicyResource = rangerPolicyResourceMap.get("database"); + if (rangerPolicyResource != null) { + resourceList = rangerPolicyResource.getValues(); + if (CollectionUtils.isNotEmpty(resourceList) && resourceList.size() == 1) { + String resourcePath = resourceList.get(0); + if (!StringUtil.isEmpty(resourcePath)) { + if (resourcePath.equals(resource)) { + policies.add(rangerPolicy); + } + } + } + } + } + } + } + } + policyLists.clear(); + if (CollectionUtils.isNotEmpty(policies)) { + policyLists.addAll(policies); + } + } + } + return policyLists; + } } http://git-wip-us.apache.org/repos/asf/ranger/blob/478f5ffc/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java ---------------------------------------------------------------------- diff --git a/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java b/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java index e4d8a57..90e5383 100644 --- a/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java +++ b/security-admin/src/main/java/org/apache/ranger/rest/ServiceREST.java @@ -2221,6 +2221,7 @@ public class ServiceREST { } } } + policyLists=serviceUtil.getMatchingPoliciesForResource(request, policyLists); Map<Long, RangerPolicy> orderedPolicies = new TreeMap<Long, RangerPolicy>(); if (!CollectionUtils.isEmpty(policyLists)) {
