Transcode HTTP header into Grpc metadata for each request

772 Views Asked by At

I'am building an API-Gateway that proxies HTTP traffic to Grpc services. All incoming HTTP requests can have JWT in Authorization header. I need to transcode this JWT to Grpc metadata at each request and send it with Grpc request. I am using grpc-kotlin library with grpc code generator for kotlin suspend functions for client stub. I have write this WebFilter to put header into ReactorContext:

@Component
class UserMetadataWebFilter : WebFilter {

  override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
    exchange.request.headers[HttpHeaders.AUTHORIZATION]?.firstOrNull()?.let { authorizationHeader ->
        return chain.filter(exchange).contextWrite { Context.of("myHeader", authorizationHeader) }
    }
    return chain.filter(exchange)
  }
}

And it can be used in controller methods like this:

identityProviderClient.createUser(protobufRequest,
            coroutineContext[ReactorContext]?.context?.get("myHeader") ?: Metadata())

I want to create Grpc client interceptor or something another to automaticly set Grpc metadata from coroutine context. I have many Grpc client calls and I believe that is to write this code for every call is not good practice. I know about envoy-proxy, but I need apply specific logic to my requests, that's why envoy-proxy is not my choice. How should I transcode Http header(s) into grpc client call metadata? Thanks.

1

There are 1 best solutions below

0
On