Skip to content

Commit

Permalink
added test case
Browse files Browse the repository at this point in the history
  • Loading branch information
naman.soni committed Aug 8, 2024
1 parent 2b08faa commit d8ed0a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import javax.inject.Singleton;
import javax.validation.ConstraintViolationException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -151,7 +153,7 @@ public void onHalfClose() {
public void onMessage(Req request) {
Context previous = attachContext(contextWithHeaders); // attaching headers to gRPC context
RequestParams requestParams = RequestParams.builder()
.clientIp(getClientIp(call.getAttributes()))
.clientIp(getClientIp(call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)))
.resourcePath(call.getMethodDescriptor().getFullMethodName().toLowerCase())
.metadata(headers)
.build();
Expand Down Expand Up @@ -229,22 +231,29 @@ private void configureAccessLog(GrpcFilterConfig grpcFilterConfig,
}
}

private static String getClientIp(Attributes attributes){
if (attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR) != null){
String ipAddressString = attributes.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR).toString();
if (StringUtils.isNotEmpty(ipAddressString)){
if (ipAddressString.startsWith("/")){
ipAddressString = ipAddressString.split("/", 2)[1];
protected static String getClientIp(SocketAddress socketAddress){
if (socketAddress != null){
if (socketAddress instanceof InetSocketAddress){
String ipAddressString =
((InetSocketAddress) socketAddress).getAddress().toString();
if (StringUtils.isNotEmpty(ipAddressString)){
int startIndex = ipAddressString.indexOf("/");
if (startIndex != -1){
ipAddressString = ipAddressString.substring(startIndex + 1);
}
}
}
if (StringUtils.isNotEmpty(ipAddressString)){
int portIndex = ipAddressString.lastIndexOf(":");
if (portIndex != -1){
ipAddressString = ipAddressString.substring(0, portIndex);
if (StringUtils.isNotEmpty(ipAddressString)){
int endIndex = ipAddressString.lastIndexOf(":");
if (endIndex != -1){
ipAddressString = ipAddressString.substring(0, endIndex);
}
}
if (StringUtils.isNotEmpty(ipAddressString)){
return ipAddressString;
}
}
return ipAddressString;
return socketAddress.toString();
}
return null;
return "0.0.0.0";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.flipkart.gjex.grpc.interceptor;

import org.junit.Assert;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

public class FilterInterceptorTest {
private static final String IP_ADDRESS = "1.2.3.4";

@Test
public void getClientIpTest(){
InetSocketAddress inetSocketAddress = new InetSocketAddress(IP_ADDRESS, 1234);
Assert.assertEquals(IP_ADDRESS, FilterInterceptor.getClientIp(inetSocketAddress));

SocketAddress socketAddress = new SocketAddress() {
@Override
public String toString() {
return IP_ADDRESS;
}
};
Assert.assertEquals(IP_ADDRESS, FilterInterceptor.getClientIp(socketAddress));
}

}

0 comments on commit d8ed0a3

Please sign in to comment.