You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jb...@apache.org on 2022/07/11 17:09:58 UTC

[activemq-artemis] branch main updated: ARTEMIS-3886 fix CLI operation retry

This is an automated email from the ASF dual-hosted git repository.

jbertram pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis.git


The following commit(s) were added to refs/heads/main by this push:
     new 597953b6e2 ARTEMIS-3886 fix CLI operation retry
     new f3954961e3 This closes #4138
597953b6e2 is described below

commit 597953b6e22762518c362d484d1a837ea4630154
Author: Justin Bertram <jb...@apache.org>
AuthorDate: Thu Jul 7 08:59:41 2022 -0500

    ARTEMIS-3886 fix CLI operation retry
    
    Commit f8b045bd2d95ec28c70c4d64c85400b28d8baef5 broke the operation
    retry as it introduced local variables named "user" and "password" which
    overried the class-level variables of the same name. Therefore, when the
    user re-enters the username and password on the command-line those
    values won't actually be used when attempting to reconnect.
---
 .../cli/commands/messages/ConnectionAbstract.java  | 26 ++++++++++--------
 .../org/apache/activemq/cli/test/ArtemisTest.java  | 32 ++++++++++++++++++++++
 2 files changed, 46 insertions(+), 12 deletions(-)

diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
index d463566657..e01f987b5e 100644
--- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
+++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/messages/ConnectionAbstract.java
@@ -163,8 +163,7 @@ public class ConnectionAbstract extends InputAbstract {
       } catch (JMSSecurityException e) {
          // if a security exception will get the user and password through an input
          context.err.println("Connection failed::" + e.getMessage());
-         userPassword();
-         cf = new JmsConnectionFactory(user, password, brokerURL);
+         cf = new JmsConnectionFactory(inputUser(user), inputPassword(password), brokerURL);
          if (clientID != null) {
             cf.setClientID(clientID);
          }
@@ -172,9 +171,7 @@ public class ConnectionAbstract extends InputAbstract {
       } catch (JMSException e) {
          // if a connection exception will ask for the URL, user and password
          context.err.println("Connection failed::" + e.getMessage());
-         brokerURL = input("--url", "Type in the broker URL for a retry (e.g. tcp://localhost:61616)", brokerURL);
-         userPassword();
-         cf = new JmsConnectionFactory(user, password, brokerURL);
+         cf = new JmsConnectionFactory(inputUser(user), inputPassword(password), inputBrokerURL(brokerURL));
          if (clientID != null) {
             cf.setClientID(clientID);
          }
@@ -204,8 +201,7 @@ public class ConnectionAbstract extends InputAbstract {
          if (context != null) {
             context.err.println("Connection failed::" + e.getMessage());
          }
-         userPassword();
-         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
+         cf = new ActiveMQConnectionFactory(brokerURL, inputUser(user), inputPassword(password));
          if (clientID != null) {
             cf.setClientID(clientID);
          }
@@ -215,9 +211,7 @@ public class ConnectionAbstract extends InputAbstract {
          if (context != null) {
             context.err.println("Connection failed::" + e.getMessage());
          }
-         brokerURL = input("--url", "Type in the broker URL for a retry (e.g. tcp://localhost:61616)", brokerURL);
-         userPassword();
-         cf = new ActiveMQConnectionFactory(brokerURL, user, password);
+         cf = new ActiveMQConnectionFactory(inputBrokerURL(brokerURL), inputUser(user), inputPassword(password));
          if (clientID != null) {
             cf.setClientID(clientID);
          }
@@ -225,13 +219,21 @@ public class ConnectionAbstract extends InputAbstract {
       }
    }
 
-   private void userPassword() {
+   private String inputBrokerURL(String defaultValue) {
+      return input("--url", "Type in the broker URL for a retry (e.g. tcp://localhost:61616)", defaultValue);
+   }
+
+   private String inputUser(String user) {
       if (user == null) {
          user = input("--user", "Type the username for a retry", null);
       }
+      return user;
+   }
+
+   private String inputPassword(String password) {
       if (password == null) {
          password = inputPassword("--password", "Type the password for a retry", null);
       }
+      return password;
    }
-
 }
diff --git a/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java b/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java
index 745dce07d6..cdaa19686e 100644
--- a/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java
+++ b/artemis-cli/src/test/java/org/apache/activemq/cli/test/ArtemisTest.java
@@ -40,6 +40,7 @@ import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URL;
 import java.nio.file.Files;
@@ -1266,6 +1267,37 @@ public class ArtemisTest extends CliTestBase {
       testSimpleRun("server");
    }
 
+   @Test
+   public void testOperationRetry() throws Exception {
+      File instanceFolder = temporaryFolder.newFolder("server");
+      setupAuth(instanceFolder);
+      Run.setEmbedded(true);
+      Artemis.main("create", instanceFolder.getAbsolutePath(), "--verbose", "--force", "--silent", "--no-web", "--queues", "q1", "--no-autotune", "--require-login", "--default-port", "61616");
+      System.setProperty("artemis.instance", instanceFolder.getAbsolutePath());
+
+      try {
+         Artemis.internalExecute("run");
+         InputStream in = new ByteArrayInputStream("admin\n".getBytes());
+         ActionContext context = new ActionContext(in, System.out, System.err);
+
+         /*
+          * This operation should fail the first time and then prompt the user to re-enter the username which
+          * it will read from the InputStream in the ActionContext. It can't read the password since it's using
+          * System.console.readPassword() for that.
+          */
+         assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin"}, context));
+
+         /*
+          * This is the same as above except it will prompt the user to re-enter both the URL and the username.
+          */
+         in = new ByteArrayInputStream("tcp://localhost:61616\nadmin\n".getBytes());
+         context = new ActionContext(in, System.out, System.err);
+         assertEquals(Integer.valueOf(100), Artemis.internalExecute(null, null, new String[] {"producer", "--destination", "queue://q1", "--message-count", "100", "--password", "admin", "--url", "tcp://badhost:11111"}, context));
+      } finally {
+         stopServer();
+      }
+   }
+
    @Test
    public void testWeirdCharacter() throws Exception {
       testSimpleRun("test%26%26x86_6");