|
| 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.transport; |
| 10 | + |
| 11 | +import org.opensearch.common.network.NetworkService; |
| 12 | +import org.opensearch.common.settings.ClusterSettings; |
| 13 | +import org.opensearch.common.settings.Settings; |
| 14 | +import org.opensearch.common.util.BigArrays; |
| 15 | +import org.opensearch.common.util.PageCacheRecycler; |
| 16 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 17 | +import org.opensearch.core.indices.breaker.CircuitBreakerService; |
| 18 | +import org.opensearch.core.rest.RestStatus; |
| 19 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 20 | +import org.opensearch.http.HttpServerTransport; |
| 21 | +import org.opensearch.http.netty4.Netty4HttpServerTransport; |
| 22 | +import org.opensearch.rest.BytesRestResponse; |
| 23 | +import org.opensearch.rest.RestChannel; |
| 24 | +import org.opensearch.rest.RestRequest; |
| 25 | +import org.opensearch.telemetry.tracing.Tracer; |
| 26 | +import org.opensearch.threadpool.ThreadPool; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.function.Supplier; |
| 31 | + |
| 32 | +import io.netty.channel.ChannelHandlerContext; |
| 33 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 34 | +import io.netty.channel.SimpleChannelInboundHandler; |
| 35 | +import io.netty.handler.codec.http.DefaultHttpRequest; |
| 36 | +import io.netty.handler.codec.http.HttpMessage; |
| 37 | +import io.netty.util.AttributeKey; |
| 38 | +import io.netty.util.ReferenceCountUtil; |
| 39 | + |
| 40 | +public class Netty4BlockingPlugin extends Netty4Plugin { |
| 41 | + |
| 42 | + private static final AttributeKey<Boolean> SHOULD_BLOCK = AttributeKey.newInstance("should-block"); |
| 43 | + |
| 44 | + public class Netty4BlockingHttpServerTransport extends Netty4HttpServerTransport { |
| 45 | + |
| 46 | + public Netty4BlockingHttpServerTransport( |
| 47 | + Settings settings, |
| 48 | + NetworkService networkService, |
| 49 | + BigArrays bigArrays, |
| 50 | + ThreadPool threadPool, |
| 51 | + NamedXContentRegistry xContentRegistry, |
| 52 | + Dispatcher dispatcher, |
| 53 | + ClusterSettings clusterSettings, |
| 54 | + SharedGroupFactory sharedGroupFactory, |
| 55 | + Tracer tracer |
| 56 | + ) { |
| 57 | + super( |
| 58 | + settings, |
| 59 | + networkService, |
| 60 | + bigArrays, |
| 61 | + threadPool, |
| 62 | + xContentRegistry, |
| 63 | + dispatcher, |
| 64 | + clusterSettings, |
| 65 | + sharedGroupFactory, |
| 66 | + tracer |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected ChannelInboundHandlerAdapter createHeaderVerifier() { |
| 72 | + return new ExampleBlockingNetty4HeaderVerifier(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Map<String, Supplier<HttpServerTransport>> getHttpTransports( |
| 78 | + Settings settings, |
| 79 | + ThreadPool threadPool, |
| 80 | + BigArrays bigArrays, |
| 81 | + PageCacheRecycler pageCacheRecycler, |
| 82 | + CircuitBreakerService circuitBreakerService, |
| 83 | + NamedXContentRegistry xContentRegistry, |
| 84 | + NetworkService networkService, |
| 85 | + HttpServerTransport.Dispatcher dispatcher, |
| 86 | + ClusterSettings clusterSettings, |
| 87 | + Tracer tracer |
| 88 | + ) { |
| 89 | + return Collections.singletonMap( |
| 90 | + NETTY_HTTP_TRANSPORT_NAME, |
| 91 | + () -> new Netty4BlockingHttpServerTransport( |
| 92 | + settings, |
| 93 | + networkService, |
| 94 | + bigArrays, |
| 95 | + threadPool, |
| 96 | + xContentRegistry, |
| 97 | + new BlockingDispatcher(dispatcher), |
| 98 | + clusterSettings, |
| 99 | + getSharedGroupFactory(settings), |
| 100 | + tracer |
| 101 | + ) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** POC for how an external header verifier would be implemented */ |
| 106 | + public class ExampleBlockingNetty4HeaderVerifier extends SimpleChannelInboundHandler<DefaultHttpRequest> { |
| 107 | + |
| 108 | + @Override |
| 109 | + public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest msg) throws Exception { |
| 110 | + ReferenceCountUtil.retain(msg); |
| 111 | + if (isBlocked(msg)) { |
| 112 | + msg.headers().add("blocked", true); |
| 113 | + } |
| 114 | + ctx.fireChannelRead(msg); |
| 115 | + } |
| 116 | + |
| 117 | + private boolean isBlocked(HttpMessage request) { |
| 118 | + final boolean shouldBlock = request.headers().contains("blockme"); |
| 119 | + |
| 120 | + return shouldBlock; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + class BlockingDispatcher implements HttpServerTransport.Dispatcher { |
| 125 | + |
| 126 | + private HttpServerTransport.Dispatcher originalDispatcher; |
| 127 | + |
| 128 | + public BlockingDispatcher(final HttpServerTransport.Dispatcher originalDispatcher) { |
| 129 | + super(); |
| 130 | + this.originalDispatcher = originalDispatcher; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void dispatchRequest(RestRequest request, RestChannel channel, ThreadContext threadContext) { |
| 135 | + if (request.getHeaders().containsKey("blocked")) { |
| 136 | + channel.sendResponse(new BytesRestResponse(RestStatus.UNAUTHORIZED, "Hit header_verifier")); |
| 137 | + return; |
| 138 | + } |
| 139 | + originalDispatcher.dispatchRequest(request, channel, threadContext); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void dispatchBadRequest(RestChannel channel, ThreadContext threadContext, Throwable cause) { |
| 145 | + originalDispatcher.dispatchBadRequest(channel, threadContext, cause); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments