Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward Client RemoteIP #3940

Closed
gsingh-ds opened this issue May 17, 2022 · 11 comments
Closed

Forward Client RemoteIP #3940

gsingh-ds opened this issue May 17, 2022 · 11 comments

Comments

@gsingh-ds
Copy link

Hello There,

We are using GraphQL Mesh in front of GRPC. We want to forward request remote ip (customer ip) to the backend. Seems like there is no way to do it with GraphQL Mesh at this point, please correct me if I am wrong.

@ardatan
Copy link
Owner

ardatan commented May 17, 2022

There is no "forwarding" mechanism in GraphQL Mesh because GraphQL Mesh gives you a gateway schema that is served with a GraphQL Server. So in GraphQL, the execution is seperated from the HTTP pipeline.
That means you cannot assume that the client is always from HTTP etc etc.
So, you can define custom metadata to pass some information from the request like below; (In this case, the consumer is responsible of providing this data. I assume that you are using HTTP server)

sources:
   - name: MyGRPC
     ...
     metaData:
         someHeaderInfo: ["headers", "some-header"]

I will create a PR to add this to our docs :)

I'll also add support for interpolation syntax like below;

sources:
   - name: MyGRPC
     ...
     metaData:
         someHeaderInfo: "{context.req.headers['some-header']}"

Thank you for reminding us for missing documentation :)

@ardatan
Copy link
Owner

ardatan commented May 17, 2022

Documents have been added! Let us know if it doesn't work for you!

@ardatan ardatan closed this as completed May 17, 2022
@gsingh-ds
Copy link
Author

Well, I understand why this is a problem but our application needs the client IP to work. Using headers to do so is risky that it can be tempered with. Do you think there is any way or hack to get this done?

@ardatan ardatan reopened this May 18, 2022
@ardatan
Copy link
Owner

ardatan commented May 18, 2022

req.ip should also have the IP address of the client.

@gsingh-ds
Copy link
Author

So here is the setup request comes from user then to GraphQL server and then hits out backend server .
user -- > graphql --> grpcserver

I used your method , I added new header in mesh config. like
testheader: ["req", "ip"]

When I log this header in grpcserver it shows 127.0.0.1 I am expecting user ip

@ardatan
Copy link
Owner

ardatan commented May 19, 2022

If you are testing this by calling the local server, it is expected to get 127.0.0.1.
Also if you serve Mesh behind the proxy, you can only access the real address with x-forwarded-for header.
req is NodeJS Incoming Message object by the way. You can take a look at their docs to learn more.
For the future reference, those are not actually Mesh features. When you use Mesh with a server that exposes request object within context. Those are the only things I can suggest.

@gsingh-ds
Copy link
Author

So our service is deployed on AWS App mesh. There is Virtual Gateway and envoy proxy infront of graphql mesh.
ref image: https://i.stack.imgur.com/ibY5P.png.
I have tried x-forward-for header, seems like envoy does not send it.

@ardatan
Copy link
Owner

ardatan commented May 20, 2022

Mesh basically uses Node.js, Express.js and Yoga. Hmm maybe ['req', 'socket', 'remoteAddress'] ??

@gsingh-ds
Copy link
Author

I think I tried with ['req', 'connection', 'remoteAddress']. I will test your recommendation today. I hope it will work.

@gsingh-ds
Copy link
Author

Well, It did not work I think because of envoy proxy as a side car, the output of ['req', 'socket', 'remoteAddress'] comes out to be 127.0.0.1. Well, my only hope is to get X-Forward-For header from envoy proxy at this time. If you have any other recommendations. Please let me know.

@theguild-bot theguild-bot mentioned this issue Aug 11, 2022
@ardatan
Copy link
Owner

ardatan commented Mar 31, 2023

IP information depends on the environment. There is no standard way to get the remote ip. If you are behind a proxy, you can get it from req.headers['x-forward-for'].
So this is not a bug in Mesh actually. Closing the issue.

@ardatan ardatan closed this as completed Mar 31, 2023
@theguild-bot theguild-bot mentioned this issue Sep 28, 2023
This was referenced Apr 30, 2024
This was referenced May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants