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

clebertsuconic 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 21368cf741 ARTEMIS-4678 JDBC User and password not used by CLI
21368cf741 is described below

commit 21368cf741f384801cb37fffdea78df93e225407
Author: Clebert Suconic <[email protected]>
AuthorDate: Thu Mar 7 23:06:28 2024 -0500

    ARTEMIS-4678 JDBC User and password not used by CLI
---
 .../artemis/cli/commands/tools/DBOption.java       | 42 +++++++++++-
 .../apache/activemq/artemis/utils/FileUtil.java    | 26 +++++++
 .../jdbc/store/file/JDBCSequentialFileFactory.java |  7 +-
 .../impl/journal/JDBCJournalStorageManager.java    |  6 +-
 .../activemq/artemis/utils/RealServerTestBase.java | 17 -----
 .../smoke/jdbccli/JDBCExportWrongUserTest.java     | 79 ++++++++++++++++++++++
 .../mirror/ClusteredMirrorSoakTest.java            |  3 +-
 .../mirror/InterruptedLargeMessageTest.java        |  3 +-
 8 files changed, 160 insertions(+), 23 deletions(-)

diff --git 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/DBOption.java
 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/DBOption.java
index 11da856bbc..919404b406 100644
--- 
a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/DBOption.java
+++ 
b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/tools/DBOption.java
@@ -93,6 +93,12 @@ public class DBOption extends OptionalLocking {
    @Option(names = "--jdbc-driver-class-name", description = "JDBC driver 
classname.")
    private String jdbcClassName = 
ActiveMQDefaultConfiguration.getDefaultDriverClassName();
 
+   @Option(names = "--jdbc-user", description = "JDBC username.")
+   private String jdbcUser = null;
+
+   @Option(names = "--jdbc-password", description = "JDBC password.")
+   private String jdbcPassword = null;
+
    public boolean isJDBC() throws Exception {
       parseDBConfig();
       return jdbc;
@@ -177,6 +183,23 @@ public class DBOption extends OptionalLocking {
       return this;
    }
 
+   public String getJdbcUser() {
+      return jdbcUser;
+   }
+
+   public DBOption setJdbcUser(String jdbcUser) {
+      this.jdbcUser = jdbcUser;
+      return this;
+   }
+
+   public String getJdbcPassword() {
+      return jdbcPassword;
+   }
+
+   public DBOption setJdbcPassword(String jdbcPassword) {
+      this.jdbcPassword = jdbcPassword;
+      return this;
+   }
 
    @Override
    public Object execute(ActionContext context) throws Exception {
@@ -217,8 +240,21 @@ public class DBOption extends OptionalLocking {
             jdbcLargeMessages = 
storageConfiguration.getLargeMessageTableName();
             jdbcPageStore = storageConfiguration.getPageStoreTableName();
             jdbcNodeManager = 
storageConfiguration.getNodeManagerStoreTableName();
-            jdbcURL = storageConfiguration.getJdbcConnectionUrl();
-            jdbcClassName = storageConfiguration.getJdbcDriverClassName();
+
+            if (jdbcURL == null || 
jdbcURL.equals(ActiveMQDefaultConfiguration.getDefaultDatabaseUrl())) {
+               jdbcURL = storageConfiguration.getJdbcConnectionUrl();
+            }
+
+            if (jdbcClassName == null || 
jdbcClassName.equals(ActiveMQDefaultConfiguration.getDefaultDriverClassName())) 
{
+               jdbcClassName = storageConfiguration.getJdbcDriverClassName();
+            }
+
+            if (jdbcUser == null) {
+               jdbcUser = storageConfiguration.getJdbcUser();
+            }
+            if (jdbcPassword == null) {
+               jdbcPassword = storageConfiguration.getJdbcPassword();
+            }
          }
       }
    }
@@ -235,6 +271,8 @@ public class DBOption extends OptionalLocking {
          storageConfiguration.setLargeMessageTableName(getJdbcLargeMessages());
          storageConfiguration.setPageStoreTableName(getJdbcPageStore());
          
storageConfiguration.setNodeManagerStoreTableName(getJdbcNodeManager());
+         storageConfiguration.setJdbcUser(getJdbcUser());
+         storageConfiguration.setJdbcPassword(getJdbcPassword());
          configuration.setStoreConfiguration(storageConfiguration);
       } else {
          configuration.setBindingsDirectory(getBinding());
diff --git 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/FileUtil.java 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/FileUtil.java
index 191bb1c362..c7fd65c213 100644
--- 
a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/FileUtil.java
+++ 
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/FileUtil.java
@@ -100,4 +100,30 @@ public class FileUtil {
          }
       });
    }
+
+
+   /**
+    * Search and replace strings on a file
+    *
+    * @param file file to be replaced
+    * @param find string expected to match
+    * @param replace string to be replaced
+    * @return true if the replacement was successful
+    * @throws Exception
+    */
+   public static boolean findReplace(File file, String find, String replace) 
throws Exception {
+      if (!file.exists()) {
+         return false;
+      }
+
+      String original = Files.readString(file.toPath());
+      String newContent = original.replace(find, replace);
+      if (!original.equals(newContent)) {
+         Files.writeString(file.toPath(), newContent);
+         return true;
+      } else {
+         return false;
+      }
+   }
+
 }
diff --git 
a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactory.java
 
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactory.java
index 73b82f252f..77791f59f1 100644
--- 
a/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactory.java
+++ 
b/artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/JDBCSequentialFileFactory.java
@@ -74,7 +74,12 @@ public class JDBCSequentialFileFactory implements 
SequentialFileFactory, ActiveM
       try {
          this.dbDriver = JDBCFileUtils.getDBFileDriver(connectionProvider, 
sqlProvider);
       } catch (SQLException e) {
-         criticalErrorListener.onIOException(e, "Failed to start JDBC Driver", 
null);
+         logger.warn(e.getMessage(), e);
+         if (criticalErrorListener != null) {
+            criticalErrorListener.onIOException(e, "Failed to start JDBC 
Driver", null);
+         } else {
+            throw new RuntimeException(e.getMessage(), e);
+         }
       }
    }
 
diff --git 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
index 9d129adb17..f971a271c6 100644
--- 
a/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
+++ 
b/artemis-server/src/main/java/org/apache/activemq/artemis/core/persistence/impl/journal/JDBCJournalStorageManager.java
@@ -98,7 +98,11 @@ public class JDBCJournalStorageManager extends 
JournalStorageManager {
          largeMessagesFactory.start();
       } catch (Exception e) {
          logger.warn(e.getMessage(), e);
-         criticalErrorListener.onIOException(e, e.getMessage(), null);
+         if (criticalErrorListener != null) {
+            criticalErrorListener.onIOException(e, e.getMessage(), null);
+         } else {
+            throw new RuntimeException(e.getMessage(), e);
+         }
       }
    }
 
diff --git 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
index 81858c11ef..b31981f018 100644
--- 
a/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
+++ 
b/tests/artemis-test-support/src/main/java/org/apache/activemq/artemis/utils/RealServerTestBase.java
@@ -31,7 +31,6 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.lang.invoke.MethodHandles;
 import java.net.MalformedURLException;
-import java.nio.file.Files;
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.Set;
@@ -62,22 +61,6 @@ public class RealServerTestBase extends ActiveMQTestBase {
 
    public static final String basedir = System.getProperty("basedir");
 
-   /**
-    * Search and replace strings on a file
-    *
-    * @param file file to be replaced
-    * @param find string expected to match
-    * @param replace string to be replaced
-    * @return true if the replacement was successful
-    * @throws Exception
-    */
-   public static boolean findReplace(File file, String find, String replace) 
throws Exception {
-      String original = Files.readString(file.toPath());
-      String newContent = original.replace(find, replace);
-      Files.writeString(file.toPath(), newContent);
-      return !original.equals(newContent);
-   }
-
    @After
    public void after() throws Exception {
       // close ServerLocators before killing the server otherwise they'll hang 
and delay test termination
diff --git 
a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jdbccli/JDBCExportWrongUserTest.java
 
b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jdbccli/JDBCExportWrongUserTest.java
new file mode 100644
index 0000000000..1bf8a0d184
--- /dev/null
+++ 
b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jdbccli/JDBCExportWrongUserTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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 regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.activemq.artemis.tests.smoke.jdbccli;
+
+import java.io.File;
+
+import org.apache.activemq.artemis.cli.commands.tools.DBOption;
+import org.apache.activemq.artemis.core.config.Configuration;
+import 
org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration;
+import org.apache.activemq.artemis.tests.smoke.common.SmokeTestBase;
+import org.apache.activemq.artemis.utils.FileUtil;
+import org.apache.activemq.artemis.utils.RandomUtil;
+import org.apache.activemq.artemis.utils.cli.helper.HelperCreate;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JDBCExportWrongUserTest extends SmokeTestBase {
+
+   @Test
+   public void testUserNameAndPasswordCaptured() throws Exception {
+
+      String serverConfigName = "JDBCExportWrongUserTest";
+
+      File server0Location = getFileServerLocation(serverConfigName);
+      deleteDirectory(server0Location);
+
+      runAfter(() -> deleteDirectory(server0Location));
+
+      HelperCreate cliCreateServer = new HelperCreate();
+      
cliCreateServer.setUser("admin").setPassword("admin").setAllowAnonymous(true).setNoWeb(true).setArtemisInstance(server0Location).addArgs("--jdbc",
 "--jdbc-connection-url", "fakeOne");
+      cliCreateServer.createServer();
+
+      File artemisInstance = getFileServerLocation(serverConfigName);
+
+      String user = RandomUtil.randomString();
+      String password = RandomUtil.randomString();
+
+      Assert.assertTrue(FileUtil.findReplace(new File(artemisInstance, 
"/etc/broker.xml"), "</database-store>", "   <jdbc-user>" + user + 
"</jdbc-user>\n" + "            <jdbc-password>" + password + 
"</jdbc-password>\n" + "         </database-store>"));
+
+      {
+         DBOption dbOption = new DBOption();
+         dbOption.setHomeValues(cliCreateServer.getArtemisHome(), 
artemisInstance, new File(artemisInstance, "/etc"));
+
+         Configuration configuration = dbOption.getParameterConfiguration();
+
+         Assert.assertEquals(user, ((DatabaseStorageConfiguration) 
configuration.getStoreConfiguration()).getJdbcUser());
+         Assert.assertEquals(password, ((DatabaseStorageConfiguration) 
configuration.getStoreConfiguration()).getJdbcPassword());
+         Assert.assertEquals(user, dbOption.getJdbcUser());
+         Assert.assertEquals(password, dbOption.getJdbcPassword());
+      }
+
+      {
+         DBOption dbOption = new DBOption();
+         dbOption.setHomeValues(cliCreateServer.getArtemisHome(), 
artemisInstance, new File(artemisInstance, "/etc"));
+         
dbOption.setJdbcUser("myNewUser").setJdbcPassword("myNewPassword").setJdbcClassName("myClass").setJdbcURL("myURL");
+         Configuration config = dbOption.getParameterConfiguration();
+         Assert.assertEquals("myNewUser", ((DatabaseStorageConfiguration) 
config.getStoreConfiguration()).getJdbcUser());
+         Assert.assertEquals("myNewPassword", ((DatabaseStorageConfiguration) 
config.getStoreConfiguration()).getJdbcPassword());
+         Assert.assertEquals("myURL", ((DatabaseStorageConfiguration) 
config.getStoreConfiguration()).getJdbcConnectionUrl());
+         Assert.assertEquals("myClass", ((DatabaseStorageConfiguration) 
config.getStoreConfiguration()).getJdbcDriverClassName());
+      }
+   }
+
+}
diff --git 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/ClusteredMirrorSoakTest.java
 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/ClusteredMirrorSoakTest.java
index fd92be3380..0964050f0c 100644
--- 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/ClusteredMirrorSoakTest.java
+++ 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/ClusteredMirrorSoakTest.java
@@ -44,6 +44,7 @@ import org.apache.activemq.artemis.tests.soak.SoakTestBase;
 import org.apache.activemq.artemis.tests.util.CFUtil;
 import org.apache.activemq.artemis.tests.util.RandomUtil;
 import org.apache.activemq.artemis.util.ServerUtil;
+import org.apache.activemq.artemis.utils.FileUtil;
 import org.apache.activemq.artemis.utils.Wait;
 import org.apache.activemq.artemis.utils.cli.helper.HelperCreate;
 import org.junit.Assert;
@@ -111,7 +112,7 @@ public class ClusteredMirrorSoakTest extends SoakTestBase {
       File brokerXml = new File(serverLocation, "/etc/broker.xml");
       Assert.assertTrue(brokerXml.exists());
       // Adding redistribution delay to broker configuration
-      Assert.assertTrue(findReplace(brokerXml, "<address-setting 
match=\"#\">", "<address-setting match=\"#\">\n\n" + "            
<redistribution-delay>0</redistribution-delay> <!-- added by 
ClusteredMirrorSoakTest.java --> \n"));
+      Assert.assertTrue(FileUtil.findReplace(brokerXml, "<address-setting 
match=\"#\">", "<address-setting match=\"#\">\n\n" + "            
<redistribution-delay>0</redistribution-delay> <!-- added by 
ClusteredMirrorSoakTest.java --> \n"));
    }
 
    @BeforeClass
diff --git 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/InterruptedLargeMessageTest.java
 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/InterruptedLargeMessageTest.java
index 88fa0098c0..2173258372 100644
--- 
a/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/InterruptedLargeMessageTest.java
+++ 
b/tests/soak-tests/src/test/java/org/apache/activemq/artemis/tests/soak/brokerConnection/mirror/InterruptedLargeMessageTest.java
@@ -41,6 +41,7 @@ import 
org.apache.activemq.artemis.core.server.impl.AddressInfo;
 import org.apache.activemq.artemis.tests.soak.SoakTestBase;
 import org.apache.activemq.artemis.tests.util.CFUtil;
 import org.apache.activemq.artemis.util.ServerUtil;
+import org.apache.activemq.artemis.utils.FileUtil;
 import org.apache.activemq.artemis.utils.Wait;
 import org.apache.activemq.artemis.utils.cli.helper.HelperCreate;
 import org.junit.Assert;
@@ -123,7 +124,7 @@ public class InterruptedLargeMessageTest extends 
SoakTestBase {
          insert = insertWriter.toString();
       }
 
-      Assert.assertTrue(findReplace(new File(getServerLocation(serverName), 
"./etc/broker.xml"), "</core>", insert));
+      Assert.assertTrue(FileUtil.findReplace(new 
File(getServerLocation(serverName), "./etc/broker.xml"), "</core>", insert));
    }
 
    @BeforeClass

Reply via email to