Search in sources :

Example 11 with Received

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Received in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method processProtocolHeader.

private void processProtocolHeader(final QpidByteBuffer msg) {
    if (msg.remaining() >= 8) {
        byte[] header = new byte[8];
        msg.get(header);
        final AuthenticationProvider<?> authenticationProvider = getPort().getAuthenticationProvider();
        if (Arrays.equals(header, SASL_HEADER)) {
            if (_saslComplete) {
                throw new ConnectionScopedRuntimeException("SASL Layer header received after SASL already established");
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(SASL_HEADER)) {
                getSender().send(protocolHeader);
            }
            SaslMechanisms mechanisms = new SaslMechanisms();
            ArrayList<Symbol> mechanismsList = new ArrayList<>();
            for (String name : authenticationProvider.getAvailableMechanisms(getTransport().isSecure())) {
                mechanismsList.add(Symbol.valueOf(name));
            }
            mechanisms.setSaslServerMechanisms(mechanismsList.toArray(new Symbol[mechanismsList.size()]));
            send(new SASLFrame(mechanisms), null);
            _connectionState = ConnectionState.AWAIT_SASL_INIT;
            _frameHandler = getFrameHandler(true);
        } else if (Arrays.equals(header, AMQP_HEADER)) {
            if (!_saslComplete) {
                final List<String> mechanisms = authenticationProvider.getAvailableMechanisms(getTransport().isSecure());
                if (mechanisms.contains(ExternalAuthenticationManagerImpl.MECHANISM_NAME) && getNetwork().getPeerPrincipal() != null) {
                    setUserPrincipal(new AuthenticatedPrincipal(getNetwork().getPeerPrincipal()));
                } else if (mechanisms.contains(AnonymousAuthenticationManager.MECHANISM_NAME)) {
                    setUserPrincipal(new AuthenticatedPrincipal(((AnonymousAuthenticationManager) authenticationProvider).getAnonymousPrincipal()));
                } else {
                    LOGGER.warn("{} : attempt to initiate AMQP connection without correctly authenticating", getLogSubject());
                    _connectionState = ConnectionState.CLOSED;
                    getNetwork().close();
                }
            }
            try (QpidByteBuffer protocolHeader = QpidByteBuffer.wrap(AMQP_HEADER)) {
                getSender().send(protocolHeader);
            }
            _connectionState = ConnectionState.AWAIT_OPEN;
            _frameHandler = getFrameHandler(false);
        } else {
            LOGGER.warn("{} : unknown AMQP header {}", getLogSubject(), Functions.str(header));
            _connectionState = ConnectionState.CLOSED;
            getNetwork().close();
        }
    }
}
Also used : Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) ArrayList(java.util.ArrayList) SaslMechanisms(org.apache.qpid.server.protocol.v1_0.type.security.SaslMechanisms) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 12 with Received

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Received in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method receiveBegin.

@Override
public void receiveBegin(final int receivingChannelId, final Begin begin) {
    assertState(ConnectionState.OPENED);
    if (begin.getRemoteChannel() != null) {
        closeConnection(ConnectionError.FRAMING_ERROR, "BEGIN received on channel " + receivingChannelId + " with given remote-channel " + begin.getRemoteChannel() + ". Since the broker does not spontaneously start channels, this must be an error.");
    } else // Peer requesting session creation
    {
        if (_receivingSessions[receivingChannelId] == null) {
            int sendingChannelId = getFirstFreeChannel();
            if (sendingChannelId == -1) {
                closeConnection(ConnectionError.FRAMING_ERROR, "BEGIN received on channel " + receivingChannelId + ". There are no free channels for the broker to respond on.");
            } else {
                Session_1_0 session = new Session_1_0(this, begin, sendingChannelId, receivingChannelId, getContextValue(Long.class, AMQPConnection_1_0.CONNECTION_SESSION_CREDIT_WINDOW_SIZE));
                session.create();
                _receivingSessions[receivingChannelId] = session;
                _sendingSessions[sendingChannelId] = session;
                Begin beginToSend = new Begin();
                beginToSend.setRemoteChannel(UnsignedShort.valueOf(receivingChannelId));
                beginToSend.setNextOutgoingId(session.getNextOutgoingId());
                beginToSend.setOutgoingWindow(session.getOutgoingWindow());
                beginToSend.setIncomingWindow(session.getIncomingWindow());
                sendFrame(sendingChannelId, beginToSend);
                synchronized (_blockingLock) {
                    _sessions.add(session);
                    if (_blocking) {
                        session.block();
                    }
                }
            }
        } else {
            closeConnection(ConnectionError.FRAMING_ERROR, "BEGIN received on channel " + receivingChannelId + " which is already in use.");
        }
    }
}
Also used : Begin(org.apache.qpid.server.protocol.v1_0.type.transport.Begin)

Example 13 with Received

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Received in project qpid-broker-j by apache.

the class AMQPConnection_1_0Impl method receiveClose.

@Override
public void receiveClose(final int channel, final Close close) {
    switch(_connectionState) {
        case AWAIT_AMQP_OR_SASL_HEADER:
        case AWAIT_SASL_INIT:
        case AWAIT_SASL_RESPONSE:
        case AWAIT_AMQP_HEADER:
            throw new ConnectionScopedRuntimeException("Received unexpected close when AMQP connection has not been established.");
        case AWAIT_OPEN:
            closeReceived();
            closeConnection(ConnectionError.CONNECTION_FORCED, "Connection close sent before connection was opened");
            break;
        case OPENED:
            _connectionState = ConnectionState.CLOSE_RECEIVED;
            closeReceived();
            if (close.getError() != null) {
                final Error error = close.getError();
                ErrorCondition condition = error.getCondition();
                Symbol errorCondition = condition == null ? null : condition.getValue();
                LOGGER.info("{} : Connection closed with error : {} - {}", getLogSubject(), errorCondition, close.getError().getDescription());
            }
            sendClose(new Close());
            _connectionState = ConnectionState.CLOSED;
            _orderlyClose.set(true);
            addCloseTicker();
            break;
        case CLOSE_SENT:
            closeReceived();
            _connectionState = ConnectionState.CLOSED;
            _orderlyClose.set(true);
            break;
        case CLOSE_RECEIVED:
        case CLOSED:
            break;
        default:
            throw new ServerScopedRuntimeException("Unknown state: " + _connectionState);
    }
}
Also used : ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ErrorCondition(org.apache.qpid.server.protocol.v1_0.type.ErrorCondition) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) ConnectionError(org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) Close(org.apache.qpid.server.protocol.v1_0.type.transport.Close) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException)

Example 14 with Received

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Received in project qpid-broker-j by apache.

the class ProtocolEngine_1_0_0Test method testProtocolEngineWithSaslNonTLSandAnon.

public void testProtocolEngineWithSaslNonTLSandAnon() throws Exception {
    final Map<String, Object> attrs = Collections.singletonMap(ConfiguredObject.NAME, getTestName());
    final AnonymousAuthenticationManager anonymousAuthenticationManager = (new AnonymousAuthenticationManagerFactory()).create(null, attrs, _broker);
    when(_port.getAuthenticationProvider()).thenReturn(anonymousAuthenticationManager);
    when(_port.getSubjectCreator(anyBoolean(), anyString())).thenReturn(new SubjectCreator(anonymousAuthenticationManager, Collections.emptyList(), null));
    allowMechanisms(AnonymousAuthenticationManager.MECHANISM_NAME);
    createEngine(Transport.TCP);
    _protocolEngine_1_0_0.received(QpidByteBuffer.wrap(ProtocolEngineCreator_1_0_0_SASL.getInstance().getHeaderIdentifier()));
    SaslInit init = new SaslInit();
    init.setMechanism(Symbol.valueOf("ANONYMOUS"));
    _frameWriter.send(new SASLFrame(init));
    _protocolEngine_1_0_0.received(QpidByteBuffer.wrap(ProtocolEngineCreator_1_0_0.getInstance().getHeaderIdentifier()));
    Open open = new Open();
    open.setContainerId("testContainerId");
    _frameWriter.send(AMQFrame.createAMQFrame((short) 0, open));
    verify(_virtualHost).registerConnection(any(AMQPConnection.class), any(ConnectionEstablishmentPolicy.class));
    AuthenticatedPrincipal principal = (AuthenticatedPrincipal) _connection.getAuthorizedPrincipal();
    assertNotNull(principal);
    assertEquals(principal, new AuthenticatedPrincipal(anonymousAuthenticationManager.getAnonymousPrincipal()));
}
Also used : ConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy) AnonymousAuthenticationManagerFactory(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManagerFactory) AnonymousAuthenticationManager(org.apache.qpid.server.security.auth.manager.AnonymousAuthenticationManager) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) SaslInit(org.apache.qpid.server.protocol.v1_0.type.security.SaslInit) SASLFrame(org.apache.qpid.server.protocol.v1_0.framing.SASLFrame) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) Matchers.anyString(org.mockito.Matchers.anyString) SubjectCreator(org.apache.qpid.server.security.SubjectCreator) Open(org.apache.qpid.server.protocol.v1_0.type.transport.Open) AuthenticatedPrincipal(org.apache.qpid.server.security.auth.AuthenticatedPrincipal)

Example 15 with Received

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Received in project qpid-broker-j by apache.

the class ProtocolEngine_1_0_0Test method testProtocolEngineWithNoSaslNonTLSandNoAnon.

public void testProtocolEngineWithNoSaslNonTLSandNoAnon() throws Exception {
    allowMechanisms("foo");
    createEngine(Transport.TCP);
    _protocolEngine_1_0_0.received(QpidByteBuffer.wrap(ProtocolEngineCreator_1_0_0.getInstance().getHeaderIdentifier()));
    Open open = new Open();
    open.setContainerId("testContainerId");
    _frameWriter.send(AMQFrame.createAMQFrame((short) 0, open));
    verify(_virtualHost, never()).registerConnection(any(AMQPConnection.class), any(ConnectionEstablishmentPolicy.class));
    verify(_networkConnection).close();
}
Also used : ConnectionEstablishmentPolicy(org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy) AMQPConnection(org.apache.qpid.server.transport.AMQPConnection) Open(org.apache.qpid.server.protocol.v1_0.type.transport.Open)

Aggregations

AmqpError (org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError)9 Error (org.apache.qpid.server.protocol.v1_0.type.transport.Error)9 Open (org.apache.qpid.server.protocol.v1_0.type.transport.Open)7 SpecificationTest (org.apache.qpid.tests.protocol.SpecificationTest)6 FrameTransport (org.apache.qpid.tests.protocol.v1_0.FrameTransport)6 Interaction (org.apache.qpid.tests.protocol.v1_0.Interaction)6 Test (org.junit.Test)6 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)5 Transfer (org.apache.qpid.server.protocol.v1_0.type.transport.Transfer)5 AuthenticatedPrincipal (org.apache.qpid.server.security.auth.AuthenticatedPrincipal)5 AMQPConnection (org.apache.qpid.server.transport.AMQPConnection)5 ConnectionEstablishmentPolicy (org.apache.qpid.server.virtualhost.ConnectionEstablishmentPolicy)5 UnsignedInteger (org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger)4 ConnectionError (org.apache.qpid.server.protocol.v1_0.type.transport.ConnectionError)4 InetSocketAddress (java.net.InetSocketAddress)3 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)3 AmqpErrorException (org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException)3 Attach (org.apache.qpid.server.protocol.v1_0.type.transport.Attach)3 End (org.apache.qpid.server.protocol.v1_0.type.transport.End)3 LinkError (org.apache.qpid.server.protocol.v1_0.type.transport.LinkError)3