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 CCCC0200B8E for ; Mon, 26 Sep 2016 15:13:43 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id CB844160AB8; Mon, 26 Sep 2016 13:13:43 +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 818D1160AE7 for ; Mon, 26 Sep 2016 15:13:42 +0200 (CEST) Received: (qmail 44617 invoked by uid 500); 26 Sep 2016 13:13:41 -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 44498 invoked by uid 99); 26 Sep 2016 13:13:41 -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; Mon, 26 Sep 2016 13:13:41 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 599B6E2F35; Mon, 26 Sep 2016 13:13:41 +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: Mon, 26 Sep 2016 13:13:46 -0000 Message-Id: <083e4d2271fe45b7b047276838e80009@git.apache.org> In-Reply-To: <2c4a3164c17d4bff9ca3fc3c3362a1e1@git.apache.org> References: <2c4a3164c17d4bff9ca3fc3c3362a1e1@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [6/9] accumulo git commit: ACCUMULO-4461: modified commands to not prompt for a password archived-at: Mon, 26 Sep 2016 13:13:44 -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/064ae0aa Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/064ae0aa Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/064ae0aa Branch: refs/heads/master Commit: 064ae0aa70e8b132fc3c8dde9792ce8a4b1f4094 Parents: 221dc3f Author: milleruntime Authored: Fri Sep 23 10:24:04 2016 -0400 Committer: Josh Elser Committed: Mon Sep 26 09:11:23 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/064ae0aa/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/064ae0aa/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/064ae0aa/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;