问题描述
我的 Java 应用程序正在执行 2 个任务。在 pub-sub 上收听新消息并创建云任务。此外,它使用已分配所有角色的默认计算服务帐户。现在,我们正在使用具有以下范围的默认服务帐户运行计算实例。
serviceAccounts:
- email: ***********-compu[email protected]
scopes:
- https://www.googleapis.com/auth/devstorage.read_only
- https://www.googleapis.com/auth/logging.write
- https://www.googleapis.com/auth/monitoring.write
- https://www.googleapis.com/auth/pubsub
- https://www.googleapis.com/auth/service.management.readonly
- https://www.googleapis.com/auth/servicecontrol
- https://www.googleapis.com/auth/taskqueue
- https://www.googleapis.com/auth/trace.append
但是,在运行时,我们可以使用消息。但是,我们在创建云任务时遇到了以下异常。
com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Request had insufficient authentication scopes.
at com.google.api.gax.rpc.ApiExceptionFactory.createException(ApiExceptionFactory.java:55)
at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:72)
at com.google.api.gax.grpc.GrpcApiExceptionFactory.create(GrpcApiExceptionFactory.java:60)
at com.google.api.gax.grpc.GrpcExceptionCallable$ExceptionTransformingFuture.onFailure(GrpcExceptionCallable.java:97)
at com.google.api.core.ApiFutures$1.onFailure(ApiFutures.java:68)
at com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1041)
at com.google.common.util.concurrent.DirectExecutor.execute(DirectExecutor.java:30)
at com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:1215)
at com.google.common.util.concurrent.AbstractFuture.addListener(AbstractFuture.java:724)
at com.google.common.util.concurrent.ForwardingListenableFuture.addListener(ForwardingListenableFuture.java:45)
at com.google.api.core.ApiFuturetoListenableFuture.addListener(ApiFuturetoListenableFuture.java:52)
at com.google.common.util.concurrent.Futures.addCallback(Futures.java:1014)
at com.google.api.core.ApiFutures.addCallback(ApiFutures.java:63)
at com.google.api.gax.grpc.GrpcExceptionCallable.futureCall(GrpcExceptionCallable.java:67)
at com.google.api.gax.rpc.UnaryCallable$1.futureCall(UnaryCallable.java:126)
at com.google.api.gax.tracing.TracedUnaryCallable.futureCall(TracedUnaryCallable.java:75)
at com.google.api.gax.rpc.UnaryCallable$1.futureCall(UnaryCallable.java:126)
at com.google.api.gax.rpc.UnaryCallable.futureCall(UnaryCallable.java:87)
at com.google.api.gax.rpc.UnaryCallable.call(UnaryCallable.java:112)
at com.google.cloud.tasks.v2.CloudTasksClient.createTask(CloudTasksClient.java:1258)
at com.google.cloud.tasks.v2.CloudTasksClient.createTask(CloudTasksClient.java:1241)
at gcp.storage.services.ExternalURLGenService.lambda$createTask$0(ExternalURLGenService.java:94)
此外,为了检查帐户是否已分配所有角色,我们启用了实例上的所有云 API 并尝试再次运行该应用程序,我们成功了。
谁能告诉我,我应该为我的实例分配什么额外的范围?
作为作业,我检查了 documentation,只提到提供 2 个范围。即
https://www.googleapis.com/auth/cloud-platform
https://www.googleapis.com/auth/cloud-tasks
现在,我已经为我的实例指定了 cloud-tasks
范围。但是,范围 cloud-platform
相当于启用所有我不想要的 Google Api。
解决方法
范围是控制访问的传统方式。今天,即使范围是正确的,访问也会通过 IAM 服务进行检查。
证明这一点,如果您在 Compute Engine 中使用自定义服务帐号,则无法选择范围,只能使用 Compute Engine 的默认服务帐号选择范围。
所以,不用担心,您可以允许使用 Cloud Platform 的全部范围。但是,您需要删除计算引擎默认服务帐户的编辑者角色。并且只授予所需的权限。
如果您将 Compute Engine 默认服务帐号用于其他产品,请创建一个新服务帐号并将其添加到您的 Compute Engine。此时,不再作用域,仅授予 IAM 角色。