diff --git beeline/src/java/org/apache/hive/beeline/Commands.java beeline/src/java/org/apache/hive/beeline/Commands.java index 8f47323700..8af256f0df 100644 --- beeline/src/java/org/apache/hive/beeline/Commands.java +++ beeline/src/java/org/apache/hive/beeline/Commands.java @@ -1627,26 +1627,27 @@ 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("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 ("NONE".equals(auth)) { + if (username == null || username.length() == 0) { + username = System.getProperty("user.name"); } + beeLine.debug("Username: " + username); 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 + ": "); + } + props.setProperty(JdbcConnectionParams.AUTH_USER, username); + if (password == null) { + password = beeLine.getConsoleReader().readLine("Enter password for " + urlForPrompt + ": ", '*'); + } + props.setProperty(JdbcConnectionParams.AUTH_PASSWD, password); } - props.setProperty(JdbcConnectionParams.AUTH_PASSWD, password); } try { @@ -1677,6 +1678,17 @@ public boolean connect(Properties props) throws IOException { } } + private 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); + } + } + 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..f1113ae1a1 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 @@ -207,6 +207,58 @@ public void testNoPasswordPrompt2() throws Exception { output.contains(tableName.toLowerCase())); } + /** + * Tests if beeline doesn't prompt for username and password, but picks up current unix username and connects when auth=NONE. + */ + @Test + public void testConnectWithoutPromptWhenAuthIsNone() throws Exception { + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("-a"); + argList.add("NONE"); + argList.add("-e"); + argList.add("show tables;"); + String output = connectBeelineWithUserPrompt(argList); + Assert.assertTrue("Current unix username is not picked up: " + output, + output.contains("username:" + System.getProperty("user.name"))); + Assert.assertTrue("Table name " + tableName + " not found in the output", + output.contains(tableName.toLowerCase())); + } + + /** + * Tests if beeline doesn't prompt, but picks up the given username when auth=NONE and user=user1. + */ + @Test + public void testConnectWithoutPromptWhenAuthIsNoneAndUserOptionSet() throws Exception { + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("-n"); + argList.add("user1"); + argList.add("-a"); + argList.add("NONE"); + argList.add("-e"); + argList.add("show tables;"); + String output = connectBeelineWithUserPrompt(argList); + Assert.assertTrue("Given username is not picked up: " + output, output.contains("username:user1")); + Assert.assertTrue("Table name " + tableName + " not found in the output", + output.contains(tableName.toLowerCase())); + } + + /** + * Tests if beeline prompts for username when auth is other than NONE and does not pick up current unix username. + */ + @Test + public void testConnectWithPromptWhenAuthIsNotNone() throws Exception { + List argList = getBaseArgs(miniHS2.getBaseJdbcURL()); + argList.add("-a"); + argList.add("KERBEROS"); + argList.add("-e"); + argList.add("show tables;"); + String output = connectBeelineWithUserPrompt(argList); + Assert.assertFalse("Current unix username is not picked up", + output.contains("username:" + System.getProperty("user.name"))); + 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