Skip to content

Commit 8df81fd

Browse files
authored
Implement SslHandler retrieval logic for transport-reactor-netty4 plugin (#19458)
Signed-off-by: Andriy Redko <[email protected]>
1 parent db5106b commit 8df81fd

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
7575
- Fix lag metric for pull-based ingestion when streaming source is empty ([#19393](https://github.com/opensearch-project/OpenSearch/pull/19393))
7676
- Fix ingestion state xcontent serialization in IndexMetadata and fail fast on mapping errors([#19320](https://github.com/opensearch-project/OpenSearch/pull/19320))
7777
- Fix updated keyword field params leading to stale responses from request cache ([#19385](https://github.com/opensearch-project/OpenSearch/pull/19385))
78+
- Implement SslHandler retrieval logic for transport-reactor-netty4 plugin ([#19458](https://github.com/opensearch-project/OpenSearch/pull/19458))
7879

7980
### Dependencies
8081
- Bump `com.gradleup.shadow:shadow-gradle-plugin` from 8.3.5 to 8.3.9 ([#19400](https://github.com/opensearch-project/OpenSearch/pull/19400))
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.http.reactor.netty4;
10+
11+
import javax.net.ssl.SSLEngine;
12+
13+
import java.util.Optional;
14+
15+
import io.netty.channel.Channel;
16+
import io.netty.channel.ChannelHandler;
17+
import io.netty.handler.ssl.SslHandler;
18+
import reactor.netty.NettyPipeline;
19+
import reactor.netty.http.server.HttpServerRequest;
20+
21+
final class ReactorNetty4BaseHttpChannel {
22+
private static final String CHANNEL_PROPERTY = "channel";
23+
private static final String SSL_HANDLER_PROPERTY = "ssl_http";
24+
private static final String SSL_ENGINE_PROPERTY = "ssl_engine";
25+
26+
private ReactorNetty4BaseHttpChannel() {}
27+
28+
@SuppressWarnings("unchecked")
29+
static <T> Optional<T> get(HttpServerRequest request, String name, Class<T> clazz) {
30+
if (CHANNEL_PROPERTY.equalsIgnoreCase(name) == true && clazz.isAssignableFrom(Channel.class) == true) {
31+
final Channel[] channels = new Channel[1];
32+
request.withConnection(connection -> { channels[0] = connection.channel(); });
33+
return Optional.of((T) channels[0]);
34+
} else if (SSL_HANDLER_PROPERTY.equalsIgnoreCase(name) == true || SSL_ENGINE_PROPERTY.equalsIgnoreCase(name) == true) {
35+
final ChannelHandler[] channels = new ChannelHandler[1];
36+
request.withConnection(connection -> {
37+
final Channel channel = connection.channel();
38+
if (channel.parent() != null) {
39+
channels[0] = channel.parent().pipeline().get(NettyPipeline.SslHandler);
40+
} else {
41+
channels[0] = channel.pipeline().get(NettyPipeline.SslHandler);
42+
}
43+
});
44+
if (channels[0] != null) {
45+
if (SSL_HANDLER_PROPERTY.equalsIgnoreCase(name) == true && clazz.isInstance(channels[0]) == true) {
46+
return Optional.of((T) channels[0]);
47+
} else if (SSL_ENGINE_PROPERTY.equalsIgnoreCase(name) == true
48+
&& clazz.isAssignableFrom(SSLEngine.class)
49+
&& channels[0] instanceof SslHandler h) {
50+
return Optional.of((T) h.engine());
51+
}
52+
}
53+
} else {
54+
final ChannelHandler[] channels = new ChannelHandler[1];
55+
request.withConnection(connection -> { channels[0] = connection.channel().pipeline().get(name); });
56+
if (channels[0] != null && clazz.isInstance(channels[0]) == true) {
57+
return Optional.of((T) channels[0]);
58+
}
59+
}
60+
61+
return Optional.empty();
62+
}
63+
}

plugins/transport-reactor-netty4/src/main/java/org/opensearch/http/reactor/netty4/ReactorNetty4HttpServerTransport.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import io.netty.handler.codec.http.HttpResponseStatus;
5454
import io.netty.handler.ssl.ApplicationProtocolConfig;
5555
import io.netty.handler.ssl.ApplicationProtocolNames;
56+
import io.netty.handler.ssl.ClientAuth;
5657
import io.netty.handler.ssl.SslContext;
5758
import io.netty.handler.ssl.SslContextBuilder;
5859
import io.netty.handler.ssl.SupportedCipherSuiteFilter;
@@ -317,6 +318,8 @@ private HttpServer configure(final HttpServer server) throws Exception {
317318
parameters.flatMap(SecureHttpTransportParameters::trustManagerFactory).ifPresent(sslContextBuilder::trustManager);
318319
parameters.map(SecureHttpTransportParameters::cipherSuites)
319320
.ifPresent(ciphers -> sslContextBuilder.ciphers(ciphers, SupportedCipherSuiteFilter.INSTANCE));
321+
parameters.flatMap(SecureHttpTransportParameters::clientAuth)
322+
.ifPresent(clientAuth -> sslContextBuilder.clientAuth(ClientAuth.valueOf(clientAuth)));
320323

321324
final SslContext sslContext = sslContextBuilder.protocols(
322325
parameters.map(SecureHttpTransportParameters::protocols).orElseGet(() -> Arrays.asList(SslUtils.DEFAULT_SSL_PROTOCOLS))

plugins/transport-reactor-netty4/src/main/java/org/opensearch/http/reactor/netty4/ReactorNetty4NonStreamingHttpChannel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.opensearch.transport.reactor.netty4.Netty4Utils;
1616

1717
import java.net.InetSocketAddress;
18+
import java.util.Optional;
1819
import java.util.concurrent.atomic.AtomicBoolean;
1920

2021
import io.netty.handler.codec.http.FullHttpResponse;
@@ -75,6 +76,11 @@ public InetSocketAddress getLocalAddress() {
7576
return (InetSocketAddress) response.hostAddress();
7677
}
7778

79+
@Override
80+
public <T> Optional<T> get(String name, Class<T> clazz) {
81+
return ReactorNetty4BaseHttpChannel.get(request, name, clazz);
82+
}
83+
7884
FullHttpResponse createResponse(HttpResponse response) {
7985
return (FullHttpResponse) response;
8086
}

plugins/transport-reactor-netty4/src/main/java/org/opensearch/http/reactor/netty4/ReactorNetty4StreamingHttpChannel.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.net.InetSocketAddress;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.Optional;
2223

2324
import io.netty.buffer.Unpooled;
2425
import io.netty.handler.codec.http.DefaultHttpContent;
@@ -123,6 +124,11 @@ public void subscribe(Subscriber<? super HttpChunk> subscriber) {
123124
receiver.subscribe(subscriber);
124125
}
125126

127+
@Override
128+
public <T> Optional<T> get(String name, Class<T> clazz) {
129+
return ReactorNetty4BaseHttpChannel.get(request, name, clazz);
130+
}
131+
126132
private static HttpContent createContent(HttpResponse response) {
127133
final FullHttpResponse fullHttpResponse = (FullHttpResponse) response;
128134
return new DefaultHttpContent(fullHttpResponse.content());

0 commit comments

Comments
 (0)