forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Peter Nied <[email protected]>
- Loading branch information
Showing
5 changed files
with
130 additions
and
136 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
56 changes: 56 additions & 0 deletions
56
modules/transport-netty4/src/main/java/org/opensearch/http/netty4/Netty4Authorizer.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,56 @@ | ||
/* | ||
* 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.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.channel.ChannelFutureListener; | ||
import io.netty.handler.codec.http.HttpRequest; | ||
import io.netty.handler.codec.http.FullHttpResponse; | ||
import io.netty.handler.codec.http.DefaultFullHttpResponse; | ||
import io.netty.handler.codec.http.HttpVersion; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.netty.util.ReferenceCountUtil; | ||
|
||
@ChannelHandler.Sharable | ||
public class Netty4Authorizer extends ChannelInboundHandlerAdapter { | ||
|
||
final static Logger log = LogManager.getLogger(Netty4Authorizer.class); | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
if (!(msg instanceof HttpRequest)) { | ||
ctx.fireChannelRead(msg); | ||
} | ||
|
||
HttpRequest request = (HttpRequest) msg; | ||
if (!isAuthenticated(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 isAuthenticated(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
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
Oops, something went wrong.