Authentication 云运行-通过服务帐户进行gRPC身份验证-Java
我已将选项“RequiredAuthentication”设置为true的gRPC服务器部署到Google Cloud Run(完全管理) 我试图通过谷歌服务帐户验证我的gRPC客户端的呼叫,但我总是遇到以下异常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
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]]
- 您需要连接到域:443(而不是:80)
- gRPC Java需要使用机器根CA证书来验证Cloud Run提供的TLS证书的有效性(而不是跳过TLS验证)
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之外的服务进行身份验证,在这种情况下,您确实需要加载服务帐户(默认情况下让它拾取,或者手动指向该文件),对吗?