diff --git a/beeline/src/java/org/apache/hive/beeline/hs2connection/HS2ConnectionFileUtils.java b/beeline/src/java/org/apache/hive/beeline/hs2connection/HS2ConnectionFileUtils.java index 7c064cfda9..ba239fba5a 100644 --- a/beeline/src/java/org/apache/hive/beeline/hs2connection/HS2ConnectionFileUtils.java +++ b/beeline/src/java/org/apache/hive/beeline/hs2connection/HS2ConnectionFileUtils.java @@ -131,20 +131,28 @@ public static String getNamedUrl(Properties userNamedConnectionURLs, String urlN jdbcURL = userNamedConnectionURLs.getProperty(urlName); if (jdbcURL == null) { throw new BeelineSiteParseException( - "The named url: " + urlName + " is not specified in the connection configuration file: " + "The named url: (" + urlName + ") is not specified in the connection configuration file: " + BeelineSiteParser.DEFAULT_BEELINE_SITE_FILE_NAME); } return jdbcURL; } else { // Try to read the default named url from the connection configuration file - String defaultURLName = userNamedConnectionURLs - .getProperty(BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY); + String defaultURLName = + userNamedConnectionURLs.getProperty(BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY); + if (defaultURLName == null) { + throw new BeelineSiteParseException( + "The default named url (beeline.hs2.jdbc.url.default) is not properly specified in the connection configuration file: " + + BeelineSiteParser.DEFAULT_BEELINE_SITE_FILE_NAME); + } jdbcURL = userNamedConnectionURLs.getProperty(defaultURLName); if (jdbcURL != null) { return jdbcURL; + } else { + throw new BeelineSiteParseException( + "The default named url (beeline.hs2.jdbc.url.default) is not properly specified in the connection configuration file: " + + BeelineSiteParser.DEFAULT_BEELINE_SITE_FILE_NAME); } } - return null; } /** diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java index 2ed631ab70..c6c07e818e 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java @@ -57,7 +57,7 @@ protected static final String KEY_STORE_TRUST_STORE_PASSWORD = "HiveJdbc"; protected static final String HS2_HTTP_MODE = "http"; protected static final String HS2_HTTP_ENDPOINT = "cliservice"; - private final String fileLocation = + private String fileLocation = System.getProperty("java.io.tmpdir") + "testHs2ConnectionConfig.xml"; protected static final String JAVA_TRUST_STORE_PROP = "javax.net.ssl.trustStore"; protected static final String JAVA_TRUST_STORE_PASS_PROP = "javax.net.ssl.trustStorePassword"; @@ -122,6 +122,21 @@ protected Hs2ConnectionXmlConfigFileWriter() throws IOException { file.deleteOnExit(); } } + + protected Hs2ConnectionXmlConfigFileWriter(String location) throws IOException { + fileLocation = location; + file = new File(fileLocation); + conf = new Configuration(false); + try { + if (file.exists()) { + file.delete(); + } + file.createNewFile(); + writer = new PrintWriter(file.getAbsolutePath(), "UTF-8"); + } finally { + file.deleteOnExit(); + } + } protected void writeProperty(String key, String value) { conf.set(key, value); diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithBeelineSite.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithBeelineSite.java new file mode 100644 index 0000000000..e99e73ccd2 --- /dev/null +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithBeelineSite.java @@ -0,0 +1,125 @@ +/* + * 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.beeline.hs2connection; + +import java.io.File; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.junit.Test; + +public class TestBeelineWithBeelineSite extends BeelineWithHS2ConnectionFileTestBase { + + @Test + public void testBeelineConnectionHttpUrl() throws Exception { + setupHttpHs2(); + String httpUrlName = "httpUrl"; + String path = createHttpHs2ConnectionBeelineSite(httpUrlName, false); + testBeeLineConnection(path, new String[] { "-c", httpUrlName, "-e", "show tables;" }, tableName); + miniHS2.stop(); + } + + @Test + public void testBeelineConnectionHttpUrlAsDefault() throws Exception { + setupHttpHs2(); + String httpUrlName = "httpUrl"; + String path = createHttpHs2ConnectionBeelineSite(httpUrlName, true); + testBeeLineConnection(path, new String[] { "-e", "show tables;" }, tableName); + miniHS2.stop(); + } + + private void setupHttpHs2() throws Exception { + confOverlay.put(ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, HS2_HTTP_MODE); + confOverlay.put(ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH.varname, HS2_HTTP_ENDPOINT); + confOverlay.put(ConfVars.HIVE_SERVER2_ENABLE_DOAS.varname, "true"); + miniHS2.start(confOverlay); + createTable(); + } + + private String createHttpHs2ConnectionBeelineSite(String urlName, boolean isDefault) throws Exception { + Hs2ConnectionXmlConfigFileWriter writer = + new Hs2ConnectionXmlConfigFileWriter(System.getProperty("java.io.tmpdir") + "/beeline-site.xml"); + String jdbcUrl = + miniHS2.getBaseJdbcURL() + "default;user=hive;password=hive;transportMode=http;httpPath=cliservice"; + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + urlName, jdbcUrl); + if (isDefault) { + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + + BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY, urlName); + } + writer.close(); + return writer.path(); + } + + @Test + public void testBeelineConnectionNoAuth() throws Exception { + setupNoAuthConfHS2(); + String noAuthUrlName = "noAuthUrl"; + String path = createNoAuthHs2ConnectionFile(noAuthUrlName, false); + testBeeLineConnection(path, new String[] { "-c", noAuthUrlName, "-e", "show tables;" }, tableName); + miniHS2.stop(); + } + + private void setupNoAuthConfHS2() throws Exception { + // use default configuration for no-auth mode + miniHS2.start(confOverlay); + createTable(); + } + + private String createNoAuthHs2ConnectionFile(String urlName, boolean isDefault) throws Exception { + Hs2ConnectionXmlConfigFileWriter writer = + new Hs2ConnectionXmlConfigFileWriter(System.getProperty("java.io.tmpdir") + "/beeline-site.xml"); + String jdbcUrl = miniHS2.getBaseJdbcURL() + "default;user=hive;password=hive"; + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + urlName, jdbcUrl); + if (isDefault) { + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + + BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY, urlName); + } + writer.close(); + return writer.path(); + } + + @Test + public void testBeelineConnectionSSL() throws Exception { + setupSslHs2(); + String sslUrlName = "sslUrl"; + String path = createSSLHs2ConnectionFile(sslUrlName, false); + testBeeLineConnection(path, new String[] { "-c", sslUrlName, "-e", "show tables;" }, tableName); + miniHS2.stop(); + } + + private String createSSLHs2ConnectionFile(String urlName, boolean isDefault) throws Exception { + Hs2ConnectionXmlConfigFileWriter writer = + new Hs2ConnectionXmlConfigFileWriter(System.getProperty("java.io.tmpdir") + "/beeline-site.xml"); + String jdbcUrl = miniHS2.getBaseJdbcURL() + "default;user=hive;password=hive;ssl=true;trustStorePassword=" + + KEY_STORE_TRUST_STORE_PASSWORD + ";sslTrustStore=" + dataFileDir + File.separator + TRUST_STORE_NAME; + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + urlName, jdbcUrl); + if (isDefault) { + writer.writeProperty(BeelineSiteParser.BEELINE_CONNECTION_NAMED_JDBC_URL_PREFIX + + BeelineSiteParser.DEFAULT_NAMED_JDBC_URL_PROPERTY_KEY, urlName); + } + writer.close(); + return writer.path(); + } + + private void setupSslHs2() throws Exception { + confOverlay.put(ConfVars.HIVE_SERVER2_USE_SSL.varname, "true"); + confOverlay.put(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PATH.varname, + dataFileDir + File.separator + LOCALHOST_KEY_STORE_NAME); + confOverlay.put(ConfVars.HIVE_SERVER2_SSL_KEYSTORE_PASSWORD.varname, KEY_STORE_TRUST_STORE_PASSWORD); + miniHS2.start(confOverlay); + createTable(); + } +}