是否可以识别其凭据用于调用Google Cloud API的用户?

时间:2018-10-15 00:48:33

标签: authentication ssh google-cloud-platform

问题:

是否可以使用Google Cloud API调用来识别正在使用其凭据来调用Google Cloud API的用户?

详细信息:

我的目标是动态地将SSH密钥提供给EC2实例以供一次使用,因此我正在编辑计算实例的ssh-key元数据,以动态地包含SSH密钥的公共部分。 GCloud要求在指定SSH密钥时包括用户名,我想使用其凭据通过Google Cloud API添加SSH密钥的用户名。

IAM APICompute Engine API看,我看不到可以用来识别当前调用用户的任何API调用。在AWS中,我有https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html,它确实可以满足我的要求。

Google Cloud的API是否支持此功能?还是我可以使用其他一些技巧?

更新:我发现实际上,我不需要此功能即可完成用例! GCP要求的“用户名”是我将用于SSH到计算实例的Linux用户,而不是GCP用户。

1 个答案:

答案 0 :(得分:0)

您可以使用Google的tokeninfo端点返回有关当前经过身份验证的用户的信息。下面显示了一个Python示例。 Google用户实际上是JWT的“ id令牌”,它们是base64编码的JSON有效负载,以“。”分隔。您也可以直接解析这些内容以获取身份,而无需远程调用。有关更多信息,请参见Google's tokeninfo documentation

import google.auth.transport.requests
import requests
credentials, project_id = google.auth.default()
credentials.refresh(google.auth.transport.requests.Request())
print('project id:', project_id)
print('id token:', credentials.id_token)
response = requests.get('https://www.googleapis.com/oauth2/v1/tokeninfo?id_token=' + credentials.id_token)
print('tokeninfo:')
print(response.text)