accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject svn commit: r1381821 - in /accumulo/trunk/core/src: main/java/org/apache/accumulo/core/client/mapreduce/ main/java/org/apache/accumulo/core/security/ test/java/org/apache/accumulo/core/security/
Date Fri, 07 Sep 2012 00:28:49 GMT
Author: kturner
Date: Fri Sep  7 00:28:49 2012
New Revision: 1381821

URL: http://svn.apache.org/viewvc?rev=1381821&view=rev
Log:
ACCUMULO-241 Allow user to pass encoding to ColumnVisibility and Authorization.  Modified
serialization of Authorizations to use Base64.

Modified:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/client/mapreduce/InputFormatBase.java
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/ColumnVisibility.java
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/VisibilityEvaluatorTest.java

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/client/mapreduce/InputFormatBase.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/client/mapreduce/InputFormatBase.java?rev=1381821&r1=1381820&r2=1381821&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/client/mapreduce/InputFormatBase.java
(original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/client/mapreduce/InputFormatBase.java
Fri Sep  7 00:28:49 2012
@@ -489,7 +489,7 @@ public abstract class InputFormatBase<K,
    */
   protected static Authorizations getAuthorizations(Configuration conf) {
     String authString = conf.get(AUTHORIZATIONS);
-    return authString == null ? Constants.NO_AUTHS : new Authorizations(authString.split(","));
+    return authString == null ? Constants.NO_AUTHS : new Authorizations(authString.getBytes());
   }
   
   /**

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java?rev=1381821&r1=1381820&r2=1381821&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
(original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/Authorizations.java
Fri Sep  7 00:28:49 2012
@@ -17,7 +17,9 @@
 package org.apache.accumulo.core.security;
 
 import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
 import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -29,6 +31,7 @@ import org.apache.accumulo.core.data.Arr
 import org.apache.accumulo.core.data.ByteSequence;
 import org.apache.accumulo.core.util.ArgumentChecker;
 import org.apache.accumulo.core.util.ByteBufferUtil;
+import org.apache.commons.codec.binary.Base64;
 
 public class Authorizations implements Iterable<byte[]>, Serializable {
   
@@ -40,6 +43,8 @@ public class Authorizations implements I
   
   private static final boolean[] validAuthChars = new boolean[256];
   
+  public static final String HEADER = "!AUTH1:";
+
   static {
     for (int i = 0; i < 256; i++) {
       validAuthChars[i] = false;
@@ -75,7 +80,7 @@ public class Authorizations implements I
         throw new IllegalArgumentException("Empty authorization");
       }
       
-      authsList.add(bs.getBackingArray());
+      authsList.add(bs.toArray());
     }
   }
   
@@ -94,39 +99,70 @@ public class Authorizations implements I
     checkAuths();
   }
   
+  /**
+   * @param authorizations
+   *          a serialized authorizations string produced by {@link #getAuthorizationsArray()}
or {@link #serialize()}
+   */
+
   public Authorizations(byte[] authorizations) {
+    
     ArgumentChecker.notNull(authorizations);
-    if (authorizations.length > 0)
-      setAuthorizations(new String(authorizations).split(","));
+
+    String authsString = new String(authorizations);
+    if (authsString.startsWith(HEADER)) {
+      // its the new format
+      for (String encAuth : authsString.substring(HEADER.length()).split(",")) {
+        byte[] auth = Base64.decodeBase64(encAuth.getBytes());
+        auths.add(new ArrayByteSequence(auth));
+      }
+      checkAuths();
+    } else {
+      // its the old format
+      ArgumentChecker.notNull(authorizations);
+      if (authorizations.length > 0)
+        setAuthorizations(authsString.split(","));
+    }
   }
   
   public Authorizations() {}
   
+  /**
+   * 
+   * @param charset
+   *          used to convert each authorization to a byte array
+   * @param authorizations
+   *          array of authorizations
+   */
+  
+  public Authorizations(Charset charset, String... authorizations) {
+    setAuthorizations(charset, authorizations);
+  }
+
   public Authorizations(String... authorizations) {
     setAuthorizations(authorizations);
   }
   
   private void setAuthorizations(String... authorizations) {
+    setAuthorizations(Charset.defaultCharset(), authorizations);
+  }
+  
+  private void setAuthorizations(Charset charset, String... authorizations) {
     ArgumentChecker.notNull(authorizations);
     auths.clear();
     for (String str : authorizations) {
       str = str.trim();
-      auths.add(new ArrayByteSequence(str));
+      try {
+        auths.add(new ArrayByteSequence(str.getBytes(charset.name())));
+      } catch (UnsupportedEncodingException e) {
+        throw new RuntimeException(e);
+      }
     }
     
     checkAuths();
   }
   
   public byte[] getAuthorizationsArray() {
-    StringBuilder sb = new StringBuilder();
-    String sep = "";
-    for (ByteSequence auth : auths) {
-      sb.append(sep);
-      sep = ",";
-      sb.append(auth.toString());
-    }
-    
-    return sb.toString().getBytes();
+    return serialize().getBytes();
   }
   
   public List<byte[]> getAuthorizations() {
@@ -138,7 +174,15 @@ public class Authorizations implements I
   }
   
   public String toString() {
-    return serialize();
+    StringBuilder sb = new StringBuilder();
+    String sep = "";
+    for (ByteSequence auth : auths) {
+      sb.append(sep);
+      sep = ",";
+      sb.append(new String(auth.toArray()));
+    }
+    
+    return sb.toString();
   }
   
   public boolean contains(byte[] auth) {
@@ -184,12 +228,12 @@ public class Authorizations implements I
   }
   
   public String serialize() {
-    StringBuilder sb = new StringBuilder();
+    StringBuilder sb = new StringBuilder(HEADER);
     String sep = "";
     for (ByteSequence auth : auths) {
       sb.append(sep);
       sep = ",";
-      sb.append(auth.toString());
+      sb.append(new String(Base64.encodeBase64(auth.toArray())));
     }
     
     return sb.toString();

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/ColumnVisibility.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/ColumnVisibility.java?rev=1381821&r1=1381820&r2=1381821&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/ColumnVisibility.java
(original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/security/ColumnVisibility.java
Fri Sep  7 00:28:49 2012
@@ -325,6 +325,18 @@ public class ColumnVisibility {
     this(expression.getBytes());
   }
   
+  /**
+   * See {@link #ColumnVisibility(byte[])}
+   * 
+   * @param expression
+   * @param encoding
+   *          uses this encoding to convert the expression to a byte array
+   * @throws UnsupportedEncodingException
+   */
+  public ColumnVisibility(String expression, String encoding) throws UnsupportedEncodingException
{
+    this(expression.getBytes(encoding));
+  }
+
   public ColumnVisibility(Text expression) {
     this(TextUtil.getBytes(expression));
   }
@@ -407,8 +419,16 @@ public class ColumnVisibility {
    * 
    */
   public static String quote(String term) {
+    return quote(term, "UTF-8");
+  }
+  
+  /**
+   * see {@link #quote(byte[])}
+   * 
+   */
+  public static String quote(String term, String encoding) {
     try {
-      return new String(quote(term.getBytes("UTF-8")), "UTF-8");
+      return new String(quote(term.getBytes(encoding)), encoding);
     } catch (UnsupportedEncodingException e) {
       throw new RuntimeException(e);
     }

Modified: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java?rev=1381821&r1=1381820&r2=1381821&view=diff
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java
(original)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/AuthorizationsTest.java
Fri Sep  7 00:28:49 2012
@@ -31,10 +31,12 @@ public class AuthorizationsTest {
   
   @Test
   public void testEncodeDecode() {
-    Authorizations a = new Authorizations("a", "abcdefg", "hijklmno");
+    Authorizations a = new Authorizations("a", "abcdefg", "hijklmno", ",");
     byte[] array = a.getAuthorizationsArray();
     Authorizations b = new Authorizations(array);
     assertEquals(a, b);
+
+    System.out.println(b);
   }
   
 }

Modified: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/VisibilityEvaluatorTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/VisibilityEvaluatorTest.java?rev=1381821&r1=1381820&r2=1381821&view=diff
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/VisibilityEvaluatorTest.java
(original)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/security/VisibilityEvaluatorTest.java
Fri Sep  7 00:28:49 2012
@@ -22,6 +22,9 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
 import org.apache.accumulo.core.util.BadArgumentException;
 import org.apache.accumulo.core.util.ByteArraySet;
 import org.junit.Test;
@@ -85,7 +88,7 @@ public class VisibilityEvaluatorTest {
   
   @Test
   public void testQuotedExpressions() throws VisibilityParseException {
-    VisibilityEvaluator ct = new VisibilityEvaluator(ByteArraySet.fromStrings("A#C", "A\"C",
"A\\C", "AC"));
+    VisibilityEvaluator ct = new VisibilityEvaluator(new Authorizations("A#C", "A\"C", "A\\C",
"AC"));
     
     assertTrue(ct.evaluate(new ColumnVisibility(quote("A#C") + "|" + quote("A?C"))));
     assertTrue(ct.evaluate(new ColumnVisibility(new ColumnVisibility(quote("A#C") + "|" +
quote("A?C")).flatten())));
@@ -110,14 +113,14 @@ public class VisibilityEvaluatorTest {
   }
   
   @Test
-  public void testNonAscii() throws VisibilityParseException {
-    VisibilityEvaluator ct = new VisibilityEvaluator(ByteArraySet.fromStrings("五", "六",
"八", "九", "五十"));
+  public void testNonAscii() throws VisibilityParseException, UnsupportedEncodingException
{
+    VisibilityEvaluator ct = new VisibilityEvaluator(new Authorizations(Charset.forName("UTF-8"),
"五", "六", "八", "九", "五十"));
     
-    assertTrue(ct.evaluate(new ColumnVisibility(quote("五") + "|" + quote("四"))));
-    assertFalse(ct.evaluate(new ColumnVisibility(quote("五") + "&" + quote("四"))));
-    assertTrue(ct.evaluate(new ColumnVisibility(quote("五") + "&(" + quote("四")
+ "|" + quote("九") + ")")));
-    assertTrue(ct.evaluate(new ColumnVisibility("\"五\"&(\"四\"|\"五十\")")));
-    assertFalse(ct.evaluate(new ColumnVisibility(quote("五") + "&(" + quote("四")
+ "|" + quote("三") + ")")));
-    assertFalse(ct.evaluate(new ColumnVisibility("\"五\"&(\"四\"|\"三\")")));
+    assertTrue(ct.evaluate(new ColumnVisibility(quote("五") + "|" + quote("四"),
"UTF-8")));
+    assertFalse(ct.evaluate(new ColumnVisibility(quote("五") + "&" + quote("四"),
"UTF-8")));
+    assertTrue(ct.evaluate(new ColumnVisibility(quote("五") + "&(" + quote("四")
+ "|" + quote("九") + ")", "UTF-8")));
+    assertTrue(ct.evaluate(new ColumnVisibility("\"五\"&(\"四\"|\"五十\")",
"UTF-8")));
+    assertFalse(ct.evaluate(new ColumnVisibility(quote("五") + "&(" + quote("四")
+ "|" + quote("三") + ")", "UTF-8")));
+    assertFalse(ct.evaluate(new ColumnVisibility("\"五\"&(\"四\"|\"三\")",
"UTF-8")));
   }
 }



Mime
View raw message