hive-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgumas...@apache.org
Subject svn commit: r1657675 - in /hive/trunk/beeline/src: java/org/apache/hive/beeline/BeeLine.java main/resources/BeeLine.properties test/org/apache/hive/beeline/TestBeelineArgParsing.java
Date Thu, 05 Feb 2015 19:52:17 GMT
Author: vgumashta
Date: Thu Feb  5 19:52:16 2015
New Revision: 1657675

URL: http://svn.apache.org/r1657675
Log:
HIVE-7175: Provide password file option to beeline (Dr. Wendell Urth, Vaibhav Gumashta reviewed
by Larry McCay, Robert Justice, Thejas Nair)

Modified:
    hive/trunk/beeline/src/java/org/apache/hive/beeline/BeeLine.java
    hive/trunk/beeline/src/main/resources/BeeLine.properties
    hive/trunk/beeline/src/test/org/apache/hive/beeline/TestBeelineArgParsing.java

Modified: hive/trunk/beeline/src/java/org/apache/hive/beeline/BeeLine.java
URL: http://svn.apache.org/viewvc/hive/trunk/beeline/src/java/org/apache/hive/beeline/BeeLine.java?rev=1657675&r1=1657674&r2=1657675&view=diff
==============================================================================
--- hive/trunk/beeline/src/java/org/apache/hive/beeline/BeeLine.java (original)
+++ hive/trunk/beeline/src/java/org/apache/hive/beeline/BeeLine.java Thu Feb  5 19:52:16 2015
@@ -39,6 +39,9 @@ import java.lang.reflect.Modifier;
 import java.net.JarURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.Driver;
@@ -87,7 +90,6 @@ import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.hadoop.io.IOUtils;
 
-
 /**
  * A console SQL shell with command completion.
  * <p>
@@ -294,6 +296,14 @@ public class BeeLine implements Closeabl
         .withDescription("the password to connect as")
         .create('p'));
 
+    // -w (or) --password-file <file>
+    options.addOption(OptionBuilder
+        .hasArg()
+        .withArgName("password-file")
+        .withDescription("the password file to read password from")
+        .withLongOpt("password-file")
+        .create('w'));
+
     // -a <authType>
     options.addOption(OptionBuilder
         .hasArg()
@@ -660,7 +670,11 @@ public class BeeLine implements Closeabl
     auth = cl.getOptionValue("a");
     user = cl.getOptionValue("n");
     getOpts().setAuthType(auth);
-    pass = cl.getOptionValue("p");
+    if (cl.hasOption("w")) {
+      pass = obtainPasswordFromFile(cl.getOptionValue("w"));
+    } else {
+      pass = cl.getOptionValue("p");
+    }
     url = cl.getOptionValue("u");
     getOpts().setInitFile(cl.getOptionValue("i"));
     getOpts().setScriptFile(cl.getOptionValue("f"));
@@ -708,6 +722,19 @@ public class BeeLine implements Closeabl
     return code;
   }
 
+  /**
+   * Obtains a password from the passed file path.
+   */
+  private String obtainPasswordFromFile(String passwordFilePath) {
+    try {
+      Path path = Paths.get(passwordFilePath);
+      byte[] passwordFileContents = Files.readAllBytes(path);
+      return new String(passwordFileContents, "UTF-8").trim();
+    } catch (Exception e) {
+      throw new RuntimeException("Unable to read user password from the password file: "
+          + passwordFilePath, e);
+    }
+  }
 
   /**
    * Start accepting input from stdin, and dispatch it

Modified: hive/trunk/beeline/src/main/resources/BeeLine.properties
URL: http://svn.apache.org/viewvc/hive/trunk/beeline/src/main/resources/BeeLine.properties?rev=1657675&r1=1657674&r2=1657675&view=diff
==============================================================================
--- hive/trunk/beeline/src/main/resources/BeeLine.properties (original)
+++ hive/trunk/beeline/src/main/resources/BeeLine.properties Thu Feb  5 19:52:16 2015
@@ -148,6 +148,7 @@ cmd-usage: Usage: java org.apache.hive.c
 \  -i <init file>                  script file for initialization\n \
 \  -e <query>                      query that should be executed\n \
 \  -f <exec file>                  script file that should be executed\n \
+\  -w (or) --password-file <password file>  the password file to read password from\n
\
 \  --hiveconf property=value       Use value for given property\n \
 \  --hivevar name=value            hive variable name and value\n \
 \                                  This is Hive specific settings in which variables\n \

Modified: hive/trunk/beeline/src/test/org/apache/hive/beeline/TestBeelineArgParsing.java
URL: http://svn.apache.org/viewvc/hive/trunk/beeline/src/test/org/apache/hive/beeline/TestBeelineArgParsing.java?rev=1657675&r1=1657674&r2=1657675&view=diff
==============================================================================
--- hive/trunk/beeline/src/test/org/apache/hive/beeline/TestBeelineArgParsing.java (original)
+++ hive/trunk/beeline/src/test/org/apache/hive/beeline/TestBeelineArgParsing.java Thu Feb
 5 19:52:16 2015
@@ -21,6 +21,9 @@ package org.apache.hive.beeline;
 import java.util.ArrayList;
 import java.util.List;
 
+import java.io.File;
+import java.io.FileOutputStream;
+
 import junit.framework.Assert;
 
 import org.junit.Test;
@@ -61,6 +64,25 @@ public class TestBeelineArgParsing {
     Assert.assertTrue(bl.getOpts().getAuthType().equals("authType"));
   }
 
+  @Test
+  public void testPasswordFileArgs() throws Exception {
+    TestBeeline bl = new TestBeeline();
+    File passFile = new File("file.password");
+    passFile.deleteOnExit();
+    FileOutputStream passFileOut = new FileOutputStream(passFile);
+    passFileOut.write("mypass\n".getBytes());
+    passFileOut.close();
+    String args[] = new String[] {"-u", "url", "-n", "name",
+      "-w", "file.password", "-p", "not-taken-if-w-is-present",
+      "-d", "driver", "-a", "authType"};
+    bl.initArgs(args);
+    System.out.println(bl.connectArgs);
+    // Password file contents are trimmed of trailing whitespaces and newlines
+    Assert.assertTrue(bl.connectArgs.equals("url name mypass driver"));
+    Assert.assertTrue(bl.getOpts().getAuthType().equals("authType"));
+    passFile.delete();
+  }
+
   /**
    * The first flag is taken by the parser.
    */



Mime
View raw message