|
| 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.core.indices.breaker.CircuitBreakerService; |
| 17 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 18 | +import org.opensearch.http.HttpServerTransport; |
| 19 | +import org.opensearch.http.netty4.Netty4HttpServerTransport; |
| 20 | +import org.opensearch.telemetry.tracing.Tracer; |
| 21 | +import org.opensearch.threadpool.ThreadPool; |
| 22 | + |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.function.Supplier; |
| 27 | + |
| 28 | +import io.netty.buffer.ByteBuf; |
| 29 | +import io.netty.buffer.Unpooled; |
| 30 | +import io.netty.channel.ChannelFutureListener; |
| 31 | +import io.netty.channel.ChannelHandlerContext; |
| 32 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 33 | +import io.netty.channel.SimpleChannelInboundHandler; |
| 34 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 35 | +import io.netty.handler.codec.http.DefaultHttpRequest; |
| 36 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 37 | +import io.netty.handler.codec.http.HttpRequest; |
| 38 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 39 | +import io.netty.util.ReferenceCountUtil; |
| 40 | + |
| 41 | +public class Netty4BlockingPlugin extends Netty4ModulePlugin { |
| 42 | + |
| 43 | + public class Netty4BlockingHttpServerTransport extends Netty4HttpServerTransport { |
| 44 | + |
| 45 | + public Netty4BlockingHttpServerTransport( |
| 46 | + Settings settings, |
| 47 | + NetworkService networkService, |
| 48 | + BigArrays bigArrays, |
| 49 | + ThreadPool threadPool, |
| 50 | + NamedXContentRegistry xContentRegistry, |
| 51 | + Dispatcher dispatcher, |
| 52 | + ClusterSettings clusterSettings, |
| 53 | + SharedGroupFactory sharedGroupFactory, |
| 54 | + Tracer tracer |
| 55 | + ) { |
| 56 | + super( |
| 57 | + settings, |
| 58 | + networkService, |
| 59 | + bigArrays, |
| 60 | + threadPool, |
| 61 | + xContentRegistry, |
| 62 | + dispatcher, |
| 63 | + clusterSettings, |
| 64 | + sharedGroupFactory, |
| 65 | + tracer |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected ChannelInboundHandlerAdapter createHeaderVerifier() { |
| 71 | + return new ExampleBlockingNetty4HeaderVerifier(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Map<String, Supplier<HttpServerTransport>> getHttpTransports( |
| 77 | + Settings settings, |
| 78 | + ThreadPool threadPool, |
| 79 | + BigArrays bigArrays, |
| 80 | + PageCacheRecycler pageCacheRecycler, |
| 81 | + CircuitBreakerService circuitBreakerService, |
| 82 | + NamedXContentRegistry xContentRegistry, |
| 83 | + NetworkService networkService, |
| 84 | + HttpServerTransport.Dispatcher dispatcher, |
| 85 | + ClusterSettings clusterSettings, |
| 86 | + Tracer tracer |
| 87 | + ) { |
| 88 | + return Collections.singletonMap( |
| 89 | + NETTY_HTTP_TRANSPORT_NAME, |
| 90 | + () -> new Netty4BlockingHttpServerTransport( |
| 91 | + settings, |
| 92 | + networkService, |
| 93 | + bigArrays, |
| 94 | + threadPool, |
| 95 | + xContentRegistry, |
| 96 | + dispatcher, |
| 97 | + clusterSettings, |
| 98 | + getSharedGroupFactory(settings), |
| 99 | + tracer |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + /** POC for how an external header verifier would be implemented */ |
| 105 | + public class ExampleBlockingNetty4HeaderVerifier extends SimpleChannelInboundHandler<DefaultHttpRequest> { |
| 106 | + |
| 107 | + @Override |
| 108 | + public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest msg) throws Exception { |
| 109 | + ReferenceCountUtil.retain(msg); |
| 110 | + if (isBlocked(msg)) { |
| 111 | + ByteBuf buf = Unpooled.copiedBuffer("Hit header_verifier".getBytes(StandardCharsets.UTF_8)); |
| 112 | + final FullHttpResponse response = new DefaultFullHttpResponse(msg.protocolVersion(), HttpResponseStatus.UNAUTHORIZED, buf); |
| 113 | + ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); |
| 114 | + ReferenceCountUtil.release(msg); |
| 115 | + } else { |
| 116 | + // Lets the request pass to the next channel handler |
| 117 | + ctx.fireChannelRead(msg); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private boolean isBlocked(HttpRequest request) { |
| 122 | + final boolean shouldBlock = request.headers().contains("blockme"); |
| 123 | + |
| 124 | + return shouldBlock; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments