RANGER-543: RangerTimeOfDataMatcher updated to support time ranges that span across midnight
Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/08990c5b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/08990c5b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/08990c5b Branch: refs/heads/tag-policy Commit: 08990c5bdee9aacc4f69eaf777202904fcc92ef6 Parents: 4c75d28 Author: Madhan Neethiraj <[email protected]> Authored: Tue Jun 9 17:55:13 2015 -0700 Committer: Madhan Neethiraj <[email protected]> Committed: Tue Jun 9 17:55:13 2015 -0700 ---------------------------------------------------------------------- .../RangerTimeOfDayMatcher.java | 70 +++++++++----------- .../RangerTimeOfDayMatcherTest.java | 15 +++-- 2 files changed, 40 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/08990c5b/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcher.java ---------------------------------------------------------------------- diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcher.java b/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcher.java index 40d86a5..3ddb250 100644 --- a/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcher.java +++ b/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcher.java @@ -72,7 +72,7 @@ public class RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { } if(LOG.isDebugEnabled()) { - LOG.debug("<== RangerTimeOfDayMatcher.init(" + condition + "): countries[" + _durations + "]"); + LOG.debug("<== RangerTimeOfDayMatcher.init(" + condition + "): durations[" + toString() + "]"); } } @@ -105,17 +105,13 @@ public class RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { endMinute = Integer.parseInt(m.group(7)); } String endType = m.group(8).toUpperCase(); - if (startType.equals("P") && endType.equals("A")) { - LOG.warn("extractDuration: Invalid duration:" + value); - } else { - if (startType.equals("P")) { - startHour += 12; - } - if (endType.equals("P")) { - endHour += 12; - } - result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; + if (startType.equals("P")) { + startHour += 12; } + if (endType.equals("P")) { + endHour += 12; + } + result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute }; } } @@ -145,12 +141,12 @@ public class RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { calendar.setTime(date); int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); int minutes = calendar.get(Calendar.MINUTE); - if (durationMatched(_durations, hourOfDay, minutes)) { + if (! durationMatched(_durations, hourOfDay, minutes)) { + matched = false; + if (LOG.isDebugEnabled()) { - LOG.debug("isMatched: None of the durations contains this hour of day[" + hourOfDay + "]"); + LOG.debug("isMatched: None of the durations contains this hour of day[" + hourOfDay + "] and minutes[" + minutes + "]"); } - } else { - matched = false; } } @@ -166,36 +162,32 @@ public class RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator { int start = aDuration[0]; int end = aDuration[1]; int minutesOfDay = hourOfDay*60 + minutes; - if (start <= minutesOfDay && minutesOfDay <= end) { - return true; + if(start < end) { + if (start <= minutesOfDay && minutesOfDay <= end) { + return true; + } + } else { + if(start <= minutesOfDay || minutesOfDay <= end) { + return true; + } } } return false; } - String extractValue(final RangerAccessRequest request, String key) { - if(LOG.isDebugEnabled()) { - LOG.debug("==> RangerSimpleMatcher.extractValue(" + request+ ")"); - } - - String value = null; - if (request == null) { - LOG.debug("isMatched: Unexpected: null request. Returning null!"); - } else if (request.getContext() == null) { - LOG.debug("isMatched: Context map of request is null. Ok. Returning null!"); - } else if (CollectionUtils.isEmpty(request.getContext().entrySet())) { - LOG.debug("isMatched: Missing context on request. Ok. Condition isn't applicable. Returning null!"); - } else if (!request.getContext().containsKey(key)) { - if (LOG.isDebugEnabled()) { - LOG.debug("isMatched: Unexpected: Context did not have data for condition[" + key + "]. Returning null!"); - } - } else { - value = (String)request.getContext().get(key); + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("RangerTimeOfDayMatcher {"); + sb.append("_allowAny=" + _allowAny).append(" "); + sb.append("_durations=["); + for(int[] duration : _durations) { + sb.append("{start=" + duration[0] + "; end=" + duration[1] + "} "); } + sb.append("]"); + sb.append("}"); - if(LOG.isDebugEnabled()) { - LOG.debug("<== RangerSimpleMatcher.extractValue(" + request+ "): " + value); - } - return value; + return sb.toString(); } } http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/08990c5b/agents-common/src/test/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcherTest.java ---------------------------------------------------------------------- diff --git a/agents-common/src/test/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcherTest.java b/agents-common/src/test/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcherTest.java index b6ca843..e894d98 100644 --- a/agents-common/src/test/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcherTest.java +++ b/agents-common/src/test/java/org/apache/ranger/plugin/conditionevaluator/RangerTimeOfDayMatcherTest.java @@ -51,7 +51,7 @@ public class RangerTimeOfDayMatcherTest { String[] durations = new String[] { "9am-5pm", " 9Am -5 Pm", " 9Am -5 Pm", "9 AM -5 p.m.", "9a.M - 5Pm.", "9:30am-5:30pm", " 9:00Am -5:59 Pm", - " 9 am - 4 pm " + " 9 am - 4 pm ", "9pm-5AM" }; check(durations, true); } @@ -60,7 +60,6 @@ public class RangerTimeOfDayMatcherTest { public void test_patterMatching_unexpectedMatches() { // even semantically illegal durations work -- parsing is just for format not for semantics! String[] durations = new String[] { - "9pm-5AM", // illegal duration where start > end is allows parsed as right! "00PM-44PM", // any two digits are allowed! "9:0am-5:7pm", // Minute part can be one digit }; @@ -101,6 +100,7 @@ public class RangerTimeOfDayMatcherTest { { "9 AM -5 p.m.", "9", null, "A", "5", null, "p" }, { "9:30AM - 5:15pm", "9", "30", "A", "5", "15", "p" }, { "9:30 AM - 5:15 p.m.", "9", "30", "A", "5", "15", "p" }, + { "9pm-5am", "9", null, "p", "5", null, "a"}, }; checkGroups(input); } @@ -125,7 +125,7 @@ public class RangerTimeOfDayMatcherTest { Object[][] input = new Object[][] { { "9am-5pm", true, 9*60, (12+5)*60 }, { "1 PM - 10P.M.", true, (12+1)*60, (12+10)*60 }, - { "1PM - 9AM", false, null, null }, // illegal duration should come back as null + { "1PM - 9AM", true, (12+1)*60, 9*60 }, { "1PM", false, null, null }, // illegal patterns should come back as null, too }; for (Object[] data: input) { @@ -175,7 +175,7 @@ public class RangerTimeOfDayMatcherTest { @Test public void test_end2end_happyPath() { RangerPolicyItemCondition itemCondition = mock(RangerPolicyItemCondition.class); - when(itemCondition.getValues()).thenReturn(Arrays.asList("2:45a.m. -7:00 AM", " 9:15AM- 5:30P.M. ")); + when(itemCondition.getValues()).thenReturn(Arrays.asList("2:45a.m. -7:00 AM", " 9:15AM- 5:30P.M. ", "11pm-2am")); RangerTimeOfDayMatcher matcher = new RangerTimeOfDayMatcher(); matcher.setConditionDef(null); @@ -183,7 +183,9 @@ public class RangerTimeOfDayMatcherTest { matcher.init(); Object[][] input = new Object[][] { - { 1, 0, false }, + { 1, 0, true }, + { 2, 0, true }, + { 2, 1, false }, { 2, 44, false }, { 2, 45, true }, { 3, 0, true }, @@ -196,7 +198,8 @@ public class RangerTimeOfDayMatcherTest { {17, 30, true}, {17, 31, false}, {18, 0, false }, - {23, 0, false }, + {22, 59, false }, + {23, 0, true }, }; RangerAccessRequest request = mock(RangerAccessRequest.class);
