9
9
import org .apache .logging .log4j .Logger ;
10
10
import org .apache .logging .log4j .message .ParameterizedMessage ;
11
11
import org .elasticsearch .Version ;
12
+ import org .elasticsearch .cluster .node .DiscoveryNode ;
12
13
import org .elasticsearch .common .Nullable ;
13
14
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
14
15
import org .elasticsearch .common .network .CloseableChannel ;
19
20
import org .elasticsearch .common .util .PageCacheRecycler ;
20
21
import org .elasticsearch .indices .breaker .CircuitBreakerService ;
21
22
import org .elasticsearch .nio .BytesChannelContext ;
23
+ import org .elasticsearch .nio .ChannelFactory ;
22
24
import org .elasticsearch .nio .InboundChannelBuffer ;
23
25
import org .elasticsearch .nio .NioSelector ;
24
26
import org .elasticsearch .nio .NioSocketChannel ;
25
27
import org .elasticsearch .nio .ServerChannelContext ;
26
28
import org .elasticsearch .nio .SocketChannelContext ;
27
29
import org .elasticsearch .threadpool .ThreadPool ;
30
+ import org .elasticsearch .transport .ConnectTransportException ;
28
31
import org .elasticsearch .transport .TcpChannel ;
29
32
import org .elasticsearch .transport .TcpTransport ;
30
33
import org .elasticsearch .transport .nio .NioTcpChannel ;
38
41
import org .elasticsearch .xpack .core .ssl .SSLService ;
39
42
import org .elasticsearch .xpack .security .transport .filter .IPFilter ;
40
43
44
+ import javax .net .ssl .SNIHostName ;
41
45
import javax .net .ssl .SSLEngine ;
46
+ import javax .net .ssl .SSLParameters ;
42
47
import java .io .IOException ;
43
48
import java .net .InetSocketAddress ;
44
49
import java .nio .ByteBuffer ;
47
52
import java .util .Collections ;
48
53
import java .util .Map ;
49
54
import java .util .function .Consumer ;
55
+ import java .util .function .Function ;
50
56
import java .util .function .Supplier ;
51
57
52
58
import static org .elasticsearch .xpack .core .security .SecurityField .setting ;
@@ -128,8 +134,29 @@ public void onException(TcpChannel channel, Exception e) {
128
134
}
129
135
130
136
@ Override
131
- protected TcpChannelFactory channelFactory (ProfileSettings profileSettings , boolean isClient ) {
132
- return new SecurityTcpChannelFactory (profileSettings , isClient );
137
+ protected TcpChannelFactory serverChannelFactory (ProfileSettings profileSettings ) {
138
+ return new SecurityTcpChannelFactory (profileSettings , false );
139
+ }
140
+
141
+ @ Override
142
+ protected Function <DiscoveryNode , TcpChannelFactory > clientChannelFactoryFunction (ProfileSettings profileSettings ) {
143
+ return (node ) -> {
144
+ final ChannelFactory .RawChannelFactory rawChannelFactory = new ChannelFactory .RawChannelFactory (profileSettings .tcpNoDelay ,
145
+ profileSettings .tcpKeepAlive , profileSettings .reuseAddress , Math .toIntExact (profileSettings .sendBufferSize .getBytes ()),
146
+ Math .toIntExact (profileSettings .receiveBufferSize .getBytes ()));
147
+ SNIHostName serverName ;
148
+ String configuredServerName = node .getAttributes ().get ("server_name" );
149
+ if (configuredServerName != null ) {
150
+ try {
151
+ serverName = new SNIHostName (configuredServerName );
152
+ } catch (IllegalArgumentException e ) {
153
+ throw new ConnectTransportException (node , "invalid DiscoveryNode server_name [" + configuredServerName + "]" , e );
154
+ }
155
+ } else {
156
+ serverName = null ;
157
+ }
158
+ return new SecurityClientTcpChannelFactory (rawChannelFactory , serverName );
159
+ };
133
160
}
134
161
135
162
private class SecurityTcpChannelFactory extends TcpChannelFactory {
@@ -139,12 +166,16 @@ private class SecurityTcpChannelFactory extends TcpChannelFactory {
139
166
private final NioIPFilter ipFilter ;
140
167
141
168
private SecurityTcpChannelFactory (ProfileSettings profileSettings , boolean isClient ) {
142
- super (new RawChannelFactory (profileSettings .tcpNoDelay ,
169
+ this (new RawChannelFactory (profileSettings .tcpNoDelay ,
143
170
profileSettings .tcpKeepAlive ,
144
171
profileSettings .reuseAddress ,
145
172
Math .toIntExact (profileSettings .sendBufferSize .getBytes ()),
146
- Math .toIntExact (profileSettings .receiveBufferSize .getBytes ())));
147
- this .profileName = profileSettings .profileName ;
173
+ Math .toIntExact (profileSettings .receiveBufferSize .getBytes ())), profileSettings .profileName , isClient );
174
+ }
175
+
176
+ private SecurityTcpChannelFactory (RawChannelFactory rawChannelFactory , String profileName , boolean isClient ) {
177
+ super (rawChannelFactory );
178
+ this .profileName = profileName ;
148
179
this .isClient = isClient ;
149
180
this .ipFilter = new NioIPFilter (authenticator , profileName );
150
181
}
@@ -162,18 +193,7 @@ public NioTcpChannel createChannel(NioSelector selector, SocketChannel channel)
162
193
163
194
SocketChannelContext context ;
164
195
if (sslEnabled ) {
165
- SSLEngine sslEngine ;
166
- SSLConfiguration defaultConfig = profileConfiguration .get (TcpTransport .DEFAULT_PROFILE );
167
- SSLConfiguration sslConfig = profileConfiguration .getOrDefault (profileName , defaultConfig );
168
- boolean hostnameVerificationEnabled = sslConfig .verificationMode ().isHostnameVerificationEnabled ();
169
- if (hostnameVerificationEnabled ) {
170
- InetSocketAddress inetSocketAddress = (InetSocketAddress ) channel .getRemoteAddress ();
171
- // we create the socket based on the name given. don't reverse DNS
172
- sslEngine = sslService .createSSLEngine (sslConfig , inetSocketAddress .getHostString (), inetSocketAddress .getPort ());
173
- } else {
174
- sslEngine = sslService .createSSLEngine (sslConfig , null , -1 );
175
- }
176
- SSLDriver sslDriver = new SSLDriver (sslEngine , isClient );
196
+ SSLDriver sslDriver = new SSLDriver (createSSLEngine (channel ), isClient );
177
197
context = new SSLChannelContext (nioChannel , selector , exceptionHandler , sslDriver , readWriteHandler , buffer , ipFilter );
178
198
} else {
179
199
context = new BytesChannelContext (nioChannel , selector , exceptionHandler , readWriteHandler , buffer , ipFilter );
@@ -192,5 +212,46 @@ public NioTcpServerChannel createServerChannel(NioSelector selector, ServerSocke
192
212
nioChannel .setContext (context );
193
213
return nioChannel ;
194
214
}
215
+
216
+ protected SSLEngine createSSLEngine (SocketChannel channel ) throws IOException {
217
+ SSLEngine sslEngine ;
218
+ SSLConfiguration defaultConfig = profileConfiguration .get (TcpTransport .DEFAULT_PROFILE );
219
+ SSLConfiguration sslConfig = profileConfiguration .getOrDefault (profileName , defaultConfig );
220
+ boolean hostnameVerificationEnabled = sslConfig .verificationMode ().isHostnameVerificationEnabled ();
221
+ if (hostnameVerificationEnabled ) {
222
+ InetSocketAddress inetSocketAddress = (InetSocketAddress ) channel .getRemoteAddress ();
223
+ // we create the socket based on the name given. don't reverse DNS
224
+ sslEngine = sslService .createSSLEngine (sslConfig , inetSocketAddress .getHostString (), inetSocketAddress .getPort ());
225
+ } else {
226
+ sslEngine = sslService .createSSLEngine (sslConfig , null , -1 );
227
+ }
228
+ return sslEngine ;
229
+ }
230
+ }
231
+
232
+ private class SecurityClientTcpChannelFactory extends SecurityTcpChannelFactory {
233
+
234
+ private final SNIHostName serverName ;
235
+
236
+ private SecurityClientTcpChannelFactory (RawChannelFactory rawChannelFactory , SNIHostName serverName ) {
237
+ super (rawChannelFactory , TcpTransport .DEFAULT_PROFILE , true );
238
+ this .serverName = serverName ;
239
+ }
240
+
241
+ @ Override
242
+ public NioTcpServerChannel createServerChannel (NioSelector selector , ServerSocketChannel channel ) {
243
+ throw new AssertionError ("Cannot create TcpServerChannel with client factory" );
244
+ }
245
+
246
+ @ Override
247
+ protected SSLEngine createSSLEngine (SocketChannel channel ) throws IOException {
248
+ SSLEngine sslEngine = super .createSSLEngine (channel );
249
+ if (serverName != null ) {
250
+ SSLParameters sslParameters = sslEngine .getSSLParameters ();
251
+ sslParameters .setServerNames (Collections .singletonList (serverName ));
252
+ sslEngine .setSSLParameters (sslParameters );
253
+ }
254
+ return sslEngine ;
255
+ }
195
256
}
196
257
}
0 commit comments