Skip to content

Commit 8b1050f

Browse files
committed
HIVE-29306: GSSException encountered during HMS Ranger authorization
1 parent f6d0b0d commit 8b1050f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

itests/hive-minikdc/src/test/java/org/apache/hive/minikdc/TestRemoteHiveMetaStoreKerberos.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,27 @@
2020

2121
import org.apache.hadoop.conf.Configuration;
2222
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
23+
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
2324
import org.apache.hadoop.hive.metastore.TestRemoteHiveMetaStore;
2425
import org.apache.hadoop.hive.metastore.api.EnvironmentContext;
2526
import org.apache.hadoop.hive.metastore.api.Partition;
2627
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
2728
import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars;
29+
import org.apache.hadoop.hive.ql.metadata.StringAppender;
30+
import org.apache.hadoop.security.UserGroupInformation;
31+
import org.apache.logging.log4j.Level;
2832
import org.apache.thrift.transport.TTransportException;
2933
import org.junit.Before;
3034
import org.junit.Test;
35+
import org.slf4j.Logger;
36+
import org.slf4j.LoggerFactory;
3137

38+
import java.io.IOException;
39+
import java.security.PrivilegedExceptionAction;
3240
import java.util.List;
3341
import java.util.ArrayList;
3442

43+
import org.junit.Assert;
3544
import static org.junit.Assert.assertEquals;
3645
import static org.junit.Assert.assertNotNull;
3746
import static org.junit.Assert.assertThrows;
@@ -107,6 +116,72 @@ public void testThriftMaxMessageSize() throws Throwable {
107116
cleanUp(dbName, tblName, typeName);
108117
}
109118

119+
@Test
120+
public void testKerberosProxyUser() throws Exception {
121+
String realUserName = "realuser";
122+
String realUserPrincipal = miniKDC.getFullyQualifiedUserPrincipal(realUserName);
123+
124+
// Add the real user principal and generate keytab
125+
miniKDC.addUserPrincipal(realUserName);
126+
127+
// Login real user with valid keytab - this gives us real TGT credentials
128+
UserGroupInformation realUserUgi = miniKDC.loginUser(realUserName);
129+
130+
// Create a proxy user on behalf of the real user
131+
String proxyUserName = "proxyuser@" + miniKDC.getKdcConf().getProperty("realm", "EXAMPLE.COM");
132+
UserGroupInformation proxyUserUgi = UserGroupInformation.createProxyUser(
133+
proxyUserName, realUserUgi);
134+
135+
proxyUserUgi.doAs(new PrivilegedExceptionAction<Void>() {
136+
@Override
137+
public Void run() throws Exception {
138+
Logger logger = null;
139+
StringAppender appender = null;
140+
try {
141+
UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
142+
143+
System.out.println("Real user: " + currentUser.getRealUser().getUserName() +
144+
" (auth:" + currentUser.getRealUser().getAuthenticationMethod() + ")");
145+
System.out.println("Proxy user: " + currentUser.getShortUserName() +
146+
" (auth:" + currentUser.getAuthenticationMethod() + ")");
147+
148+
// Set up log capture to catch "Failed to find any Kerberos tgt" error in logs
149+
logger = LoggerFactory.getLogger("org.apache.hadoop.hive.metastore.security");
150+
appender = StringAppender.createStringAppender(null);
151+
appender.addToLogger(logger.getName(), Level.INFO);
152+
appender.start();
153+
154+
// Attempt to create metastore client connection as Kerberos proxy user
155+
// This should work properly (after TUGIAssumingTransport fix)
156+
IMetaStoreClient client = new HiveMetaStoreClient(conf);
157+
158+
// Clean up
159+
if (client != null) {
160+
client.close();
161+
}
162+
163+
// The test has successfully demonstrated:
164+
// 1. Real user has valid Kerberos authentication with real TGT from MiniKdc
165+
// 2. Proxy user is properly created with PROXY authentication method
166+
// 3. TUGIAssumingTransport fix is working - no "Failed to find any Kerberos tgt" error
167+
System.out.println("Successfully verified Kerberos proxy user setup with real KDC");
168+
169+
} catch (Exception clientException) {
170+
// Check the captured logs for the specific "Failed to find any Kerberos tgt" error
171+
if (appender.getOutput().contains("Failed to find any Kerberos tgt")) {
172+
// This is expected behavior before TUGIAssumingTransport fix
173+
Assert.fail("EXPECTED BEFORE FIX: HMS client creation failed with 'Failed to find any Kerberos tgt' error in logs");
174+
} else {
175+
Assert.fail("Unexpected error (not 'Failed to find any Kerberos tgt'): " + clientException.getMessage());
176+
}
177+
} finally {
178+
appender.removeFromLogger(logger.getName());
179+
}
180+
return null;
181+
}
182+
});
183+
}
184+
110185
@Override
111186
protected HiveMetaStoreClient createClient() throws Exception {
112187
MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://localhost:" + port);

standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/security/TUGIAssumingTransport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ public TUGIAssumingTransport(TTransport wrapped, UserGroupInformation ugi) {
4545
@Override
4646
public void open() throws TTransportException {
4747
try {
48-
ugi.doAs(new PrivilegedExceptionAction<Void>() {
48+
UserGroupInformation loggedInUGI = ugi.getRealUser() != null ? ugi.getRealUser() : ugi;
49+
loggedInUGI.doAs(new PrivilegedExceptionAction<Void>() {
4950
public Void run() {
5051
try {
5152
wrapped.open();

0 commit comments

Comments
 (0)