accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From els...@apache.org
Subject [2/6] accumulo git commit: ACCUMULO-4461: modified commands to not prompt for a password
Date Fri, 23 Sep 2016 14:53:47 GMT
ACCUMULO-4461: modified commands to not prompt for a password

Closes apache/accumulo#154

Signed-off-by: Josh Elser <elserj@apache.org>


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

Branch: refs/heads/1.8
Commit: d23676dc3698d2ca6084b17cd4326b43dbcec41c
Parents: 985651b
Author: Josh Elser <elserj@apache.org>
Authored: Fri Sep 23 10:24:04 2016 -0400
Committer: Josh Elser <elserj@apache.org>
Committed: Fri Sep 23 10:24:04 2016 -0400

----------------------------------------------------------------------
 .../apache/accumulo/core/cli/ClientOpts.java    | 37 ++++++++++++++++----
 .../accumulo/core/cli/TestClientOpts.java       | 12 ++++---
 .../org/apache/accumulo/server/util/Admin.java  | 14 +-------
 3 files changed, 39 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/d23676dc/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java b/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
index 54e8b53..98421d5 100644
--- a/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
+++ b/core/src/main/java/org/apache/accumulo/core/cli/ClientOpts.java
@@ -58,6 +58,8 @@ import com.beust.jcommander.IStringConverter;
 import com.beust.jcommander.Parameter;
 import com.google.common.base.Predicate;
 
+import jline.console.ConsoleReader;
+
 public class ClientOpts extends Help {
 
   public static class TimeConverter implements IStringConverter<Long> {
@@ -92,6 +94,20 @@ public class ClientOpts extends Help {
     public String toString() {
       return new String(value, UTF_8);
     }
+
+    /**
+     * Prompts user for a password
+     *
+     * @return user entered Password object, null if no console exists
+     */
+    public static Password promptUser() throws IOException {
+      if (System.console() == null) {
+        throw new IOException("Attempted to prompt user on the console when System.console
= null");
+      }
+      ConsoleReader reader = new ConsoleReader();
+      String enteredPass = reader.readLine("Enter password: ", '*');
+      return new Password(enteredPass);
+    }
   }
 
   public static class PasswordConverter implements IStringConverter<Password> {
@@ -142,13 +158,20 @@ public class ClientOpts extends Help {
       }
     }
 
-    if (securePassword != null)
-      return new PasswordToken(securePassword.value);
-
-    if (password != null)
-      return new PasswordToken(password.value);
-
-    return null;
+    // other token types should have resolved by this point, so return PasswordToken
+    Password pass = null;
+    if (securePassword != null) {
+      pass = securePassword;
+    } else if (password != null) {
+      pass = password;
+    } else {
+      try {
+        pass = Password.promptUser();
+      } catch (IOException e) {
+        throw new RuntimeException(e);
+      }
+    }
+    return new PasswordToken(pass.value);
   }
 
   @Parameter(names = {"-z", "--keepers"}, description = "Comma separated list of zookeeper
hosts (host:port,host:port)")

http://git-wip-us.apache.org/repos/asf/accumulo/blob/d23676dc/core/src/test/java/org/apache/accumulo/core/cli/TestClientOpts.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/accumulo/core/cli/TestClientOpts.java b/core/src/test/java/org/apache/accumulo/core/cli/TestClientOpts.java
index 65df5c9..3da5b30 100644
--- a/core/src/test/java/org/apache/accumulo/core/cli/TestClientOpts.java
+++ b/core/src/test/java/org/apache/accumulo/core/cli/TestClientOpts.java
@@ -33,7 +33,6 @@ import java.util.concurrent.TimeUnit;
 
 import javax.security.auth.DestroyFailedException;
 
-import org.apache.accumulo.core.client.AccumuloSecurityException;
 import org.apache.accumulo.core.client.BatchWriterConfig;
 import org.apache.accumulo.core.client.ClientConfiguration;
 import org.apache.accumulo.core.client.ClientConfiguration.ClientProperty;
@@ -66,14 +65,19 @@ public class TestClientOpts {
     BatchWriterOpts bwOpts = new BatchWriterOpts();
     BatchScannerOpts bsOpts = new BatchScannerOpts();
     try {
-      assertNull(args.getPrincipal());
+      args.getPrincipal();
       fail("Expected to receive exception fetching non-existent principal");
-    } catch (AccumuloSecurityException e) {
+    } catch (RuntimeException e) {
       // Pass -- no explicit principal and no token to infer a principal from
     }
 
     assertNull(args.getSecurePassword());
-    assertNull(args.getToken());
+    try {
+      args.getToken();
+      fail("Expected to receive exception fetching non-existent token");
+    } catch (RuntimeException e) {
+      // pass - no console to prompt user for password
+    }
     assertEquals(Long.valueOf(cfg.getMaxLatency(TimeUnit.MILLISECONDS)), bwOpts.batchLatency);
     assertEquals(Long.valueOf(cfg.getTimeout(TimeUnit.MILLISECONDS)), bwOpts.batchTimeout);
     assertEquals(Long.valueOf(cfg.getMaxMemory()), bwOpts.batchMemory);

http://git-wip-us.apache.org/repos/asf/accumulo/blob/d23676dc/server/base/src/main/java/org/apache/accumulo/server/util/Admin.java
----------------------------------------------------------------------
diff --git a/server/base/src/main/java/org/apache/accumulo/server/util/Admin.java b/server/base/src/main/java/org/apache/accumulo/server/util/Admin.java
index 9a8f6ed..bd080ec 100644
--- a/server/base/src/main/java/org/apache/accumulo/server/util/Admin.java
+++ b/server/base/src/main/java/org/apache/accumulo/server/util/Admin.java
@@ -43,7 +43,6 @@ import org.apache.accumulo.core.client.TableNotFoundException;
 import org.apache.accumulo.core.client.admin.InstanceOperations;
 import org.apache.accumulo.core.client.impl.ClientContext;
 import org.apache.accumulo.core.client.impl.ClientExec;
-import org.apache.accumulo.core.client.impl.Credentials;
 import org.apache.accumulo.core.client.impl.MasterClient;
 import org.apache.accumulo.core.conf.AccumuloConfiguration;
 import org.apache.accumulo.core.conf.DefaultConfiguration;
@@ -209,18 +208,7 @@ public class Admin implements KeywordExecutable {
     ServerConfigurationFactory confFactory = new ServerConfigurationFactory(instance);
 
     try {
-      ClientContext context;
-      if (opts.getToken() == null) {
-        context = new AccumuloServerContext(confFactory);
-      } else {
-        final Credentials userCreds = new Credentials(opts.getPrincipal(), opts.getToken());
-        context = new AccumuloServerContext(confFactory) {
-          @Override
-          public synchronized Credentials getCredentials() {
-            return userCreds;
-          }
-        };
-      }
+      ClientContext context = new AccumuloServerContext(confFactory);
 
       int rc = 0;
 


Mime
View raw message