How do I access request metadata for a java grpc service I am defining?
<p>For some background, I am attempting to use <a href="http://www.grpc.io/docs/guides/auth.html#authenticate-with-google-4" rel="nofollow">grpc auth</a> in order to provide security for some services I am defining.</p>

<p>Let's see if I can ask this is a way that makes sense. For my python code, it was pretty easy to implement the server side code. </p>

<pre><code>class TestServiceServer(service_pb2.TestServiceServer):

    def TestHello(self, request, context):

        ## credential metadata for the incoming request
        metadata = context.invocation_metadata()

        ## authenticate the user using the metadata
</code></pre>

<p>So, as you can tell, I am able to get the metadata from "context" quite easily. What is harder for me is to do the same thing in java.</p>

<pre><code>public class TestImpl extends TestServiceGrpc.TestServiceImplBase {

    @Override
    public void testHello(TestRequest req, StreamObserver&lt;TestResponse&gt; responseObserver) {

        // How do I get access to similar request metadata here?

        // from the parameter positions, it looks like it should be
        // "responseObserver" but that doesn't seem similar to "context"

    }

}
</code></pre>

<p>I'll admit my problem comes from a few directions.</p>

<p>1) I am not well versed in Java</p>

<p>2) I heavily used python's "pdb" in order to debug the classes and see what methods are available to me. I don't know of/am not proficient at a similar tool for java.</p>

<p>3) The documentation seems rather sparse at this point. It shows you how to set up an ssl connection on the server side, but I can't find an example of the server taking a look at request metadata, as I have shown in python.</p>

<p>Could someone please give me an idea of how to do this, or perhaps show me a useful debugging tool for java in the same vein of python's pdb?</p>

<p>EDIT/ANSWER :</p>

<p>I needed to first write a definition implementing the interface ServerInterceptor.</p>

<pre><code>private class TestInterceptor implements ServerInterceptor {
    ....
</code></pre>

<p>Then, before actually binding my service and building my server, I needed to do this.</p>

<pre><code>TestImpl service = new TestImpl();
ServerServiceDefinition intercepted = ServerInterceptors.intercept(service, new TestInterceptor());
</code></pre>

<p>Now I was able to create the server.</p>

<pre><code>server = NettyServerBuilder.forPort(port)

    // enable tls
    .useTransportSecurity(
        new File(serverCert),
        new File(serverKey)
    )
    .addService(
        intercepted  // had been "new TestImpl()"
    )
    .build();

server.start();
</code></pre>

<p>This allowed my ServerInterceptor to actually be called when I fired off a client side request.</p>

<p><a href="https://github.com/grpc/grpc-java/blob/16c07ba434787f68e256fc50cece1425f421b03e/core/src/test/java/io/grpc/ServerInterceptorsTest.java" rel="nofollow">This link</a> was quite helpful in figuring this out.</p>

<p>Use a <code>ServerInterceptor</code> and then propagate the identity via <code>Context</code>. This allows you to have a central policy for authentication.</p>

<p>The interceptor can retrieve the identity from <code>Metadata headers</code>. It <em>should then validate</em> the identity. The validated identity can then be communicated to the application (i.e., <code>testHello</code>) via <code>io.grpc.Context</code>:</p>

<pre><code>/** Interceptor that validates user's identity. */
class MyAuthInterceptor implements ServerInterceptor {
  public static final Context.Key&lt;Object&gt; USER_IDENTITY
      = Context.key("identity"); // "identity" is just for debugging

  @Override
  public &lt;ReqT, RespT&gt; ServerCall.Listener&lt;ReqT&gt; interceptCall(
      ServerCall&lt;ReqT, RespT&gt; call,
      Metadata headers,
      ServerCallHandler&lt;ReqT, RespT&gt; next) {
    // You need to implement validateIdentity
    Object identity = validateIdentity(headers);
    if (identity == null) { // this is optional, depending on your needs
      // Assume user not authenticated
      call.close(Status.UNAUTENTICATED.withDescription("some more info"),
                 new Metadata());
      return new ServerCall.Listener() {};
    }
    Context context = Context.current().withValue(USER_IDENTITY, identity);
    return Contexts.interceptCall(context, call, headers, next);
  }
}

public class TestImpl extends TestServiceGrpc.TestServiceImplBase {
  @Override
  public void testHello(TestRequest req, StreamObserver&lt;TestResponse&gt; responseObserver) {
    // Access to identity.
    Object identity = MyAuthInterceptor.USER_IDENTITY.get();
    ...
  }
}

// Need to use ServerInterceptors to enable the interceptor
Server server = ServerBuilder.forPort(PORT)
    .addService(ServerInterceptors.intercept(new TestImpl(),
        new MyAuthInterceptor()))
    .build()
    .start();
</code></pre>

java python request metadata grpc