Use the context of the subscription when the producer adds a destination
that matches its wildcard.
This commit is contained in:
Timothy Bish 2015-07-02 11:36:53 -04:00
parent 0c72e5d7dc
commit e4af2eb635
2 changed files with 162 additions and 5 deletions

View File

@ -28,10 +28,10 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import javax.jms.IllegalStateException; import javax.jms.IllegalStateException;
import javax.jms.JMSException; import javax.jms.JMSException;
import org.apache.activemq.DestinationDoesNotExistException;
import org.apache.activemq.advisory.AdvisorySupport; import org.apache.activemq.advisory.AdvisorySupport;
import org.apache.activemq.broker.ConnectionContext; import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.ConsumerBrokerExchange; import org.apache.activemq.broker.ConsumerBrokerExchange;
import org.apache.activemq.DestinationDoesNotExistException;
import org.apache.activemq.broker.ProducerBrokerExchange; import org.apache.activemq.broker.ProducerBrokerExchange;
import org.apache.activemq.broker.region.policy.PolicyEntry; import org.apache.activemq.broker.region.policy.PolicyEntry;
import org.apache.activemq.broker.region.virtual.CompositeDestinationFilter; import org.apache.activemq.broker.region.virtual.CompositeDestinationFilter;
@ -90,6 +90,7 @@ public abstract class AbstractRegion implements Region {
this.destinationFactory = destinationFactory; this.destinationFactory = destinationFactory;
} }
@Override
public final void start() throws Exception { public final void start() throws Exception {
started = true; started = true;
@ -113,6 +114,7 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public void stop() throws Exception { public void stop() throws Exception {
started = false; started = false;
destinationsLock.readLock().lock(); destinationsLock.readLock().lock();
@ -136,6 +138,7 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public Destination addDestination(ConnectionContext context, ActiveMQDestination destination, public Destination addDestination(ConnectionContext context, ActiveMQDestination destination,
boolean createIfTemporary) throws Exception { boolean createIfTemporary) throws Exception {
@ -230,16 +233,15 @@ public abstract class AbstractRegion implements Region {
} }
} }
protected List<Subscription> addSubscriptionsForDestination(ConnectionContext context, Destination dest) protected List<Subscription> addSubscriptionsForDestination(ConnectionContext context, Destination dest) throws Exception {
throws Exception {
List<Subscription> rc = new ArrayList<Subscription>(); List<Subscription> rc = new ArrayList<Subscription>();
// Add all consumers that are interested in the destination. // Add all consumers that are interested in the destination.
for (Iterator<Subscription> iter = subscriptions.values().iterator(); iter.hasNext();) { for (Iterator<Subscription> iter = subscriptions.values().iterator(); iter.hasNext();) {
Subscription sub = iter.next(); Subscription sub = iter.next();
if (sub.matches(dest.getActiveMQDestination())) { if (sub.matches(dest.getActiveMQDestination())) {
try { try {
dest.addSubscription(context, sub); ConnectionContext originalContext = sub.getContext() != null ? sub.getContext() : context;
dest.addSubscription(originalContext, sub);
rc.add(sub); rc.add(sub);
} catch (SecurityException e) { } catch (SecurityException e) {
if (sub.isWildcard()) { if (sub.isWildcard()) {
@ -255,6 +257,7 @@ public abstract class AbstractRegion implements Region {
} }
@Override
public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout) public void removeDestination(ConnectionContext context, ActiveMQDestination destination, long timeout)
throws Exception { throws Exception {
@ -313,6 +316,7 @@ public abstract class AbstractRegion implements Region {
* *
* @return a set of matching destination objects. * @return a set of matching destination objects.
*/ */
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Set<Destination> getDestinations(ActiveMQDestination destination) { public Set<Destination> getDestinations(ActiveMQDestination destination) {
destinationsLock.readLock().lock(); destinationsLock.readLock().lock();
@ -323,10 +327,12 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public Map<ActiveMQDestination, Destination> getDestinationMap() { public Map<ActiveMQDestination, Destination> getDestinationMap() {
return destinations; return destinations;
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception { public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
LOG.debug("{} adding consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() }); LOG.debug("{} adding consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() });
@ -446,6 +452,7 @@ public abstract class AbstractRegion implements Region {
return inactiveDests; return inactiveDests;
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception { public void removeConsumer(ConnectionContext context, ConsumerInfo info) throws Exception {
LOG.debug("{} removing consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() }); LOG.debug("{} removing consumer: {} for destination: {}", new Object[]{ broker.getBrokerName(), info.getConsumerId(), info.getDestination() });
@ -479,10 +486,12 @@ public abstract class AbstractRegion implements Region {
sub.destroy(); sub.destroy();
} }
@Override
public void removeSubscription(ConnectionContext context, RemoveSubscriptionInfo info) throws Exception { public void removeSubscription(ConnectionContext context, RemoveSubscriptionInfo info) throws Exception {
throw new JMSException("Invalid operation."); throw new JMSException("Invalid operation.");
} }
@Override
public void send(final ProducerBrokerExchange producerExchange, Message messageSend) throws Exception { public void send(final ProducerBrokerExchange producerExchange, Message messageSend) throws Exception {
final ConnectionContext context = producerExchange.getConnectionContext(); final ConnectionContext context = producerExchange.getConnectionContext();
@ -498,6 +507,7 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public void acknowledge(ConsumerBrokerExchange consumerExchange, MessageAck ack) throws Exception { public void acknowledge(ConsumerBrokerExchange consumerExchange, MessageAck ack) throws Exception {
Subscription sub = consumerExchange.getSubscription(); Subscription sub = consumerExchange.getSubscription();
if (sub == null) { if (sub == null) {
@ -516,6 +526,7 @@ public abstract class AbstractRegion implements Region {
sub.acknowledge(consumerExchange.getConnectionContext(), ack); sub.acknowledge(consumerExchange.getConnectionContext(), ack);
} }
@Override
public Response messagePull(ConnectionContext context, MessagePull pull) throws Exception { public Response messagePull(ConnectionContext context, MessagePull pull) throws Exception {
Subscription sub = subscriptions.get(pull.getConsumerId()); Subscription sub = subscriptions.get(pull.getConsumerId());
if (sub == null) { if (sub == null) {
@ -557,6 +568,7 @@ public abstract class AbstractRegion implements Region {
return dest; return dest;
} }
@Override
public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception { public void processDispatchNotification(MessageDispatchNotification messageDispatchNotification) throws Exception {
Subscription sub = subscriptions.get(messageDispatchNotification.getConsumerId()); Subscription sub = subscriptions.get(messageDispatchNotification.getConsumerId());
if (sub != null) { if (sub != null) {
@ -594,6 +606,7 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public void gc() { public void gc() {
for (Subscription sub : subscriptions.values()) { for (Subscription sub : subscriptions.values()) {
sub.gc(); sub.gc();
@ -624,6 +637,7 @@ public abstract class AbstractRegion implements Region {
this.autoCreateDestinations = autoCreateDestinations; this.autoCreateDestinations = autoCreateDestinations;
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception { public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception {
destinationsLock.readLock().lock(); destinationsLock.readLock().lock();
@ -644,6 +658,7 @@ public abstract class AbstractRegion implements Region {
* @throws Exception * @throws Exception
* TODO * TODO
*/ */
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception { public void removeProducer(ConnectionContext context, ProducerInfo info) throws Exception {
destinationsLock.readLock().lock(); destinationsLock.readLock().lock();
@ -662,6 +677,7 @@ public abstract class AbstractRegion implements Region {
destinationFactory.removeDestination(dest); destinationFactory.removeDestination(dest);
} }
@Override
public void processConsumerControl(ConsumerBrokerExchange consumerExchange, ConsumerControl control) { public void processConsumerControl(ConsumerBrokerExchange consumerExchange, ConsumerControl control) {
Subscription sub = subscriptions.get(control.getConsumerId()); Subscription sub = subscriptions.get(control.getConsumerId());
if (sub != null && sub instanceof AbstractSubscription) { if (sub != null && sub instanceof AbstractSubscription) {
@ -681,6 +697,7 @@ public abstract class AbstractRegion implements Region {
} }
} }
@Override
public void reapplyInterceptor() { public void reapplyInterceptor() {
destinationsLock.writeLock().lock(); destinationsLock.writeLock().lock();
try { try {

View File

@ -0,0 +1,140 @@
/**
* 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.bugs;
import static org.junit.Assert.assertNotNull;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerPlugin;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.TransportConnector;
import org.apache.activemq.filter.DestinationMapEntry;
import org.apache.activemq.security.AuthenticationUser;
import org.apache.activemq.security.AuthorizationEntry;
import org.apache.activemq.security.AuthorizationPlugin;
import org.apache.activemq.security.DefaultAuthorizationMap;
import org.apache.activemq.security.SimpleAuthenticationPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class AMQ5814Test {
private BrokerService brokerService;
private String openwireClientUrl;
public BrokerPlugin configureAuthentication() throws Exception {
List<AuthenticationUser> users = new ArrayList<>();
users.add(new AuthenticationUser("publisher", "123", "publisher"));
users.add(new AuthenticationUser("subscriber", "123", "subscriber"));
users.add(new AuthenticationUser("admin", "123", "publisher,subscriber"));
SimpleAuthenticationPlugin authenticationPlugin = new SimpleAuthenticationPlugin(users);
return authenticationPlugin;
}
public BrokerPlugin configureAuthorization() throws Exception {
@SuppressWarnings("rawtypes")
List<DestinationMapEntry> authorizationEntries = new ArrayList<>();
AuthorizationEntry entry = new AuthorizationEntry();
entry.setTopic("dcu.>");
entry.setRead("subscriber");
entry.setWrite("publisher");
entry.setAdmin("publisher,subscriber");
authorizationEntries.add(entry);
entry = new AuthorizationEntry();
entry.setTopic("ActiveMQ.Advisory.>");
entry.setRead("publisher,subscriber");
entry.setWrite("publisher,subscriber");
entry.setAdmin("publisher,subscriber");
authorizationEntries.add(entry);
DefaultAuthorizationMap authorizationMap = new DefaultAuthorizationMap(authorizationEntries);
AuthorizationPlugin authorizationPlugin = new AuthorizationPlugin(authorizationMap);
return authorizationPlugin;
}
@Before
public void setup() throws Exception {
TransportConnector openwireConnector = new TransportConnector();
openwireConnector.setUri(new URI("tcp://localhost:0"));
openwireConnector.setName("openwire");
ArrayList<BrokerPlugin> plugins = new ArrayList<>();
plugins.add(configureAuthentication());
plugins.add(configureAuthorization());
brokerService = new BrokerService();
brokerService.setPersistent(false);
brokerService.addConnector(openwireConnector);
if (!plugins.isEmpty()) {
BrokerPlugin[] array = new BrokerPlugin[plugins.size()];
brokerService.setPlugins(plugins.toArray(array));
}
brokerService.start();
brokerService.waitUntilStarted();
openwireClientUrl = openwireConnector.getPublishableConnectString();
}
@After
public void shutdown() throws Exception {
if (brokerService != null) {
brokerService.stop();
brokerService.waitUntilStopped();
brokerService = null;
}
}
@Test(timeout=30000)
public void testProduceConsumeWithAuthorization() throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory(openwireClientUrl);
Connection connection1 = factory.createConnection("subscriber", "123");
Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic wildCarded = session1.createTopic("dcu.>");
MessageConsumer consumer = session1.createConsumer(wildCarded);
connection1.start();
Connection connection2 = factory.createConnection("publisher", "123");
Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic named = session2.createTopic("dcu.id");
MessageProducer producer = session2.createProducer(named);
producer.send(session2.createTextMessage("test"));
assertNotNull(consumer.receive(2000));
connection1.close();
connection2.close();
}
}