-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathSSLNettyTransport.java
328 lines (288 loc) · 11.4 KB
/
SSLNettyTransport.java
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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.sdk.ssl;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.ssl.SslHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
import org.opensearch.Version;
import org.opensearch.cluster.node.DiscoveryNode;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.network.NetworkService;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.core.indices.breaker.CircuitBreakerService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.transport.SharedGroupFactory;
import org.opensearch.transport.TcpChannel;
import org.opensearch.transport.netty4.Netty4Transport;
/**
* Class that setups up secure TLS channel for this extension to use with transport requests
*/
public class SSLNettyTransport extends Netty4Transport {
private static final Logger logger = LogManager.getLogger(SSLNettyTransport.class);
private final SslKeyStore ossks;
/**
*
* @param settings SSL Settings
* @param version version
* @param threadPool threadPool
* @param networkService networkService
* @param pageCacheRecycler pageCacheRecycler
* @param namedWriteableRegistry namedWriteableRegistry
* @param circuitBreakerService circuitBreakerService
* @param ossks ossks
* @param sharedGroupFactory sharedGroupFactory
*/
public SSLNettyTransport(
final Settings settings,
final Version version,
final ThreadPool threadPool,
final NetworkService networkService,
final PageCacheRecycler pageCacheRecycler,
final NamedWriteableRegistry namedWriteableRegistry,
final CircuitBreakerService circuitBreakerService,
final SslKeyStore ossks,
SharedGroupFactory sharedGroupFactory
) {
super(
settings,
version,
threadPool,
networkService,
pageCacheRecycler,
namedWriteableRegistry,
circuitBreakerService,
sharedGroupFactory
);
this.ossks = ossks;
}
/**
*
* @param channel The channel
* @param e Exception
*/
@Override
public void onException(TcpChannel channel, Exception e) {
Throwable cause = e;
if (e instanceof DecoderException && e != null) {
cause = e.getCause();
}
logger.error("Exception during establishing a SSL connection: " + cause, cause);
super.onException(channel, e);
}
/**
*
* @param name name of channel
* @return ChannelHandler
*/
@Override
protected ChannelHandler getServerChannelInitializer(String name) {
return new SSLServerChannelInitializer(name);
}
/**
*
* @param node Node this channel is connected to
* @return ChannelHandler
*/
@Override
protected ChannelHandler getClientChannelInitializer(DiscoveryNode node) {
return new SSLClientChannelInitializer(node);
}
/**
* SSLServerChannelInitializer
*/
protected class SSLServerChannelInitializer extends Netty4Transport.ServerChannelInitializer {
/**
*
* @param name name of this channel
*/
public SSLServerChannelInitializer(String name) {
super(name);
}
/**
*
* @param ch the {@link Channel} which was registered.
* @throws Exception
*/
@Override
protected void initChannel(Channel ch) throws Exception {
super.initChannel(ch);
final SslHandler sslHandler = new SslHandler(ossks.createServerTransportSSLEngine());
ch.pipeline().addFirst("ssl_server", sslHandler);
}
/**
*
* @param ctx ChannelHandlerContext
* @param cause Throwable
* @throws Exception
*/
@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof DecoderException && cause != null) {
cause = cause.getCause();
}
logger.error("Exception during establishing a SSL connection: " + cause, cause);
super.exceptionCaught(ctx, cause);
}
}
/**
* ClientSSLHandler
*/
protected static class ClientSSLHandler extends ChannelOutboundHandlerAdapter {
private final Logger log = LogManager.getLogger(this.getClass());
private final SslKeyStore sks;
private final boolean hostnameVerificationEnabled;
private final boolean hostnameVerificationResovleHostName;
/**
*
* @param sks Security Keystore
* @param hostnameVerificationEnabled flag to indicate if hostname verification is enabled
* @param hostnameVerificationResolveHostName flag to indicate if hostnames should be resolved with hostname
* verification
*/
private ClientSSLHandler(
final SslKeyStore sks,
final boolean hostnameVerificationEnabled,
final boolean hostnameVerificationResolveHostName
) {
this.sks = sks;
this.hostnameVerificationEnabled = hostnameVerificationEnabled;
this.hostnameVerificationResovleHostName = hostnameVerificationResolveHostName;
}
/**
*
* @param ctx ChannelHandlerContext
* @param cause Throwable
* @throws Exception
*/
@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof DecoderException && cause != null) {
cause = cause.getCause();
}
logger.error("Exception during establishing a SSL connection: " + cause, cause);
super.exceptionCaught(ctx, cause);
}
/**
*
* @param ctx the {@link ChannelHandlerContext} for which the connect operation is made
* @param remoteAddress the {@link SocketAddress} to which it should connect
* @param localAddress the {@link SocketAddress} which is used as source on connect
* @param promise the {@link ChannelPromise} to notify once the operation completes
* @throws Exception
*/
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise)
throws Exception {
SSLEngine engine = null;
try {
if (hostnameVerificationEnabled) {
final InetSocketAddress inetSocketAddress = (InetSocketAddress) remoteAddress;
String hostname = null;
if (hostnameVerificationResovleHostName) {
hostname = inetSocketAddress.getHostName();
} else {
hostname = inetSocketAddress.getHostString();
}
if (log.isDebugEnabled()) {
log.debug(
"Hostname of peer is {} ({}/{}) with hostnameVerificationResolveHostName: {}",
hostname,
inetSocketAddress.getHostName(),
inetSocketAddress.getHostString(),
hostnameVerificationResovleHostName
);
}
engine = sks.createClientTransportSSLEngine(hostname, inetSocketAddress.getPort());
} else {
engine = sks.createClientTransportSSLEngine(null, -1);
}
} catch (final SSLException e) {
throw ExceptionsHelper.convertToOpenSearchException(e);
}
final SslHandler sslHandler = new SslHandler(engine);
ctx.pipeline().replace(this, "ssl_client", sslHandler);
super.connect(ctx, remoteAddress, localAddress, promise);
}
}
/**
* SSLClientChannelInitializer
*/
protected class SSLClientChannelInitializer extends Netty4Transport.ClientChannelInitializer {
private final boolean hostnameVerificationEnabled;
private final boolean hostnameVerificationResovleHostName;
private final DiscoveryNode node;
private SSLConnectionTestResult connectionTestResult;
/**
*
* @param node The node to connect to
*/
@SuppressWarnings("removal")
public SSLClientChannelInitializer(DiscoveryNode node) {
this.node = node;
hostnameVerificationEnabled = settings.getAsBoolean(SSLConfigConstants.SSL_TRANSPORT_ENFORCE_HOSTNAME_VERIFICATION, true);
hostnameVerificationResovleHostName = settings.getAsBoolean(
SSLConfigConstants.SSL_TRANSPORT_ENFORCE_HOSTNAME_VERIFICATION_RESOLVE_HOST_NAME,
true
);
connectionTestResult = SSLConnectionTestResult.SSL_AVAILABLE;
}
/**
*
* @param ch the {@link Channel} which was registered.
* @throws Exception
*/
@Override
protected void initChannel(Channel ch) throws Exception {
super.initChannel(ch);
if (connectionTestResult == SSLConnectionTestResult.OPENSEARCH_PING_FAILED) {
logger.error(
"SSL dual mode is enabled but dual mode handshake and OpenSearch ping has failed during client connection setup, closing channel"
);
ch.close();
return;
}
if (connectionTestResult == SSLConnectionTestResult.SSL_AVAILABLE) {
logger.debug("Connection to {} needs to be ssl, adding ssl handler to the client channel ", node.getHostName());
ch.pipeline()
.addFirst(
"client_ssl_handler",
new ClientSSLHandler(ossks, hostnameVerificationEnabled, hostnameVerificationResovleHostName)
);
} else {
logger.debug("Connection to {} needs to be non ssl", node.getHostName());
}
}
/**
*
* @param ctx ChannelHandlerContext
* @param cause Throwable
* @throws Exception
*/
@Override
public final void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
if (cause instanceof DecoderException && cause != null) {
cause = cause.getCause();
}
logger.error("Exception during establishing a SSL connection: " + cause, cause);
super.exceptionCaught(ctx, cause);
}
}
}