Skip to content

Commit 2d5b5ff

Browse files
jinhyukifyjunegunn
authored andcommitted
HBASE-30042 Test AuthUtil.loginClient with existing Kerberos login (#8002)
Signed-off-by: Junegunn Choi <junegunn@apache.org>
1 parent 460fa22 commit 2d5b5ff

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestUsersOperationsWithSecureHadoop.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ public class TestUsersOperationsWithSecureHadoop {
6262

6363
private static String CLIENT_NAME;
6464

65+
private static String OTHER_CLIENT_NAME;
66+
6567
@BeforeClass
6668
public static void setUp() throws Exception {
6769
KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
6870
PRINCIPAL = "hbase/" + HOST;
6971
CLIENT_NAME = "foo";
70-
KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, CLIENT_NAME);
72+
OTHER_CLIENT_NAME = "bar";
73+
KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL, CLIENT_NAME, OTHER_CLIENT_NAME);
7174
HBaseKerberosUtils.setPrincipalForTesting(PRINCIPAL + "@" + KDC.getRealm());
7275
HBaseKerberosUtils.setKeytabFileForTesting(KEYTAB_FILE.getAbsolutePath());
7376
HBaseKerberosUtils.setClientPrincipalForTesting(CLIENT_NAME + "@" + KDC.getRealm());
@@ -133,17 +136,49 @@ public void testLoginWithUserKeytabAndPrincipal() throws Exception {
133136
}
134137

135138
@Test
136-
public void testAuthUtilLogin() throws Exception {
139+
public void testAuthUtilLoginWithExistingLoginUser() throws Exception {
140+
String clientKeytab = getClientKeytabForTesting();
141+
String clientPrincipal = getClientPrincipalForTesting();
142+
Configuration conf = getSecuredConfiguration();
143+
conf.set(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, clientKeytab);
144+
conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, clientPrincipal);
145+
UserGroupInformation.setConfiguration(conf);
146+
147+
UserGroupInformation.loginUserFromKeytab(CLIENT_NAME, clientKeytab);
148+
149+
User user = AuthUtil.loginClient(conf);
150+
assertTrue(user.isLoginFromKeytab());
151+
assertEquals(CLIENT_NAME, user.getShortName());
152+
assertEquals(getClientPrincipalForTesting(), user.getName());
153+
}
154+
155+
@Test
156+
public void testAuthUtilLoginWithDifferentExistingUser() throws Exception {
137157
String clientKeytab = getClientKeytabForTesting();
138158
String clientPrincipal = getClientPrincipalForTesting();
139159
Configuration conf = getSecuredConfiguration();
140160
conf.set(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, clientKeytab);
141161
conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, clientPrincipal);
142162
UserGroupInformation.setConfiguration(conf);
143163

164+
// Login with other principal first
165+
String otherPrincipal = OTHER_CLIENT_NAME + "@" + KDC.getRealm();
166+
UserGroupInformation.loginUserFromKeytab(otherPrincipal, clientKeytab);
167+
144168
User user = AuthUtil.loginClient(conf);
145169
assertTrue(user.isLoginFromKeytab());
170+
// The existing login user (bar) doesn't match the principal configured in
171+
// HBASE_CLIENT_KERBEROS_PRINCIPAL (foo), so loginClient should re-login
172+
// with the configured principal.
146173
assertEquals(CLIENT_NAME, user.getShortName());
147174
assertEquals(getClientPrincipalForTesting(), user.getName());
175+
176+
conf.set(AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL, otherPrincipal);
177+
178+
user = AuthUtil.loginClient(conf);
179+
assertTrue(user.isLoginFromKeytab());
180+
// After updating HBASE_CLIENT_KERBEROS_PRINCIPAL to bar, loginClient should re-login with bar.
181+
assertEquals(OTHER_CLIENT_NAME, user.getShortName());
182+
assertEquals(otherPrincipal, user.getName());
148183
}
149184
}

0 commit comments

Comments
 (0)