ARTEMIS-4678 JDBC User and password not used by CLI

This commit is contained in:
Clebert Suconic 2024-03-07 23:06:28 -05:00 committed by clebertsuconic
parent 063968bb4f
commit 21368cf741
8 changed files with 160 additions and 23 deletions

View File

@ -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());

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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());
}
}
}

View File

@ -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

View File

@ -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