PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/shims/src/test/org/apache/hadoop/hive/thrift/TestHadoop20SAuthBridge.java

#
Java | 195 lines | 143 code | 20 blank | 32 comment | 7 complexity | fcc2963eaefb0fc2a8b9af035ade709a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.thrift;
  19. import java.net.InetSocketAddress;
  20. import java.net.Socket;
  21. import java.security.PrivilegedExceptionAction;
  22. import junit.framework.TestCase;
  23. import org.apache.hadoop.fs.Path;
  24. import org.apache.hadoop.hive.conf.HiveConf;
  25. import org.apache.hadoop.hive.metastore.HiveMetaStore;
  26. import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
  27. import org.apache.hadoop.hive.metastore.api.Database;
  28. import org.apache.hadoop.hive.metastore.api.MetaException;
  29. import org.apache.hadoop.security.SaslRpcServer;
  30. import org.apache.hadoop.security.UserGroupInformation;
  31. import org.apache.hadoop.security.SaslRpcServer.AuthMethod;
  32. import org.apache.hadoop.security.token.Token;
  33. import org.apache.thrift.transport.TSaslServerTransport;
  34. import org.apache.thrift.transport.TTransportException;
  35. import org.apache.thrift.transport.TTransportFactory;
  36. public class TestHadoop20SAuthBridge extends TestCase {
  37. private static class MyHadoopThriftAuthBridge20S extends HadoopThriftAuthBridge20S {
  38. @Override
  39. public Server createServer(String keytabFile, String principalConf)
  40. throws TTransportException {
  41. //Create a Server that doesn't interpret any Kerberos stuff
  42. return new Server();
  43. }
  44. static class Server extends HadoopThriftAuthBridge20S.Server {
  45. public Server() throws TTransportException {
  46. super();
  47. }
  48. @Override
  49. public TTransportFactory createTransportFactory()
  50. throws TTransportException {
  51. TSaslServerTransport.Factory transFactory =
  52. new TSaslServerTransport.Factory();
  53. transFactory.addServerDefinition(AuthMethod.DIGEST.getMechanismName(),
  54. null, SaslRpcServer.SASL_DEFAULT_REALM,
  55. SaslRpcServer.SASL_PROPS,
  56. new SaslDigestCallbackHandler(secretManager));
  57. return new TUGIAssumingTransportFactory(transFactory, realUgi);
  58. }
  59. }
  60. }
  61. private static final int port = 10000;
  62. private final HiveConf conf;
  63. public TestHadoop20SAuthBridge(String name) {
  64. super(name);
  65. System.setProperty(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL.varname,
  66. "true");
  67. System.setProperty(HiveConf.ConfVars.METASTOREURIS.varname,
  68. "thrift://localhost:" + port);
  69. System.setProperty(HiveConf.ConfVars.METASTOREWAREHOUSE.varname, new Path(
  70. System.getProperty("test.build.data", "/tmp")).toString());
  71. conf = new HiveConf(TestHadoop20SAuthBridge.class);
  72. conf.setBoolean("hive.metastore.local", false);
  73. }
  74. public void testSaslWithHiveMetaStore() throws Exception {
  75. Thread thread = new Thread(new Runnable() {
  76. public void run() {
  77. try {
  78. HiveMetaStore.startMetaStore(port,new MyHadoopThriftAuthBridge20S());
  79. } catch (Throwable e) {
  80. System.exit(1);
  81. }
  82. }
  83. });
  84. thread.setDaemon(true);
  85. thread.start();
  86. loopUntilHMSReady();
  87. UserGroupInformation clientUgi = UserGroupInformation.getCurrentUser();
  88. obtainTokenAndAddIntoUGI(clientUgi, null);
  89. obtainTokenAndAddIntoUGI(clientUgi, "tokenForFooTablePartition");
  90. }
  91. private void obtainTokenAndAddIntoUGI(UserGroupInformation clientUgi,
  92. String tokenSig) throws Exception {
  93. //obtain a token by directly invoking the metastore operation(without going
  94. //through the thrift interface). Obtaining a token makes the secret manager
  95. //aware of the user and that it gave the token to the user
  96. String tokenStrForm;
  97. if (tokenSig == null) {
  98. tokenStrForm =
  99. HiveMetaStore.getDelegationToken(clientUgi.getShortUserName());
  100. } else {
  101. tokenStrForm =
  102. HiveMetaStore.getDelegationToken(clientUgi.getShortUserName(),
  103. tokenSig);
  104. conf.set("hive.metastore.token.signature", tokenSig);
  105. }
  106. Token<DelegationTokenIdentifier> t= new Token<DelegationTokenIdentifier>();
  107. t.decodeFromUrlString(tokenStrForm);
  108. //add the token to the clientUgi for securely talking to the metastore
  109. clientUgi.addToken(t);
  110. //Create the metastore client as the clientUgi. Doing so this
  111. //way will give the client access to the token that was added earlier
  112. //in the clientUgi
  113. HiveMetaStoreClient hiveClient =
  114. clientUgi.doAs(new PrivilegedExceptionAction<HiveMetaStoreClient>() {
  115. public HiveMetaStoreClient run() throws Exception {
  116. HiveMetaStoreClient hiveClient =
  117. new HiveMetaStoreClient(conf);
  118. return hiveClient;
  119. }
  120. });
  121. assertTrue("Couldn't connect to metastore", hiveClient != null);
  122. //try out some metastore operations
  123. createDBAndVerifyExistence(hiveClient);
  124. hiveClient.close();
  125. //Now cancel the delegation token
  126. HiveMetaStore.cancelDelegationToken(tokenStrForm);
  127. //now metastore connection should fail
  128. hiveClient =
  129. clientUgi.doAs(new PrivilegedExceptionAction<HiveMetaStoreClient>() {
  130. public HiveMetaStoreClient run() {
  131. try {
  132. HiveMetaStoreClient hiveClient =
  133. new HiveMetaStoreClient(conf);
  134. return hiveClient;
  135. } catch (MetaException e) {
  136. return null;
  137. }
  138. }
  139. });
  140. assertTrue("Expected metastore operations to fail", hiveClient == null);
  141. }
  142. /**
  143. * A simple connect test to make sure that the metastore is up
  144. * @throws Exception
  145. */
  146. private void loopUntilHMSReady() throws Exception {
  147. int retries = 0;
  148. Exception exc = null;
  149. while (true) {
  150. try {
  151. Socket socket = new Socket();
  152. socket.connect(new InetSocketAddress(port), 5000);
  153. socket.close();
  154. return;
  155. } catch (Exception e) {
  156. if (retries++ > 6) { //give up
  157. exc = e;
  158. break;
  159. }
  160. Thread.sleep(10000);
  161. }
  162. }
  163. throw exc;
  164. }
  165. private void createDBAndVerifyExistence(HiveMetaStoreClient client)
  166. throws Exception {
  167. String dbName = "simpdb";
  168. Database db = new Database();
  169. db.setName(dbName);
  170. client.createDatabase(db);
  171. Database db1 = client.getDatabase(dbName);
  172. client.dropDatabase(dbName);
  173. assertTrue("Databases do not match", db1.getName().equals(db.getName()));
  174. }
  175. }