You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ga...@apache.org on 2011/10/01 03:46:14 UTC

svn commit: r1177883 - in /hbase/branches/0.90: CHANGES.txt src/main/java/org/apache/hadoop/hbase/security/User.java src/test/java/org/apache/hadoop/hbase/security/TestUser.java

Author: garyh
Date: Sat Oct  1 01:46:13 2011
New Revision: 1177883

URL: http://svn.apache.org/viewvc?rev=1177883&view=rev
Log:
HBASE-4515  User.getCurrent() can fail to initialize the current user

Modified:
    hbase/branches/0.90/CHANGES.txt
    hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/security/User.java
    hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/security/TestUser.java

Modified: hbase/branches/0.90/CHANGES.txt
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/CHANGES.txt?rev=1177883&r1=1177882&r2=1177883&view=diff
==============================================================================
--- hbase/branches/0.90/CHANGES.txt (original)
+++ hbase/branches/0.90/CHANGES.txt Sat Oct  1 01:46:13 2011
@@ -66,6 +66,7 @@ Release 0.90.5 - Unreleased
                certain circumstances (David Revell)
    HBASE-4512  JVMClusterUtil throwing wrong exception when master thread cannot be created (Ram)
    HBASE-4212  TestMasterFailover fails occasionally (Gao Jinchao)
+   HBASE-4515  User.getCurrent() can fail to initialize the current user
 
   IMPROVEMENT
    HBASE-4205  Enhance HTable javadoc (Eric Charles)

Modified: hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/security/User.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/security/User.java?rev=1177883&r1=1177882&r2=1177883&view=diff
==============================================================================
--- hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/security/User.java (original)
+++ hbase/branches/0.90/src/main/java/org/apache/hadoop/hbase/security/User.java Sat Oct  1 01:46:13 2011
@@ -22,6 +22,7 @@ package org.apache.hadoop.hbase.security
 
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
 import org.apache.hadoop.hbase.util.Methods;
 import org.apache.hadoop.mapred.JobConf;
 import org.apache.hadoop.mapreduce.Job;
@@ -97,11 +98,16 @@ public abstract class User {
    * Returns the {@code User} instance within current execution context.
    */
   public static User getCurrent() throws IOException {
+    User user;
     if (IS_SECURE_HADOOP) {
-      return new SecureHadoopUser();
+      user = new SecureHadoopUser();
     } else {
-      return new HadoopUser();
+      user = new HadoopUser();
     }
+    if (user.ugi == null) {
+      return null;
+    }
+    return user;
   }
 
   /**
@@ -170,6 +176,17 @@ public abstract class User {
     private HadoopUser() {
       try {
         ugi = (UserGroupInformation) callStatic("getCurrentUGI");
+        if (ugi == null) {
+          // Secure Hadoop UGI will perform an implicit login if the current
+          // user is null.  Emulate the same behavior here for consistency
+          Configuration conf = HBaseConfiguration.create();
+          ugi = (UserGroupInformation) callStatic("login",
+              new Class[]{ Configuration.class }, new Object[]{ conf });
+          if (ugi != null) {
+            callStatic("setCurrentUser",
+                new Class[]{ UserGroupInformation.class }, new Object[]{ ugi });
+          }
+        }
       } catch (RuntimeException re) {
         throw re;
       } catch (Exception e) {

Modified: hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/security/TestUser.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/security/TestUser.java?rev=1177883&r1=1177882&r2=1177883&view=diff
==============================================================================
--- hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/security/TestUser.java (original)
+++ hbase/branches/0.90/src/test/java/org/apache/hadoop/hbase/security/TestUser.java Sat Oct  1 01:46:13 2011
@@ -21,8 +21,12 @@ package org.apache.hadoop.hbase.security
 
 import static org.junit.Assert.*;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.security.UnixUserGroupInformation;
+import org.apache.hadoop.security.UserGroupInformation;
 import org.junit.Test;
 
 import java.io.IOException;
@@ -30,6 +34,8 @@ import java.security.PrivilegedAction;
 import java.security.PrivilegedExceptionAction;
 
 public class TestUser {
+  private static Log LOG = LogFactory.getLog(TestUser.class);
+
   @Test
   public void testBasicAttributes() throws Exception {
     Configuration conf = HBaseConfiguration.create();
@@ -79,4 +85,22 @@ public class TestUser {
       }
     });
   }
+
+  /**
+   * Make sure that we're returning a result for the current user.
+   * Previously getCurrent() was returning null if not initialized on
+   * non-secure Hadoop variants.
+   */
+  @Test
+  public void testGetCurrent() throws Exception {
+    User user1 = User.getCurrent();
+    assertNotNull(user1.ugi);
+    LOG.debug("User1 is "+user1.getName());
+
+    for (int i =0 ; i< 100; i++) {
+      User u = User.getCurrent();
+      assertNotNull(u);
+      assertEquals(user1.getName(), u.getName());
+    }
+  }
 }