Search in sources :

Example 36 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class FailoverOnFlowControlTest method testOverflowSend.

@Test
public void testOverflowSend() throws Exception {
    ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setProducerWindowSize(1000).setRetryInterval(100);
    final ArrayList<ClientSession> sessionList = new ArrayList<>();
    Interceptor interceptorClient = new Interceptor() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            log.debug("Intercept..." + packet.getClass().getName());
            if (packet instanceof SessionProducerCreditsMessage) {
                SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;
                log.debug("Credits: " + credit.getCredits());
                if (count.incrementAndGet() == 2) {
                    log.debug("### crashing server");
                    try {
                        InVMConnection.setFlushEnabled(false);
                        crash(false, sessionList.get(0));
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        InVMConnection.setFlushEnabled(true);
                    }
                    return false;
                }
            }
            return true;
        }
    };
    locator.addIncomingInterceptor(interceptorClient);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sf.createSession(true, true);
    sessionList.add(session);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    final int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[5000]);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionProducerCreditsMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ArrayList(java.util.ArrayList) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 37 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class ReplicatedDistributionTest method fail.

/**
 * @param session
 * @throws InterruptedException
 */
private void fail(final ClientSession session) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    session.addFailureListener(new CountDownSessionFailureListener(latch, session));
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    // Simulate failure on connection
    conn.fail(new ActiveMQNotConnectedException());
    // Wait to be informed of failure
    boolean ok = latch.await(1000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ok);
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)

Example 38 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class SecurityTest method testCustomSecurityManager2.

@Test
public void testCustomSecurityManager2() throws Exception {
    final Configuration configuration = createDefaultInVMConfig().setSecurityEnabled(true);
    final ActiveMQSecurityManager customSecurityManager = new ActiveMQSecurityManager2() {

        @Override
        public boolean validateUser(final String username, final String password) {
            fail("Unexpected call to overridden method");
            return false;
        }

        @Override
        public boolean validateUser(final String username, final String password, final X509Certificate[] certificates) {
            return (username.equals("foo") || username.equals("bar") || username.equals("all")) && password.equals("frobnicate");
        }

        @Override
        public boolean validateUserAndRole(final String username, final String password, final Set<Role> requiredRoles, final CheckType checkType) {
            fail("Unexpected call to overridden method");
            return false;
        }

        @Override
        public boolean validateUserAndRole(final String username, final String password, final Set<Role> requiredRoles, final CheckType checkType, final String address, final RemotingConnection connection) {
            if (!(connection.getTransportConnection() instanceof InVMConnection)) {
                return false;
            }
            if ((username.equals("foo") || username.equals("bar") || username.equals("all")) && password.equals("frobnicate")) {
                if (username.equals("all")) {
                    return true;
                } else if (username.equals("foo")) {
                    return address.equals("test.queue") && checkType == CheckType.CONSUME;
                } else if (username.equals("bar")) {
                    return address.equals("test.queue") && checkType == CheckType.SEND;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    };
    final ActiveMQServer server = addServer(new ActiveMQServerImpl(configuration, customSecurityManager));
    server.start();
    final ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
    final ClientSessionFactory factory = createSessionFactory(locator);
    ClientSession adminSession = factory.createSession("all", "frobnicate", false, true, true, false, -1);
    final String queueName = "test.queue";
    adminSession.createQueue(queueName, queueName, false);
    final String otherQueueName = "other.queue";
    adminSession.createQueue(otherQueueName, otherQueueName, false);
    // Wrong user name
    try {
        factory.createSession("baz", "frobnicate", false, true, true, false, -1);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Wrong password
    try {
        factory.createSession("foo", "xxx", false, true, true, false, -1);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, wrong queue for sending
    try {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(otherQueueName, session, adminSession);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, wrong queue for receiving
    try {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(otherQueueName, session, adminSession);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, allowed to send but not receive
    {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(queueName, session, adminSession);
    }
    // Correct user and password, allowed to receive but not send
    {
        final ClientSession session = factory.createSession("bar", "frobnicate", false, true, true, false, -1);
        checkUserSendNoReceive(queueName, session);
    }
}
Also used : InVMConnection(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnection) Set(java.util.Set) HashSet(java.util.HashSet) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQSecurityManager2(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager2) CheckType(org.apache.activemq.artemis.core.security.CheckType) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) ActiveMQSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 39 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class SecurityTest method testCustomSecurityManager3.

@Test
public void testCustomSecurityManager3() throws Exception {
    final Configuration configuration = createDefaultInVMConfig().setSecurityEnabled(true);
    final ActiveMQSecurityManager customSecurityManager = new ActiveMQSecurityManager3() {

        @Override
        public boolean validateUser(final String username, final String password) {
            fail("Unexpected call to overridden method");
            return false;
        }

        @Override
        public String validateUser(final String username, final String password, final RemotingConnection remotingConnection) {
            if ((username.equals("foo") || username.equals("bar") || username.equals("all")) && password.equals("frobnicate")) {
                return username;
            } else {
                return null;
            }
        }

        @Override
        public boolean validateUserAndRole(final String username, final String password, final Set<Role> requiredRoles, final CheckType checkType) {
            fail("Unexpected call to overridden method");
            return false;
        }

        @Override
        public String validateUserAndRole(final String username, final String password, final Set<Role> requiredRoles, final CheckType checkType, final String address, final RemotingConnection connection) {
            if (!(connection.getTransportConnection() instanceof InVMConnection)) {
                return null;
            }
            if ((username.equals("foo") || username.equals("bar") || username.equals("all")) && password.equals("frobnicate")) {
                if (username.equals("all")) {
                    return username;
                } else if (username.equals("foo")) {
                    if (address.equals("test.queue") && checkType == CheckType.CONSUME)
                        return username;
                    else
                        return null;
                } else if (username.equals("bar")) {
                    if (address.equals("test.queue") && checkType == CheckType.SEND)
                        return username;
                    else
                        return null;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    };
    final ActiveMQServer server = addServer(new ActiveMQServerImpl(configuration, customSecurityManager));
    server.start();
    final ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
    final ClientSessionFactory factory = createSessionFactory(locator);
    ClientSession adminSession = factory.createSession("all", "frobnicate", false, true, true, false, -1);
    final String queueName = "test.queue";
    adminSession.createQueue(queueName, queueName, false);
    final String otherQueueName = "other.queue";
    adminSession.createQueue(otherQueueName, otherQueueName, false);
    // Wrong user name
    try {
        factory.createSession("baz", "frobnicate", false, true, true, false, -1);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Wrong password
    try {
        factory.createSession("foo", "xxx", false, true, true, false, -1);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, wrong queue for sending
    try {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(otherQueueName, session, adminSession);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, wrong queue for receiving
    try {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(otherQueueName, session, adminSession);
        Assert.fail("should throw exception");
    } catch (ActiveMQSecurityException se) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    // Correct user and password, allowed to send but not receive
    {
        final ClientSession session = factory.createSession("foo", "frobnicate", false, true, true, false, -1);
        checkUserReceiveNoSend(queueName, session, adminSession);
    }
    // Correct user and password, allowed to receive but not send
    {
        final ClientSession session = factory.createSession("bar", "frobnicate", false, true, true, false, -1);
        checkUserSendNoReceive(queueName, session);
    }
}
Also used : InVMConnection(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnection) Set(java.util.Set) HashSet(java.util.HashSet) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQSecurityManager3(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager3) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CheckType(org.apache.activemq.artemis.core.security.CheckType) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ActiveMQSecurityException(org.apache.activemq.artemis.api.core.ActiveMQSecurityException) ActiveMQSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQSecurityManager) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 40 with RemotingConnection

use of org.apache.activemq.artemis.spi.core.protocol.RemotingConnection in project activemq-artemis by apache.

the class StompWithClientIdValidationTest method createServer.

@Override
protected JMSServerManager createServer() throws Exception {
    Configuration config = createBasicConfig().setSecurityEnabled(isSecurityEnabled()).setPersistenceEnabled(isPersistenceEnabled()).addAcceptorConfiguration("stomp", "tcp://localhost:61613?enabledProtocols=STOMP").addAcceptorConfiguration(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
    ActiveMQJAASSecurityManager securityManager = new ActiveMQJAASSecurityManager(InVMLoginModule.class.getName(), new SecurityConfiguration()) {

        @Override
        public String validateUser(String user, String password, RemotingConnection remotingConnection) {
            String validatedUser = super.validateUser(user, password, remotingConnection);
            if (validatedUser == null) {
                return null;
            }
            if ("STOMP".equals(remotingConnection.getProtocolName())) {
                final String clientId = remotingConnection.getClientID();
                /*
                * perform some kind of clientId validation, e.g. check presence or format
                */
                if (clientId == null || clientId.length() == 0) {
                    System.err.println("ClientID not set!");
                    return null;
                }
            }
            return validatedUser;
        }
    };
    securityManager.getConfiguration().addUser(defUser, defPass);
    ActiveMQServer activeMqServer = addServer(ActiveMQServers.newActiveMQServer(config, ManagementFactory.getPlatformMBeanServer(), securityManager));
    JMSConfiguration jmsConfig = new JMSConfigurationImpl();
    server = new JMSServerManagerImpl(activeMqServer, jmsConfig);
    server.setRegistry(new JndiBindingRegistry(new InVMNamingContext()));
    return server;
}
Also used : JMSConfiguration(org.apache.activemq.artemis.jms.server.config.JMSConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) SecurityConfiguration(org.apache.activemq.artemis.core.config.impl.SecurityConfiguration) JMSConfiguration(org.apache.activemq.artemis.jms.server.config.JMSConfiguration) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQJAASSecurityManager(org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) InVMLoginModule(org.apache.activemq.artemis.spi.core.security.jaas.InVMLoginModule) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) JndiBindingRegistry(org.apache.activemq.artemis.core.registry.JndiBindingRegistry) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) JMSServerManagerImpl(org.apache.activemq.artemis.jms.server.impl.JMSServerManagerImpl) JMSConfigurationImpl(org.apache.activemq.artemis.jms.server.config.impl.JMSConfigurationImpl) SecurityConfiguration(org.apache.activemq.artemis.core.config.impl.SecurityConfiguration) InVMNamingContext(org.apache.activemq.artemis.tests.unit.util.InVMNamingContext)

Aggregations

RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)84 Test (org.junit.Test)44 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)36 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)31 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)30 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)22 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)21 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 CountDownLatch (java.util.concurrent.CountDownLatch)18 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)17 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)16 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)16 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)15 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 CoreRemotingConnection (org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection)13 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)12 Connection (javax.jms.Connection)11 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)11 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)11 Session (javax.jms.Session)10