Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication 云运行-通过服务帐户进行gRPC身份验证-Java_Authentication_Grpc_Service Accounts_Google Cloud Run_Grpc Java - Fatal编程技术网

Authentication 云运行-通过服务帐户进行gRPC身份验证-Java

Authentication 云运行-通过服务帐户进行gRPC身份验证-Java,authentication,grpc,service-accounts,google-cloud-run,grpc-java,Authentication,Grpc,Service Accounts,Google Cloud Run,Grpc Java,我已将选项“RequiredAuthentication”设置为true的gRPC服务器部署到Google Cloud Run(完全管理) 我试图通过谷歌服务帐户验证我的gRPC客户端的呼叫,但我总是遇到以下异常 Exception in thread "main" io.grpc.StatusRuntimeException: UNAUTHENTICATED: HTTP status code 401 下面是我如何创建gRPC频道和附加服务帐户 public GrpcClient(Chann

我已将选项“RequiredAuthentication”设置为true的gRPC服务器部署到Google Cloud Run(完全管理)

我试图通过谷歌服务帐户验证我的gRPC客户端的呼叫,但我总是遇到以下异常

Exception in thread "main" io.grpc.StatusRuntimeException: UNAUTHENTICATED: HTTP status code 401
下面是我如何创建gRPC频道和附加服务帐户

public GrpcClient(Channel channel) throws IOException {
    Credentials credentials = GoogleCredentials.getApplicationDefault();

    blockingStub = CalculatorServiceGrpc
            .newBlockingStub(channel)
            .withCallCredentials(MoreCallCredentials.from(credentials));
}
Obs.:env var GOOGLE_APPLICATION_凭证由SA的路径设置,SA具有云运行调用程序权限


有什么我遗漏的吗?

当从通用HTTP客户端调用云运行服务器时,设置
GOOGLE\u应用程序\u凭据
没有效果。(只有在使用Google客户端库调用Google的API时,这才有效。)

即使部署到Cloud Run,gRPC也只是HTTP/2,因此对Cloud Run服务的身份验证记录在页面上。简而言之,这包括:

  • 从容器内的元数据服务端点获取JWT(标识令牌)
  • 将其设置为云运行应用程序请求的标头,如
    Authorization:Bearer[[ID\u TOKEN]]
在gRPC中,头称为“元数据”,因此您应该找到等效的gRPC Java方法来设置它。(可能是每个RPC选项。)

读一读a,它基本上解释了运行在云上的gRPC服务器仍然以相同的方式运行身份验证。在这种情况下,还要确保告诉Java:

  • 您需要连接到域:443(而不是:80)
  • gRPC Java需要使用机器根CA证书来验证Cloud Run提供的TLS证书的有效性(而不是跳过TLS验证)

经过进一步研究,我能够使用IdTokenCredentials对请求进行身份验证。请参见下面的结果

public GrpcClient(Channel channel) throws IOException {
    ServiceAccountCredentials saCreds = ServiceAccountCredentials
            .fromStream(new FileInputStream("path\to\sa"));

    IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder().setIdTokenProvider(saCreds)
            .setTargetAudience("https://{projectId}-{hash}-uc.a.run.app").build();

    blockingStub = CalculatorServiceGrpc
            .newBlockingStub(channel)
            .withCallCredentials(MoreCallCredentials.from(tokenCredential));
}

谢谢@AhmentB,事实上我可以使用IdTokenCredentials(我将在新的注释中发布代码片段,以便正确格式化)。是的,如果您认为需要使用文件自定义服务帐户,也可以这样做。但是,Cloud Run已经有一个服务帐户作为其运行,您不需要向其发送凭据文件来获取ID令牌或访问Google API。@AhmetB Google我认为他们正在尝试从GCP/Cloud Run之外的服务进行身份验证,在这种情况下,您确实需要加载服务帐户(默认情况下让它拾取,或者手动指向该文件),对吗?