ranger-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mad...@apache.org
Subject incubator-ranger git commit: RANGER-895: Mask UDF implementation updated to handle byte and char datatypes
Date Sun, 24 Apr 2016 06:59:57 GMT
Repository: incubator-ranger
Updated Branches:
  refs/heads/master 252cb4d3c -> a3a472d99


RANGER-895: Mask UDF implementation updated to handle byte and char datatypes


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

Branch: refs/heads/master
Commit: a3a472d999fe77f516b20f0c0644149c5ebe7470
Parents: 252cb4d
Author: Madhan Neethiraj <madhan@apache.org>
Authored: Wed Apr 20 23:48:15 2016 -0700
Committer: Madhan Neethiraj <madhan@apache.org>
Committed: Sat Apr 23 23:56:03 2016 -0700

----------------------------------------------------------------------
 .../service-defs/ranger-servicedef-hive.json    |  24 +-
 .../authorization/hive/udf/RangerBaseUdf.java   | 109 +++++++---
 .../authorization/hive/udf/RangerUdfMask.java   | 217 +++++++++++++------
 .../hive/udf/RangerUdfMaskFirstN.java           |  15 +-
 .../hive/udf/RangerUdfMaskHash.java             |   5 +
 .../hive/udf/RangerUdfMaskLastN.java            |  16 +-
 .../hive/udf/RangerUdfMaskShowFirstN.java       |  15 +-
 .../hive/udf/RangerUdfMaskShowLastN.java        |  17 +-
 8 files changed, 289 insertions(+), 129 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/agents-common/src/main/resources/service-defs/ranger-servicedef-hive.json
----------------------------------------------------------------------
diff --git a/agents-common/src/main/resources/service-defs/ranger-servicedef-hive.json b/agents-common/src/main/resources/service-defs/ranger-servicedef-hive.json
index eda2d22..4bb2607 100644
--- a/agents-common/src/main/resources/service-defs/ranger-servicedef-hive.json
+++ b/agents-common/src/main/resources/service-defs/ranger-servicedef-hive.json
@@ -253,7 +253,7 @@
 				"name": "MASK",
 				"label": "Mask",
 				"description": "Replace lowercase with 'x', uppercase with 'X', digits with '0'",
-				"transformer": "rangerUdfMask({col})",
+				"transformer": "default.mask({col})",
 				"dataMaskOptions": {
 				}
 			},
@@ -262,21 +262,21 @@
 				"name": "MASK_SHOW_LAST_4",
 				"label": "Partial mask: show last 4",
 				"description": "Show last 4 characters; replace rest with 'x'",
-				"transformer": "rangerUdfMaskShowLastN({col}, 4, 'x', 'x', 'x', -1, '1')"
+				"transformer": "default.mask_show_last_n({col}, 4, 'x', 'x', 'x', -1, '1')"
 			},
 			{
 				"itemId": 3,
 				"name": "MASK_SHOW_FIRST_4",
 				"label": "Partial mask: show first 4",
 				"description": "Show first 4 characters; replace rest with 'x'",
-				"transformer": "rangerUdfMaskShowFirstN({col}, 4, 'x', 'x', 'x', -1, '1')"
+				"transformer": "default.mask_show_first_n({col}, 4, 'x', 'x', 'x', -1, '1')"
 			},
 			{
 				"itemId": 4,
 				"name": "MASK_HASH",
 				"label": "Hash",
 				"description": "Hash the value",
-				"transformer": "rangerUdfMaskHash({col})"
+				"transformer": "default.mask_hash({col})"
 			},
 			{
 				"itemId": 5,
@@ -295,46 +295,42 @@
 				"name": "MASK_DATE_DAY",
 				"label": "Date: mask day",
 				"description": "Date: mask day",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', 1, -1, -1)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', 1, -1, -1)"
 			},
 			{
 				"itemId": 8,
 				"name": "MASK_DATE_MONTH",
 				"label": "Date: mask month",
 				"description": "Date: mask month",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', -1, 0, -1)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', -1, 0, -1)"
 			},
 			{
 				"itemId": 9,
 				"name": "MASK_DATE_YEAR",
 				"label": "Date: mask year",
 				"description": "Date: mask year",
-				"transformer": "rangerUdfMask",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', -1, -1, 0)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', -1, -1, 0)"
 			},
 			{
 				"itemId": 10,
 				"name": "MASK_DATE_SHOW_DAY",
 				"label": "Date: show only day",
 				"description": "Date: show only day",
-				"transformer": "rangerUdfMask",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', -1, 0, 0)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', -1, 0, 0)"
 			},
 			{
 				"itemId": 11,
 				"name": "MASK_DATE_SHOW_MONTH",
 				"label": "Date: show only month",
 				"description": "Date: show only month",
-				"transformer": "rangerUdfMask",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', 1, -1, 0)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', 1, -1, 0)"
 			},
 			{
 				"itemId": 12,
 				"name": "MASK_DATE_SHOW_YEAR",
 				"label": "Date: show only year",
 				"description": "Date: show only year",
-				"transformer": "rangerUdfMask",
-				"transformer": "rangerUdfMask({col}, 'x', 'x', 'x', -1, '1', 1, 0, -1)"
+				"transformer": "default.mask({col}, 'x', 'x', 'x', -1, '1', 1, 0, -1)"
 			}
 		]
 	},

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerBaseUdf.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerBaseUdf.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerBaseUdf.java
index 389b2a5..d2fbed9 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerBaseUdf.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerBaseUdf.java
@@ -22,14 +22,13 @@ package org.apache.ranger.authorization.hive.udf;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hive.common.type.HiveChar;
 import org.apache.hadoop.hive.common.type.HiveVarchar;
 import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
 import org.apache.hadoop.hive.ql.metadata.HiveException;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
 import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject;
-import org.apache.hadoop.hive.serde2.io.DateWritable;
-import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
-import org.apache.hadoop.hive.serde2.io.ShortWritable;
+import org.apache.hadoop.hive.serde2.io.*;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.*;
@@ -70,17 +69,7 @@ public abstract class RangerBaseUdf extends GenericUDF {
 
 	@Override
 	public Object evaluate(DeferredObject[] arguments) throws HiveException {
-		if (LOG.isDebugEnabled()) {
-			LOG.debug("==> RangerBaseUdf.evaluate(arguments.length=" + arguments.length + ")");
-		}
-
-		Object ret = transformerAdapter.getTransformedWritable(arguments[0]);
-
-		if (LOG.isDebugEnabled()) {
-			LOG.debug("<== RangerBaseUdf.evaluate(arguments.length=" + arguments.length + "): ret="
+ ret);
-		}
-
-		return ret;
+		return transformerAdapter.getTransformedWritable(arguments[0]);
 	}
 
 	@Override
@@ -89,16 +78,10 @@ public abstract class RangerBaseUdf extends GenericUDF {
 	}
 
 	static abstract class RangerTransformer {
-		static final Short   SHORT_0   = Short.valueOf((short)0);
-		static final Integer INTEGER_0 = Integer.valueOf((int)0);
-		static final Long    LONG_0    = Long.valueOf((long)0);
-		static final Date    DATE_0    = new Date(0);
-		static final String  STRING_0  = new String("0");
-
-
 		abstract void    init(ObjectInspector[] arguments, int startIdx);
 
 		abstract String  transform(String value);
+		abstract Byte    transform(Byte value);
 		abstract Short   transform(Short value);
 		abstract Integer transform(Integer value);
 		abstract Long    transform(Long value);
@@ -128,6 +111,14 @@ abstract class RangerTransformerAdapter {
 				ret = new HiveVarcharTransformerAdapter((HiveVarcharObjectInspector)columnType, transformer);
 				break;
 
+			case CHAR:
+				ret = new HiveCharTransformerAdapter((HiveCharObjectInspector)columnType, transformer);
+				break;
+
+			case BYTE:
+				ret = new ByteTransformerAdapter((ByteObjectInspector)columnType, transformer);
+				break;
+
 			case SHORT:
 				ret = new ShortTransformerAdapter((ShortObjectInspector)columnType, transformer);
 				break;
@@ -153,9 +144,40 @@ abstract class RangerTransformerAdapter {
 	}
 }
 
+class ByteTransformerAdapter extends RangerTransformerAdapter {
+	final ByteObjectInspector columnType;
+	final ByteWritable        writable;
+
+	public ByteTransformerAdapter(ByteObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
+		this(columnType, transformer, new ByteWritable());
+	}
+
+	public ByteTransformerAdapter(ByteObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer, ByteWritable writable) {
+		super(transformer);
+
+		this.columnType = columnType;
+		this.writable   = writable;
+	}
+
+	@Override
+	public Object getTransformedWritable(DeferredObject object) throws HiveException {
+		Byte value = (Byte)columnType.getPrimitiveJavaObject(object.get());
+
+		if(value != null) {
+			Byte transformedValue = transformer.transform(value);
+
+			writable.set(transformedValue);
+
+			return writable;
+		}
+
+		return null;
+	}
+}
+
 class DateTransformerAdapter extends RangerTransformerAdapter {
 	final DateObjectInspector columnType;
-	final DateWritable writable;
+	final DateWritable        writable;
 
 	public DateTransformerAdapter(DateObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new DateWritable());
@@ -184,9 +206,40 @@ class DateTransformerAdapter extends RangerTransformerAdapter {
 	}
 }
 
+class HiveCharTransformerAdapter extends RangerTransformerAdapter {
+	final HiveCharObjectInspector columnType;
+	final HiveCharWritable        writable;
+
+	public HiveCharTransformerAdapter(HiveCharObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
+		this(columnType, transformer, new HiveCharWritable());
+	}
+
+	public HiveCharTransformerAdapter(HiveCharObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer, HiveCharWritable writable) {
+		super(transformer);
+
+		this.columnType = columnType;
+		this.writable   = writable;
+	}
+
+	@Override
+	public Object getTransformedWritable(DeferredObject object) throws HiveException {
+		HiveChar value = columnType.getPrimitiveJavaObject(object.get());
+
+		if(value != null) {
+			String transformedValue = transformer.transform(value.getValue());
+
+			writable.set(transformedValue);
+
+			return writable;
+		}
+
+		return null;
+	}
+}
+
 class HiveVarcharTransformerAdapter extends RangerTransformerAdapter {
 	final HiveVarcharObjectInspector columnType;
-	final HiveVarcharWritable writable;
+	final HiveVarcharWritable        writable;
 
 	public HiveVarcharTransformerAdapter(HiveVarcharObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new HiveVarcharWritable());
@@ -217,7 +270,7 @@ class HiveVarcharTransformerAdapter extends RangerTransformerAdapter {
 
 class IntegerTransformerAdapter extends RangerTransformerAdapter {
 	final IntObjectInspector columnType;
-	final IntWritable writable;
+	final IntWritable        writable;
 
 	public IntegerTransformerAdapter(IntObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new IntWritable());
@@ -248,7 +301,7 @@ class IntegerTransformerAdapter extends RangerTransformerAdapter {
 
 class LongTransformerAdapter extends RangerTransformerAdapter {
 	final LongObjectInspector columnType;
-	final LongWritable writable;
+	final LongWritable        writable;
 
 	public LongTransformerAdapter(LongObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new LongWritable());
@@ -288,13 +341,13 @@ class NoTransformAdapter extends RangerTransformerAdapter {
 
 	@Override
 	public Object getTransformedWritable(DeferredObject object) throws HiveException {
-		return columnType.getPrimitiveWritableObject(object);
+		return columnType.getPrimitiveWritableObject(object.get());
 	}
 }
 
 class ShortTransformerAdapter extends RangerTransformerAdapter {
 	final ShortObjectInspector columnType;
-	final ShortWritable writable;
+	final ShortWritable        writable;
 
 	public ShortTransformerAdapter(ShortObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new ShortWritable());
@@ -325,7 +378,7 @@ class ShortTransformerAdapter extends RangerTransformerAdapter {
 
 class StringTransformerAdapter extends RangerTransformerAdapter {
 	final StringObjectInspector columnType;
-	final Text writable;
+	final Text                  writable;
 
 	public StringTransformerAdapter(StringObjectInspector columnType, RangerBaseUdf.RangerTransformer
transformer) {
 		this(columnType, transformer, new Text());

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMask.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMask.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMask.java
index fac4e57..9d5b2ce 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMask.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMask.java
@@ -21,12 +21,12 @@ package org.apache.ranger.authorization.hive.udf;
 
 import java.sql.Date;
 
-import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.serde2.io.ShortWritable;
+import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
-import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector;
-import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantStringObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.*;
 import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.Text;
+import org.apache.hadoop.io.LongWritable;
 
 
 public class RangerUdfMask extends RangerBaseUdf {
@@ -43,71 +43,88 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 	final static int MASKED_DAY_COMPONENT_VAL    = 1;
 	final static int MASKED_MONTH_COMPONENT_VAL  = 0;
 	final static int MASKED_YEAR_COMPONENT_VAL   = 0;
-	final static int UNMASKED_DATE_COMPONENT_VAL = -1;
+	final static int UNMASKED_VAL                = -1;
 
-	int maskedUpperChar   = MASKED_UPPERCASE;
-	int maskedLowerChar   = MASKED_LOWERCASE;
-	int maskedDigitChar   = MASKED_NUMBER;
-	int maskedOtherChar   = MASKED_OTHER_CHAR;
-	int maskedNumber      = MASKED_NUMBER;
-	int maskedDayValue    = MASKED_DAY_COMPONENT_VAL;
-	int maskedMonthValue  = MASKED_MONTH_COMPONENT_VAL;
-	int maskedYearValue   = MASKED_YEAR_COMPONENT_VAL;
+	int maskedUpperChar  = MASKED_UPPERCASE;
+	int maskedLowerChar  = MASKED_LOWERCASE;
+	int maskedDigitChar  = MASKED_NUMBER;
+	int maskedOtherChar  = MASKED_OTHER_CHAR;
+	int maskedNumber     = MASKED_NUMBER;
+	int maskedDayValue   = MASKED_DAY_COMPONENT_VAL;
+	int maskedMonthValue = MASKED_MONTH_COMPONENT_VAL;
+	int maskedYearValue  = MASKED_YEAR_COMPONENT_VAL;
 
 	public MaskTransformer() {
 	}
 
 	@Override
 	public void init(ObjectInspector[] arguments, int startIdx) {
-		maskedUpperChar  = getCharArg(arguments, startIdx + 0, MASKED_UPPERCASE);
-		maskedLowerChar  = getCharArg(arguments, startIdx + 1, MASKED_LOWERCASE);
-		maskedDigitChar  = getCharArg(arguments, startIdx + 2, MASKED_NUMBER);
-		maskedOtherChar  = getCharArg(arguments, startIdx + 3, MASKED_OTHER_CHAR);
-		maskedNumber     = getCharArg(arguments, startIdx + 4, MASKED_NUMBER);
-		maskedDayValue   = getIntArg(arguments, startIdx + 5, MASKED_DAY_COMPONENT_VAL);
-		maskedMonthValue = getIntArg(arguments, startIdx + 6, MASKED_MONTH_COMPONENT_VAL);
-		maskedYearValue  = getIntArg(arguments, startIdx + 7, MASKED_YEAR_COMPONENT_VAL);
+		int idx = startIdx;
+
+		maskedUpperChar  = getCharArg(arguments, idx++, MASKED_UPPERCASE);
+		maskedLowerChar  = getCharArg(arguments, idx++, MASKED_LOWERCASE);
+		maskedDigitChar  = getCharArg(arguments, idx++, MASKED_NUMBER);
+		maskedOtherChar  = getCharArg(arguments, idx++, MASKED_OTHER_CHAR);
+		maskedNumber     = getCharArg(arguments, idx++, MASKED_NUMBER);
+		maskedDayValue   = getIntArg(arguments, idx++, MASKED_DAY_COMPONENT_VAL);
+		maskedMonthValue = getIntArg(arguments, idx++, MASKED_MONTH_COMPONENT_VAL);
+		maskedYearValue  = getIntArg(arguments, idx++, MASKED_YEAR_COMPONENT_VAL);
 	}
 
 	@Override
 	String transform(String value) {
-		return maskString(value, maskedUpperChar, maskedLowerChar, maskedDigitChar, maskedOtherChar,
0, value.length());
+		return maskString(value, 0, value.length());
+	}
+
+	@Override
+	Byte transform(Byte value) {
+		String strValue = value.toString();
+
+		return toByte(Long.parseLong(maskNumber(strValue, 0, strValue.length())));
 	}
 
 	@Override
 	Short transform(Short value) {
 		String strValue = value.toString();
 
-		return Short.parseShort(maskNumber(strValue, maskedDigitChar, 0, strValue.length()));
+		return toShort(Long.parseLong(maskNumber(strValue, 0, strValue.length())));
 	}
 
 	@Override
 	Integer transform(Integer value) {
 		String strValue = value.toString();
 
-		return Integer.parseInt(maskNumber(strValue, maskedDigitChar, 0, strValue.length()));
+		return toInteger(Long.parseLong(maskNumber(strValue, 0, strValue.length())));
 	}
 
 	@Override
 	Long transform(Long value) {
 		String strValue = value.toString();
 
-		return Long.parseLong(maskNumber(strValue, maskedDigitChar, 0, strValue.length()));
+		return Long.parseLong(maskNumber(strValue, 0, strValue.length()));
 	}
 
 	@Override
 	Date transform(Date value) {
-		return maskDate(value, maskedDayValue, maskedMonthValue, maskedYearValue);
+		return maskDate(value);
 	}
 
 
-	String maskString(String val, int replaceUpperChar, int replaceLowerChar, int replaceDigitChar,
int replaceOtherChar, int startIdx, int endIdx) {
-		StringBuffer strBuf = new StringBuffer(val.length());
+	String maskString(String val, int startIdx, int endIdx) {
+		StringBuilder strBuf = new StringBuilder(val.length());
 
 		if(startIdx < 0) {
 			startIdx = 0;
 		}
 
+		if(endIdx < 0) {
+			endIdx = 0;
+		}
+
+		if(startIdx > val.length()) {
+			startIdx = val.length();
+		}
+
 		if(endIdx > val.length()) {
 			endIdx = val.length();
 		}
@@ -121,26 +138,26 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 
 			switch(Character.getType(c)) {
 				case Character.UPPERCASE_LETTER:
-					if(replaceUpperChar != -1) {
-						c = replaceUpperChar;
+					if(maskedUpperChar != UNMASKED_VAL) {
+						c = maskedUpperChar;
 					}
 					break;
 
 				case Character.LOWERCASE_LETTER:
-					if(replaceLowerChar != -1) {
-						c = replaceLowerChar;
+					if(maskedLowerChar != UNMASKED_VAL) {
+						c = maskedLowerChar;
 					}
 					break;
 
 				case Character.DECIMAL_DIGIT_NUMBER:
-					if(replaceDigitChar != -1) {
-						c = replaceDigitChar;
+					if(maskedDigitChar != UNMASKED_VAL) {
+						c = maskedDigitChar;
 					}
 					break;
 
 				default:
-					if(replaceOtherChar != -1) {
-						c = replaceOtherChar;
+					if(maskedOtherChar != UNMASKED_VAL) {
+						c = maskedOtherChar;
 					}
 					break;
 			}
@@ -155,14 +172,22 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 		return strBuf.toString();
 	}
 
-	String maskNumber(String val, int replaceDigit, int startIdx, int endIdx) {
-		if(replaceDigit != -1) {
-			StringBuffer strBuf = new StringBuffer(val.length());
+	String maskNumber(String val, int startIdx, int endIdx) {
+		if(maskedNumber != UNMASKED_VAL) {
+			StringBuilder strBuf = new StringBuilder(val.length());
 
 			if (startIdx < 0) {
 				startIdx = 0;
 			}
 
+			if (endIdx < 0) {
+				endIdx = 0;
+			}
+
+			if (startIdx > val.length()) {
+				startIdx = val.length();
+			}
+
 			if (endIdx > val.length()) {
 				endIdx = val.length();
 			}
@@ -176,8 +201,8 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 
 				switch (Character.getType(c)) {
 					case Character.DECIMAL_DIGIT_NUMBER:
-						c = replaceDigit;
-					break;
+						c = maskedNumber;
+						break;
 				}
 
 				strBuf.appendCodePoint(c);
@@ -193,39 +218,79 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 		return val;
 	}
 
-	Date maskDate(Date value, int maskedDay, int maskedMonth, int maskedYear) {
-		int year  = maskedYear  == UNMASKED_DATE_COMPONENT_VAL ? value.getYear()  : MASKED_YEAR_COMPONENT_VAL;
-		int month = maskedMonth == UNMASKED_DATE_COMPONENT_VAL ? value.getMonth() : MASKED_MONTH_COMPONENT_VAL;
-		int day   = maskedDay   == UNMASKED_DATE_COMPONENT_VAL ? value.getDate()  : MASKED_DAY_COMPONENT_VAL;
+	Date maskDate(Date value) {
+		int year  = maskedYearValue  == UNMASKED_VAL ? value.getYear()  : maskedYearValue;
+		int month = maskedMonthValue == UNMASKED_VAL ? value.getMonth() : maskedMonthValue;
+		int day   = maskedDayValue   == UNMASKED_VAL ? value.getDate()  : maskedDayValue;
 
 		return new Date(year, month, day);
 	}
 
-	String getStringArg(ObjectInspector[] arguments, int index, String defaultValue) {
-		String ret = defaultValue;
+	Byte toByte(long value) {
+		if(value < Byte.MIN_VALUE) {
+			return Byte.MIN_VALUE;
+		} else if(value > Byte.MAX_VALUE) {
+			return Byte.MAX_VALUE;
+		} else {
+			return (byte)value;
+		}
+	}
+
+	Short toShort(long value) {
+		if(value < Short.MIN_VALUE) {
+			return Short.MIN_VALUE;
+		} else if(value > Short.MAX_VALUE) {
+			return Short.MAX_VALUE;
+		} else {
+			return (short)value;
+		}
+	}
+
+	Integer toInteger(long value) {
+		if(value < Integer.MIN_VALUE) {
+			return Integer.MIN_VALUE;
+		} else if(value > Integer.MAX_VALUE) {
+			return Integer.MAX_VALUE;
+		} else {
+			return (int)value;
+		}
+	}
+
+	int getCharArg(ObjectInspector[] arguments, int index, int defaultValue) {
+		int ret = defaultValue;
 
 		ObjectInspector arg = (arguments != null && arguments.length > index) ? arguments[index]
: null;
 
 		if (arg != null) {
-			if (arg instanceof WritableConstantStringObjectInspector) {
-				Text value = ((WritableConstantStringObjectInspector) arg).getWritableConstantValue();
+			if(arg instanceof WritableConstantIntObjectInspector) {
+				IntWritable value = ((WritableConstantIntObjectInspector)arg).getWritableConstantValue();
 
-				if (value != null && value.getLength() > 0) {
-					ret = value.toString();
+				if(value != null) {
+					ret = value.get();
 				}
-			}
-		}
+			} else if(arg instanceof WritableConstantLongObjectInspector) {
+				LongWritable value = ((WritableConstantLongObjectInspector)arg).getWritableConstantValue();
 
-		return ret;
-	}
+				if(value != null) {
+					ret = (int)value.get();
+				}
+			} else if(arg instanceof WritableConstantShortObjectInspector) {
+				ShortWritable value = ((WritableConstantShortObjectInspector)arg).getWritableConstantValue();
 
-	int getCharArg(ObjectInspector[] arguments, int index, int defaultValue) {
-		int ret = defaultValue;
+				if(value != null) {
+					ret = value.get();
+				}
+			} else if(arg instanceof ConstantObjectInspector) {
+				Object value = ((ConstantObjectInspector) arg).getWritableConstantValue();
 
-		String value = getStringArg(arguments, index, null);
+				if (value != null) {
+					String strValue = value.toString();
 
-		if (StringUtils.isNotEmpty(value)) {
-			ret = value.charAt(0);
+					if (strValue != null && strValue.length() > 0) {
+						ret = strValue.charAt(0);
+					}
+				}
+			}
 		}
 
 		return ret;
@@ -237,22 +302,34 @@ class MaskTransformer extends RangerBaseUdf.RangerTransformer {
 		ObjectInspector arg = (arguments != null && arguments.length > index) ? arguments[index]
: null;
 
 		if (arg != null) {
-			if (arg instanceof WritableConstantStringObjectInspector) {
-				Text value = ((WritableConstantStringObjectInspector) arg).getWritableConstantValue();
+			if (arg instanceof WritableConstantIntObjectInspector) {
+				IntWritable value = ((WritableConstantIntObjectInspector) arg).getWritableConstantValue();
 
 				if (value != null) {
-					try {
-						ret = Integer.parseInt(value.toString());
-					} catch(NumberFormatException excp) {
-						// ignored; defaultValue will be returned
-					}
+					ret = value.get();
 				}
-			} else if(arg instanceof WritableConstantIntObjectInspector) {
-				IntWritable value = ((WritableConstantIntObjectInspector)arg).getWritableConstantValue();
+			} else if (arg instanceof WritableConstantLongObjectInspector) {
+				LongWritable value = ((WritableConstantLongObjectInspector) arg).getWritableConstantValue();
 
-				if(value != null) {
+				if (value != null) {
+					ret = (int) value.get();
+				}
+			} else if (arg instanceof WritableConstantShortObjectInspector) {
+				ShortWritable value = ((WritableConstantShortObjectInspector) arg).getWritableConstantValue();
+
+				if (value != null) {
 					ret = value.get();
 				}
+			} else if (arg instanceof ConstantObjectInspector) {
+				Object value = ((ConstantObjectInspector) arg).getWritableConstantValue();
+
+				if (value != null) {
+					String strValue = value.toString();
+
+					if (strValue != null && strValue.length() > 0) {
+						ret = Integer.parseInt(value.toString());
+					}
+				}
 			}
 		}
 

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskFirstN.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskFirstN.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskFirstN.java
index f6ece4d..0c2605f 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskFirstN.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskFirstN.java
@@ -43,27 +43,34 @@ class MaskFirstNTransformer extends MaskTransformer {
 
     @Override
     String transform(String value) {
-        return maskString(value, maskedUpperChar, maskedLowerChar, maskedDigitChar, maskedOtherChar,
0, charCount);
+        return maskString(value, 0, charCount);
+    }
+
+    @Override
+    Byte transform(Byte value) {
+        String strValue = value.toString();
+
+        return toByte(Long.parseLong(maskNumber(strValue, 0, charCount)));
     }
 
     @Override
     Short transform(Short value) {
         String strValue = value.toString();
 
-        return Short.parseShort(maskNumber(strValue, maskedDigitChar, 0, charCount));
+        return toShort(Long.parseLong(maskNumber(strValue, 0, charCount)));
     }
 
     @Override
     Integer transform(Integer value) {
         String strValue = value.toString();
 
-        return Integer.parseInt(maskNumber(strValue, maskedDigitChar, 0, charCount));
+        return toInteger(Long.parseLong(maskNumber(strValue, 0, charCount)));
     }
 
     @Override
     Long transform(Long value) {
         String strValue = value.toString();
 
-        return Long.parseLong(maskNumber(strValue, maskedDigitChar, 0, charCount));
+        return Long.parseLong(maskNumber(strValue, 0, charCount));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskHash.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskHash.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskHash.java
index 5afc66b..4152949 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskHash.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskHash.java
@@ -41,6 +41,11 @@ class MaskHashTransformer extends RangerBaseUdf.RangerTransformer {
     }
 
     @Override
+    Byte transform(Byte value) {
+        return value;
+    }
+
+    @Override
     Short transform(Short value) {
         return value;
     }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskLastN.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskLastN.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskLastN.java
index 1ea433d..97b24e4 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskLastN.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskLastN.java
@@ -18,6 +18,7 @@
 
 package org.apache.ranger.authorization.hive.udf;
 
+import org.apache.hadoop.hive.ql.udf.generic.*;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
 
 
@@ -43,27 +44,34 @@ class MaskLastNTransformer extends MaskTransformer {
 
     @Override
     String transform(String value) {
-        return maskString(value, maskedUpperChar, maskedLowerChar, maskedDigitChar, maskedOtherChar,
value.length() - charCount, value.length());
+        return maskString(value, value.length() - charCount, value.length());
+    }
+
+    @Override
+    Byte transform(Byte value) {
+        String strValue = value.toString();
+
+        return toByte(Long.parseLong(maskNumber(strValue, strValue.length() - charCount,
strValue.length())));
     }
 
     @Override
     Short transform(Short value) {
         String strValue = value.toString();
 
-        return Short.parseShort(maskNumber(strValue, maskedDigitChar, strValue.length() -
charCount, strValue.length()));
+        return toShort(Long.parseLong(maskNumber(strValue, strValue.length() - charCount,
strValue.length())));
     }
 
     @Override
     Integer transform(Integer value) {
         String strValue = value.toString();
 
-        return Integer.parseInt(maskNumber(strValue, maskedDigitChar, strValue.length() -
charCount, strValue.length()));
+        return toInteger(Long.parseLong(maskNumber(strValue, strValue.length() - charCount,
strValue.length())));
     }
 
     @Override
     Long transform(Long value) {
         String strValue = value.toString();
 
-        return Long.parseLong(maskNumber(strValue, maskedDigitChar, strValue.length() - charCount,
strValue.length()));
+        return Long.parseLong(maskNumber(strValue, strValue.length() - charCount, strValue.length()));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowFirstN.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowFirstN.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowFirstN.java
index a7ce40f..06a902c 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowFirstN.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowFirstN.java
@@ -43,27 +43,34 @@ class MaskShowFirstNTransformer extends MaskTransformer {
 
     @Override
     String transform(String value) {
-        return maskString(value, maskedUpperChar, maskedLowerChar, maskedDigitChar, maskedOtherChar,
charCount, value.length());
+        return maskString(value, charCount, value.length());
+    }
+
+    @Override
+    Byte transform(Byte value) {
+        String strValue = value.toString();
+
+        return toByte(Long.parseLong(maskNumber(strValue, charCount, strValue.length())));
     }
 
     @Override
     Short transform(Short value) {
         String strValue = value.toString();
 
-        return Short.parseShort(maskNumber(strValue, maskedDigitChar, charCount, strValue.length()));
+        return toShort(Long.parseLong(maskNumber(strValue, charCount, strValue.length())));
     }
 
     @Override
     Integer transform(Integer value) {
         String strValue = value.toString();
 
-        return Integer.parseInt(maskNumber(strValue, maskedDigitChar, charCount, strValue.length()));
+        return toInteger(Long.parseLong(maskNumber(strValue, charCount, strValue.length())));
     }
 
     @Override
     Long transform(Long value) {
         String strValue = value.toString();
 
-        return Long.parseLong(maskNumber(strValue, maskedDigitChar, charCount, strValue.length()));
+        return Long.parseLong(maskNumber(strValue, charCount, strValue.length()));
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/a3a472d9/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowLastN.java
----------------------------------------------------------------------
diff --git a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowLastN.java
b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowLastN.java
index 3abe636..be72097 100644
--- a/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowLastN.java
+++ b/ranger-hive-utils/src/main/java/org/apache/ranger/authorization/hive/udf/RangerUdfMaskShowLastN.java
@@ -28,7 +28,7 @@ public class RangerUdfMaskShowLastN extends RangerBaseUdf {
 }
 
 class MaskShowLastNTransformer extends MaskTransformer {
-    int charCount;
+    int charCount = 4;
 
     public MaskShowLastNTransformer() {
         super();
@@ -43,27 +43,34 @@ class MaskShowLastNTransformer extends MaskTransformer {
 
     @Override
     String transform(String value) {
-        return maskString(value, maskedUpperChar, maskedLowerChar, maskedDigitChar, maskedOtherChar,
0, value.length() - charCount);
+        return maskString(value, 0, value.length() - charCount);
+    }
+
+    @Override
+    Byte transform(Byte value) {
+        String strValue = value.toString();
+
+        return toByte(Long.parseLong(maskNumber(strValue, 0, strValue.length() - charCount)));
     }
 
     @Override
     Short transform(Short value) {
         String strValue = value.toString();
 
-        return Short.parseShort(maskNumber(strValue, maskedDigitChar, 0, strValue.length()
- charCount));
+        return toShort(Long.parseLong(maskNumber(strValue, 0, strValue.length() - charCount)));
     }
 
     @Override
     Integer transform(Integer value) {
         String strValue = value.toString();
 
-        return Integer.parseInt(maskNumber(strValue, maskedDigitChar, 0, strValue.length()
- charCount));
+        return toInteger(Long.parseLong(maskNumber(strValue, 0, strValue.length() - charCount)));
     }
 
     @Override
     Long transform(Long value) {
         String strValue = value.toString();
 
-        return Long.parseLong(maskNumber(strValue, maskedDigitChar, 0, strValue.length()
- charCount));
+        return Long.parseLong(maskNumber(strValue, 0, strValue.length() - charCount));
     }
 }


Mime
View raw message