Skip to content

Commit e2fd2f2

Browse files
committed
add ssl rmi registry support
1 parent 8a781ba commit e2fd2f2

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxConnectorBuilder.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import com.google.errorprone.annotations.CanIgnoreReturnValue;
99
import java.io.IOException;
1010
import java.net.MalformedURLException;
11+
import java.net.URI;
12+
import java.rmi.NotBoundException;
13+
import java.rmi.registry.LocateRegistry;
14+
import java.rmi.registry.Registry;
1115
import java.security.Provider;
1216
import java.security.Security;
1317
import java.util.HashMap;
@@ -19,6 +23,9 @@
1923
import javax.management.remote.JMXConnector;
2024
import javax.management.remote.JMXConnectorFactory;
2125
import javax.management.remote.JMXServiceURL;
26+
import javax.management.remote.rmi.RMIConnector;
27+
import javax.management.remote.rmi.RMIServer;
28+
import javax.rmi.ssl.SslRMIClientSocketFactory;
2229
import javax.security.auth.callback.Callback;
2330
import javax.security.auth.callback.CallbackHandler;
2431
import javax.security.auth.callback.NameCallback;
@@ -37,6 +44,10 @@ public class JmxConnectorBuilder {
3744
@Nullable private String realm;
3845
private boolean sslRegistry;
3946

47+
// used only with ssl registry
48+
private static final SslRMIClientSocketFactory sslRmiClientSocketFactory =
49+
new SslRMIClientSocketFactory();
50+
4051
private JmxConnectorBuilder(JMXServiceURL url) {
4152
this.url = url;
4253
}
@@ -91,10 +102,10 @@ public JMXConnector build() throws IOException {
91102
try {
92103
if (sslRegistry) {
93104
return doConnectSslRegistry(url, env);
105+
} else {
106+
return doConnect(url, env);
94107
}
95108

96-
return doConnect(url, env);
97-
98109
} catch (IOException e) {
99110
throw new IOException("Unable to connect to " + url.getHost() + ":" + url.getPort(), e);
100111
}
@@ -148,7 +159,29 @@ private static JMXConnector doConnect(JMXServiceURL url, Map<String, Object> env
148159
}
149160

150161
public JMXConnector doConnectSslRegistry(JMXServiceURL url, Map<String, Object> env) {
151-
throw new IllegalStateException("TODO");
162+
163+
logger.info("Connecting with SSL protected RMI registry to " + url);
164+
String hostName;
165+
int port;
166+
167+
if (url.getURLPath().startsWith("/jndi/")) {
168+
String[] components = url.getURLPath().split("/", 3);
169+
URI uri = URI.create(components[2]);
170+
hostName = uri.getHost();
171+
port = uri.getPort();
172+
} else {
173+
hostName = url.getHost();
174+
port = url.getPort();
175+
}
176+
177+
try {
178+
JMXConnector jmxConn = new RMIConnector(getStub(hostName, port), null);
179+
jmxConn.connect(env);
180+
return jmxConn;
181+
} catch (IOException e) {
182+
throw new IllegalStateException("Unable to connect to " + url, e);
183+
}
184+
152185
}
153186

154187
private static JMXServiceURL buildUrl(String host, int port) {
@@ -164,4 +197,13 @@ private static JMXServiceURL buildUrl(String url) {
164197
throw new IllegalArgumentException("invalid url", e);
165198
}
166199
}
200+
201+
private static RMIServer getStub(String hostName, int port) throws IOException {
202+
try {
203+
Registry registry = LocateRegistry.getRegistry(hostName, port, sslRmiClientSocketFactory);
204+
return (RMIServer) registry.lookup("jmxrmi");
205+
} catch (NotBoundException nbe) {
206+
throw new IOException(nbe);
207+
}
208+
}
167209
}

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public static void main(String[] args) {
8383
Optional.ofNullable(scraperConfig.getUsername()).ifPresent(connectorBuilder::withUser);
8484
Optional.ofNullable(scraperConfig.getPassword()).ifPresent(connectorBuilder::withPassword);
8585

86+
if (scraperConfig.isRegistrySsl()) {
87+
connectorBuilder.withSslRegistry();
88+
}
89+
8690
if (testMode) {
8791
System.exit(testConnection(connectorBuilder) ? 0 : 1);
8892
} else {

0 commit comments

Comments
 (0)