accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From md...@apache.org
Subject git commit: ACCUMULO-1412 Adding PasswordConverter
Date Sat, 31 Aug 2013 20:43:27 GMT
Updated Branches:
  refs/heads/master 12c676ae7 -> 714c12f63


ACCUMULO-1412 Adding PasswordConverter


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/714c12f6
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/714c12f6
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/714c12f6

Branch: refs/heads/master
Commit: 714c12f63b78f83a14f5285c139ac5ba3b503b4c
Parents: 12c676a
Author: Mike Drob <mdrob@mdrob.com>
Authored: Sat Aug 31 16:39:04 2013 -0400
Committer: Mike Drob <mdrob@mdrob.com>
Committed: Sat Aug 31 16:39:04 2013 -0400

----------------------------------------------------------------------
 .../apache/accumulo/core/util/shell/Shell.java  |   3 +-
 .../core/util/shell/ShellOptionsJC.java         |  79 ++++++++++++-
 .../core/util/shell/PasswordConverterTest.java  | 113 +++++++++++++++++++
 3 files changed, 193 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/714c12f6/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
index 175d67e..72f6df2 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
@@ -290,7 +290,8 @@ public class Shell extends ShellOptions {
         props.putAllStrings(loginOptions);
         token.init(props);
       } else {
-        if (password == null) {
+        // Read password if the user explicitly asked for it, or didn't specify anything
at all
+        if ("stdin".equals(password) || password == null) {
           password = reader.readLine("Password: ", '*');
         }
         

http://git-wip-us.apache.org/repos/asf/accumulo/blob/714c12f6/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
index 29dc4a7..1ebf244 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/shell/ShellOptionsJC.java
@@ -17,12 +17,15 @@
 package org.apache.accumulo.core.util.shell;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
+import java.util.Scanner;
 import java.util.TreeMap;
 
 import org.apache.accumulo.core.client.security.tokens.AuthenticationToken;
+import org.apache.log4j.Logger;
 
 import com.beust.jcommander.DynamicParameter;
 import com.beust.jcommander.IStringConverter;
@@ -31,11 +34,85 @@ import com.beust.jcommander.ParameterException;
 import com.beust.jcommander.converters.FileConverter;
 
 public class ShellOptionsJC {
+  // Use the Shell logger because this is really just an extension.
+  public static final Logger log = Logger.getLogger(Shell.class);
+  
   @Parameter(names = {"-u", "--user"}, description = "username (defaults to your OS user)")
   private String username = System.getProperty("user.name", "root");
   
+  public static class PasswordConverter implements IStringConverter<String> {
+    public static final String STDIN = "stdin";
+    private enum KeyType {
+      PASS("pass:"), ENV("env:") {
+        @Override
+        String process(String value) {
+          return System.getenv(value);
+        }
+      },
+      FILE("file:") {
+        @Override
+        String process(String value) {
+          Scanner scanner = null;
+          try {
+            scanner = new Scanner(new File(value));
+            return scanner.nextLine();
+          } catch (FileNotFoundException e) {
+            throw new ParameterException("Password file does not exist", e);
+          } finally {
+            if (scanner != null) {
+              scanner.close();
+            }
+          }
+        }
+      },
+      STDIN(PasswordConverter.STDIN) {
+        @Override
+        public boolean matches(String value) {
+          return prefix.equals(value);
+        }
+        
+        @Override
+        public String convert(String value) {
+          // Will check for this later
+          return prefix;
+        }
+      };
+      
+      String prefix;
+      
+      private KeyType(String prefix) {
+        this.prefix = prefix;
+      }
+      
+      public boolean matches(String value) {
+        return value.startsWith(prefix);
+      }
+      
+      public String convert(String value) {
+        return process(value.substring(prefix.length()));
+      }
+      
+      String process(String value) {
+        return value;
+      }
+    };
+    
+    public String convert(String value) {
+      for (KeyType keyType : KeyType.values()) {
+        if (keyType.matches(value)) {
+          return keyType.convert(value);
+        }
+      }
+      
+      // No match found, assume it is the password for compatibility
+      log.warn("Specifying a raw password is deprecated.");
+      return value;
+    }
+  }
+  
   // Note: Don't use "password = true" because then it will prompt even if we have a token
-  @Parameter(names = {"-p", "--password"}, description = "password (prompt for password if
this option is missing)")
+  @Parameter(names = {"-p", "--password"}, description = "password (can be specified as 'pass:<password>',
'file:<local file containing the password>', "
+      + "'env:<variable containing the pass>', or stdin)", converter = PasswordConverter.class)
   private String password;
   
   public static class TokenConverter implements IStringConverter<AuthenticationToken>
{

http://git-wip-us.apache.org/repos/asf/accumulo/blob/714c12f6/core/src/test/java/org/apache/accumulo/core/util/shell/PasswordConverterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/accumulo/core/util/shell/PasswordConverterTest.java
b/core/src/test/java/org/apache/accumulo/core/util/shell/PasswordConverterTest.java
new file mode 100644
index 0000000..d1d24a6
--- /dev/null
+++ b/core/src/test/java/org/apache/accumulo/core/util/shell/PasswordConverterTest.java
@@ -0,0 +1,113 @@
+/*
+ * 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.accumulo.core.util.shell;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+import java.util.Scanner;
+
+import org.apache.accumulo.core.util.shell.ShellOptionsJC.PasswordConverter;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.beust.jcommander.JCommander;
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.ParameterException;
+
+public class PasswordConverterTest {
+  
+  private class Password {
+    @Parameter(names = "--password", converter = PasswordConverter.class)
+    String password;
+  }
+  
+  private String[] argv;
+  private Password password;
+  private static InputStream realIn;
+  
+  @BeforeClass
+  public static void saveIn() {
+    realIn = System.in;
+  }
+  
+  @Before
+  public void setup() throws IOException {
+    argv = new String[] {"--password", ""};
+    password = new Password();
+    
+    PipedInputStream in = new PipedInputStream();
+    PipedOutputStream out = new PipedOutputStream(in);
+    OutputStreamWriter osw = new OutputStreamWriter(out);
+    osw.write("secret");
+    osw.close();
+    
+    System.setIn(in);
+  }
+  
+  @After
+  public void teardown() {
+    System.setIn(realIn);
+  }
+  
+  @Test
+  public void testPass() {
+    String expected = String.valueOf(Math.random());
+    argv[1] = "pass:" + expected;
+    new JCommander(password, argv);
+    assertEquals(expected, password.password);
+  }
+  
+  @Test
+  public void testEnv() {
+    String name = System.getenv().keySet().iterator().next();
+    argv[1] = "env:" + name;
+    new JCommander(password, argv);
+    assertEquals(System.getenv(name), password.password);
+  }
+  
+  @Test
+  public void testFile() throws FileNotFoundException {
+    argv[1] = "file:pom.xml";
+    Scanner scan = new Scanner(new File("pom.xml"));
+    String expected = scan.nextLine();
+    scan.close();
+    new JCommander(password, argv);
+    assertEquals(expected, password.password);
+  }
+  
+  @Test(expected=ParameterException.class)
+  public void testNoFile() throws FileNotFoundException {
+    argv[1] = "file:doesnotexist";
+    new JCommander(password, argv);
+  }
+
+  @Test
+  public void testStdin() {
+    argv[1] = "stdin";
+    new JCommander(password, argv);
+    assertEquals("stdin", password.password);
+  }
+}


Mime
View raw message