12
12
import io .netty .channel .ChannelPromise ;
13
13
import io .netty .handler .ssl .SslHandler ;
14
14
import org .apache .logging .log4j .message .ParameterizedMessage ;
15
+ import org .elasticsearch .cluster .node .DiscoveryNode ;
15
16
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
16
17
import org .elasticsearch .common .network .NetworkService ;
17
18
import org .elasticsearch .common .settings .Settings ;
18
19
import org .elasticsearch .common .util .BigArrays ;
19
20
import org .elasticsearch .indices .breaker .CircuitBreakerService ;
20
21
import org .elasticsearch .threadpool .ThreadPool ;
22
+ import org .elasticsearch .transport .ConnectTransportException ;
21
23
import org .elasticsearch .transport .TcpChannel ;
22
24
import org .elasticsearch .transport .TcpTransport ;
23
25
import org .elasticsearch .transport .netty4 .Netty4Transport ;
26
28
import org .elasticsearch .xpack .core .ssl .SSLConfiguration ;
27
29
import org .elasticsearch .xpack .core .ssl .SSLService ;
28
30
31
+ import javax .net .ssl .SNIHostName ;
32
+ import javax .net .ssl .SNIServerName ;
29
33
import javax .net .ssl .SSLEngine ;
34
+ import javax .net .ssl .SSLParameters ;
30
35
import java .net .InetSocketAddress ;
31
36
import java .net .SocketAddress ;
32
37
import java .util .Collections ;
@@ -105,8 +110,8 @@ protected ChannelHandler getNoSslChannelInitializer(final String name) {
105
110
}
106
111
107
112
@ Override
108
- protected ChannelHandler getClientChannelInitializer () {
109
- return new SecurityClientChannelInitializer ();
113
+ protected ChannelHandler getClientChannelInitializer (DiscoveryNode node ) {
114
+ return new SecurityClientChannelInitializer (node );
110
115
}
111
116
112
117
@ Override
@@ -166,16 +171,28 @@ protected ServerChannelInitializer getSslChannelInitializer(final String name, f
166
171
private class SecurityClientChannelInitializer extends ClientChannelInitializer {
167
172
168
173
private final boolean hostnameVerificationEnabled ;
174
+ private final SNIHostName serverName ;
169
175
170
- SecurityClientChannelInitializer () {
176
+ SecurityClientChannelInitializer (DiscoveryNode node ) {
171
177
this .hostnameVerificationEnabled = sslEnabled && sslConfiguration .verificationMode ().isHostnameVerificationEnabled ();
178
+ String configuredServerName = node .getAttributes ().get ("server_name" );
179
+ if (configuredServerName != null ) {
180
+ try {
181
+ serverName = new SNIHostName (configuredServerName );
182
+ } catch (IllegalArgumentException e ) {
183
+ throw new ConnectTransportException (node , "invalid DiscoveryNode server_name [" + configuredServerName + "]" , e );
184
+ }
185
+ } else {
186
+ serverName = null ;
187
+ }
172
188
}
173
189
174
190
@ Override
175
191
protected void initChannel (Channel ch ) throws Exception {
176
192
super .initChannel (ch );
177
193
if (sslEnabled ) {
178
- ch .pipeline ().addFirst (new ClientSslHandlerInitializer (sslConfiguration , sslService , hostnameVerificationEnabled ));
194
+ ch .pipeline ().addFirst (new ClientSslHandlerInitializer (sslConfiguration , sslService , hostnameVerificationEnabled ,
195
+ serverName ));
179
196
}
180
197
}
181
198
}
@@ -185,11 +202,14 @@ private static class ClientSslHandlerInitializer extends ChannelOutboundHandlerA
185
202
private final boolean hostnameVerificationEnabled ;
186
203
private final SSLConfiguration sslConfiguration ;
187
204
private final SSLService sslService ;
205
+ private final SNIServerName serverName ;
188
206
189
- private ClientSslHandlerInitializer (SSLConfiguration sslConfiguration , SSLService sslService , boolean hostnameVerificationEnabled ) {
207
+ private ClientSslHandlerInitializer (SSLConfiguration sslConfiguration , SSLService sslService , boolean hostnameVerificationEnabled ,
208
+ SNIServerName serverName ) {
190
209
this .sslConfiguration = sslConfiguration ;
191
210
this .hostnameVerificationEnabled = hostnameVerificationEnabled ;
192
211
this .sslService = sslService ;
212
+ this .serverName = serverName ;
193
213
}
194
214
195
215
@ Override
@@ -206,6 +226,11 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
206
226
}
207
227
208
228
sslEngine .setUseClientMode (true );
229
+ if (serverName != null ) {
230
+ SSLParameters sslParameters = sslEngine .getSSLParameters ();
231
+ sslParameters .setServerNames (Collections .singletonList (serverName ));
232
+ sslEngine .setSSLParameters (sslParameters );
233
+ }
209
234
ctx .pipeline ().replace (this , "ssl" , new SslHandler (sslEngine ));
210
235
super .connect (ctx , remoteAddress , localAddress , promise );
211
236
}
0 commit comments