|
| 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.Netty4HttpServerTransport; |
| 41 | +import org.opensearch.plugins.SecureTransportSettingsProvider; |
| 42 | +import org.opensearch.telemetry.tracing.Tracer; |
| 43 | +import org.opensearch.threadpool.ThreadPool; |
| 44 | +import org.opensearch.transport.SharedGroupFactory; |
| 45 | +import org.opensearch.transport.netty4.ssl.SslUtils; |
| 46 | + |
| 47 | +import javax.net.ssl.SSLEngine; |
| 48 | + |
| 49 | +import io.netty.channel.Channel; |
| 50 | +import io.netty.channel.ChannelHandler; |
| 51 | +import io.netty.handler.codec.DecoderException; |
| 52 | +import io.netty.handler.ssl.SslHandler; |
| 53 | + |
| 54 | +/** |
| 55 | + * @see <a href="https://github.com/opensearch-project/security/blob/d526c9f6c2a438c14db8b413148204510b9fe2e2/src/main/java/org/opensearch/security/ssl/http/netty/SecuritySSLNettyHttpServerTransport.java">SecuritySSLNettyHttpServerTransport</a> |
| 56 | + */ |
| 57 | +public class SecureNetty4HttpServerTransport extends Netty4HttpServerTransport { |
| 58 | + private static final Logger logger = LogManager.getLogger(SecureNetty4HttpServerTransport.class); |
| 59 | + private final SecureTransportSettingsProvider secureTransportSettingsProvider; |
| 60 | + private final SecureTransportSettingsProvider.ServerExceptionHandler exceptionHandler; |
| 61 | + |
| 62 | + public SecureNetty4HttpServerTransport( |
| 63 | + final Settings settings, |
| 64 | + final NetworkService networkService, |
| 65 | + final BigArrays bigArrays, |
| 66 | + final ThreadPool threadPool, |
| 67 | + final NamedXContentRegistry namedXContentRegistry, |
| 68 | + final Dispatcher dispatcher, |
| 69 | + final ClusterSettings clusterSettings, |
| 70 | + final SharedGroupFactory sharedGroupFactory, |
| 71 | + final SecureTransportSettingsProvider secureTransportSettingsProvider, |
| 72 | + final Tracer tracer |
| 73 | + ) { |
| 74 | + super( |
| 75 | + settings, |
| 76 | + networkService, |
| 77 | + bigArrays, |
| 78 | + threadPool, |
| 79 | + namedXContentRegistry, |
| 80 | + dispatcher, |
| 81 | + clusterSettings, |
| 82 | + sharedGroupFactory, |
| 83 | + tracer |
| 84 | + ); |
| 85 | + this.secureTransportSettingsProvider = secureTransportSettingsProvider; |
| 86 | + this.exceptionHandler = secureTransportSettingsProvider.buildHttpServerExceptionHandler(settings, this) |
| 87 | + .orElse(SecureTransportSettingsProvider.ServerExceptionHandler.NOOP); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public ChannelHandler configureServerChannelHandler() { |
| 92 | + return new SslHttpChannelHandler(this, handlingSettings); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onException(HttpChannel channel, Exception cause0) { |
| 97 | + Throwable cause = cause0; |
| 98 | + |
| 99 | + if (cause0 instanceof DecoderException && cause0 != null) { |
| 100 | + cause = cause0.getCause(); |
| 101 | + } |
| 102 | + |
| 103 | + exceptionHandler.onError(cause); |
| 104 | + logger.error("Exception during establishing a SSL connection: " + cause, cause); |
| 105 | + super.onException(channel, cause0); |
| 106 | + } |
| 107 | + |
| 108 | + protected class SslHttpChannelHandler extends Netty4HttpServerTransport.HttpChannelHandler { |
| 109 | + protected SslHttpChannelHandler(final Netty4HttpServerTransport transport, final HttpHandlingSettings handlingSettings) { |
| 110 | + super(transport, handlingSettings); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void initChannel(Channel ch) throws Exception { |
| 115 | + super.initChannel(ch); |
| 116 | + |
| 117 | + final SSLEngine sslEngine = secureTransportSettingsProvider.buildSecureHttpServerEngine( |
| 118 | + settings, |
| 119 | + SecureNetty4HttpServerTransport.this |
| 120 | + ).orElseGet(SslUtils::createDefaultServerSSLEngine); |
| 121 | + |
| 122 | + final SslHandler sslHandler = new SslHandler(sslEngine); |
| 123 | + ch.pipeline().addFirst("ssl_http", sslHandler); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments