From 1d091170344ec622d2f34e3bdeb292c48ecdd7c5 Mon Sep 17 00:00:00 2001 From: prashant Date: Wed, 29 Nov 2023 14:58:09 +0530 Subject: [PATCH] RANGER-4542: updated zone search API to support search by zoneNamePartial Signed-off-by: Madhan Neethiraj --- .../plugin/store/AbstractPredicateUtil.java | 7 ++-- .../store/SecurityZonePredicateUtil.java | 34 +++++++++++++++++++ .../ranger/biz/SecurityZoneDBStore.java | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/store/AbstractPredicateUtil.java b/agents-common/src/main/java/org/apache/ranger/plugin/store/AbstractPredicateUtil.java index 07c561506c..39aa155138 100644 --- a/agents-common/src/main/java/org/apache/ranger/plugin/store/AbstractPredicateUtil.java +++ b/agents-common/src/main/java/org/apache/ranger/plugin/store/AbstractPredicateUtil.java @@ -59,11 +59,12 @@ public void applyFilter(List objList, SearchFil } Comparator sorter = getSorter(filter); + boolean isDesc = (filter.getSortType() != null) && "desc".equalsIgnoreCase(filter.getSortType()); - if (sorter != null) { - boolean isDesc = "desc".equalsIgnoreCase(filter.getSortType()); - + if (sorter != null){ Collections.sort(objList, isDesc ? new ReverseComparator(sorter) : sorter); + }else if(isDesc) { + Collections.reverse(objList); } } diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/store/SecurityZonePredicateUtil.java b/agents-common/src/main/java/org/apache/ranger/plugin/store/SecurityZonePredicateUtil.java index 9164091ae2..54b4775029 100644 --- a/agents-common/src/main/java/org/apache/ranger/plugin/store/SecurityZonePredicateUtil.java +++ b/agents-common/src/main/java/org/apache/ranger/plugin/store/SecurityZonePredicateUtil.java @@ -39,6 +39,7 @@ public void addPredicates(SearchFilter filter, List predicates) { addPredicateForMatchingZoneId(filter.getParam(SearchFilter.ZONE_ID), predicates); addPredicateForMatchingZoneName(filter.getParam(SearchFilter.ZONE_NAME), predicates); addPredicateForNonMatchingZoneName(filter.getParam(SearchFilter.NOT_ZONE_NAME), predicates); + addPredicateForMatchingZoneNamePartial(filter.getParam(SearchFilter.ZONE_NAME_PARTIAL), predicates); } private Predicate addPredicateForServiceName(final String serviceName, List predicates) { @@ -170,5 +171,38 @@ public boolean evaluate(Object object) { return ret; } + + private Predicate addPredicateForMatchingZoneNamePartial(final String zoneName, List predicates) { + if (StringUtils.isEmpty(zoneName)) { + return null; + } + + Predicate ret = new Predicate() { + @Override + public boolean evaluate(Object object) { + if (object == null) { + return false; + } + + boolean ret = false; + + if (object instanceof RangerSecurityZone) { + RangerSecurityZone securityZone = (RangerSecurityZone) object; + + if (StringUtils.contains(securityZone.getName(), zoneName)) { + ret = true; + } + } + + return ret; + } + }; + + if (predicates != null) { + predicates.add(ret); + } + + return ret; + } } diff --git a/security-admin/src/main/java/org/apache/ranger/biz/SecurityZoneDBStore.java b/security-admin/src/main/java/org/apache/ranger/biz/SecurityZoneDBStore.java index 6c6ddc49f2..422dd0fe5f 100755 --- a/security-admin/src/main/java/org/apache/ranger/biz/SecurityZoneDBStore.java +++ b/security-admin/src/main/java/org/apache/ranger/biz/SecurityZoneDBStore.java @@ -213,7 +213,7 @@ public List getSecurityZones(SearchFilter filter) throws Exc } } - if (CollectionUtils.isNotEmpty(ret) && filter != null && !filter.isEmpty()) { + if (CollectionUtils.isNotEmpty(ret) && filter != null) { List copy = new ArrayList<>(ret); predicateUtil.applyFilter(copy, filter);