diff --git a/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestNoSaslAuth.java b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestNoSaslAuth.java new file mode 100644 index 0000000..75fa586 --- /dev/null +++ b/itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestNoSaslAuth.java @@ -0,0 +1,107 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hive.jdbc; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.Map; +import java.util.HashMap; + +import junit.framework.Assert; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.apache.hive.jdbc.miniHS2.MiniHS2; +import org.apache.hive.service.cli.HiveSQLException; +import org.apache.hive.service.cli.session.HiveSessionHook; +import org.apache.hive.service.cli.session.HiveSessionHookContext; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestNoSaslAuth { + private static MiniHS2 miniHS2 = null; + private static String sessionUserName = ""; + + public static class NoSaslSessionHook implements HiveSessionHook { + public static boolean checkUser = false; + + @Override + public void run(HiveSessionHookContext sessionHookContext) + throws HiveSQLException { + if (checkUser) { + Assert.assertEquals(sessionHookContext.getSessionUser(), sessionUserName); + } + } + } + + private Connection hs2Conn = null; + + @BeforeClass + public static void beforeTest() throws Exception { + Class.forName(MiniHS2.getJdbcDriverName()); + HiveConf conf = new HiveConf(); + conf.setBoolVar(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, false); + conf.setBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS, false); + conf.setVar(ConfVars.HIVE_SERVER2_SESSION_HOOK, + NoSaslSessionHook.class.getName()); + /* + * TODO: The authfactory reads new config, hence needs to set as system property + * That is addressed as part of proposed patch of HIVE-6665. After that patch, + * we can set this property directly in the conf + */ + System.setProperty(ConfVars.HIVE_SERVER2_AUTHENTICATION.varname, "NOSASL"); + miniHS2 = new MiniHS2(conf); + Map overlayProps = new HashMap(); + miniHS2.start(overlayProps); + } + + @Before + public void setUp() throws Exception { + // enable the hook check after the server startup, + NoSaslSessionHook.checkUser = true; + } + + @After + public void tearDown() throws Exception { + hs2Conn.close(); + NoSaslSessionHook.checkUser = false; + } + + @AfterClass + public static void afterTest() throws Exception { + if (miniHS2.isStarted()) + miniHS2.stop(); + } + + /** + * Initiate a non-sasl connection. The session hook will verfiy the user name + * set correctly + * + * @throws Exception + */ + @Test + public void testConnection() throws Exception { + sessionUserName = "user1"; + hs2Conn = DriverManager.getConnection( + miniHS2.getJdbcURL() + ";auth=noSasl", sessionUserName, "foo"); + } +} diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index e0d2d6d..46b27b5 100644 --- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -443,6 +443,12 @@ private void openSession() throws SQLException { } openReq.setConfiguration(openConf); + // Store the user name in the open request in case no non-sasl authentication + if (JdbcConnectionParams.AUTH_SIMPLE.equals(sessConfMap.get(JdbcConnectionParams.AUTH_TYPE))) { + openReq.setUsername(sessConfMap.get(JdbcConnectionParams.AUTH_USER)); + openReq.setPassword(sessConfMap.get(JdbcConnectionParams.AUTH_PASSWD)); + } + try { TOpenSessionResp openResp = client.OpenSession(openReq);