Skip to content

Commit 44e13ed

Browse files
committed
fix(jmx): set java.rmi.server.hostname for SSL to prevent hostname verification issues on multi-homed hosts
1 parent 3ab96af commit 44e13ed

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

activemq-broker/src/main/java/org/apache/activemq/broker/jmx/ManagementContext.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,23 @@ public void run() {
170170
try {
171171
// need to remove MDC as we must not inherit MDC in child threads causing leaks
172172
MDC.remove("activemq.broker");
173-
connectorServer.start();
173+
// When SSL is enabled, temporarily set java.rmi.server.hostname
174+
// to connectorHost so RMI stubs embed the configured host rather
175+
// than the machine's auto-detected IP. Without this, SSL hostname
176+
// verification fails on multi-homed hosts because the stub carries
177+
// an IP that is not covered by the certificate's SAN entries.
178+
// Pre-existing user-defined values are respected and not overwritten.
179+
final String prevRmiHostname = System.getProperty("java.rmi.server.hostname");
180+
if (sslContext != null && prevRmiHostname == null) {
181+
System.setProperty("java.rmi.server.hostname", connectorHost);
182+
}
183+
try {
184+
connectorServer.start();
185+
} finally {
186+
if (sslContext != null && prevRmiHostname == null) {
187+
System.clearProperty("java.rmi.server.hostname");
188+
}
189+
}
174190
serverStub = server.toStub();
175191
} finally {
176192
if (brokerName != null) {

activemq-broker/src/test/java/org/apache/activemq/broker/jmx/ManagementContextSslTest.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public class ManagementContextSslTest {
6161
private SSLContext savedDefaultSslContext;
6262
private String savedTrustStore;
6363
private String savedTrustStorePassword;
64-
private String savedRmiHostname;
6564

6665
@BeforeClass
6766
public static void createKeyStore() throws Exception {
@@ -110,7 +109,6 @@ public void setUp() throws Exception {
110109
savedDefaultSslContext = SSLContext.getDefault();
111110
savedTrustStore = System.getProperty("javax.net.ssl.trustStore");
112111
savedTrustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
113-
savedRmiHostname = System.getProperty("java.rmi.server.hostname");
114112
}
115113

116114
@After
@@ -121,7 +119,6 @@ public void tearDown() throws Exception {
121119
SSLContext.setDefault(savedDefaultSslContext);
122120
restoreSystemProperty("javax.net.ssl.trustStore", savedTrustStore);
123121
restoreSystemProperty("javax.net.ssl.trustStorePassword", savedTrustStorePassword);
124-
restoreSystemProperty("java.rmi.server.hostname", savedRmiHostname);
125122
}
126123

127124
@Test
@@ -195,9 +192,6 @@ public void testConnectorStartsWithSsl() throws Exception {
195192
SSLContext.setDefault(testSslContext);
196193
System.setProperty("javax.net.ssl.trustStore", keystoreFile.toString());
197194
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
198-
// Force RMI stubs to advertise "localhost" so SSL hostname verification
199-
// matches the certificate SAN (dns:localhost) instead of the machine's IP.
200-
System.setProperty("java.rmi.server.hostname", "localhost");
201195

202196
context = createSslManagementContext();
203197
context.start();
@@ -217,11 +211,6 @@ public void testSslJmxConnectionSucceeds() throws Exception {
217211
SSLContext.setDefault(testSslContext);
218212
System.setProperty("javax.net.ssl.trustStore", keystoreFile.toString());
219213
System.setProperty("javax.net.ssl.trustStorePassword", KEYSTORE_PASSWORD);
220-
// Force RMI stubs to advertise "localhost" so SSL hostname verification
221-
// matches the certificate SAN (dns:localhost) instead of the machine's actual IP.
222-
// RMI normally embeds InetAddress.getLocalHost() in stubs; on a multi-homed
223-
// machine this can be an IP not covered by the test certificate.
224-
System.setProperty("java.rmi.server.hostname", "localhost");
225214

226215
context = createSslManagementContext();
227216
context.start();

0 commit comments

Comments
 (0)