This is an automated email from the ASF dual-hosted git repository. cshannon pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/activemq.git
The following commit(s) were added to refs/heads/main by this push: new 49f1b2c [AMQ-8053] UserIDBroker unit tests new fb03697 Merge pull request #758 from mattrpav/AMQ-8053b 49f1b2c is described below commit 49f1b2c90349db9a365fb750eb2f65133ac30765 Author: Matt Pavlovich <m...@hyte.io> AuthorDate: Sun Feb 6 12:46:44 2022 -0600 [AMQ-8053] UserIDBroker unit tests --- .../network/UserIDBrokerPopulateFalseTest.java | 41 ++++++ .../network/UserIDBrokerPopulateMixedTest.java | 41 ++++++ .../apache/activemq/network/UserIDBrokerTest.java | 150 +++++++++++++++++++++ .../apache/activemq/network/localBroker-authn.xml | 58 ++++++++ .../apache/activemq/network/remoteBroker-authn.xml | 45 +++++++ 5 files changed, 335 insertions(+) diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateFalseTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateFalseTest.java new file mode 100644 index 0000000..4b750ff --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateFalseTest.java @@ -0,0 +1,41 @@ +/** + * 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.network; + +import javax.jms.JMSException; +import org.junit.Test; + +public class UserIDBrokerPopulateFalseTest extends UserIDBrokerTest { + + @Override + protected void doSetUp(boolean deleteAllMessages) throws Exception { + doSetUp(deleteAllMessages, false, false); + } + + @Test + @Override + public void testPopulateJMSXUserIdLocalAndNetwork() throws JMSException { + + sendTextMessage(localConnection, "exclude.test.foo","This local message is JMSXUserID=null"); + verifyTextMessage(localConnection, "exclude.test.foo", "This local message is JMSXUserID=null", "JMSXUserID", null, false); + + // Across the network + sendTextMessage(localConnection, "include.test.foo", "This network message is JMSXUserID=null"); + verifyTextMessage(remoteConnection, "include.test.foo", "This network message is JMSXUserID=null", "JMSXUserID", null, false); + } + +} diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateMixedTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateMixedTest.java new file mode 100644 index 0000000..5498028 --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerPopulateMixedTest.java @@ -0,0 +1,41 @@ +/** + * 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.network; + +import javax.jms.JMSException; +import org.junit.Test; + +public class UserIDBrokerPopulateMixedTest extends UserIDBrokerTest { + + @Override + protected void doSetUp(boolean deleteAllMessages) throws Exception { + doSetUp(deleteAllMessages, false, true); + } + + @Test + @Override + public void testPopulateJMSXUserIdLocalAndNetwork() throws JMSException { + + sendTextMessage(localConnection, "exclude.test.foo","This local message is JMSXUserID=null"); + verifyTextMessage(localConnection, "exclude.test.foo", "This local message is JMSXUserID=null", "JMSXUserID", null, false); + + // Across the network + sendTextMessage(localConnection, "include.test.foo", "This network message is sent as JMSXUserID=null"); + verifyTextMessage(remoteConnection, "include.test.foo", "This network message is sent as JMSXUserID=null", "JMSXUserID", "userB", true); + } + +} diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerTest.java new file mode 100644 index 0000000..550eaeb --- /dev/null +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/network/UserIDBrokerTest.java @@ -0,0 +1,150 @@ +/** + * 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.network; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.net.URI; + +import javax.jms.Connection; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageConsumer; +import javax.jms.MessageProducer; +import javax.jms.Session; +import javax.jms.TextMessage; + +import org.apache.activemq.ActiveMQConnectionFactory; +import org.junit.Test; + +public class UserIDBrokerTest extends BaseNetworkTest { + + @Override + protected void doSetUp(boolean deleteAllMessages) throws Exception { + doSetUp(deleteAllMessages, true, true); + } + + protected void doSetUp(boolean deleteAllMessages, boolean populateJMSXUserIDLocalBroker, boolean populateJMSXUserIDRemoteBroker) throws Exception { + remoteBroker = createRemoteBroker(); + remoteBroker.setDeleteAllMessagesOnStartup(deleteAllMessages); + + if(populateJMSXUserIDRemoteBroker) { + remoteBroker.setPopulateJMSXUserID(true); + remoteBroker.setUseAuthenticatedPrincipalForJMSXUserID(true); + } + + remoteBroker.start(); + remoteBroker.waitUntilStarted(); + localBroker = createLocalBroker(); + localBroker.setDeleteAllMessagesOnStartup(deleteAllMessages); + + if(populateJMSXUserIDLocalBroker) { + localBroker.setPopulateJMSXUserID(true); + localBroker.setUseAuthenticatedPrincipalForJMSXUserID(true); + } + + localBroker.start(); + localBroker.waitUntilStarted(); + URI localURI = localBroker.getVmConnectorURI(); + ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(localURI); + fac.setAlwaysSyncSend(true); + fac.setDispatchAsync(false); + localConnection = fac.createConnection("userA", "passwordA"); + localConnection.setClientID("clientId"); + localConnection.start(); + URI remoteURI = remoteBroker.getVmConnectorURI(); + fac = new ActiveMQConnectionFactory(remoteURI); + remoteConnection = fac.createConnection("userB", "passwordB"); + remoteConnection.setClientID("clientId"); + remoteConnection.start(); + localSession = localConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + remoteSession = remoteConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); + } + + @Override + protected String getRemoteBrokerURI() { + return "org/apache/activemq/network/remoteBroker-authn.xml"; + } + + @Override + protected String getLocalBrokerURI() { + return "org/apache/activemq/network/localBroker-authn.xml"; + } + + @Test + public void testPopulateJMSXUserIdLocalAndNetwork() throws JMSException { + + sendTextMessage(localConnection, "exclude.test.foo","This local message is JMSXUserID=userA"); + verifyTextMessage(localConnection, "exclude.test.foo", "This local message is JMSXUserID=userA", "JMSXUserID", "userA", true); + + // Across the network + sendTextMessage(localConnection, "include.test.foo", "This network message is JMSXUserID=userA"); + verifyTextMessage(remoteConnection, "include.test.foo", "This network message is JMSXUserID=userA", "JMSXUserID", "userA", true); + } + + protected void sendTextMessage(Connection connection, String queueName, String textBody) throws JMSException { + Session tmpSession = null; + MessageProducer tmpProducer = null; + try { + tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + MessageProducer tmpLocalProducer = tmpSession.createProducer(tmpSession.createQueue(queueName)); + tmpLocalProducer.send(tmpSession.createTextMessage(textBody)); + } finally { + if(tmpProducer != null) { tmpProducer.close(); } + if(tmpSession != null) { tmpSession.close(); } + } + } + + protected void verifyTextMessage(Connection connection, String queueName, String body, String property, String value, boolean propertyExists) throws JMSException { + int maxLoops = 50; + int loopCount = 0; + boolean found = false; + + Session tmpSession = null; + MessageConsumer tmpConsumer = null; + + try { + tmpSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + tmpConsumer = tmpSession.createConsumer(tmpSession.createQueue(queueName)); + do { + Message tmpMessage = tmpConsumer.receive(100l); + if(tmpMessage != null) { + assert(TextMessage.class.isAssignableFrom(tmpMessage.getClass())); + TextMessage tmpTextMessage = TextMessage.class.cast(tmpMessage); + assertEquals(body, tmpTextMessage.getText()); + + if(propertyExists) { + assertNotNull(tmpTextMessage.getStringProperty(property)); + assertEquals(value, tmpTextMessage.getStringProperty(property)); + } else { + assertNull(tmpTextMessage.getStringProperty(property)); + } + + found = true; + } + loopCount++; + } while(!found && loopCount < maxLoops); + assertTrue(found); + } finally { + if(tmpConsumer != null) { tmpConsumer.close(); } + if(tmpSession != null) { tmpSession.close(); } + } + } +} diff --git a/activemq-unit-tests/src/test/resources/org/apache/activemq/network/localBroker-authn.xml b/activemq-unit-tests/src/test/resources/org/apache/activemq/network/localBroker-authn.xml new file mode 100644 index 0000000..67f45f4 --- /dev/null +++ b/activemq-unit-tests/src/test/resources/org/apache/activemq/network/localBroker-authn.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans + xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + +<broker brokerName="localBroker" start="false" persistent="true" useShutdownHook="false" monitorConnectionSplits="true" xmlns="http://activemq.apache.org/schema/core"> + <networkConnectors> + <networkConnector uri="static:(tcp://localhost:61617)" + dynamicOnly = "false" + conduitSubscriptions = "true" + decreaseNetworkConsumerPriority = "false" + name="networkConnector" + userName="userB" + password="passwordB"> + <dynamicallyIncludedDestinations> + <queue physicalName="include.test.foo"/> + <topic physicalName="include.test.bar"/> + </dynamicallyIncludedDestinations> + <excludedDestinations> + <queue physicalName="exclude.test.foo"/> + <topic physicalName="exclude.test.bar"/> + </excludedDestinations> + </networkConnector> + </networkConnectors> + <plugins> + <simpleAuthenticationPlugin> + <users> + <authenticationUser username="userA" password="passwordA" groups="groupA"/> + <authenticationUser username="userB" password="passwordB" groups="groupB"/> + </users> + </simpleAuthenticationPlugin> + </plugins> + <transportConnectors> + <transportConnector uri="tcp://localhost:61616"/> + </transportConnectors> + </broker> +</beans> + diff --git a/activemq-unit-tests/src/test/resources/org/apache/activemq/network/remoteBroker-authn.xml b/activemq-unit-tests/src/test/resources/org/apache/activemq/network/remoteBroker-authn.xml new file mode 100644 index 0000000..6b22c3f --- /dev/null +++ b/activemq-unit-tests/src/test/resources/org/apache/activemq/network/remoteBroker-authn.xml @@ -0,0 +1,45 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<beans + xmlns="http://www.springframework.org/schema/beans" + xmlns:amq="http://activemq.apache.org/schema/core" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> + + <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> + + <broker brokerName="remoteBroker" start="false" useJmx="false" persistent="true" useShutdownHook="false" monitorConnectionSplits="false" xmlns="http://activemq.apache.org/schema/core"> + <networkConnectors> + <networkConnector uri="static:(tcp://localhost:61616)" userName="userB" password="passwordB"/> + </networkConnectors> + <plugins> + <simpleAuthenticationPlugin> + <users> + <authenticationUser username="userA" password="passwordA" groups="groupA"/> + <authenticationUser username="userB" password="passwordB" groups="groupB"/> + </users> + </simpleAuthenticationPlugin> + </plugins> + <transportConnectors> + <transportConnector uri="tcp://localhost:61617"/> + </transportConnectors> + </broker> + +</beans> +