ranger-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sneet...@apache.org
Subject incubator-ranger git commit: RANGER-568: fixed RangerTimeOfDayMatcher condition to handle 12am/12pm correctly
Date Thu, 25 Jun 2015 23:17:50 GMT
Repository: incubator-ranger
Updated Branches:
  refs/heads/ranger-0.5 70887df6c -> cc690cd7a


RANGER-568: fixed RangerTimeOfDayMatcher condition to handle 12am/12pm correctly


Project: http://git-wip-us.apache.org/repos/asf/incubator-ranger/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ranger/commit/cc690cd7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ranger/tree/cc690cd7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ranger/diff/cc690cd7

Branch: refs/heads/ranger-0.5
Commit: cc690cd7a9fd1ec8b4e8bb6013c97fd4ff31d999
Parents: 70887df
Author: Madhan Neethiraj <madhan@apache.org>
Authored: Thu Jun 25 02:08:14 2015 -0700
Committer: sneethiraj <sneethir@apache.org>
Committed: Thu Jun 25 16:17:39 2015 -0700

----------------------------------------------------------------------
 .../RangerTimeOfDayMatcher.java                 | 14 ++++-
 .../RangerTimeOfDayMatcherTest.java             | 55 +++++++++++++++++++-
 2 files changed, 66 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/cc690cd7/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 3ddb250..f65f29e 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
@@ -105,12 +105,22 @@ public class RangerTimeOfDayMatcher extends RangerAbstractConditionEvaluator
{
 					endMinute = Integer.parseInt(m.group(7));
 				}
 				String endType = m.group(8).toUpperCase();
-				if (startType.equals("P")) {
+				if(startHour == 12) {
+					if(startType.equals("A")) {
+						startHour = 0;
+					}
+				} else if (startType.equals("P")) {
 					startHour += 12;
 				}
-				if (endType.equals("P")) {
+
+				if(endHour == 12) {
+					if(endType.equals("A")) {
+						endHour = 0;
+					}
+				} else if (endType.equals("P")) {
 					endHour += 12;
 				}
+
 				result = new int[] { (startHour*60)+startMin, (endHour*60)+endMinute };
 			}
 		}

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/cc690cd7/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 e894d98..7e8ef0a 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,8 @@ 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  ", "9pm-5AM"
+				"   9   am   -  4 pm  ", "9pm-5AM",
+				"11am-12pm", "11pm-12am", "12am-12pm", "12pm-12am"
 		};
 		check(durations, true);
 	}
@@ -101,6 +102,10 @@ public class RangerTimeOfDayMatcherTest {
 				{ "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"},
+				{ "11am-12pm", "11", null, "a", "12", null, "p" },
+				{ "11pm-12am", "11", null, "p", "12", null, "a" },
+				{ "12am-12pm", "12", null, "a", "12", null, "p" },
+				{ "12pm-12am", "12", null, "p", "12", null, "a" },
 		};
 		checkGroups(input);
 	}
@@ -126,6 +131,10 @@ public class RangerTimeOfDayMatcherTest {
 				{ "9am-5pm", true, 9*60, (12+5)*60 },
 				{ "1 PM - 10P.M.", true, (12+1)*60, (12+10)*60 },
 				{ "1PM - 9AM", true, (12+1)*60, 9*60 },
+				{ "11am-12pm", true, 11*60, 12*60 },
+				{ "11pm-12am", true, (12+11)*60, 0*60 },
+				{ "12am-12pm", true, 0*60, 12*60 },
+				{ "12pm-12am", true, 12*60, 0*60 },
 				{ "1PM", false, null, null }, // illegal patterns should come back as null, too
 		};
 		for (Object[] data: input) {
@@ -217,4 +226,48 @@ public class RangerTimeOfDayMatcherTest {
 			}
 		}
 	}
+
+	@Test
+	public void test_end2end_happyPath_12_oClock() {
+		RangerPolicyItemCondition itemCondition = mock(RangerPolicyItemCondition.class);
+		when(itemCondition.getValues()).thenReturn(Arrays.asList("12am-1am", "11am-12pm", "12pm-1pm",
"11pm-12am"));
+
+		RangerTimeOfDayMatcher matcher = new RangerTimeOfDayMatcher();
+		matcher.setConditionDef(null);
+		matcher.setPolicyItemCondition(itemCondition);
+		matcher.init();
+
+		Object[][] input = new Object[][] {
+				{ 0, 00, true },
+				{ 0, 01, true },
+				{ 1, 00, true },
+				{ 1, 01, false },
+				{ 10, 59, false },
+				{ 11, 00, true },
+				{ 11, 59, true },
+				{ 12, 00, true },
+				{ 12, 01, true },
+				{ 12, 59, true },
+				{ 13, 00, true },
+				{ 13, 01, false },
+				{22, 59, false },
+				{23, 0, true },
+				{23, 59, true },
+		};
+
+		RangerAccessRequest request = mock(RangerAccessRequest.class);
+		for (Object[] data : input) {
+			int hour = (int)data[0];
+			int minute = (int)data[1];
+			Calendar c = new GregorianCalendar(2015, Calendar.APRIL, 1, hour, minute);
+			Date aDate = c.getTime();
+			when(request.getAccessTime()).thenReturn(aDate);
+			boolean matchExpected = (boolean)data[2];
+			if (matchExpected) {
+				assertTrue("" + hour, matcher.isMatched(request));
+			} else {
+				assertFalse("" + hour, matcher.isMatched(request));
+			}
+		}
+	}
 }


Mime
View raw message