2016-09-26 89 views
1

我正尝试在容器引擎上使用应用程序默认凭证,以用于python IAM API。但是我收到以下错误指出认证范围不足。我的项目启用了IAM API,代码在本地工作。所以,我不知道我错过了什么。应用程序默认凭证不适用于Google容器引擎

我的错误消息:

22:26:16.000 
ERROR:root:<HttpError 403 when requesting https://iam.googleapis.com/v1/projects/henry-dev/serviceAccounts/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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,我得到无,但预计类似[email protected]

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. 

回答

1

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

在云端控制台,您可以通过点击“更多”链接,并设置“云平台”云平台范围添加到new cluster为“Enabled”

如果您正在使用gcloud,你可以指定通过将--scopes标志传递给gcloud container clusters creategcloud container node-pools create

+0

谢谢,通过创建启用了云平台范围的新集群,我可以为我工作。如果可以在不需要创建新集群的情况下更改范围,那将更加可怕。 –

+0

您可以通过创建新节点池(包含所需的范围)然后删除旧节点池来更改正在运行的集群上的范围。但看起来你已经过去了:)。很高兴它对你有效。 –

相关问题