Add each user's credential to each rpc reqeust

520 views
Skip to first unread message

Min Yao

unread,
Jun 16, 2016, 3:25:34 PM6/16/16
to Kun Zhang, Yang Gao, grp...@googlegroups.com, Ming Zhao
Hi Yang and Kun,
     I am Min Yao from Luminate Wireless Inc.. We have used gRPC in our projects. Currently we have an application case, where many users share a stub to send rpc requests with per user's credential carried on request metadata. It is easy to achieve this in grpc C++ since the user can add the credential to ClientContext and server can fetch the credential from ServerContext. However, grpc-java doesn't provide the method to add each user's credential to each rpc request metadata. Is there a good way to solve this problem? Thanks.

Best regard,
    Min Yao

Xiao Hang (杭啸)

unread,
Jun 16, 2016, 3:32:34 PM6/16/16
to Min Yao, Kun Zhang, Yang Gao, grp...@googlegroups.com, Ming Zhao
You can create a interceptor to do so.  MetadataUtils.newAttachHeadersInterceptor should work for you.

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/CAPVo65Ds3EfE%2B%3DiN-weTTKZnVJwRKez7b_wc3g-wdh8O0EjtOA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
rocking

Kun Zhang

unread,
Jun 16, 2016, 4:40:02 PM6/16/16
to Xiao Hang (杭啸), Min Yao, Yang Gao, grpc.io, Ming Zhao
We just added the CallCredentials, which writes credentials to the headers.

The client can do:
stub.withCallCredentials(creds).foo(req);

The server needs to use a ServerInterceptor to extract the creds from the headers and put in the Context, then the service handler can read it from the Context.


For more options, visit https://groups.google.com/d/optout.



--
- Kun

Min Yao

unread,
Jun 16, 2016, 4:45:51 PM6/16/16
to Xiao Hang (杭啸), Kun Zhang, Yang Gao, grp...@googlegroups.com, Ming Zhao
Hi Xiao,
    The interceptor is binded to the stub, it will add the same header to each rpc request. But we want to add different headers to the request based on different users.
Best regard,
    Min Yao

On Thu, Jun 16, 2016 at 12:32 PM, Xiao Hang (杭啸) <roc...@google.com> wrote:
You can create a interceptor to do so.  MetadataUtils.newAttachHeadersInterceptor should work for you.
On Thu, Jun 16, 2016 at 12:25 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Yang and Kun,
     I am Min Yao from Luminate Wireless Inc.. We have used gRPC in our projects. Currently we have an application case, where many users share a stub to send rpc requests with per user's credential carried on request metadata. It is easy to achieve this in grpc C++ since the user can add the credential to ClientContext and server can fetch the credential from ServerContext. However, grpc-java doesn't provide the method to add each user's credential to each rpc request metadata. Is there a good way to solve this problem? Thanks.

Best regard,
    Min Yao

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.



--
rocking

Kun Zhang

unread,
Jun 16, 2016, 4:59:48 PM6/16/16
to Min Yao, Xiao Hang (杭啸), Yang Gao, grpc.io, Ming Zhao
stub.withInterceptors() creates a new stub, and such stub creation is so cheap that you can do:

stub.withInterceptors(...).foo(request);

to attach different interceptors for individual RPCs. Of course the resulting stub is also good for multiple uses if you want.

It is also the case for stub.withCallCredentials(). It's a dedicated interface for credentials, thus more user-friendly and easier to implement than interceptors.

On Thu, Jun 16, 2016 at 1:45 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Xiao,
    The interceptor is binded to the stub, it will add the same header to each rpc request. But we want to add different headers to the request based on different users.
Best regard,
    Min Yao
On Thu, Jun 16, 2016 at 12:32 PM, Xiao Hang (杭啸) <roc...@google.com> wrote:
You can create a interceptor to do so.  MetadataUtils.newAttachHeadersInterceptor should work for you.
On Thu, Jun 16, 2016 at 12:25 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Yang and Kun,
     I am Min Yao from Luminate Wireless Inc.. We have used gRPC in our projects. Currently we have an application case, where many users share a stub to send rpc requests with per user's credential carried on request metadata. It is easy to achieve this in grpc C++ since the user can add the credential to ClientContext and server can fetch the credential from ServerContext. However, grpc-java doesn't provide the method to add each user's credential to each rpc request metadata. Is there a good way to solve this problem? Thanks.

Best regard,
    Min Yao

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.



--
rocking




--
- Kun

Min Yao

unread,
Jun 16, 2016, 5:16:49 PM6/16/16
to Kun Zhang, Xiao Hang (杭啸), Yang Gao, grpc.io, Ming Zhao
Hi Kun,
    Is it thread safe to use stub.withInterceptors()? In our case, a stub can be used in multiple threads.
    And can you show me the code example of "use a ServerInterceptor to extract the creds from the headers and put in the Context, then the service handler can read it from the Context."?
Best

On Thu, Jun 16, 2016 at 1:59 PM, Kun Zhang <zhan...@google.com> wrote:
stub.withInterceptors() creates a new stub, and such stub creation is so cheap that you can do:

stub.withInterceptors(...).foo(request);

to attach different interceptors for individual RPCs. Of course the resulting stub is also good for multiple uses if you want.

It is also the case for stub.withCallCredentials(). It's a dedicated interface for credentials, thus more user-friendly and easier to implement than interceptors.
On Thu, Jun 16, 2016 at 1:45 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Xiao,
    The interceptor is binded to the stub, it will add the same header to each rpc request. But we want to add different headers to the request based on different users.
Best regard,
    Min Yao
On Thu, Jun 16, 2016 at 12:32 PM, Xiao Hang (杭啸) <roc...@google.com> wrote:
You can create a interceptor to do so.  MetadataUtils.newAttachHeadersInterceptor should work for you.
On Thu, Jun 16, 2016 at 12:25 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Yang and Kun,
     I am Min Yao from Luminate Wireless Inc.. We have used gRPC in our projects. Currently we have an application case, where many users share a stub to send rpc requests with per user's credential carried on request metadata. It is easy to achieve this in grpc C++ since the user can add the credential to ClientContext and server can fetch the credential from ServerContext. However, grpc-java doesn't provide the method to add each user's credential to each rpc request metadata. Is there a good way to solve this problem? Thanks.

Best regard,
    Min Yao

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.



--
rocking




--
- Kun

Kun Zhang

unread,
Jun 16, 2016, 6:12:12 PM6/16/16
to Min Yao, Xiao Hang (杭啸), Yang Gao, grpc.io, Ming Zhao
It is thread-safe because stub is immutable, and each withInterceptor() creates a new stub.

I wrote up some example:

On Thu, Jun 16, 2016 at 2:16 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Kun,
    Is it thread safe to use stub.withInterceptors()? In our case, a stub can be used in multiple threads.
    And can you show me the code example of "use a ServerInterceptor to extract the creds from the headers and put in the Context, then the service handler can read it from the Context."?
Best
On Thu, Jun 16, 2016 at 1:59 PM, Kun Zhang <zhan...@google.com> wrote:
stub.withInterceptors() creates a new stub, and such stub creation is so cheap that you can do:

stub.withInterceptors(...).foo(request);

to attach different interceptors for individual RPCs. Of course the resulting stub is also good for multiple uses if you want.

It is also the case for stub.withCallCredentials(). It's a dedicated interface for credentials, thus more user-friendly and easier to implement than interceptors.
On Thu, Jun 16, 2016 at 1:45 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Xiao,
    The interceptor is binded to the stub, it will add the same header to each rpc request. But we want to add different headers to the request based on different users.
Best regard,
    Min Yao
On Thu, Jun 16, 2016 at 12:32 PM, Xiao Hang (杭啸) <roc...@google.com> wrote:
You can create a interceptor to do so.  MetadataUtils.newAttachHeadersInterceptor should work for you.
On Thu, Jun 16, 2016 at 12:25 PM, Min Yao <min...@luminatewireless.com> wrote:
Hi Yang and Kun,
     I am Min Yao from Luminate Wireless Inc.. We have used gRPC in our projects. Currently we have an application case, where many users share a stub to send rpc requests with per user's credential carried on request metadata. It is easy to achieve this in grpc C++ since the user can add the credential to ClientContext and server can fetch the credential from ServerContext. However, grpc-java doesn't provide the method to add each user's credential to each rpc request metadata. Is there a good way to solve this problem? Thanks.

Best regard,
    Min Yao

--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+unsubscribe@googlegroups.com.



--
rocking




--
- Kun




--
- Kun

Min Yao

unread,
Jun 17, 2016, 2:03:55 PM6/17/16
to Kun Zhang, Xiao Hang (杭啸), Yang Gao, grpc.io, Ming Zhao
Hi Kun, Xiao and Yang,
    Thanks very much for your kindly help. Based on your suggestions and example code, I solve the problem of carrying each user's credential on each rpc request, and extracting the user's credential from each rpc request in server side. Thanks again.
Best regard,
    Min Yao 

To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.



--
rocking




--
- Kun




--
- Kun

Reply all
Reply to author
Forward
0 new messages