-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add header verifition netty channel handler
Based off work of @cwperks in #10261 Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4HeaderVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.http.netty4; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import io.netty.util.ReferenceCountUtil; | ||
|
||
/** POC for how an external header verifier would be implemented */ | ||
@ChannelHandler.Sharable | ||
public class Netty4HeaderVerifier extends ChannelInboundHandlerAdapter { | ||
|
||
final static Logger log = LogManager.getLogger(Netty4HeaderVerifier.class); | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
if (!(msg instanceof HttpRequest)) { | ||
ctx.fireChannelRead(msg); | ||
} | ||
|
||
HttpRequest request = (HttpRequest) msg; | ||
if (!isVerified(request)) { | ||
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED); | ||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); | ||
ReferenceCountUtil.release(msg); | ||
} else { | ||
// Lets the request pass to the next channel handler | ||
ctx.fireChannelRead(msg); | ||
} | ||
} | ||
|
||
private boolean isVerified(HttpRequest request) { | ||
log.info("Checking if request is authenticated:\n" + request); | ||
|
||
final boolean shouldBlock = request.headers().contains("blockme"); | ||
|
||
return !shouldBlock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters