|
6 | 6 | */
|
7 | 7 | package org.elasticsearch.xpack.core.security.transport.netty4;
|
8 | 8 |
|
| 9 | +import io.netty.bootstrap.Bootstrap; |
9 | 10 | import io.netty.channel.Channel;
|
10 | 11 | import io.netty.channel.ChannelHandler;
|
11 | 12 | import io.netty.channel.ChannelHandlerContext;
|
| 13 | +import io.netty.channel.ChannelOption; |
12 | 14 | import io.netty.channel.ChannelOutboundHandlerAdapter;
|
13 | 15 | import io.netty.channel.ChannelPromise;
|
14 | 16 | import io.netty.handler.ssl.SslHandler;
|
|
22 | 24 | import org.elasticsearch.common.network.NetworkService;
|
23 | 25 | import org.elasticsearch.common.settings.Settings;
|
24 | 26 | import org.elasticsearch.common.ssl.SslConfiguration;
|
| 27 | +import org.elasticsearch.common.unit.ByteSizeValue; |
25 | 28 | import org.elasticsearch.common.util.PageCacheRecycler;
|
26 | 29 | import org.elasticsearch.indices.breaker.CircuitBreakerService;
|
27 | 30 | import org.elasticsearch.threadpool.ThreadPool;
|
28 | 31 | import org.elasticsearch.transport.ConnectTransportException;
|
29 | 32 | import org.elasticsearch.transport.ConnectionProfile;
|
| 33 | +import org.elasticsearch.transport.RemoteClusterPortSettings; |
30 | 34 | import org.elasticsearch.transport.TcpChannel;
|
31 | 35 | import org.elasticsearch.transport.TransportSettings;
|
32 | 36 | import org.elasticsearch.transport.netty4.Netty4Transport;
|
@@ -66,6 +70,7 @@ public class SecurityNetty4Transport extends Netty4Transport {
|
66 | 70 | private final boolean remoteClusterPortEnabled;
|
67 | 71 | private final boolean remoteClusterServerSslEnabled;
|
68 | 72 | private final SslConfiguration remoteClusterClientSslConfiguration;
|
| 73 | + private final RemoteClusterClientBootstrapOptions remoteClusterClientBootstrapOptions; |
69 | 74 |
|
70 | 75 | public SecurityNetty4Transport(
|
71 | 76 | final Settings settings,
|
@@ -104,6 +109,7 @@ public SecurityNetty4Transport(
|
104 | 109 | } else {
|
105 | 110 | this.remoteClusterClientSslConfiguration = null;
|
106 | 111 | }
|
| 112 | + this.remoteClusterClientBootstrapOptions = RemoteClusterClientBootstrapOptions.fromSettings(settings); |
107 | 113 | }
|
108 | 114 |
|
109 | 115 | @Override
|
@@ -143,6 +149,21 @@ protected ChannelHandler getClientChannelInitializer(DiscoveryNode node, Connect
|
143 | 149 | return new SecurityClientChannelInitializer(node, connectionProfile);
|
144 | 150 | }
|
145 | 151 |
|
| 152 | + @Override |
| 153 | + protected Bootstrap getClientBootstrap(ConnectionProfile connectionProfile) { |
| 154 | + final Bootstrap bootstrap = super.getClientBootstrap(connectionProfile); |
| 155 | + if (false == REMOTE_CLUSTER_PROFILE.equals(connectionProfile.getTransportProfile()) |
| 156 | + || remoteClusterClientBootstrapOptions.isEmpty()) { |
| 157 | + return bootstrap; |
| 158 | + } |
| 159 | + |
| 160 | + logger.trace("reconfiguring client bootstrap for remote cluster client connection"); |
| 161 | + // Only client connections to a new RCS remote cluster can have transport profile of _remote_cluster |
| 162 | + // All other client connections use the default transport profile regardless of the transport profile used on the server side. |
| 163 | + remoteClusterClientBootstrapOptions.configure(bootstrap); |
| 164 | + return bootstrap; |
| 165 | + } |
| 166 | + |
146 | 167 | @Override
|
147 | 168 | public void onException(TcpChannel channel, Exception e) {
|
148 | 169 | exceptionHandler.accept(channel, e);
|
@@ -279,4 +300,163 @@ public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, Sock
|
279 | 300 | super.connect(ctx, remoteAddress, localAddress, connectPromise);
|
280 | 301 | }
|
281 | 302 | }
|
| 303 | + |
| 304 | + // This class captures the differences of client side TCP network settings between default and _remote_cluster transport profiles. |
| 305 | + // A field will be null if there is no difference between associated settings of the two profiles. It has a non-null value only |
| 306 | + // when the _remote_cluster profile has a different value from the default profile. |
| 307 | + record RemoteClusterClientBootstrapOptions( |
| 308 | + Boolean tcpNoDelay, |
| 309 | + Boolean tcpKeepAlive, |
| 310 | + Integer tcpKeepIdle, |
| 311 | + Integer tcpKeepInterval, |
| 312 | + Integer tcpKeepCount, |
| 313 | + ByteSizeValue tcpSendBufferSize, |
| 314 | + ByteSizeValue tcpReceiveBufferSize, |
| 315 | + Boolean tcpReuseAddress |
| 316 | + ) { |
| 317 | + |
| 318 | + boolean isEmpty() { |
| 319 | + return tcpNoDelay == null |
| 320 | + && tcpKeepAlive == null |
| 321 | + && tcpKeepIdle == null |
| 322 | + && tcpKeepInterval == null |
| 323 | + && tcpKeepCount == null |
| 324 | + && tcpSendBufferSize == null |
| 325 | + && tcpReceiveBufferSize == null |
| 326 | + && tcpReuseAddress == null; |
| 327 | + } |
| 328 | + |
| 329 | + void configure(Bootstrap bootstrap) { |
| 330 | + if (tcpNoDelay != null) { |
| 331 | + bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay); |
| 332 | + } |
| 333 | + |
| 334 | + if (tcpKeepAlive != null) { |
| 335 | + bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive); |
| 336 | + if (tcpKeepAlive) { |
| 337 | + // Note that Netty logs a warning if it can't set the option |
| 338 | + if (tcpKeepIdle != null) { |
| 339 | + if (tcpKeepIdle >= 0) { |
| 340 | + bootstrap.option(OPTION_TCP_KEEP_IDLE, tcpKeepIdle); |
| 341 | + } else { |
| 342 | + bootstrap.option(OPTION_TCP_KEEP_IDLE, null); |
| 343 | + } |
| 344 | + } |
| 345 | + if (tcpKeepInterval != null) { |
| 346 | + if (tcpKeepInterval >= 0) { |
| 347 | + bootstrap.option(OPTION_TCP_KEEP_INTERVAL, tcpKeepInterval); |
| 348 | + } else { |
| 349 | + bootstrap.option(OPTION_TCP_KEEP_INTERVAL, null); |
| 350 | + } |
| 351 | + } |
| 352 | + if (tcpKeepCount != null) { |
| 353 | + if (tcpKeepCount >= 0) { |
| 354 | + bootstrap.option(OPTION_TCP_KEEP_COUNT, tcpKeepCount); |
| 355 | + } else { |
| 356 | + bootstrap.option(OPTION_TCP_KEEP_COUNT, null); |
| 357 | + } |
| 358 | + } |
| 359 | + } else { |
| 360 | + bootstrap.option(OPTION_TCP_KEEP_IDLE, null); |
| 361 | + bootstrap.option(OPTION_TCP_KEEP_INTERVAL, null); |
| 362 | + bootstrap.option(OPTION_TCP_KEEP_COUNT, null); |
| 363 | + } |
| 364 | + } |
| 365 | + |
| 366 | + if (tcpSendBufferSize != null) { |
| 367 | + if (tcpSendBufferSize.getBytes() > 0) { |
| 368 | + bootstrap.option(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.getBytes())); |
| 369 | + } else { |
| 370 | + bootstrap.option(ChannelOption.SO_SNDBUF, null); |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + if (tcpReceiveBufferSize != null) { |
| 375 | + if (tcpReceiveBufferSize.getBytes() > 0) { |
| 376 | + bootstrap.option(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.getBytes())); |
| 377 | + } else { |
| 378 | + bootstrap.option(ChannelOption.SO_RCVBUF, null); |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + if (tcpReuseAddress != null) { |
| 383 | + bootstrap.option(ChannelOption.SO_REUSEADDR, tcpReuseAddress); |
| 384 | + } |
| 385 | + } |
| 386 | + |
| 387 | + static RemoteClusterClientBootstrapOptions fromSettings(Settings settings) { |
| 388 | + Boolean tcpNoDelay = RemoteClusterPortSettings.TCP_NO_DELAY.get(settings); |
| 389 | + if (tcpNoDelay == TransportSettings.TCP_NO_DELAY.get(settings)) { |
| 390 | + tcpNoDelay = null; |
| 391 | + } |
| 392 | + |
| 393 | + // It is possible that both default and _remote_cluster enable keepAlive but have different |
| 394 | + // values for either keepIdle, keepInterval or keepCount. In this case, we need have a |
| 395 | + // non-null value for keepAlive even it is the same between default and _remote_cluster. |
| 396 | + Boolean tcpKeepAlive = RemoteClusterPortSettings.TCP_KEEP_ALIVE.get(settings); |
| 397 | + Integer tcpKeepIdle = RemoteClusterPortSettings.TCP_KEEP_IDLE.get(settings); |
| 398 | + Integer tcpKeepInterval = RemoteClusterPortSettings.TCP_KEEP_INTERVAL.get(settings); |
| 399 | + Integer tcpKeepCount = RemoteClusterPortSettings.TCP_KEEP_COUNT.get(settings); |
| 400 | + final Boolean defaultTcpKeepAlive = TransportSettings.TCP_KEEP_ALIVE.get(settings); |
| 401 | + |
| 402 | + if (tcpKeepAlive) { |
| 403 | + if (defaultTcpKeepAlive) { |
| 404 | + // Both profiles have keepAlive enabled, we need to check whether any keepIdle, keepInterval, keepCount is different |
| 405 | + if (tcpKeepIdle.equals(TransportSettings.TCP_KEEP_IDLE.get(settings))) { |
| 406 | + tcpKeepIdle = null; |
| 407 | + } |
| 408 | + if (tcpKeepInterval.equals(TransportSettings.TCP_KEEP_INTERVAL.get(settings))) { |
| 409 | + tcpKeepInterval = null; |
| 410 | + } |
| 411 | + if (tcpKeepCount.equals(TransportSettings.TCP_KEEP_COUNT.get(settings))) { |
| 412 | + tcpKeepCount = null; |
| 413 | + } |
| 414 | + if (tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null) { |
| 415 | + // If keepIdle, keepInterval, keepCount are all identical, keepAlive can be null as well. |
| 416 | + // That is no need to update anything keepXxx related |
| 417 | + tcpKeepAlive = null; |
| 418 | + } |
| 419 | + } |
| 420 | + } else { |
| 421 | + if (false == defaultTcpKeepAlive) { |
| 422 | + tcpKeepAlive = null; |
| 423 | + } |
| 424 | + // _remote_cluster has keepAlive disabled, all other keepXxx has no reason to exist |
| 425 | + tcpKeepIdle = null; |
| 426 | + tcpKeepInterval = null; |
| 427 | + tcpKeepCount = null; |
| 428 | + } |
| 429 | + |
| 430 | + assert (tcpKeepAlive == null && tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null) |
| 431 | + || (tcpKeepAlive == false && tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null) |
| 432 | + || (tcpKeepAlive && (tcpKeepIdle != null || tcpKeepInterval != null || tcpKeepCount != null)) |
| 433 | + : "keepAlive == true must be accompanied with either keepIdle, keepInterval or keepCount change"; |
| 434 | + |
| 435 | + ByteSizeValue tcpSendBufferSize = RemoteClusterPortSettings.TCP_SEND_BUFFER_SIZE.get(settings); |
| 436 | + if (tcpSendBufferSize.equals(TransportSettings.TCP_SEND_BUFFER_SIZE.get(settings))) { |
| 437 | + tcpSendBufferSize = null; |
| 438 | + } |
| 439 | + |
| 440 | + ByteSizeValue tcpReceiveBufferSize = RemoteClusterPortSettings.TCP_RECEIVE_BUFFER_SIZE.get(settings); |
| 441 | + if (tcpReceiveBufferSize.equals(TransportSettings.TCP_RECEIVE_BUFFER_SIZE.get(settings))) { |
| 442 | + tcpReceiveBufferSize = null; |
| 443 | + } |
| 444 | + |
| 445 | + Boolean tcpReuseAddress = RemoteClusterPortSettings.TCP_REUSE_ADDRESS.get(settings); |
| 446 | + if (tcpReuseAddress == TransportSettings.TCP_REUSE_ADDRESS.get(settings)) { |
| 447 | + tcpReuseAddress = null; |
| 448 | + } |
| 449 | + |
| 450 | + return new RemoteClusterClientBootstrapOptions( |
| 451 | + tcpNoDelay, |
| 452 | + tcpKeepAlive, |
| 453 | + tcpKeepIdle, |
| 454 | + tcpKeepInterval, |
| 455 | + tcpKeepCount, |
| 456 | + tcpSendBufferSize, |
| 457 | + tcpReceiveBufferSize, |
| 458 | + tcpReuseAddress |
| 459 | + ); |
| 460 | + } |
| 461 | + } |
282 | 462 | }
|
0 commit comments