|
20 | 20 |
|
21 | 21 | import org.apache.hadoop.conf.Configuration; |
22 | 22 | import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; |
| 23 | +import org.apache.hadoop.hive.metastore.IMetaStoreClient; |
23 | 24 | import org.apache.hadoop.hive.metastore.TestRemoteHiveMetaStore; |
24 | 25 | import org.apache.hadoop.hive.metastore.api.EnvironmentContext; |
25 | 26 | import org.apache.hadoop.hive.metastore.api.Partition; |
26 | 27 | import org.apache.hadoop.hive.metastore.conf.MetastoreConf; |
27 | 28 | 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; |
28 | 32 | import org.apache.thrift.transport.TTransportException; |
29 | 33 | import org.junit.Before; |
30 | 34 | import org.junit.Test; |
| 35 | +import org.slf4j.Logger; |
| 36 | +import org.slf4j.LoggerFactory; |
31 | 37 |
|
| 38 | +import java.io.IOException; |
| 39 | +import java.security.PrivilegedExceptionAction; |
32 | 40 | import java.util.List; |
33 | 41 | import java.util.ArrayList; |
34 | 42 |
|
| 43 | +import org.junit.Assert; |
35 | 44 | import static org.junit.Assert.assertEquals; |
36 | 45 | import static org.junit.Assert.assertNotNull; |
37 | 46 | import static org.junit.Assert.assertThrows; |
@@ -107,6 +116,72 @@ public void testThriftMaxMessageSize() throws Throwable { |
107 | 116 | cleanUp(dbName, tblName, typeName); |
108 | 117 | } |
109 | 118 |
|
| 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 | + |
110 | 185 | @Override |
111 | 186 | protected HiveMetaStoreClient createClient() throws Exception { |
112 | 187 | MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://localhost:" + port); |
|
0 commit comments