【问题标题】:Application Default Credential Is Not Working on Google Container Engine应用程序默认凭据在 Google Container Engine 上不起作用
【发布时间】:2016-09-26 05:57:48
【问题描述】:

我正在尝试将容器引擎上的应用程序默认凭据用于 python IAM API。但是我收到以下错误,指出身份验证范围不足。我的项目启用了 IAM API,并且代码在本地运行。所以,我不确定我错过了什么。

我的错误信息:

22:26:16.000
ERROR:root:<HttpError 403 when requesting https://iam.googleapis.com/v1/projects/henry-dev/serviceAccounts/test123@henry-dev.iam.gserviceaccount.com/keys?alt=json returned "Request had insufficient authentication scopes.">
{
 metadata: {…}   
 textPayload: "ERROR:root:<HttpError 403 when requesting https://iam.googleapis.com/v1/projects/henry-dev/serviceAccounts/test123@henry-dev.iam.gserviceaccount.com/keys?alt=json returned "Request had insufficient authentication scopes.">
"   
 insertId: "116wpgtg3n4zndx"   
 log: "simplekubeserver"   
}

22:26:16.000
HttpError: <HttpError 403 when requesting https://iam.googleapis.com/v1/projects/henry-dev/serviceAccounts/test123@henry-dev.iam.gserviceaccount.com/keys?alt=json returned "Request had insufficient authentication scopes.">
{
 metadata: {…}   
 textPayload: "HttpError: <HttpError 403 when requesting https://iam.googleapis.com/v1/projects/henry-dev/serviceAccounts/test123@henry-dev.iam.gserviceaccount.com/keys?alt=json returned "Request had insufficient authentication scopes.">
"   
 insertId: "116wpgtg3n4znej"   
 log: "simplekubeserver"   
}

我的代码,可在本地运行,但不能在 GKE 上运行:

from oauth2client.client import GoogleCredentials

def _iam_service():
  credentials = GoogleCredentials.get_application_default()
  return discovery.build(serviceName='iam', 
                         version='v1',
                         credentials=credentials)

def list_keys(project_id, service_account_id):
  full_name = 'projects/{0}/serviceAccounts/{1}'.format(project_id, service_account_id)
  keys = _iam_service().projects().serviceAccounts().keys()
  request = keys.list(name=full_name)
  return request.execute()

我进行故障排除的一件事是获取正在使用的服务帐户。

print credentials.service_account_email

在本地,这显示了我正在使用的正确服务帐户。而在 GKE 上,我得到了 None,但期望像 123456789-compute@developer.gserviceaccount.com

source code,我看到了:

_get_application_default_credential_GCE()          
_get_application_default_credential_GAE()

但对于 GKE,没有任何明确的规定。所以,我假设使用 GCE 的那个。

This doc 提到这应该在 Container Engine 上运行。

Application Default Credentials are best suited for cases
when the call needs to have the same identity and authorization level
for the application independent of the user. This is the recommended
approach to authorize calls to Google Cloud Platform APIs, particularly
when you're building an application that is deployed to Google App
Engine, **Google Container Engine**, or Google Compute Engine virtual
machines.

【问题讨论】:

    标签: google-kubernetes-engine


    【解决方案1】:

    IAM Service Accounts API 需要 https://www.googleapis.com/auth/iamhttps://www.googleapis.com/auth/cloud-platform 范围。 GKE 集群节点上的范围是在集群创建(或节点池创建)时定义的。如果您通过 Cloud Console 或 gcloud 创建了集群,则默认范围不包括这些。

    在 Cloud Console 中,您可以通过点击“更多”链接并将“Cloud Platform”设置为“Enabled”,将 Cloud-Platform 范围添加到 new cluster

    如果您使用的是 gcloud,则可以通过将 --scopes 标志传递给 gcloud container clusters creategcloud container node-pools create 来指定范围

    【讨论】:

    • 谢谢,通过创建一个启用了云平台范围的新集群,这对我有用。如果可以更改范围而无需创建新集群,那就更棒了。
    • 您可以通过创建一个新的节点池(具有您想要的范围)然后删除旧的节点池来更改正在运行的集群上的范围。但看起来你已经过去了:)。很高兴它对你有用。
    猜你喜欢
    • 1970-01-01
    • 2018-05-04
    • 2019-01-23
    • 2019-04-26
    • 1970-01-01
    • 2016-06-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多