ARTEMIS-1473 fix default user/pass system props

This commit is contained in:
Justin Bertram 2017-10-25 12:45:17 -05:00
parent ac73f38d3d
commit c93a250635
2 changed files with 106 additions and 19 deletions

View File

@ -34,12 +34,19 @@ import java.security.PrivilegedAction;
*/
public class DefaultConnectionProperties {
public static final String DEFAULT_BROKER_HOST;
public static final int DEFAULT_BROKER_PORT;
public static final String DEFAULT_BROKER_BIND_URL;
public static final String DEFAULT_BROKER_URL;
public static final String DEFAULT_USER;
public static final String DEFAULT_PASSWORD;
public static final String AMQ_HOST = "AMQ_HOST";
public static final String AMQ_PORT = "AMQ_PORT";
public static final String AMQ_USER = "AMQ_USER";
public static final String AMQ_PASSWORD = "AMQ_PASSWORD";
public static final String BROKER_BIND_URL = "BROKER_BIND_URL";
public static final String PREFIX = "org.apache.activemq.";
public static String DEFAULT_BROKER_HOST;
public static int DEFAULT_BROKER_PORT;
public static String DEFAULT_BROKER_BIND_URL;
public static String DEFAULT_BROKER_URL;
public static String DEFAULT_USER;
public static String DEFAULT_PASSWORD;
static String getProperty(final String defaultValue, final String... propertyNames) {
return AccessController.doPrivileged(new PrivilegedAction<String>() {
@ -57,21 +64,20 @@ public class DefaultConnectionProperties {
}
static {
String host = getProperty("localhost", "AMQ_HOST", "org.apache.activemq.AMQ_HOST");
String port = getProperty("61616", "AMQ_PORT", "org.apache.activemq.AMQ_PORT");
initialize();
}
public static void initialize() {
String host = getProperty("localhost", AMQ_HOST, PREFIX + AMQ_HOST);
String port = getProperty("61616", AMQ_PORT, PREFIX + AMQ_PORT);
DEFAULT_BROKER_HOST = host;
DEFAULT_BROKER_PORT = Integer.parseInt(port);
String url = getProperty("tcp://" + host + ":" + port, "org.apache.activemq.BROKER_BIND_URL", "BROKER_BIND_URL");
DEFAULT_USER = getProperty(null, "AMQ_USER", "org.apache.activemq.AMQ_USER");
DEFAULT_PASSWORD = getProperty(null, "AMQ_PASSWORD", "org.apache.activemq.AMQ_PASSWORD");
if (DEFAULT_USER != null && DEFAULT_PASSWORD != null) {
url += "?user=" + DEFAULT_USER + "&password=" + DEFAULT_PASSWORD;
}
String url = getProperty("tcp://" + host + ":" + port, PREFIX + BROKER_BIND_URL, BROKER_BIND_URL);
DEFAULT_USER = getProperty(null, AMQ_USER, PREFIX + AMQ_USER);
DEFAULT_PASSWORD = getProperty(null, AMQ_PASSWORD, PREFIX + AMQ_PASSWORD);
DEFAULT_BROKER_BIND_URL = url;
// TODO: improve this once we implement failover:// as ActiveMQ5 does
DEFAULT_BROKER_URL = DEFAULT_BROKER_BIND_URL;
}
}

View File

@ -22,7 +22,12 @@ import javax.jms.IllegalStateException;
import javax.jms.JMSSecurityException;
import javax.jms.Session;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.DefaultConnectionProperties;
import org.apache.activemq.artemis.jms.tests.util.ProxyAssertSupport;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
/**
@ -31,10 +36,40 @@ import org.junit.Test;
* This test must be run with the Test security config. on the server
*/
public class SecurityTest extends JMSTestCase {
private String originalAmqUser;
private String originalAmqPassword;
private String originalBrokerBindUrl;
/**
* Login with no user, no password Should allow login (equivalent to guest)
*/
@Before
public void setupProperty() {
originalAmqUser = System.getProperty(DefaultConnectionProperties.AMQ_USER);
originalAmqPassword = System.getProperty(DefaultConnectionProperties.AMQ_PASSWORD);
originalBrokerBindUrl = System.getProperty(DefaultConnectionProperties.BROKER_BIND_URL);
}
@After
public void clearProperty() {
if (originalAmqUser == null) {
System.clearProperty(DefaultConnectionProperties.AMQ_USER);
} else {
System.setProperty(DefaultConnectionProperties.AMQ_USER, originalAmqUser);
}
if (originalAmqPassword == null) {
System.clearProperty(DefaultConnectionProperties.AMQ_PASSWORD);
} else {
System.setProperty(DefaultConnectionProperties.AMQ_PASSWORD, originalAmqPassword);
}
if (originalBrokerBindUrl == null) {
System.clearProperty(DefaultConnectionProperties.BROKER_BIND_URL);
} else {
System.setProperty(DefaultConnectionProperties.BROKER_BIND_URL, originalBrokerBindUrl);
}
}
/**
* Login with no user, no password Should allow login (equivalent to guest)
*/
@Test
public void testLoginNoUserNoPassword() throws Exception {
createConnection();
@ -60,6 +95,34 @@ public class SecurityTest extends JMSTestCase {
createConnection("guest", "guest");
}
/**
* Login with valid user and password
* Should allow
*/
@Test
public void testLoginValidUserAndPasswordSystemProperty() throws Exception {
System.setProperty(DefaultConnectionProperties.AMQ_USER, "guest");
System.setProperty(DefaultConnectionProperties.AMQ_PASSWORD, "guest");
DefaultConnectionProperties.initialize();
ConnectionFactory cf = new ActiveMQConnectionFactory();
Connection conn = addConnection(cf.createConnection());
}
/**
* Login with valid user and password
* Should allow
*/
@Test
public void testLoginValidUserAndPasswordSystemPropertyWithAdditionalProperties() throws Exception {
System.setProperty(DefaultConnectionProperties.AMQ_USER, "guest");
System.setProperty(DefaultConnectionProperties.AMQ_PASSWORD, "guest");
System.setProperty(DefaultConnectionProperties.BROKER_BIND_URL, "tcp://localhost:61616?compressLargeMessage=true");
DefaultConnectionProperties.initialize();
ConnectionFactory cf = new ActiveMQConnectionFactory();
Connection conn = addConnection(cf.createConnection());
Assert.assertTrue(((ActiveMQConnectionFactory) cf).isCompressLargeMessage());
}
/**
* Login with valid user and invalid password
* Should allow
@ -74,6 +137,24 @@ public class SecurityTest extends JMSTestCase {
}
}
/**
* Login with valid user and invalid password
* Should allow
*/
@Test
public void testLoginValidUserInvalidPasswordSystemProperty() throws Exception {
System.setProperty(DefaultConnectionProperties.AMQ_USER, "guest");
System.setProperty(DefaultConnectionProperties.AMQ_PASSWORD, "not.the.valid.password");
DefaultConnectionProperties.initialize();
try {
ConnectionFactory cf = new ActiveMQConnectionFactory();
Connection conn1 = addConnection(cf.createConnection());
ProxyAssertSupport.fail();
} catch (JMSSecurityException e) {
// Expected
}
}
/**
* Login with invalid user and invalid password
* Should allow