|
| 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 | +/* |
| 19 | + * SPDX-License-Identifier: Apache-2.0 |
| 20 | + * |
| 21 | + * The OpenSearch Contributors require contributions made to |
| 22 | + * this file be licensed under the Apache-2.0 license or a |
| 23 | + * compatible open source license. |
| 24 | + * |
| 25 | + * Modifications Copyright OpenSearch Contributors. See |
| 26 | + * GitHub history for details. |
| 27 | + */ |
| 28 | + |
| 29 | +package org.opensearch.http.netty4.ssl; |
| 30 | + |
| 31 | +import org.apache.logging.log4j.LogManager; |
| 32 | +import org.apache.logging.log4j.Logger; |
| 33 | +import org.opensearch.common.network.NetworkService; |
| 34 | +import org.opensearch.common.settings.ClusterSettings; |
| 35 | +import org.opensearch.common.settings.Settings; |
| 36 | +import org.opensearch.common.util.BigArrays; |
| 37 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 38 | +import org.opensearch.http.HttpChannel; |
| 39 | +import org.opensearch.http.HttpHandlingSettings; |
| 40 | +import org.opensearch.http.netty4.Netty4HttpChannel; |
| 41 | +import org.opensearch.http.netty4.Netty4HttpServerTransport; |
| 42 | +import org.opensearch.plugins.SecureSettingProvider; |
| 43 | +import org.opensearch.telemetry.tracing.Tracer; |
| 44 | +import org.opensearch.threadpool.ThreadPool; |
| 45 | +import org.opensearch.transport.SharedGroupFactory; |
| 46 | + |
| 47 | +import javax.net.ssl.SSLContext; |
| 48 | +import javax.net.ssl.SSLEngine; |
| 49 | + |
| 50 | +import io.netty.channel.Channel; |
| 51 | +import io.netty.channel.ChannelHandler; |
| 52 | +import io.netty.channel.ChannelHandlerContext; |
| 53 | +import io.netty.handler.codec.DecoderException; |
| 54 | +import io.netty.handler.ssl.ApplicationProtocolNames; |
| 55 | +import io.netty.handler.ssl.ApplicationProtocolNegotiationHandler; |
| 56 | +import io.netty.handler.ssl.SslHandler; |
| 57 | + |
| 58 | +public class SecureNetty4HttpServerTransport extends Netty4HttpServerTransport { |
| 59 | + private static final Logger logger = LogManager.getLogger(SecureNetty4HttpServerTransport.class); |
| 60 | + private final SecureSettingProvider ssp; |
| 61 | + private final SecureSettingProvider.ServerExceptionHandler exceptionHandler; |
| 62 | + |
| 63 | + public SecureNetty4HttpServerTransport( |
| 64 | + final Settings settings, |
| 65 | + final NetworkService networkService, |
| 66 | + final BigArrays bigArrays, |
| 67 | + final ThreadPool threadPool, |
| 68 | + final NamedXContentRegistry namedXContentRegistry, |
| 69 | + final Dispatcher dispatcher, |
| 70 | + final ClusterSettings clusterSettings, |
| 71 | + final SharedGroupFactory sharedGroupFactory, |
| 72 | + final SecureSettingProvider ssp, |
| 73 | + final Tracer tracer |
| 74 | + ) { |
| 75 | + super( |
| 76 | + settings, |
| 77 | + networkService, |
| 78 | + bigArrays, |
| 79 | + threadPool, |
| 80 | + namedXContentRegistry, |
| 81 | + dispatcher, |
| 82 | + clusterSettings, |
| 83 | + sharedGroupFactory, |
| 84 | + tracer |
| 85 | + ); |
| 86 | + this.ssp = ssp; |
| 87 | + this.exceptionHandler = ssp.buildHttpServerExceptionHandler(settings, this) |
| 88 | + .orElse(SecureSettingProvider.ServerExceptionHandler.NOOP); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public ChannelHandler configureServerChannelHandler() { |
| 93 | + return new SslHttpChannelHandler(this, handlingSettings); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onException(HttpChannel channel, Exception cause0) { |
| 98 | + Throwable cause = cause0; |
| 99 | + |
| 100 | + if (cause0 instanceof DecoderException && cause0 != null) { |
| 101 | + cause = cause0.getCause(); |
| 102 | + } |
| 103 | + |
| 104 | + exceptionHandler.onError(cause); |
| 105 | + logger.error("Exception during establishing a SSL connection: " + cause, cause); |
| 106 | + super.onException(channel, cause0); |
| 107 | + } |
| 108 | + |
| 109 | + protected class SslHttpChannelHandler extends Netty4HttpServerTransport.HttpChannelHandler { |
| 110 | + /** |
| 111 | + * Application negotiation handler to select either HTTP 1.1 or HTTP 2 protocol, based |
| 112 | + * on client/server ALPN negotiations. |
| 113 | + */ |
| 114 | + private class Http2OrHttpHandler extends ApplicationProtocolNegotiationHandler { |
| 115 | + protected Http2OrHttpHandler() { |
| 116 | + super(ApplicationProtocolNames.HTTP_1_1); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception { |
| 121 | + if (ApplicationProtocolNames.HTTP_2.equals(protocol)) { |
| 122 | + configureDefaultHttp2Pipeline(ctx.pipeline()); |
| 123 | + } else if (ApplicationProtocolNames.HTTP_1_1.equals(protocol)) { |
| 124 | + configureDefaultHttpPipeline(ctx.pipeline()); |
| 125 | + } else { |
| 126 | + throw new IllegalStateException("Unknown application protocol: " + protocol); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| 132 | + super.exceptionCaught(ctx, cause); |
| 133 | + Netty4HttpChannel channel = ctx.channel().attr(HTTP_CHANNEL_KEY).get(); |
| 134 | + if (channel != null) { |
| 135 | + if (cause instanceof Error) { |
| 136 | + onException(channel, new Exception(cause)); |
| 137 | + } else { |
| 138 | + onException(channel, (Exception) cause); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + protected SslHttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) { |
| 145 | + super(transport, handlingSettings); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected void initChannel(Channel ch) throws Exception { |
| 150 | + super.initChannel(ch); |
| 151 | + |
| 152 | + final SSLEngine sslEngine = ssp.buildSecureHttpServerEngine(settings, SecureNetty4HttpServerTransport.this) |
| 153 | + .orElseGet(SSLContext.getDefault()::createSSLEngine); |
| 154 | + |
| 155 | + final SslHandler sslHandler = new SslHandler(sslEngine); |
| 156 | + ch.pipeline().addFirst("ssl_http", sslHandler); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + protected void configurePipeline(Channel ch) { |
| 161 | + ch.pipeline().addLast(new Http2OrHttpHandler()); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments