Search in sources :

Example 1 with ProtonClientConnectionManager

use of org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager in project activemq-artemis by apache.

the class JMSSaslExternalTest method testOutbound.

@Test
public void testOutbound() throws Exception {
    final Map<String, Object> config = new LinkedHashMap<>();
    config.put(TransportConstants.HOST_PROP_NAME, "localhost");
    config.put(TransportConstants.PORT_PROP_NAME, String.valueOf(61616));
    config.put(TransportConstants.KEYSTORE_PATH_PROP_NAME, "client_not_revoked.jks");
    config.put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, "changeit");
    config.put(TransportConstants.TRUSTSTORE_PATH_PROP_NAME, "truststore.jks");
    config.put(TransportConstants.TRUSTSTORE_PASSWORD_PROP_NAME, "changeit");
    config.put(TransportConstants.NEED_CLIENT_AUTH_PROP_NAME, true);
    config.put(TransportConstants.SSL_ENABLED_PROP_NAME, true);
    final AtomicBoolean connectionOpened = new AtomicBoolean();
    final AtomicBoolean authFailed = new AtomicBoolean();
    EventHandler eventHandler = new EventHandler() {

        @Override
        public void onRemoteOpen(org.apache.qpid.proton.engine.Connection connection) throws Exception {
            connectionOpened.set(true);
        }

        @Override
        public void onAuthFailed(ProtonHandler protonHandler, org.apache.qpid.proton.engine.Connection connection) {
            authFailed.set(true);
        }
    };
    final ClientSASLFactory clientSASLFactory = new ClientSASLFactory() {

        @Override
        public ClientSASL chooseMechanism(String[] availableMechanims) {
            ExternalMechanism externalMechanism = new ExternalMechanism();
            return new ClientSASL() {

                @Override
                public String getName() {
                    return externalMechanism.getName();
                }

                @Override
                public byte[] getInitialResponse() {
                    return externalMechanism.getInitialResponse();
                }

                @Override
                public byte[] getResponse(byte[] challenge) {
                    return new byte[0];
                }
            };
        }
    };
    ProtonClientConnectionManager lifeCycleListener = new ProtonClientConnectionManager(new AMQPClientConnectionFactory(server, "myid", Collections.singletonMap(Symbol.getSymbol("myprop"), "propvalue"), 5000), Optional.of(eventHandler), clientSASLFactory);
    ProtonClientProtocolManager protocolManager = new ProtonClientProtocolManager(new ProtonProtocolManagerFactory(), server);
    NettyConnector connector = new NettyConnector(config, lifeCycleListener, lifeCycleListener, server.getExecutorFactory().getExecutor(), server.getExecutorFactory().getExecutor(), server.getScheduledPool(), protocolManager);
    connector.start();
    connector.createConnection();
    try {
        Wait.assertEquals(1, server::getConnectionCount);
        Wait.assertTrue(connectionOpened::get);
        Wait.assertFalse(authFailed::get);
        lifeCycleListener.stop();
        Wait.assertEquals(0, server::getConnectionCount);
    } finally {
        lifeCycleListener.stop();
    }
}
Also used : ProtonProtocolManagerFactory(org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManagerFactory) Connection(javax.jms.Connection) EventHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.EventHandler) ProtonHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.ProtonHandler) ProtonClientProtocolManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientProtocolManager) ProtonClientConnectionManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager) NettyConnector(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector) LinkedHashMap(java.util.LinkedHashMap) ExternalMechanism(org.apache.qpid.jms.sasl.ExternalMechanism) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientSASL(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASL) AMQPClientConnectionFactory(org.apache.activemq.artemis.protocol.amqp.client.AMQPClientConnectionFactory) ClientSASLFactory(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory) Test(org.junit.Test)

Example 2 with ProtonClientConnectionManager

use of org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager in project activemq-artemis by apache.

the class JMSSaslGssapiTest method testOutboundWithSlowMech.

@Test
public void testOutboundWithSlowMech() throws Exception {
    final Map<String, Object> config = new LinkedHashMap<>();
    config.put(TransportConstants.HOST_PROP_NAME, "localhost");
    config.put(TransportConstants.PORT_PROP_NAME, String.valueOf(AMQP_PORT));
    final ClientSASLFactory clientSASLFactory = new ClientSASLFactory() {

        @Override
        public ClientSASL chooseMechanism(String[] availableMechanims) {
            GssapiMechanism gssapiMechanism = new GssapiMechanism();
            return new ClientSASL() {

                @Override
                public String getName() {
                    return gssapiMechanism.getName();
                }

                @Override
                public byte[] getInitialResponse() {
                    gssapiMechanism.setUsername("client");
                    gssapiMechanism.setServerName("localhost");
                    try {
                        return gssapiMechanism.getInitialResponse();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return new byte[0];
                }

                @Override
                public byte[] getResponse(byte[] challenge) {
                    try {
                        // simulate a slow client
                        TimeUnit.SECONDS.sleep(4);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        return gssapiMechanism.getChallengeResponse(challenge);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return new byte[0];
                }
            };
        }
    };
    final AtomicBoolean connectionOpened = new AtomicBoolean();
    final AtomicBoolean authFailed = new AtomicBoolean();
    EventHandler eventHandler = new EventHandler() {

        @Override
        public void onRemoteOpen(org.apache.qpid.proton.engine.Connection connection) throws Exception {
            connectionOpened.set(true);
        }

        @Override
        public void onAuthFailed(ProtonHandler protonHandler, org.apache.qpid.proton.engine.Connection connection) {
            authFailed.set(true);
        }
    };
    ProtonClientConnectionManager lifeCycleListener = new ProtonClientConnectionManager(new AMQPClientConnectionFactory(server, "myid", Collections.singletonMap(Symbol.getSymbol("myprop"), "propvalue"), 5000), Optional.of(eventHandler), clientSASLFactory);
    ProtonClientProtocolManager protocolManager = new ProtonClientProtocolManager(new ProtonProtocolManagerFactory(), server);
    NettyConnector connector = new NettyConnector(config, lifeCycleListener, lifeCycleListener, server.getExecutorFactory().getExecutor(), server.getExecutorFactory().getExecutor(), server.getScheduledPool(), protocolManager);
    connector.start();
    connector.createConnection();
    try {
        Wait.assertEquals(1, server::getConnectionCount);
        Wait.assertTrue(connectionOpened::get);
        Wait.assertFalse(authFailed::get);
        lifeCycleListener.stop();
        Wait.assertEquals(0, server::getConnectionCount);
    } finally {
        lifeCycleListener.stop();
    }
}
Also used : ProtonProtocolManagerFactory(org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManagerFactory) Connection(javax.jms.Connection) EventHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.EventHandler) GssapiMechanism(org.apache.qpid.jms.sasl.GssapiMechanism) ProtonHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.ProtonHandler) ProtonClientProtocolManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientProtocolManager) ProtonClientConnectionManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager) NettyConnector(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector) JMSSecurityException(javax.jms.JMSSecurityException) LinkedHashMap(java.util.LinkedHashMap) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientSASL(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASL) AMQPClientConnectionFactory(org.apache.activemq.artemis.protocol.amqp.client.AMQPClientConnectionFactory) ClientSASLFactory(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory) Test(org.junit.Test)

Example 3 with ProtonClientConnectionManager

use of org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager in project activemq-artemis by apache.

the class AmqpOutboundConnectionTest method runOutboundConnectionTest.

private void runOutboundConnectionTest(boolean withSecurity, boolean closeFromClient) throws Exception {
    final ActiveMQServer remote;
    try {
        securityEnabled = withSecurity;
        remote = createServer(AMQP_PORT + 1);
    } finally {
        securityEnabled = false;
    }
    Wait.assertTrue(remote::isActive);
    final Map<String, Object> config = new LinkedHashMap<>();
    config.put(TransportConstants.HOST_PROP_NAME, "localhost");
    config.put(TransportConstants.PORT_PROP_NAME, String.valueOf(AMQP_PORT + 1));
    final ClientSASLFactory clientSASLFactory;
    if (withSecurity) {
        clientSASLFactory = availableMechanims -> {
            if (availableMechanims != null && Arrays.asList(availableMechanims).contains("PLAIN")) {
                return new PlainSASLMechanism(fullUser, fullPass);
            } else {
                return null;
            }
        };
    } else {
        clientSASLFactory = null;
    }
    final AtomicBoolean connectionOpened = new AtomicBoolean();
    EventHandler eventHandler = new EventHandler() {

        @Override
        public void onRemoteOpen(Connection connection) throws Exception {
            connectionOpened.set(true);
        }
    };
    ProtonClientConnectionManager lifeCycleListener = new ProtonClientConnectionManager(new AMQPClientConnectionFactory(server, "myid", Collections.singletonMap(Symbol.getSymbol("myprop"), "propvalue"), 5000), Optional.of(eventHandler), clientSASLFactory);
    ProtonClientProtocolManager protocolManager = new ProtonClientProtocolManager(new ProtonProtocolManagerFactory(), server);
    NettyConnector connector = new NettyConnector(config, lifeCycleListener, lifeCycleListener, server.getExecutorFactory().getExecutor(), server.getExecutorFactory().getExecutor(), server.getScheduledPool(), protocolManager);
    connector.start();
    Object connectionId = connector.createConnection().getID();
    assertNotNull(connectionId);
    RemotingConnection remotingConnection = lifeCycleListener.getConnection(connectionId);
    AtomicReference<ActiveMQException> ex = new AtomicReference<>();
    AtomicBoolean closed = new AtomicBoolean(false);
    remotingConnection.addCloseListener(() -> closed.set(true));
    remotingConnection.addFailureListener(new FailureListener() {

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver) {
            ex.set(exception);
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            ex.set(exception);
        }
    });
    try {
        Wait.assertEquals(1, remote::getConnectionCount);
        Wait.assertTrue(connectionOpened::get);
        if (closeFromClient) {
            lifeCycleListener.stop();
        } else {
            remote.stop();
        }
        Wait.assertEquals(0, remote::getConnectionCount);
        assertTrue(remotingConnection.isDestroyed());
        if (!closeFromClient) {
            assertTrue(ex.get() instanceof ActiveMQRemoteDisconnectException);
        } else {
            assertNull(ex.get());
        }
    } finally {
        if (closeFromClient) {
            remote.stop();
        } else {
            lifeCycleListener.stop();
        }
    }
}
Also used : ProtonProtocolManagerFactory(org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManagerFactory) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) EventHandler(org.apache.activemq.artemis.protocol.amqp.proton.handler.EventHandler) ProtonClientProtocolManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientProtocolManager) LinkedHashMap(java.util.LinkedHashMap) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSASLFactory(org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory) ActiveMQRemoteDisconnectException(org.apache.activemq.artemis.api.core.ActiveMQRemoteDisconnectException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(org.apache.qpid.proton.engine.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClientConnectionManager(org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager) NettyConnector(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AMQPClientConnectionFactory(org.apache.activemq.artemis.protocol.amqp.client.AMQPClientConnectionFactory) FailureListener(org.apache.activemq.artemis.core.remoting.FailureListener)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 NettyConnector (org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnector)3 ProtonProtocolManagerFactory (org.apache.activemq.artemis.protocol.amqp.broker.ProtonProtocolManagerFactory)3 AMQPClientConnectionFactory (org.apache.activemq.artemis.protocol.amqp.client.AMQPClientConnectionFactory)3 ProtonClientConnectionManager (org.apache.activemq.artemis.protocol.amqp.client.ProtonClientConnectionManager)3 ProtonClientProtocolManager (org.apache.activemq.artemis.protocol.amqp.client.ProtonClientProtocolManager)3 EventHandler (org.apache.activemq.artemis.protocol.amqp.proton.handler.EventHandler)3 ClientSASLFactory (org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASLFactory)3 Connection (javax.jms.Connection)2 ProtonHandler (org.apache.activemq.artemis.protocol.amqp.proton.handler.ProtonHandler)2 ClientSASL (org.apache.activemq.artemis.protocol.amqp.sasl.ClientSASL)2 Test (org.junit.Test)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JMSSecurityException (javax.jms.JMSSecurityException)1 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)1 ActiveMQRemoteDisconnectException (org.apache.activemq.artemis.api.core.ActiveMQRemoteDisconnectException)1 FailureListener (org.apache.activemq.artemis.core.remoting.FailureListener)1 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)1 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)1