Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id DCE7A200B9E for ; Fri, 23 Sep 2016 16:53:48 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id DA004160AD0; Fri, 23 Sep 2016 14:53:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id C90BB160AD6 for ; Fri, 23 Sep 2016 16:53:47 +0200 (CEST) Received: (qmail 70183 invoked by uid 500); 23 Sep 2016 14:53:47 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 70092 invoked by uid 99); 23 Sep 2016 14:53:46 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 Sep 2016 14:53:46 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AEF9EE0B1D; Fri, 23 Sep 2016 14:53:46 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@accumulo.apache.org Date: Fri, 23 Sep 2016 14:53:48 -0000 Message-Id: <80ca216424ee47849fb2ecb04c0e2ff1@git.apache.org> In-Reply-To: <2e89afe86422434ab018a98c53c16171@git.apache.org> References: <2e89afe86422434ab018a98c53c16171@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [3/6] accumulo git commit: ACCUMULO-4461: modified commands to not prompt for a password archived-at: Fri, 23 Sep 2016 14:53:49 -0000 ACCUMULO-4461: modified commands to not prompt for a password Closes apache/accumulo#154 Signed-off-by: Josh Elser 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/master Commit: d23676dc3698d2ca6084b17cd4326b43dbcec41c Parents: 985651b Author: Josh Elser Authored: Fri Sep 23 10:24:04 2016 -0400 Committer: Josh Elser 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 { @@ -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 { @@ -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;