From 39c0909e3e7e04792a2e6c648feba5664c49df54 Mon Sep 17 00:00:00 2001 From: Gary Helmling Date: Sat, 1 Oct 2011 01:44:39 +0000 Subject: [PATCH] HBASE-4515 User.getCurrent() can fail to initialize the current user git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1177881 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../apache/hadoop/hbase/security/User.java | 21 ++++++++++++++-- .../hadoop/hbase/security/TestUser.java | 24 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 3d46e8ab80d..cc107775e14 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -682,6 +682,7 @@ Release 0.90.5 - Unreleased (Lars Hofhansl) HBASE-4295 rowcounter does not return the correct number of rows in certain circumstances (David Revell) + HBASE-4515 User.getCurrent() can fail to initialize the current user IMPROVEMENT HBASE-4205 Enhance HTable javadoc (Eric Charles) diff --git a/src/main/java/org/apache/hadoop/hbase/security/User.java b/src/main/java/org/apache/hadoop/hbase/security/User.java index d90f2c72211..00bd06d022e 100644 --- a/src/main/java/org/apache/hadoop/hbase/security/User.java +++ b/src/main/java/org/apache/hadoop/hbase/security/User.java @@ -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) { diff --git a/src/test/java/org/apache/hadoop/hbase/security/TestUser.java b/src/test/java/org/apache/hadoop/hbase/security/TestUser.java index 6e497daf90e..cef900ec1c7 100644 --- a/src/test/java/org/apache/hadoop/hbase/security/TestUser.java +++ b/src/test/java/org/apache/hadoop/hbase/security/TestUser.java @@ -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()); + } + } }