From 4d46588cdf87f2c00e27cc1122ac1118aad0aeb2 Mon Sep 17 00:00:00 2001 From: Marvin Blauth Date: Fri, 17 Feb 2023 17:40:53 +0100 Subject: [PATCH] ARTEMIS-4174 Listen only to provided connector-host for JMX RMI sockets --- .../management/ManagementConnector.java | 1 + .../server/management/RmiRegistryFactory.java | 50 +++++++++++++++++- .../management/JMXRMIRegistryPortTest.java | 52 +++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 artemis-server/src/test/java/org/apache/activemq/artemis/core/server/management/JMXRMIRegistryPortTest.java diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementConnector.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementConnector.java index 8b8aecbe68..b75159d6e1 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementConnector.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementConnector.java @@ -55,6 +55,7 @@ public class ManagementConnector implements ActiveMQComponent { public void start() throws Exception { rmiRegistryFactory = new RmiRegistryFactory(); rmiRegistryFactory.setPort(configuration.getConnectorPort()); + rmiRegistryFactory.setHost(configuration.getConnectorHost()); rmiRegistryFactory.init(); mbeanServerFactory = new MBeanServerFactory(); diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/RmiRegistryFactory.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/RmiRegistryFactory.java index 4c5cab2e95..dd0d87ded5 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/RmiRegistryFactory.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/RmiRegistryFactory.java @@ -16,16 +16,26 @@ */ package org.apache.activemq.artemis.core.server.management; +import javax.net.ServerSocketFactory; +import javax.net.SocketFactory; +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; import java.net.UnknownHostException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; +import java.rmi.server.RMIClientSocketFactory; +import java.rmi.server.RMIServerSocketFactory; import java.rmi.server.UnicastRemoteObject; public class RmiRegistryFactory { private int port = Registry.REGISTRY_PORT; private Registry registry; + private String host; + private HostLimitedServerSocketFactory socketFactory; /** * @return the port */ @@ -40,12 +50,50 @@ public class RmiRegistryFactory { this.port = port; } + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + /** + * Create a server socket for testing purposes. + */ + ServerSocket createTestSocket() throws IOException { + return socketFactory.createServerSocket(1100); + } + public Object getObject() throws Exception { return registry; } + class HostLimitedServerSocketFactory implements RMIServerSocketFactory { + @Override + public ServerSocket createServerSocket(int port) throws IOException { + InetAddress hostAddress; + if (host != null) { + hostAddress = InetAddress.getByName(host); + } else { + hostAddress = null; // accept connections on all local addresses + } + return ServerSocketFactory.getDefault().createServerSocket(port, 0, hostAddress); + } + } + + private static class PassThroughToDefaultSocketFactory implements RMIClientSocketFactory { + @Override + public Socket createSocket(String host, int port) throws IOException { + return SocketFactory.getDefault().createSocket(host, port); + } + } + public void init() throws RemoteException, UnknownHostException { - registry = LocateRegistry.createRegistry(port); + socketFactory = new HostLimitedServerSocketFactory(); + registry = LocateRegistry.createRegistry(port, + new PassThroughToDefaultSocketFactory(), + socketFactory); } public void destroy() throws RemoteException { diff --git a/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/management/JMXRMIRegistryPortTest.java b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/management/JMXRMIRegistryPortTest.java new file mode 100644 index 0000000000..9e3ac560d4 --- /dev/null +++ b/artemis-server/src/test/java/org/apache/activemq/artemis/core/server/management/JMXRMIRegistryPortTest.java @@ -0,0 +1,52 @@ +package org.apache.activemq.artemis.core.server.management; + +import org.apache.activemq.artemis.core.config.JMXConnectorConfiguration; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.ServerSocket; + +public class JMXRMIRegistryPortTest { + + @Test + public void explicitLocalhostRegistry() throws IOException { + RmiRegistryFactory registryFactory = new RmiRegistryFactory(); + registryFactory.setHost("localhost"); + registryFactory.setPort(1099); + registryFactory.init(); + try (ServerSocket testSocket = registryFactory.createTestSocket()) { + Assert.assertEquals(InetAddress.getByName("localhost"), + testSocket.getInetAddress()); + } + registryFactory.destroy(); + } + + @Test + public void unlimitedHostRegistry() throws IOException { + RmiRegistryFactory registryFactory = new RmiRegistryFactory(); + registryFactory.setHost(null); + registryFactory.setPort(1099); + registryFactory.init(); + try (ServerSocket testSocket = registryFactory.createTestSocket()) { + Assert.assertEquals(InetAddress.getByAddress(new byte[] { 0, 0, 0, 0 }), + testSocket.getInetAddress()); + } + registryFactory.destroy(); + } + + @Test + public void defaultRegistry() throws IOException { + RmiRegistryFactory registryFactory = new RmiRegistryFactory(); + JMXConnectorConfiguration configuration = new JMXConnectorConfiguration(); + registryFactory.setHost(configuration.getConnectorHost()); + registryFactory.setPort(configuration.getConnectorPort()); + registryFactory.init(); + try (ServerSocket testSocket = registryFactory.createTestSocket()) { + Assert.assertEquals(InetAddress.getByName("localhost"), + testSocket.getInetAddress()); + } + registryFactory.destroy(); + } +}