diff --git beeline/src/java/org/apache/hive/beeline/Commands.java beeline/src/java/org/apache/hive/beeline/Commands.java index 8f47323700..f99165e588 100644 --- beeline/src/java/org/apache/hive/beeline/Commands.java +++ beeline/src/java/org/apache/hive/beeline/Commands.java @@ -1627,28 +1627,34 @@ public boolean connect(Properties props) throws IOException { } } - String auth = getProperty(props, new String[] {JdbcConnectionParams.AUTH_TYPE}); - if (auth == null) { - auth = beeLine.getOpts().getAuthType(); - if (auth != null) { - props.setProperty(JdbcConnectionParams.AUTH_TYPE, auth); - } - } + String auth = getAuthType(props); + beeLine.info("Authentication type:" + auth); beeLine.info("Connecting to " + url); - if (Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_PRINCIPAL) == null) { - String urlForPrompt = url.substring(0, url.contains(";") ? url.indexOf(';') : url.length()); - if (username == null) { - username = beeLine.getConsoleReader().readLine("Enter username for " + urlForPrompt + ": "); + if (HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION.getDefaultValue().equals(auth)) { + if (username == null){ + username = "hive"; + } + if (password == null){ + password = "DUMMY"; } - props.setProperty(JdbcConnectionParams.AUTH_USER, username); - if (password == null) { - password = beeLine.getConsoleReader().readLine("Enter password for " + urlForPrompt + ": ", - new Character('*')); + } + + else { + if (Utils.parsePropertyFromUrl(url, JdbcConnectionParams.AUTH_PRINCIPAL) == null) { + String urlForPrompt = url.substring(0, url.contains(";") ? url.indexOf(';') : url.length()); + if (username == null) { + username = beeLine.getConsoleReader().readLine("Enter username for " + urlForPrompt + ": "); + } + if (password == null) { + password = beeLine.getConsoleReader().readLine("Enter password for " + urlForPrompt + ": ", '*'); + } } - props.setProperty(JdbcConnectionParams.AUTH_PASSWD, password); } + props.setProperty(JdbcConnectionParams.AUTH_USER, username); + props.setProperty(JdbcConnectionParams.AUTH_PASSWD, password); + try { beeLine.getDatabaseConnections().setConnection( new DatabaseConnection(beeLine, driver, url, props)); @@ -1677,6 +1683,20 @@ public boolean connect(Properties props) throws IOException { } } + public String getAuthType(Properties props) { + String auth = getProperty(props, new String[] {JdbcConnectionParams.AUTH_TYPE}); + if (auth == null) { + auth = beeLine.getOpts().getAuthType(); + if (auth != null) { + props.setProperty(JdbcConnectionParams.AUTH_TYPE, auth); + } + else { + auth = getHiveConf(false).get(HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION.varname); + } + } + return auth; + } + public boolean rehash(String line) { try { if (!(beeLine.assertConnection())) { diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeelinePasswordOption.java itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeelinePasswordOption.java index b6d01ce578..07093bc7bb 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeelinePasswordOption.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/TestBeelinePasswordOption.java @@ -62,8 +62,8 @@ public static void preTests() throws Exception { } /** - * Test if beeline prompts for a password when optional password option is at the beginning of - * arguments + * Test if beeline prompts for a password when optional password option is at the beginning of arguments + * and authentication is other than NONE */ @Test public void testPromptPasswordOptionAsFirst() throws Throwable { @@ -72,15 +72,20 @@ public void testPromptPasswordOptionAsFirst() throws Throwable { argList.addAll(getBaseArgs(miniHS2.getBaseJdbcURL())); argList.add("-n"); argList.add("hive"); + argList.add("-a"); + argList.add("NOT_NONE"); connectBeelineWithUserPrompt(argList, "hivepassword"); } /** * Test if beeline prompts for a password when optional password option is at the end of arguments + * and authentication is other than NONE */ @Test public void testPromptPasswordOptionLast() throws Exception { List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("-a"); + argList.add("NOT_NONE"); argList.add("-n"); argList.add("hive"); argList.add("-p"); @@ -88,8 +93,8 @@ public void testPromptPasswordOptionLast() throws Exception { } /** - * Test if beeline prompts for a password when optional password option is at the middle of - * arguments + * Test if beeline prompts for a password when optional password option is at the middle of arguments + * and authentication is other than NONE */ @Test public void testPromptPasswordOptionMiddle() throws Exception { @@ -97,6 +102,8 @@ public void testPromptPasswordOptionMiddle() throws Exception { argList.add("-p"); argList.add("-n"); argList.add("hive"); + argList.add("-a"); + argList.add("NOT_NONE"); connectBeelineWithUserPrompt(argList, "hivepassword"); } @@ -207,6 +214,19 @@ public void testNoPasswordPrompt2() throws Exception { output.contains(tableName.toLowerCase())); } + /** + * Tests if beeline doesn't prompt for a username and password and connects, when no username and password options provided + */ + @Test + public void testNoUsernameAndPasswordPromptWhenAuthIsNONE() throws Exception { + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("-e"); + argList.add("show tables;"); + String output = connectBeelineWithUserPrompt(argList); + Assert.assertTrue("Table name " + tableName + " not found in the output", + output.contains(tableName.toLowerCase())); + } + /** * Tests if Beeline prompts for password when -p is the last argument and argList has CommandLine * options as well as BeelineOpts