-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjmxtunnel.html
343 lines (293 loc) · 11 KB
/
jmxtunnel.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<style>
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote {
margin: 0;
padding: 0;
}
body {
font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif;
font-size: 13px;
line-height: 18px;
color: #737373;
background-color: white;
margin: 10px 13px 10px 13px;
}
table {
margin: 10px 0 15px 0;
border-collapse: collapse;
}
td,th {
border: 1px solid #ddd;
padding: 3px 10px;
}
th {
padding: 5px 10px;
}
a {
color: #0069d6;
}
a:hover {
color: #0050a3;
text-decoration: none;
}
a img {
border: none;
}
p {
margin-bottom: 9px;
}
h1,
h2,
h3,
h4,
h5,
h6 {
color: #404040;
line-height: 36px;
}
h1 {
margin-bottom: 18px;
font-size: 30px;
}
h2 {
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
font-size: 13px;
}
hr {
margin: 0 0 19px;
border: 0;
border-bottom: 1px solid #ccc;
}
blockquote {
padding: 13px 13px 21px 15px;
margin-bottom: 18px;
font-family:georgia,serif;
font-style: italic;
}
blockquote:before {
content:"\201C";
font-size:40px;
margin-left:-10px;
font-family:georgia,serif;
color:#eee;
}
blockquote p {
font-size: 14px;
font-weight: 300;
line-height: 18px;
margin-bottom: 0;
font-style: italic;
}
code, pre {
font-family: Monaco, Andale Mono, Courier New, monospace;
}
code {
background-color: #fee9cc;
color: rgba(0, 0, 0, 0.75);
padding: 1px 3px;
font-size: 12px;
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
pre {
display: block;
padding: 14px;
margin: 0 0 18px;
line-height: 16px;
font-size: 11px;
border: 1px solid #d9d9d9;
white-space: pre-wrap;
word-wrap: break-word;
}
pre code {
background-color: #fff;
color:#737373;
font-size: 11px;
padding: 0;
}
sup {
font-size: 0.83em;
vertical-align: super;
line-height: 0;
}
* {
-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
body {
width: 854px;
margin:10px auto;
}
}
@media print {
body,code,pre code,h1,h2,h3,h4,h5,h6 {
color: black;
}
table, pre {
page-break-inside: avoid;
}
}
</style>
<title>Connecting to a remote JVM Process via JMX interface using ssh tunneling</title>
</head>
<body>
<h1>Connecting to a remote JVM Process via JMX interface using ssh tunneling</h1>
<h2>Scenario :</h2>
<p>Suppose you have JVM processes or a server running on a remote host and that network is private or restricted to only ssh access, and say we had to connect to the JVM to debug or to inspect anything. What we would use is something like JVisualVM and try to connect to the JVM via the JMX interface. This should be straight-forward, but given a multi-network topology, this soon stops to work as it comes out-of-the-box. This tutorial guides you through the steps of setting up LMX for such a scenario</p>
<h2>Constraints and Challenges</h2>
<ul>
<li>The JVM process is running on a remote host in remote network</li>
<li>Only way we can reach the intented destination hosts is via ssh</li>
<li><p>Only access to the network is via ssh via bastion/jumper host with the network, i.e we cannot ssh directly to the destination host and have to reach by hopping via intermediate hosts</p>
<ul>
<li><code>client is--> |F| <--> bastion-host <--> server</code></li>
</ul>
</li>
<li><p>We need to connect to the java process via <code>jvisualvm</code> for debugging, monitoring and other operations. Basically the use-case could be to do anything via JMX interface to a java process, so this overall setup works for other mechanisms which use the JMX interface</p></li>
<li>We cannot run the <code>jvisualvm</code> on the remote/destination host as this is network intensive and may not have privileges. The bandwidth required for this strategy is not practical for operations</li>
<li>The default/standard way of using JMX via RMI does not work as RMI protocol cannot be tunneled. It is an arcance non-OSI 'layer' compliant protocol</li>
<li>We need the data over the network to be secure ( security provided by ssh )</li>
</ul>
<h2>Solution</h2>
<ul>
<li>The network connectivity problems are solved using powerful networking primitives which exploit the OSI layering abstractions. These features are all provided by SSH. We will use two main features of SSH to achive this
<ul>
<li>SSH Local Port Forwarding : SSH port-forwarding primitives provide us means to communicate with abitrary ports on arbitrary networks over an uniform SSH connection</li>
<li>SSH transparent proxying using ProxyCommand and netcat : Provides us means to transparently proxy the ssh-tunnel across mutliple host hops.</li>
</ul>
</li>
<li>Standard JMX works over RMI protocol which is not a transparent layered protocol and hence cannot be tunneled across networks. It requires special knowledge of hosts and has unnecessay complex control protocol which is arcane. Fortunately there is another protocol upon which JMX can be interface called JMXMP. Unfortunately, it does not come standard out-of-the-box and requires some non-trivial setup. But once you have set this up on the server once, it is does not require any more special effort</li>
</ul>
<h2>Environment For Walkthrough</h2>
<pre><code>myhost.naboo.org |F| <- WAN -> |F| bastion.deathstar.org <--> node-01.deathstar.org
</code></pre>
<p>.</p>
<pre><code>$myhost.naboo>netcat -vz bastion.deathstar.org 22
bastion.deathstar.org [178.85.206.132] 22 (ssh) open
$myhost.naboo>netcat -vz node-01.deathstat.org 22
Error: Couldn't resolve host "node-01.deathstat.org" ( or timeout )
$myhost.naboo>netcat -vz node-01.deathstat.org 59991
Error: Couldn't resolve host "node-01.deathstat.org" ( or timeout )
$bastion.deathstat>netcat -vz node-01.deathstat.org 22
node-01.deathstat.org [10.0.0.132] 22 (ssh) open
$node-01.deathstar> lsof -i TCP -n -P
sshd 2179 root 3u IPv4 13812 0t0 TCP *:22 (LISTEN)
java 24601 foo 148u IPv6 22442856 0t0 TCP *:8888 (LISTEN)
java 24942 foo 153u IPv6 22449275 0t0 TCP 127.0.0.1:59991 (LISTEN)
</code></pre>
<h2>Hands-On</h2>
<h3>Steps in the process</h3>
<ol>
<li>Download the jar for JMXMP implementation as this is not available in the standard JDK (see refs)</li>
<li>Setup the JVM server/processs with a JMX server which uses JMXMP protocol</li>
<li>Setup a SSH tunnel (local port-fowarding)to the destination host. You will need to setup to transparently proxy SSH via the intermediate hop host</li>
<li>Invoke <code>jvisualvm</code> with JMXMP enabled protocol to connect to the destination host and JMX port via the SSH tunnel</li>
</ol>
<h3>Details</h3>
<h4>JMXMP Setup</h4>
<h5>JMXMP library</h5>
<ul>
<li>An implementation of the JMXMP protocol is not bundled default with the standard JDK. So you will have to download it seperately as a jar from Oracle or one of the other sources (See refs). This is available in a few Maven repos too to include it easily in your JVM application or server.</li>
<li>Eg. Maven Repo</li>
</ul>
<pre><code> <dependency>
<groupId>javax.management</groupId>
<artifactId>jmxremote_optional</artifactId>
<version>1.0.1_04</version>
</dependency>
</code></pre>
<h5>JVM Server / Process Side</h5>
<ul>
<li>Include the JMXMP jar in the classpath of your JVM application</li>
<li>During the start of your JVM process/server, start a JMX server which uses JMXMP protocol. I have included an example of how I did it with DropWizard based Java server during bootstrap</li>
</ul>
<pre><code> /**
* Usage JVM Arg Example :
* -Djavax.management.remote.JMXServiceURL=service:jmx:jmxmp://127.0.0.1:59991
*/
private static class JMXMPJmxConnectorServerBundle extends RuntimeBundle {
static final String JMXServiceURLPropKey = "javax.management.remote.JMXServiceURL";
@Override
public void run(Environment environment) {
try {
String jmxServiceUrlPropVal = System.getProperty(JMXServiceURLPropKey);
JMXServiceURL jmxUrl = new JMXServiceURL(jmxServiceUrlPropVal);
Map<String, String> jmxEnvironment = new ImmutableMap.Builder<String, String>()
.put("jmx.remote.server.address.wildcard", "false")
.build();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXConnectorServer jmxRemoteServer = JMXConnectorServerFactory.newJMXConnectorServer(
jmxUrl, jmxEnvironment, mbs);
jmxRemoteServer.start();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
</code></pre>
<h5>Client Side</h5>
<ul>
<li>Have the JMXMP jar handy as you will need to include it in the classpath of the client ( assuming a java client )</li>
<li>Invoke <code>jvisualvm</code> as follows ( pay attention to the JMX url )</li>
</ul>
<pre><code>/usr/bin/jvisualvm -cp:a <full path to jmxmp jar> --openjmx service:jmx:jmxmp://127.0.0.1:59991
</code></pre>
<h4>SSH Transparent Proxying Setup</h4>
<ul>
<li>Configure ssh client to do transparent proxying using netcat</li>
<li>Make sure you setup ssh-keys and publish them on the remote network for passwordless logins if you need convenience.</li>
<li>Sections in your <code>.ssh/config</code> look like this :</li>
</ul>
<pre><code>Host bastion-deathstar
Hostname bastion.deathstar.org
User hansolo
PreferredAuthentications publickey
IdentityFile /Users/hansolo/ssh-keys/id_rsa_deathstar
IdentitiesOnly yes
Host node-01-deathstar
Hostname node-01.deathstar.org
User hansolo
ProxyCommand ssh bastion-deathstar nc %h %p
PreferredAuthentications publickey
IdentityFile /Users/hansolo/ssh-keys/id_rsa_deathstar
IdentitiesOnly yes
</code></pre>
<h4>SSH Tunelling Setup</h4>
<ul>
<li>Setup local port-forwarding of the JMX port as follows. Note the host in this is the bastion host, but the proxying setup you already did will tunnel the ssh connection all the way to the destination host</li>
</ul>
<pre><code> ssh -L 59991:127.0.0.1:59991 bastion.deathstar.org -N
</code></pre>
<h2>References</h2>
<ul>
<li><a href="http://blog.markfeeney.com/2010/10/jmx-through-ssh-tunnel.html">http://blog.markfeeney.com/2010/10/jmx-through-ssh-tunnel.html</a></li>
<li><a href="http://stackoverflow.com/questions/11412560/where-to-download-jmxmp">http://stackoverflow.com/questions/11412560/where-to-download-jmxmp</a></li>
<li><a href="https://github.com/frankgrimes97/jmxmp-java-agent/blob/master/pom.xml">https://github.com/frankgrimes97/jmxmp-java-agent/blob/master/pom.xml</a></li>
<li><a href="http://backdrift.org/transparent-proxy-with-ssh">http://backdrift.org/transparent-proxy-with-ssh</a></li>
<li><a href="http://www.amazon.com/gp/product/0596008953">http://www.amazon.com/gp/product/0596008953</a></li>
</ul>
</body>
</html>