You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by br...@apache.org on 2014/02/19 18:40:26 UTC

[2/3] git commit: Allow nodetool to use a file/prompt for password Patch by Clément Lardeur, reviewed by Sankalp Kohli for CASSANDRA-6660

Allow nodetool to use a file/prompt for password
Patch by Clément Lardeur, reviewed by Sankalp Kohli for CASSANDRA-6660


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

Branch: refs/heads/trunk
Commit: 657e16006e56b52cdb06a6014e4a2a8bfd87d77c
Parents: 8e101be
Author: Brandon Williams <br...@apache.org>
Authored: Wed Feb 19 11:35:35 2014 -0600
Committer: Brandon Williams <br...@apache.org>
Committed: Wed Feb 19 11:37:24 2014 -0600

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 ++
 .../org/apache/cassandra/tools/NodeTool.java    | 56 ++++++++++++++++++--
 2 files changed, 54 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/657e1600/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 23a5173..583245a 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,6 @@
+2.1.0-beta2
+ * Allow nodetool to use a file or prompt for password (CASSANDRA-6660)
+
 2.1.0-beta1
  * Add flush directory distinct from compaction directories (CASSANDRA-6357)
  * Require JNA by default (CASSANDRA-6575)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/657e1600/src/java/org/apache/cassandra/tools/NodeTool.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/tools/NodeTool.java b/src/java/org/apache/cassandra/tools/NodeTool.java
index a12efac..fedf2c1 100644
--- a/src/java/org/apache/cassandra/tools/NodeTool.java
+++ b/src/java/org/apache/cassandra/tools/NodeTool.java
@@ -17,9 +17,7 @@
  */
 package org.apache.cassandra.tools;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
+import java.io.*;
 import java.lang.management.MemoryUsage;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
@@ -60,8 +58,7 @@ import static com.google.common.collect.Lists.newArrayList;
 import static java.lang.Integer.parseInt;
 import static java.lang.String.format;
 import static org.apache.commons.lang3.ArrayUtils.EMPTY_STRING_ARRAY;
-import static org.apache.commons.lang3.StringUtils.EMPTY;
-import static org.apache.commons.lang3.StringUtils.join;
+import static org.apache.commons.lang3.StringUtils.*;
 
 public class NodeTool
 {
@@ -223,9 +220,20 @@ public class NodeTool
         @Option(type = OptionType.GLOBAL, name = {"-pw", "--password"}, description = "Remote jmx agent password")
         private String password = EMPTY;
 
+        @Option(type = OptionType.GLOBAL, name = {"-pwf", "--password-file"}, description = "Path to the JMX password file")
+        private String passwordFilePath = EMPTY;
+
         @Override
         public void run()
         {
+            if (isNotEmpty(username)) {
+                if (isNotEmpty(passwordFilePath))
+                    password = readUserPasswordFromFile(username, passwordFilePath);
+
+                if (isEmpty(password))
+                    password = promptAndReadPassword();
+            }
+
             try (NodeProbe probe = connect())
             {
                 execute(probe);
@@ -236,6 +244,44 @@ public class NodeTool
 
         }
 
+        private String readUserPasswordFromFile(String username, String passwordFilePath) {
+            String password = EMPTY;
+
+            File passwordFile = new File(passwordFilePath);
+            try (Scanner scanner = new Scanner(passwordFile).useDelimiter("\\s+"))
+            {
+                while (scanner.hasNextLine())
+                {
+                    if (scanner.hasNext())
+                    {
+                        String jmxRole = scanner.next();
+                        if (jmxRole.equals(username) && scanner.hasNext())
+                        {
+                            password = scanner.next();
+                            break;
+                        }
+                    }
+                    scanner.nextLine();
+                }
+            } catch (FileNotFoundException e)
+            {
+                throw new RuntimeException(e);
+            }
+
+            return password;
+        }
+
+        private String promptAndReadPassword()
+        {
+            String password = EMPTY;
+
+            Console console = System.console();
+            if (console != null)
+                password = String.valueOf(console.readPassword("Password:"));
+
+            return password;
+        }
+
         protected abstract void execute(NodeProbe probe);
 
         private NodeProbe connect()