ranger-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ab...@apache.org
Subject ranger git commit: RANGER-1581: Ranger plugins need to support additional date formats for tag attribute values
Date Thu, 18 May 2017 18:20:09 GMT
Repository: ranger
Updated Branches:
  refs/heads/master b08baf923 -> 39184d44a


RANGER-1581: Ranger plugins need to support additional date formats for tag attribute values


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

Branch: refs/heads/master
Commit: 39184d44aa26645fd000c7e4620807af6a1f2ee7
Parents: b08baf9
Author: Abhay Kulkarni <akulkarni@hortonworks.com>
Authored: Thu May 18 11:19:57 2017 -0700
Committer: Abhay Kulkarni <akulkarni@hortonworks.com>
Committed: Thu May 18 11:19:57 2017 -0700

----------------------------------------------------------------------
 .../RangerScriptExecutionContext.java           | 49 +++++++++++++-------
 .../plugin/policyengine/TestPolicyEngine.java   |  2 +-
 .../test_policyengine_tag_hive.json             |  2 +-
 3 files changed, 34 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ranger/blob/39184d44/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerScriptExecutionContext.java
----------------------------------------------------------------------
diff --git a/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerScriptExecutionContext.java
b/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerScriptExecutionContext.java
index c46f527..b0bba80 100644
--- a/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerScriptExecutionContext.java
+++ b/agents-common/src/main/java/org/apache/ranger/plugin/conditionevaluator/RangerScriptExecutionContext.java
@@ -30,19 +30,23 @@ import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
 import org.apache.ranger.plugin.policyengine.RangerAccessResource;
 import org.apache.ranger.plugin.util.RangerAccessRequestUtil;
 
-import java.text.DateFormat;
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.TimeZone;
 
 public final class RangerScriptExecutionContext {
 	private static final Log LOG = LogFactory.getLog(RangerScriptExecutionContext.class);
 	private static final String TAG_ATTR_DATE_FORMAT_PROP                   = "ranger.plugin.tag.attr.additional.date.formats";
+	private static final String TAG_ATTR_DATE_FORMAT_SEPARATOR              = "||";
+	private static final String TAG_ATTR_DATE_FORMAT_SEPARATOR_REGEX        = "\\|\\|";
 	private static final String DEFAULT_RANGER_TAG_ATTRIBUTE_DATE_FORMAT    = "yyyy/MM/dd";
 	private static final String DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT_NAME = "ATLAS_DATE_FORMAT";
 	private static final String DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT     = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
@@ -54,19 +58,26 @@ public final class RangerScriptExecutionContext {
 
 	static {
 		StringBuilder sb = new StringBuilder(DEFAULT_RANGER_TAG_ATTRIBUTE_DATE_FORMAT);
+		sb.append(TAG_ATTR_DATE_FORMAT_SEPARATOR).append(DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT_NAME);
 
 		String additionalDateFormatsValue = RangerConfiguration.getInstance().get(TAG_ATTR_DATE_FORMAT_PROP);
 		if (StringUtils.isNotBlank(additionalDateFormatsValue)) {
-			sb.append(", ").append(additionalDateFormatsValue);
+			sb.append(TAG_ATTR_DATE_FORMAT_SEPARATOR).append(additionalDateFormatsValue);
 		}
 
-		dateFormatStrings = sb.toString().split(", ");
+		dateFormatStrings = sb.toString().split(TAG_ATTR_DATE_FORMAT_SEPARATOR_REGEX);
+		Arrays.sort(dateFormatStrings, new Comparator<String>() {
+			@Override
+			public int compare(String first, String second) {
+				return Integer.compare(second.length(), first.length());
+			}
+		});
 	}
 
-	private static final ThreadLocal<List<DateFormat>> THREADLOCAL_DATE_FORMATS
=
-			new ThreadLocal<List<DateFormat>>() {
-				@Override protected List<DateFormat> initialValue() {
-					List<DateFormat> ret = new ArrayList<>();
+	private static final ThreadLocal<List<SimpleDateFormat>> THREADLOCAL_DATE_FORMATS
=
+			new ThreadLocal<List<SimpleDateFormat>>() {
+				@Override protected List<SimpleDateFormat> initialValue() {
+					List<SimpleDateFormat> ret = new ArrayList<>();
 
 					for (String dateFormatString : dateFormatStrings) {
 						try {
@@ -74,7 +85,8 @@ public final class RangerScriptExecutionContext {
 								if (StringUtils.equalsIgnoreCase(dateFormatString, DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT_NAME))
{
 									dateFormatString = DEFAULT_ATLAS_TAG_ATTRIBUTE_DATE_FORMAT;
 								}
-								DateFormat df = new SimpleDateFormat(dateFormatString);
+								SimpleDateFormat df = new SimpleDateFormat(dateFormatString);
+								df.setLenient(false);
 								ret.add(df);
 							}
 						} catch (Exception exception) {
@@ -250,16 +262,16 @@ public final class RangerScriptExecutionContext {
 		this.result = result;
 	}
 
-	// Utilities - TODO
-
-	public Date getAsDate(String value, DateFormat df) {
+	private Date getAsDate(String value, SimpleDateFormat df) {
 		Date ret = null;
 
+		TimeZone savedTimeZone = df.getTimeZone();
 		try {
-			if (df != null) {
-				ret = df.parse(value);
-			}
-		} catch (Exception ex) {
+			ret = df.parse(value);
+		} catch (ParseException exception) {
+			// Ignore
+		} finally {
+			df.setTimeZone(savedTimeZone);
 		}
 
 		return ret;
@@ -269,9 +281,12 @@ public final class RangerScriptExecutionContext {
 		Date ret = null;
 
 		if (StringUtils.isNotBlank(value)) {
-			for (DateFormat dateFormat : THREADLOCAL_DATE_FORMATS.get()) {
-				ret = getAsDate(value, dateFormat);
+			for (SimpleDateFormat simpleDateFormat : THREADLOCAL_DATE_FORMATS.get()) {
+				ret = getAsDate(value, simpleDateFormat);
 				if (ret != null) {
+					if (LOG.isDebugEnabled()) {
+						LOG.debug("The best match found for Format-String:[" + simpleDateFormat.toPattern()
+ "], date:[" + ret +"]");
+					}
 					break;
 				}
 			}

http://git-wip-us.apache.org/repos/asf/ranger/blob/39184d44/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java
----------------------------------------------------------------------
diff --git a/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java
b/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java
index e556f16..bdbb823 100644
--- a/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java
+++ b/agents-common/src/test/java/org/apache/ranger/plugin/policyengine/TestPolicyEngine.java
@@ -149,7 +149,7 @@ public class TestPolicyEngine {
 				"        </property>\n" +
 				"        <property>\n" +
 				"                <name>ranger.plugin.tag.attr.additional.date.formats</name>\n"
+
-				"                <value>abcd, ATLAS_DATE_FORMAT</value>\n" +
+				"                <value>abcd||xyz||yyyy/MM/dd'T'HH:mm:ss.SSS'Z'</value>\n"
+
 				"        </property>\n" +
 				"</configuration>\n");
 		writer.close();

http://git-wip-us.apache.org/repos/asf/ranger/blob/39184d44/agents-common/src/test/resources/policyengine/test_policyengine_tag_hive.json
----------------------------------------------------------------------
diff --git a/agents-common/src/test/resources/policyengine/test_policyengine_tag_hive.json
b/agents-common/src/test/resources/policyengine/test_policyengine_tag_hive.json
index 34ccaac..04b9afe 100644
--- a/agents-common/src/test/resources/policyengine/test_policyengine_tag_hive.json
+++ b/agents-common/src/test/resources/policyengine/test_policyengine_tag_hive.json
@@ -233,7 +233,7 @@
         "resource":{"elements":{"database":"employee", "table":"personal", "column":"ssn"}},
         "accessType":"select","user":"user1","userGroups":[],"requestData":"select ssn from
employee.personal;' for user1",
 
-        "context": {"TAGS":"[{\"type\":\"EXPIRES_ON\", \"attributes\":{\"expiry_date\":\"2015/08/10\"}}]"}
+        "context": {"TAGS":"[{\"type\":\"EXPIRES_ON\", \"attributes\":{\"expiry_date\":\"2015/08/10T15:05:15.000Z\"}}]"}
       },
       "result":{"isAudited":true,"isAllowed":false,"policyId":5}
     },


Mime
View raw message