Skip to content

Commit 5cbfd7c

Browse files
committed
HADOOP-7101. UserGroupInformation.getCurrentUser() fails when called from non-Hadoop JAAS context. Contributed by Todd Lipcon
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1058875 13f79535-47bb-0310-9956-ffa450edef68
1 parent 429a832 commit 5cbfd7c

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,9 @@ Release 0.22.0 - Unreleased
440440

441441
HADOOP-7093. Servlets should default to text/plain (todd)
442442

443+
HADOOP-7101. UserGroupInformation.getCurrentUser() fails when called from
444+
non-Hadoop JAAS context. (todd)
445+
443446
Release 0.21.1 - Unreleased
444447

445448
IMPROVEMENTS

src/java/org/apache/hadoop/security/UserGroupInformation.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,11 @@ public boolean hasKerberosCredentials() {
468468
public static UserGroupInformation getCurrentUser() throws IOException {
469469
AccessControlContext context = AccessController.getContext();
470470
Subject subject = Subject.getSubject(context);
471-
return subject == null ? getLoginUser() : new UserGroupInformation(subject);
471+
if (subject == null || subject.getPrincipals(User.class).isEmpty()) {
472+
return getLoginUser();
473+
} else {
474+
return new UserGroupInformation(subject);
475+
}
472476
}
473477

474478
/**

src/test/core/org/apache/hadoop/security/TestUserGroupInformation.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616
*/
1717
package org.apache.hadoop.security;
1818

19-
import static org.junit.Assert.assertArrayEquals;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
19+
import static org.junit.Assert.*;
2420
import org.mockito.Mockito;
2521
import static org.mockito.Mockito.mock;
2622

@@ -32,6 +28,7 @@
3228
import java.util.Collection;
3329
import java.util.List;
3430

31+
import javax.security.auth.Subject;
3532
import javax.security.auth.login.AppConfigurationEntry;
3633
import javax.security.auth.login.LoginContext;
3734

@@ -383,4 +380,22 @@ public void testDelegateJaasConfiguration() throws Exception {
383380
// for "foobar"
384381
LoginContext login = new LoginContext("foobar-app");
385382
}
383+
384+
/**
385+
* Test for the case that UserGroupInformation.getCurrentUser()
386+
* is called when the AccessControlContext has a Subject associated
387+
* with it, but that Subject was not created by Hadoop (ie it has no
388+
* associated User principal)
389+
*/
390+
@Test
391+
public void testUGIUnderNonHadoopContext() throws Exception {
392+
Subject nonHadoopSubject = new Subject();
393+
Subject.doAs(nonHadoopSubject, new PrivilegedExceptionAction<Void>() {
394+
public Void run() throws IOException {
395+
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
396+
assertNotNull(ugi);
397+
return null;
398+
}
399+
});
400+
}
386401
}

0 commit comments

Comments
 (0)