Search in sources :

Example 1 with AMQPConnection

use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.

the class ProtocolEngine_1_0_0Test method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    _networkConnection = mock(ServerNetworkConnection.class);
    when(_networkConnection.getLocalAddress()).thenReturn(new InetSocketAddress(0));
    _broker = mock(Broker.class);
    when(_broker.getModel()).thenReturn(BrokerModel.getInstance());
    when(_broker.getNetworkBufferSize()).thenReturn(256 * 1026);
    final TaskExecutor taskExecutor = new TaskExecutorImpl();
    taskExecutor.start();
    when(_broker.getChildExecutor()).thenReturn(taskExecutor);
    when(_broker.getTaskExecutor()).thenReturn(taskExecutor);
    when(_broker.getId()).thenReturn(UUID.randomUUID());
    when(_broker.getEventLogger()).thenReturn(new EventLogger());
    when(((Broker) _broker).getCategoryClass()).thenReturn(Broker.class);
    _port = mock(AmqpPort.class);
    when(_port.getChildExecutor()).thenReturn(taskExecutor);
    when(_port.getCategoryClass()).thenReturn(Port.class);
    when(_port.getModel()).thenReturn(BrokerModel.getInstance());
    final SubjectCreator subjectCreator = mock(SubjectCreator.class);
    _authenticationProvider = mock(AuthenticationProvider.class);
    when(_port.getAuthenticationProvider()).thenReturn(_authenticationProvider);
    _virtualHost = mock(VirtualHost.class);
    when(_virtualHost.getChildExecutor()).thenReturn(taskExecutor);
    when(_virtualHost.getModel()).thenReturn(BrokerModel.getInstance());
    when(_virtualHost.getState()).thenReturn(State.ACTIVE);
    when(_virtualHost.isActive()).thenReturn(true);
    final ArgumentCaptor<AMQPConnection> connectionCaptor = ArgumentCaptor.forClass(AMQPConnection.class);
    final ArgumentCaptor<ConnectionEstablishmentPolicy> establishmentPolicyCaptor = ArgumentCaptor.forClass(ConnectionEstablishmentPolicy.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            _connection = connectionCaptor.getValue();
            return null;
        }
    }).when(_virtualHost).registerConnection(connectionCaptor.capture(), establishmentPolicyCaptor.capture());
    when(_virtualHost.getPrincipal()).thenReturn(mock(VirtualHostPrincipal.class));
    when(_port.getAddressSpace(anyString())).thenReturn(_virtualHost);
    when(_port.getSubjectCreator(anyBoolean(), anyString())).thenReturn(subjectCreator);
    final ArgumentCaptor<Principal> userCaptor = ArgumentCaptor.forClass(Principal.class);
    when(subjectCreator.createSubjectWithGroups(userCaptor.capture())).then(new Answer<Subject>() {

        @Override
        public Subject answer(final InvocationOnMock invocation) throws Throwable {
            Subject subject = new Subject();
            subject.getPrincipals().add(userCaptor.getValue());
            return subject;
        }
    });
    final ByteBufferSender sender = mock(ByteBufferSender.class);
    when(_networkConnection.getSender()).thenReturn(sender);
    AMQPDescribedTypeRegistry registry = AMQPDescribedTypeRegistry.newInstance().registerTransportLayer().registerMessagingLayer().registerTransactionLayer().registerSecurityLayer();
    _frameWriter = new FrameWriter(registry, new ByteBufferSender() {

        @Override
        public boolean isDirectBufferPreferred() {
            return false;
        }

        @Override
        public void send(final QpidByteBuffer msg) {
            _protocolEngine_1_0_0.received(msg);
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() {
        }
    });
}
Also used : ByteBufferSender(org.apache.qpid.server.transport.ByteBufferSender) InetSocketAddress(java.net.InetSocketAddress) VirtualHostPrincipal(org.apache.qpid.server.virtualhost.VirtualHostPrincipal) AmqpPort(org.apache.qpid.server.model.port.AmqpPort) ServerNetworkConnection(org.apache.qpid.server.transport.ServerNetworkConnection) TaskExecutorImpl(org.apache.qpid.server.configuration.updater.TaskExecutorImpl) AMQPDescribedTypeRegistry(org.apache.qpid.server.protocol.v1_0.type.codec.AMQPDescribedTypeRegistry) Broker(org.apache.qpid.server.model.Broker) EventLogger(org.apache.qpid.server.logging.EventLogger) AuthenticationProvider(org.apache.qpid.server.model.AuthenticationProvider) Subject(javax.security.auth.Subject) FrameWriter(org.apache.qpid.server.protocol.v1_0.codec.FrameWriter) ConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) TaskExecutor(org.apache.qpid.server.configuration.updater.TaskExecutor) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) VirtualHost(org.apache.qpid.server.model.VirtualHost) SubjectCreator(org.apache.qpid.server.security.SubjectCreator) VirtualHostPrincipal(org.apache.qpid.server.virtualhost.VirtualHostPrincipal) Principal(java.security.Principal) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal)

Example 2 with AMQPConnection

use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.

the class VirtualHostTest method testStopVirtualHost_ClosesConnections.

public void testStopVirtualHost_ClosesConnections() {
    String virtualHostName = getName();
    QueueManagingVirtualHost<?> virtualHost = createVirtualHost(virtualHostName);
    assertEquals("Unexpected state", State.ACTIVE, virtualHost.getState());
    AbstractAMQPConnection connection = createMockProtocolConnection(virtualHost);
    assertEquals("Unexpected number of connections before connection registered", 0, virtualHost.getConnectionCount());
    AMQPConnection modelConnection = mock(AMQPConnection.class);
    when(modelConnection.closeAsync()).thenReturn(Futures.immediateFuture(null));
    virtualHost.registerConnection(modelConnection, new NoopConnectionEstablishmentPolicy());
    assertEquals("Unexpected number of connections after connection registered", 1, virtualHost.getConnectionCount());
    ((AbstractConfiguredObject<?>) virtualHost).stop();
    assertEquals("Unexpected state", State.STOPPED, virtualHost.getState());
    assertEquals("Unexpected number of connections after virtualhost stopped", 0, virtualHost.getConnectionCount());
    verify(modelConnection).closeAsync();
}
Also used : AbstractAMQPConnection(org.apache.qpid.server.transport.AbstractAMQPConnection) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) AbstractAMQPConnection(org.apache.qpid.server.transport.AbstractAMQPConnection) NoopConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.NoopConnectionEstablishmentPolicy) Matchers.anyString(org.mockito.Matchers.anyString)

Example 3 with AMQPConnection

use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.

the class AbstractQueue method switchToConnectionExclusivity.

private void switchToConnectionExclusivity() throws ExistingConsumerPreventsExclusive {
    switch(_exclusive) {
        case NONE:
        case CONTAINER:
        case PRINCIPAL:
            AMQPConnection con = null;
            Iterator<QueueConsumer<?, ?>> queueConsumerIterator = _queueConsumerManager.getAllIterator();
            while (queueConsumerIterator.hasNext()) {
                QueueConsumer<?, ?> c = queueConsumerIterator.next();
                if (con == null) {
                    con = c.getSession().getAMQPConnection();
                } else if (!con.equals(c.getSession().getAMQPConnection())) {
                    throw new ExistingConsumerPreventsExclusive();
                }
            }
            _exclusiveOwner = con;
            break;
        case SESSION:
            _exclusiveOwner = _exclusiveOwner == null ? null : ((AMQPSession<?, ?>) _exclusiveOwner).getAMQPConnection();
            break;
        case LINK:
            _exclusiveOwner = _exclusiveSubscriber == null ? null : _exclusiveSubscriber.getSession().getAMQPConnection();
    }
}
Also used : AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) AMQPSession(org.apache.qpid.server.session.AMQPSession)

Example 4 with AMQPConnection

use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.

the class AbstractQueue method switchToPrincipalExclusivity.

private void switchToPrincipalExclusivity() throws ExistingConsumerPreventsExclusive {
    switch(_exclusive) {
        case NONE:
        case CONTAINER:
            Principal principal = null;
            Iterator<QueueConsumer<?, ?>> queueConsumerIterator = _queueConsumerManager.getAllIterator();
            while (queueConsumerIterator.hasNext()) {
                QueueConsumer<?, ?> c = queueConsumerIterator.next();
                if (principal == null) {
                    principal = c.getSession().getAMQPConnection().getAuthorizedPrincipal();
                } else if (!Objects.equals(principal.getName(), c.getSession().getAMQPConnection().getAuthorizedPrincipal().getName())) {
                    throw new ExistingConsumerPreventsExclusive();
                }
            }
            _exclusiveOwner = principal;
            break;
        case CONNECTION:
            _exclusiveOwner = _exclusiveOwner == null ? null : ((AMQPConnection) _exclusiveOwner).getAuthorizedPrincipal();
            break;
        case SESSION:
            _exclusiveOwner = _exclusiveOwner == null ? null : ((AMQPSession<?, ?>) _exclusiveOwner).getAMQPConnection().getAuthorizedPrincipal();
            break;
        case LINK:
            _exclusiveOwner = _exclusiveSubscriber == null ? null : _exclusiveSubscriber.getSession().getAMQPConnection().getAuthorizedPrincipal();
    }
}
Also used : AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) GenericPrincipal(org.apache.qpid.server.model.preferences.GenericPrincipal) SessionPrincipal(org.apache.qpid.server.connection.SessionPrincipal) Principal(java.security.Principal) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal)

Example 5 with AMQPConnection

use of org.apache.qpid.server.transport.AMQPConnection in project qpid-broker-j by apache.

the class ConnectionAndUserPredicateTest method testEvaluateConnectionNameForAmqp.

public void testEvaluateConnectionNameForAmqp() {
    AMQPConnection connection = mock(AMQPConnection.class);
    when(connection.getRemoteAddressString()).thenReturn("foo:1234");
    when(connection.getRemoteContainerName()).thenReturn(null);
    _subject.getPrincipals().add(new ConnectionPrincipal(connection));
    _predicate.setConnectionNamePattern(".*:1234");
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            assertTrue("predicate unexpectedly did not match", _predicate.evaluate(mock(ILoggingEvent.class)));
            return null;
        }
    });
    _predicate.setConnectionNamePattern(".*:4321");
    Subject.doAs(_subject, new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            assertFalse("predicate unexpectedly matched", _predicate.evaluate(mock(ILoggingEvent.class)));
            return null;
        }
    });
}
Also used : AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) ConnectionPrincipal(org.apache.qpid.server.connection.ConnectionPrincipal) ManagementConnectionPrincipal(org.apache.qpid.server.security.auth.ManagementConnectionPrincipal)

Aggregations

AMQPConnection (org.apache.qpid.server.transport.AMQPConnection)12 Principal (java.security.Principal)4 ConnectionPrincipal (org.apache.qpid.server.connection.ConnectionPrincipal)4 InetSocketAddress (java.net.InetSocketAddress)3 Subject (javax.security.auth.Subject)3 AbstractAMQPConnection (org.apache.qpid.server.transport.AbstractAMQPConnection)3 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 InetAddress (java.net.InetAddress)2 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)2 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)2 ManagementConnectionPrincipal (org.apache.qpid.server.security.auth.ManagementConnectionPrincipal)2 Iterators (com.google.common.collect.Iterators)1 PeekingIterator (com.google.common.collect.PeekingIterator)1 Sets (com.google.common.collect.Sets)1 Futures.allAsList (com.google.common.util.concurrent.Futures.allAsList)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 SocketAddress (java.net.SocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 AccessControlContext (java.security.AccessControlContext)1 AccessControlException (java.security.AccessControlException)1