-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright 2015-2017 floragunn GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.http.netty4.ssl; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.network.NetworkService; | ||
import org.opensearch.common.settings.ClusterSettings; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.BigArrays; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.http.HttpChannel; | ||
import org.opensearch.http.HttpHandlingSettings; | ||
import org.opensearch.http.netty4.Netty4HttpChannel; | ||
import org.opensearch.http.netty4.Netty4HttpServerTransport; | ||
import org.opensearch.plugins.SecureSettingProvider; | ||
import org.opensearch.telemetry.tracing.Tracer; | ||
import org.opensearch.threadpool.ThreadPool; | ||
import org.opensearch.transport.SharedGroupFactory; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.handler.codec.DecoderException; | ||
import io.netty.handler.ssl.ApplicationProtocolNames; | ||
import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; | ||
import io.netty.handler.ssl.SslHandler; | ||
|
||
public class SecureNetty4HttpServerTransport extends Netty4HttpServerTransport { | ||
private static final Logger logger = LogManager.getLogger(SecureNetty4HttpServerTransport.class); | ||
Check warning on line 59 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L59
|
||
private final SecureSettingProvider ssp; | ||
private final SecureSettingProvider.ServerExceptionHandler exceptionHandler; | ||
|
||
public SecureNetty4HttpServerTransport( | ||
final Settings settings, | ||
final NetworkService networkService, | ||
final BigArrays bigArrays, | ||
final ThreadPool threadPool, | ||
final NamedXContentRegistry namedXContentRegistry, | ||
final Dispatcher dispatcher, | ||
final ClusterSettings clusterSettings, | ||
final SharedGroupFactory sharedGroupFactory, | ||
final SecureSettingProvider ssp, | ||
final Tracer tracer | ||
) { | ||
super( | ||
Check warning on line 75 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L75
|
||
settings, | ||
networkService, | ||
bigArrays, | ||
threadPool, | ||
namedXContentRegistry, | ||
dispatcher, | ||
clusterSettings, | ||
sharedGroupFactory, | ||
tracer | ||
); | ||
this.ssp = ssp; | ||
this.exceptionHandler = ssp.buildHttpServerExceptionHandler(settings, this) | ||
.orElse(SecureSettingProvider.ServerExceptionHandler.NOOP); | ||
} | ||
Check warning on line 89 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L86-L89
|
||
|
||
@Override | ||
public ChannelHandler configureServerChannelHandler() { | ||
return new SslHttpChannelHandler(this, handlingSettings); | ||
Check warning on line 93 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L93
|
||
} | ||
|
||
@Override | ||
public void onException(HttpChannel channel, Exception cause0) { | ||
Throwable cause = cause0; | ||
Check warning on line 98 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L98
|
||
|
||
if (cause0 instanceof DecoderException && cause0 != null) { | ||
cause = cause0.getCause(); | ||
Check warning on line 101 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L101
|
||
} | ||
|
||
exceptionHandler.onError(cause); | ||
logger.error("Exception during establishing a SSL connection: " + cause, cause); | ||
super.onException(channel, cause0); | ||
} | ||
Check warning on line 107 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L104-L107
|
||
|
||
protected class SslHttpChannelHandler extends Netty4HttpServerTransport.HttpChannelHandler { | ||
/** | ||
* Application negotiation handler to select either HTTP 1.1 or HTTP 2 protocol, based | ||
* on client/server ALPN negotiations. | ||
*/ | ||
private class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler { | ||
protected Http2OrHttpHandler() { | ||
super(ApplicationProtocolNames.HTTP_1_1); | ||
} | ||
Check warning on line 117 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L115-L117
|
||
|
||
@Override | ||
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { | ||
if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { | ||
configureDefaultHttp2Pipeline(ctx.pipeline()); | ||
Check warning on line 122 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L122
|
||
} else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) { | ||
configureDefaultHttpPipeline(ctx.pipeline()); | ||
Check warning on line 124 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L124
|
||
} else { | ||
throw new IllegalStateException("Unknown application protocol: " + protocol); | ||
Check warning on line 126 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L126
|
||
} | ||
} | ||
Check warning on line 128 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L128
|
||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
super.exceptionCaught(ctx, cause); | ||
Netty4HttpChannel channel = ctx.channel().attr(HTTP_CHANNEL_KEY).get(); | ||
Check warning on line 133 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L132-L133
|
||
if (channel != null) { | ||
if (cause instanceof Error) { | ||
onException(channel, new Exception(cause)); | ||
Check warning on line 136 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L136
|
||
} else { | ||
onException(channel, (Exception) cause); | ||
Check warning on line 138 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L138
|
||
} | ||
} | ||
} | ||
Check warning on line 141 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L141
|
||
} | ||
|
||
protected SslHttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) { | ||
super(transport, handlingSettings); | ||
} | ||
Check warning on line 146 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L144-L146
|
||
|
||
@Override | ||
protected void initChannel(Channel ch) throws Exception { | ||
super.initChannel(ch); | ||
Check warning on line 150 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L150
|
||
|
||
final SSLEngine sslEngine = ssp.buildSecureHttpServerEngine(settings, SecureNetty4HttpServerTransport.this) | ||
.orElseGet(SSLContext.getDefault()::createSSLEngine); | ||
Check warning on line 153 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L152-L153
|
||
|
||
final SslHandler sslHandler = new SslHandler(sslEngine); | ||
ch.pipeline().addFirst("ssl_http", sslHandler); | ||
} | ||
Check warning on line 157 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L155-L157
|
||
|
||
@Override | ||
protected void configurePipeline(Channel ch) { | ||
ch.pipeline().addLast(new Http2OrHttpHandler()); | ||
} | ||
Check warning on line 162 in modules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/http/netty4/ssl/SecureNetty4HttpServerTransport.java#L161-L162
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
package org.opensearch.transport.netty4.ssl; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.plugins.SecureSettingProvider; | ||
import org.opensearch.transport.TcpTransport; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLEngine; | ||
import javax.net.ssl.SSLException; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.handler.codec.ByteToMessageDecoder; | ||
import io.netty.handler.ssl.SslHandler; | ||
|
||
/** | ||
* Modifies the current pipeline dynamically to enable TLS | ||
*/ | ||
public class DualModeSslHandler extends ByteToMessageDecoder { | ||
|
||
private static final Logger logger = LogManager.getLogger(DualModeSslHandler.class); | ||
Check warning on line 40 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L40
|
||
private final Settings settings; | ||
private final SecureSettingProvider ssp; | ||
private final TcpTransport transport; | ||
private final SslHandler providedSSLHandler; | ||
|
||
public DualModeSslHandler(final Settings settings, final SecureSettingProvider ssp, final TcpTransport transport) { | ||
this(settings, ssp, transport, null); | ||
} | ||
Check warning on line 48 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L47-L48
|
||
|
||
protected DualModeSslHandler( | ||
final Settings settings, | ||
final SecureSettingProvider ssp, | ||
final TcpTransport transport, | ||
SslHandler providedSSLHandler | ||
) { | ||
this.settings = settings; | ||
this.ssp = ssp; | ||
this.transport = transport; | ||
this.providedSSLHandler = providedSSLHandler; | ||
} | ||
Check warning on line 60 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L55-L60
|
||
|
||
@Override | ||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { | ||
// Will use the first six bytes to detect a protocol. | ||
if (in.readableBytes() < 6) { | ||
return; | ||
Check warning on line 66 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L66
|
||
} | ||
int offset = in.readerIndex(); | ||
Check warning on line 68 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L68
|
||
if (in.getCharSequence(offset, 6, StandardCharsets.UTF_8).equals(SecureConnectionTestUtil.DUAL_MODE_CLIENT_HELLO_MSG)) { | ||
logger.debug("Received DualSSL Client Hello message"); | ||
ByteBuf responseBuffer = Unpooled.buffer(6); | ||
responseBuffer.writeCharSequence(SecureConnectionTestUtil.DUAL_MODE_SERVER_HELLO_MSG, StandardCharsets.UTF_8); | ||
ctx.writeAndFlush(responseBuffer).addListener(ChannelFutureListener.CLOSE); | ||
return; | ||
Check warning on line 74 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L70-L74
|
||
} | ||
|
||
if (SslUtils.isTLS(in)) { | ||
logger.debug("Identified request as SSL request"); | ||
enableSsl(ctx); | ||
Check warning on line 79 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L78-L79
|
||
} else { | ||
logger.debug("Identified request as non SSL request, running in HTTP mode as dual mode is enabled"); | ||
ctx.pipeline().remove(this); | ||
Check warning on line 82 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L81-L82
|
||
} | ||
} | ||
Check warning on line 84 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L84
|
||
|
||
private void enableSsl(ChannelHandlerContext ctx) throws SSLException, NoSuchAlgorithmException { | ||
final SSLEngine sslEngine = ssp.buildSecureServerTransportEngine(settings, transport) | ||
.orElseGet(SSLContext.getDefault()::createSSLEngine); | ||
Check warning on line 88 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L87-L88
|
||
|
||
SslHandler sslHandler; | ||
if (providedSSLHandler != null) { | ||
sslHandler = providedSSLHandler; | ||
Check warning on line 92 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L92
|
||
} else { | ||
sslHandler = new SslHandler(sslEngine); | ||
Check warning on line 94 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L94
|
||
} | ||
ChannelPipeline p = ctx.pipeline(); | ||
p.addAfter("port_unification_handler", "ssl_server", sslHandler); | ||
p.remove(this); | ||
logger.debug("Removed port unification handler and added SSL handler as incoming request is SSL"); | ||
} | ||
Check warning on line 100 in modules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java Codecov / codecov/patchmodules/transport-netty4/src/main/java/org/opensearch/transport/netty4/ssl/DualModeSslHandler.java#L96-L100
|
||
} |