|
| 1 | +/* |
| 2 | + * Copyright 2015-2017 floragunn GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.opensearch.http.netty4.ssl; |
| 19 | + |
| 20 | +import org.apache.logging.log4j.LogManager; |
| 21 | +import org.apache.logging.log4j.Logger; |
| 22 | +import org.opensearch.common.network.NetworkService; |
| 23 | +import org.opensearch.common.settings.ClusterSettings; |
| 24 | +import org.opensearch.common.settings.Settings; |
| 25 | +import org.opensearch.common.util.BigArrays; |
| 26 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 27 | +import org.opensearch.http.HttpChannel; |
| 28 | +import org.opensearch.http.HttpHandlingSettings; |
| 29 | +import org.opensearch.http.netty4.Netty4HttpChannel; |
| 30 | +import org.opensearch.http.netty4.Netty4HttpServerTransport; |
| 31 | +import org.opensearch.plugins.SecureSettingProvider; |
| 32 | +import org.opensearch.telemetry.tracing.Tracer; |
| 33 | +import org.opensearch.threadpool.ThreadPool; |
| 34 | +import org.opensearch.transport.SharedGroupFactory; |
| 35 | + |
| 36 | +import javax.net.ssl.SSLContext; |
| 37 | +import javax.net.ssl.SSLEngine; |
| 38 | + |
| 39 | +import io.netty.channel.Channel; |
| 40 | +import io.netty.channel.ChannelHandler; |
| 41 | +import io.netty.channel.ChannelHandlerContext; |
| 42 | +import io.netty.handler.codec.DecoderException; |
| 43 | +import io.netty.handler.ssl.ApplicationProtocolNames; |
| 44 | +import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; |
| 45 | +import io.netty.handler.ssl.SslHandler; |
| 46 | + |
| 47 | +public class SecureNetty4HttpServerTransport extends Netty4HttpServerTransport { |
| 48 | + private static final Logger logger = LogManager.getLogger(SecureNetty4HttpServerTransport.class); |
| 49 | + private final SecureSettingProvider ssp; |
| 50 | + |
| 51 | + public SecureNetty4HttpServerTransport( |
| 52 | + final Settings settings, |
| 53 | + final NetworkService networkService, |
| 54 | + final BigArrays bigArrays, |
| 55 | + final ThreadPool threadPool, |
| 56 | + final NamedXContentRegistry namedXContentRegistry, |
| 57 | + final Dispatcher dispatcher, |
| 58 | + final ClusterSettings clusterSettings, |
| 59 | + final SharedGroupFactory sharedGroupFactory, |
| 60 | + final SecureSettingProvider ssp, |
| 61 | + final Tracer tracer |
| 62 | + ) { |
| 63 | + super( |
| 64 | + settings, |
| 65 | + networkService, |
| 66 | + bigArrays, |
| 67 | + threadPool, |
| 68 | + namedXContentRegistry, |
| 69 | + dispatcher, |
| 70 | + clusterSettings, |
| 71 | + sharedGroupFactory, |
| 72 | + tracer |
| 73 | + ); |
| 74 | + this.ssp = ssp; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ChannelHandler configureServerChannelHandler() { |
| 79 | + return new SslHttpChannelHandler(this, handlingSettings); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onException(HttpChannel channel, Exception cause0) { |
| 84 | + Throwable cause = cause0; |
| 85 | + |
| 86 | + if (cause0 instanceof DecoderException && cause0 != null) { |
| 87 | + cause = cause0.getCause(); |
| 88 | + } |
| 89 | + |
| 90 | + logger.error("Exception during establishing a SSL connection: " + cause, cause); |
| 91 | + super.onException(channel, cause0); |
| 92 | + } |
| 93 | + |
| 94 | + protected class SslHttpChannelHandler extends Netty4HttpServerTransport.HttpChannelHandler { |
| 95 | + /** |
| 96 | + * Application negotiation handler to select either HTTP 1.1 or HTTP 2 protocol, based |
| 97 | + * on client/server ALPN negotiations. |
| 98 | + */ |
| 99 | + private class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler { |
| 100 | + protected Http2OrHttpHandler() { |
| 101 | + super(ApplicationProtocolNames.HTTP_1_1); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { |
| 106 | + if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { |
| 107 | + configureDefaultHttp2Pipeline(ctx.pipeline()); |
| 108 | + } else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) { |
| 109 | + configureDefaultHttpPipeline(ctx.pipeline()); |
| 110 | + } else { |
| 111 | + throw new IllegalStateException("Unknown application protocol: " + protocol); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| 117 | + super.exceptionCaught(ctx, cause); |
| 118 | + Netty4HttpChannel channel = ctx.channel().attr(HTTP_CHANNEL_KEY).get(); |
| 119 | + if (channel != null) { |
| 120 | + if (cause instanceof Error) { |
| 121 | + onException(channel, new Exception(cause)); |
| 122 | + } else { |
| 123 | + onException(channel, (Exception) cause); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + protected SslHttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) { |
| 130 | + super(transport, handlingSettings); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + protected void initChannel(Channel ch) throws Exception { |
| 135 | + super.initChannel(ch); |
| 136 | + |
| 137 | + final SSLEngine sslEngine = ssp.buildSecureHttpEngine(settings, SecureNetty4HttpServerTransport.this) |
| 138 | + .orElseGet(SSLContext.getDefault()::createSSLEngine); |
| 139 | + |
| 140 | + final SslHandler sslHandler = new SslHandler(sslEngine); |
| 141 | + ch.pipeline().addFirst("ssl_http", sslHandler); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + protected void configurePipeline(Channel ch) { |
| 146 | + ch.pipeline().addLast(new Http2OrHttpHandler()); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments