首页 文章

从android调用grpc google endpoints 导致来自nginx esp的HTTP状态代码400

提问于
浏览
1

我正在尝试使用Google Cloud 平台上的grpc-java(flavor lite)从android进行GRPC调用,我从nginx esp获得HTTP状态代码400

在我的本地网络上它工作,在golang中使用grpc服务器 .

但在谷歌 Cloud 平台上,我们在gRPC Google Cloud Endpoints前面使用TCP负载均衡器,而我们的golang后端则部署在Google容器引擎中 .

From our first analyze, it appears only when we are using grpc metadata with a JWT token on grpc-java, if we are not sending metadata it works.

My endpoint config.yaml

type: google.api.Service
config_version: 3

name: auth.endpoints.MYPROJECT.cloud.goog

title: auth gRPC API
apis:
- name: api.AuthApi

usage:
  rules:
  # Allow unregistered calls for all methods.
  - selector: "*"
  allow_unregistered_calls: true

My Backend config

Go version on backend 1.9.1 / 1.9.2
grpc-go version : 1.7.1
protoc version : 3.4.0

My Client config

protoc version : 3.4.0
grpc-java on android : 1.6.1 (i will test with 1.7.0)

Sample of Go code

我们使用来自firebase的JWT令牌和自定义声明,传递给元数据 .

// go client sample
md["authorization"] = []string{"bearer " + token}
ctx = metadata.NewOutgoingContext(ctx, md)

** java代码示例**

Metadata headers = new Metadata();
headers.put(TOKEN_KEY, "Bearer " + token);
authClient.attachHeaders(headers);

blockingStub = MetadataUtils.attachHeaders(blockingStub, headers);

My issue

使用GCP上的Go Client可以正常工作 .

使用GCP上的grpcc(NodeJS)客户端,它可以工作 .

使用grpc-java在android上它失败了这个跟踪:

10-26 11:13:49.340 22025-22025/com.mypackage.customer.debug E/AuthenticationManager: Fail to get the custom token from server                                                                                  
io.grpc.StatusRuntimeException: INTERNAL: HTTP status code 400                                                                                  
invalid content-type: text/html                                                                                  
headers: Metadata(:status=400,server=nginx,date=Thu, 26 Oct 2017 09:13:48 GMT,content-type=text/html,content-length=166)                                                                                 
DATA-----------------------------

<html>                                                                                  
<head><title>400 Bad Request</title></head>                                                                                    
<body bgcolor="white">                                                                                   
<center><h1>400 Bad Request</h1></center>                                                                                   
<hr><center>nginx</center>                                                                                   
</body>                                                                                 
</html>

at io.grpc.stub.ClientCalls.toStatusRuntimeException(ClientCalls.java:210)                                                                                      
at io.grpc.stub.ClientCalls.getUnchecked(ClientCalls.java:191)                                                                              
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:124)                                                                                    
at com.mypackage.protobuf.AuthApiGrpc$AuthApiBlockingStub.generateToken(AuthApiGrpc.java:163)

在Google Cloud Endpoints上我可以在我的esp上看到这个日志:

10.12.0.6 - - [26/Oct/2017:11:13:49 +0000] “-” 400 166 “-” “grpc-java-okhttp/0.0”

代替

10.12.0.6 - - [26/Oct/2017:11:13:49 +0000] "POST /api.AuthApi/generateToken HTTP/2.0" 200 95 "-" "grpc-java-okhttp/0.0"

任何的想法 ?

1 回答

  • 0

    我们发现了这个问题,它是在我们的grpc日志记录拦截器上,如果我们通过调用headers.toString()来记录Metadata,它就会失败 .

    这很奇怪,因为它仅对谷歌 Cloud endpoints 失败 . 而toString()改变了一些东西是非常奇怪的,我将在grpc-java github上打开一个问题 .

    Our simple interceptor

    package com.myproject.android.common.api.grpc.interceptor;
    
    import android.util.Log;
    
    import io.grpc.CallOptions;
    import io.grpc.Channel;
    import io.grpc.ClientCall;
    import io.grpc.ClientInterceptor;
    import io.grpc.ForwardingClientCall;
    import io.grpc.ForwardingClientCallListener;
    import io.grpc.Metadata;
    import io.grpc.MethodDescriptor;
    
    /**
    * Interceptor that log (via Android logger) gRPC request and response contents.
    * <p>
    * The content might be truncated if too long.
    * </p>
    * Created on 18/10/2017.
    */
    public class GrpcLoggingInterceptor implements ClientInterceptor {
        private static final String TAG = GrpcLoggingInterceptor.class.getSimpleName();
    
        @Override
        public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
            return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
    
                @Override
                public void sendMessage(ReqT message) {
                    Log.v(TAG, "--> [REQUEST] " + method.getFullMethodName() + ": \n" + message.toString());
                    super.sendMessage(message);
                }
    
                @Override
                public void start(Listener<RespT> responseListener, Metadata headers) {
    //                Log.v(TAG, "--> [HEADERS] " + headers.toString()); // /!\ DO NOT LOG METADATA: causes error 400 on GCP
                    ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT> listener = new
                            ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {
                                @Override
                                public void onMessage(RespT message) {
                                    Log.v(TAG, "<-- [RESPONSE] " + method.getFullMethodName() + " (" + message.toString().length() + " bytes): \n" + message.toString());
                                    super.onMessage(message);
                                }
                            };
                    super.start(listener, headers);
                }
            };
        }
    }
    

相关问题