Merge pull request #2483 from neuyilan/apache_master_0113_fix_node_tools

Add authentication in the node tool and modify the node tools doc
diff --git a/cluster/src/main/java/org/apache/iotdb/cluster/utils/nodetool/function/NodeToolCmd.java b/cluster/src/main/java/org/apache/iotdb/cluster/utils/nodetool/function/NodeToolCmd.java
index 57d3c7b..ff9fd3f 100644
--- a/cluster/src/main/java/org/apache/iotdb/cluster/utils/nodetool/function/NodeToolCmd.java
+++ b/cluster/src/main/java/org/apache/iotdb/cluster/utils/nodetool/function/NodeToolCmd.java
@@ -1,4 +1,4 @@
-/**
+/*
  * 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
@@ -25,6 +25,8 @@
 import io.airlift.airline.Option;
 import io.airlift.airline.OptionType;
 import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
 import javax.management.JMX;
 import javax.management.MBeanServerConnection;
 import javax.management.MalformedObjectNameException;
@@ -37,6 +39,7 @@
 import org.apache.iotdb.cluster.utils.nodetool.ClusterMonitor;
 import org.apache.iotdb.cluster.utils.nodetool.ClusterMonitorMBean;
 
+@SuppressWarnings("squid:S2068")
 public abstract class NodeToolCmd implements Runnable {
 
   @Option(type = OptionType.GLOBAL, name = {"-h",
@@ -47,6 +50,14 @@
       "--port"}, description = "Remote jmx agent port number")
   private String port = "31999";
 
+  @Option(type = OptionType.GLOBAL, name = {"-u",
+      "--user"}, description = "The username to access the remote jmx")
+  private String user = "iotdb";
+
+  @Option(type = OptionType.GLOBAL, name = {"-pw",
+      "--password"}, description = "The password to access the remote jmx")
+  private String password = "passw!d";
+
   private static final String JMX_URL_FORMAT = "service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi";
 
   static final String BUILDING_CLUSTER_INFO = "The cluster is being created.";
@@ -72,7 +83,9 @@
     try {
       String jmxURL = String.format(JMX_URL_FORMAT, host, port);
       JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
-      JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
+      Map<String, Object> environment = Collections
+          .singletonMap(JMXConnector.CREDENTIALS, new String[]{user, password});
+      JMXConnector connector = JMXConnectorFactory.connect(serviceURL, environment);
       mbsc = connector.getMBeanServerConnection();
     } catch (IOException e) {
       Throwable rootCause = Throwables.getRootCause(e);
@@ -84,8 +97,9 @@
     return mbsc;
   }
 
-  String nodeToString(Node node){
-    return String.format("%s:%d:%d", node.getIp(), node.getMetaPort(), node.getDataPort());
+  String nodeToString(Node node) {
+    return String.format("%s:%d:%d:%d", node.getIp(), node.getMetaPort(), node.getDataPort(),
+        node.getClientPort());
   }
 
   String partitionGroupToString(PartitionGroup group) {
diff --git a/docs/UserGuide/System Tools/NodeTool.md b/docs/UserGuide/System Tools/NodeTool.md
index bc5387d..062e9d9 100644
--- a/docs/UserGuide/System Tools/NodeTool.md
+++ b/docs/UserGuide/System Tools/NodeTool.md
@@ -26,6 +26,9 @@
 The following describes the usage and examples of each instruction, 
 where $IOTDB_CLUSTER_HOME indicates the path of the distributed IoTDB installation directory.
 
+# Prerequisites
+To use the nodetool, you need to enable JMX service. Please refer to [JMX tool](JMX Tool.md) for details.
+
 # Instructions
 ## Get Started
 The nodetool shell tool startup script is located at $IOTDB_CLUSTER_HOME/sbin folder,
@@ -34,11 +37,15 @@
 IP is the IP (or hostname) of the node that you expect to connect to,
 and port is the JMX service port specified when the IoTDB cluster is started.
 
-The default values are 127.0.0.1 and 31999, respectively.
+The default values are `127.0.0.1` and `31999`, respectively.
 
 If you need to monitor the remote cluster or modify the JMX service port number,
 set the actual IP and port with the `-h` and `-p` options.
 
+When JMX authentication service is enabled, the username and password of JMX service should be specified. 
+The default values are `iotdb` and `passw!d`, respectively. Please use the start parameters `-u` and `-pw`
+to specify the username and password of the JMX service.
+
 ## Explains
 In a distributed system, a node is identified by node IP, metadata port, data port and cluster port \<IP:METAPORT:DATAPORT:CLUSTERPORT>.
 ### Show The Ring Of Node
diff --git a/docs/zh/UserGuide/System Tools/NodeTool.md b/docs/zh/UserGuide/System Tools/NodeTool.md
index 612b804..0a01c0b 100644
--- a/docs/zh/UserGuide/System Tools/NodeTool.md
+++ b/docs/zh/UserGuide/System Tools/NodeTool.md
@@ -25,14 +25,20 @@
 
 下面具体介绍每个指令的使用方式及示例,其中$IOTDB_CLUSTER_HOME表示IoTDB分布式的安装目录所在路径。
 
+# 前提条件
+使用NodeTool需要开启JMX服务,具体请参考[JMX Tool](JMX Tool.md)。
+
 # 使用说明
 
 ## 运行方式
+
 NodeTool Shell工具启动脚本位于$IOTDB_CLUSTER_HOME/sbin文件夹下,启动时可以指定集群运行的IP和PORT。
+其中IP为您期望连接的节点的IP,PORT为IoTDB集群启动时指定的JMX服务端口号,分别默认为`127.0.0.1`和`31999`。
 
-其中IP为您期望连接的节点的IP,PORT为IoTDB集群启动时指定的JMX服务端口号,分别默认为127.0.0.1和31999。
+如果您需要监控远程集群或修改了JMX服务端口号,请通过启动参数`-h`和`-p`项来使用实际的IP和PORT。
 
-如果您需要监控远程集群或修改了JMX服务端口号,请通过启动参数-h和-p项来使用实际的IP和PORT。
+对于开启了JMX鉴权服务的,启动时候需要指定JMX服务的用户名和密码,默认分别为`iotdb`和`passw!d`,请通过启动参数`-u` 和`-pw`
+项来指定JMX服务的用户名和密码。
 
 ## 指令说明
 在分布式系统中,一个节点由节点IP,元数据端口,数据端口和服务端口来标识,即Node\<IP:METAPORT:DATAPORT:CLUSTERPORT>。
diff --git a/server/src/assembly/resources/conf/iotdb-env.bat b/server/src/assembly/resources/conf/iotdb-env.bat
index 73f03ac..c915a16 100644
--- a/server/src/assembly/resources/conf/iotdb-env.bat
+++ b/server/src/assembly/resources/conf/iotdb-env.bat
@@ -35,7 +35,6 @@
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.port=%JMX_PORT%"
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.rmi.port=%JMX_PORT%"
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Djava.rmi.server.randomIDs=true"
-  set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.authenticate=true"
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.ssl=false"
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.authenticate=true"
   set IOTDB_JMX_OPTS="%IOTDB_JMX_OPTS% -Dcom.sun.management.jmxremote.password.file=%IOTDB_CONF%\jmx.password"
diff --git a/server/src/assembly/resources/conf/iotdb-env.sh b/server/src/assembly/resources/conf/iotdb-env.sh
index 10e8baa..9f3e653 100755
--- a/server/src/assembly/resources/conf/iotdb-env.sh
+++ b/server/src/assembly/resources/conf/iotdb-env.sh
@@ -189,7 +189,6 @@
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.port=$JMX_PORT"
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.rmi.port=$JMX_PORT"
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Djava.rmi.server.randomIDs=true"
-  IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.authenticate=true"
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.ssl=false"
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.authenticate=true"
   IOTDB_JMX_OPTS="$IOTDB_JMX_OPTS -Dcom.sun.management.jmxremote.password.file=${IOTDB_CONF}/jmx.password"