From 094f00750fafea213c176f5d10a1a0a0dad9ede5 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 26 Sep 2006 13:32:52 +0000 Subject: [PATCH] applied patch from Kelly Campbell for AMQ-940 wth thanks git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@450044 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/broker/ConnectionContext.java | 7 ++- .../activemq/openwire/OpenWireFormat.java | 2 +- .../security/SecurityTestSupport.java | 56 +++++++++++++++++-- .../SimpleSecurityBrokerSystemTest.java | 6 +- .../apache/activemq/security/jaas-broker.xml | 2 +- 5 files changed, 63 insertions(+), 10 deletions(-) diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/ConnectionContext.java b/activemq-core/src/main/java/org/apache/activemq/broker/ConnectionContext.java index 485742dda3..9fdfd6ea2b 100755 --- a/activemq-core/src/main/java/org/apache/activemq/broker/ConnectionContext.java +++ b/activemq-core/src/main/java/org/apache/activemq/broker/ConnectionContext.java @@ -72,6 +72,11 @@ public class ConnectionContext { public void setSecurityContext(SecurityContext subject) { this.securityContext = subject; + if (subject != null) { + setUserName(subject.getUserName()); + } else { + setUserName(null); + } } /** @@ -202,7 +207,7 @@ public class ConnectionContext { return userName; } - public void setUserName(String userName) { + protected void setUserName(String userName) { this.userName = userName; } diff --git a/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java b/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java index d4f3b394d2..faaa357f4e 100755 --- a/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java +++ b/activemq-core/src/main/java/org/apache/activemq/openwire/OpenWireFormat.java @@ -46,7 +46,7 @@ final public class OpenWireFormat implements WireFormat { private static final int MARSHAL_CACHE_PREFERED_SIZE = MARSHAL_CACHE_SIZE-100; private DataStreamMarshaller dataMarshallers[]; - private int version; + private int version = 2; private boolean stackTraceEnabled=false; private boolean tcpNoDelayEnabled=false; private boolean cacheEnabled=false; diff --git a/activemq-core/src/test/java/org/apache/activemq/security/SecurityTestSupport.java b/activemq-core/src/test/java/org/apache/activemq/security/SecurityTestSupport.java index aae87d9d19..3b3cd2142a 100644 --- a/activemq-core/src/test/java/org/apache/activemq/security/SecurityTestSupport.java +++ b/activemq-core/src/test/java/org/apache/activemq/security/SecurityTestSupport.java @@ -17,8 +17,11 @@ */ package org.apache.activemq.security; +import org.apache.activemq.CombinationTestSupport; import org.apache.activemq.JmsTestSupport; +import org.apache.activemq.broker.BrokerService; import org.apache.activemq.command.ActiveMQDestination; +import org.apache.activemq.command.ActiveMQMessage; import org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; @@ -37,6 +40,15 @@ public class SecurityTestSupport extends JmsTestSupport { public ActiveMQDestination destination; + /** + * Overrides to set the JMSXUserID flag to true. + */ + protected BrokerService createBroker() throws Exception { + BrokerService broker = super.createBroker(); + broker.setPopulateJMSXUserID(true); + return broker; + } + public void testUserReceiveFails() throws JMSException { doReceive(true); } @@ -74,7 +86,9 @@ public class SecurityTestSupport extends JmsTestSupport { } public void testUserReceiveSucceeds() throws JMSException { - doReceive(false); + Message m = doReceive(false); + assertEquals("system", ((ActiveMQMessage)m).getUserID()); + assertEquals("system", m.getStringProperty("JMSXUserID")); } public void testGuestReceiveSucceeds() throws JMSException { @@ -86,7 +100,9 @@ public class SecurityTestSupport extends JmsTestSupport { } public void testUserSendSucceeds() throws JMSException { - doSend(false); + Message m = doSend(false); + assertEquals("user", ((ActiveMQMessage)m).getUserID()); + assertEquals("user", m.getStringProperty("JMSXUserID")); } public void testUserSendFails() throws JMSException { @@ -104,7 +120,7 @@ public class SecurityTestSupport extends JmsTestSupport { /** * @throws JMSException */ - public void doSend(boolean fail) throws JMSException { + public Message doSend(boolean fail) throws JMSException { Connection adminConnection = factory.createConnection("system", "manager"); connections.add(adminConnection); @@ -134,13 +150,13 @@ public class SecurityTestSupport extends JmsTestSupport { assertEquals("0", ((TextMessage) m).getText()); assertNull(consumer.receiveNoWait()); } - + return m; } /** * @throws JMSException */ - public void doReceive(boolean fail) throws JMSException { + public Message doReceive(boolean fail) throws JMSException { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); @@ -152,7 +168,7 @@ public class SecurityTestSupport extends JmsTestSupport { } catch (JMSException e) { if (fail && e.getCause() instanceof SecurityException) - return; + return null; throw e; } @@ -165,9 +181,13 @@ public class SecurityTestSupport extends JmsTestSupport { assertNotNull(m); assertEquals("0", ((TextMessage) m).getText()); assertNull(consumer.receiveNoWait()); + return m; } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestUserReceiveFails() { addCombinationValues("userName", new Object[] { "user" }); addCombinationValues("password", new Object[] { "password" }); @@ -175,23 +195,35 @@ public class SecurityTestSupport extends JmsTestSupport { new ActiveMQTopic("GUEST.BAR"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestInvalidAuthentication() { addCombinationValues("userName", new Object[] { "user" }); addCombinationValues("password", new Object[] { "password" }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestUserReceiveSucceeds() { addCombinationValues("userName", new Object[] { "user" }); addCombinationValues("password", new Object[] { "password" }); addCombinationValues("destination", new Object[] { new ActiveMQQueue("USERS.FOO"), new ActiveMQTopic("USERS.FOO"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestGuestReceiveSucceeds() { addCombinationValues("userName", new Object[] { "guest" }); addCombinationValues("password", new Object[] { "password" }); addCombinationValues("destination", new Object[] { new ActiveMQQueue("GUEST.BAR"), new ActiveMQTopic("GUEST.BAR"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestGuestReceiveFails() { addCombinationValues("userName", new Object[] { "guest" }); addCombinationValues("password", new Object[] { "password" }); @@ -199,6 +231,9 @@ public class SecurityTestSupport extends JmsTestSupport { new ActiveMQTopic("USERS.FOO"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestUserSendSucceeds() { addCombinationValues("userName", new Object[] { "user" }); addCombinationValues("password", new Object[] { "password" }); @@ -206,12 +241,18 @@ public class SecurityTestSupport extends JmsTestSupport { new ActiveMQTopic("GUEST.BAR"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestUserSendFails() { addCombinationValues("userName", new Object[] { "user" }); addCombinationValues("password", new Object[] { "password" }); addCombinationValues("destination", new Object[] { new ActiveMQQueue("TEST"), new ActiveMQTopic("TEST"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestGuestSendFails() { addCombinationValues("userName", new Object[] { "guest" }); addCombinationValues("password", new Object[] { "password" }); @@ -219,6 +260,9 @@ public class SecurityTestSupport extends JmsTestSupport { new ActiveMQTopic("USERS.FOO"), }); } + /** + * @see {@link CombinationTestSupport} + */ public void initCombosForTestGuestSendSucceeds() { addCombinationValues("userName", new Object[] { "guest" }); addCombinationValues("password", new Object[] { "password" }); diff --git a/activemq-core/src/test/java/org/apache/activemq/security/SimpleSecurityBrokerSystemTest.java b/activemq-core/src/test/java/org/apache/activemq/security/SimpleSecurityBrokerSystemTest.java index 05d36cc429..82245cbf97 100644 --- a/activemq-core/src/test/java/org/apache/activemq/security/SimpleSecurityBrokerSystemTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/security/SimpleSecurityBrokerSystemTest.java @@ -17,6 +17,7 @@ */ package org.apache.activemq.security; +import org.apache.activemq.CombinationTestSupport; import org.apache.activemq.broker.Broker; import org.apache.activemq.broker.BrokerPlugin; import org.apache.activemq.broker.BrokerService; @@ -124,13 +125,16 @@ public class SimpleSecurityBrokerSystemTest extends SecurityTestSupport { } } + /** + * @see {@link CombinationTestSupport} + */ public void initCombos() { addCombinationValues("authorizationPlugin", new Object[] { new AuthorizationPlugin(createAuthorizationMap()), }); addCombinationValues("authenticationPlugin", new Object[] { new SimpleAuthenticationFactory(), new JaasAuthenticationPlugin(), }); } protected BrokerService createBroker() throws Exception { - BrokerService broker = new BrokerService(); + BrokerService broker = super.createBroker(); broker.setPlugins(new BrokerPlugin[] { authorizationPlugin, authenticationPlugin }); broker.setPersistent(false); return broker; diff --git a/activemq-core/src/test/resources/org/apache/activemq/security/jaas-broker.xml b/activemq-core/src/test/resources/org/apache/activemq/security/jaas-broker.xml index 67c5c5cfb4..57a3720e21 100644 --- a/activemq-core/src/test/resources/org/apache/activemq/security/jaas-broker.xml +++ b/activemq-core/src/test/resources/org/apache/activemq/security/jaas-broker.xml @@ -21,7 +21,7 @@ - +