ARTEMIS-4174 Listen only to provided connector-host for JMX RMI sockets

This commit is contained in:
Marvin Blauth 2023-02-17 17:40:53 +01:00 committed by Justin Bertram
parent fd5b64f035
commit 4d46588cdf
No known key found for this signature in database
GPG Key ID: F41830B875BB8633
3 changed files with 102 additions and 1 deletions

View File

@ -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();

View File

@ -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 {

View File

@ -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();
}
}