0

我正在尝试创建一个脚本来从 GCP 实例查询 google groups API。该实例附加了 SA,此 SA 具有 SCOPE - GSuite 中允许的“ https://www.googleapis.com/auth/admin.directory.group.readonly ”,并且用户也在 GSuite 中设置了自定义角色附加到它(列表组)。

对于 SA,我在 GCP 控制台中创建了一个密钥文件。然后我得到凭证,因为文档说:

from googleapiclient.discovery import build
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

然后添加用户 - 充当。

creds = creds.with_subject('user@domain.com')

service = build('admin', 'directory_v1', credentials=creds)

results = service.groups().list(domain=tenant, maxResults=10,
                    orderBy='email',
                    query='email:{}*'.format(group_name)).execute()

然后我查询 API,一切正常,我得到了组。

所以我的问题是:有没有办法在不生成 json 密钥文件的情况下使用附加到实例的 SA。就像从实例元数据中获取 compute_instance / 默认凭据 / 然后以某种方式对 GSuite API 进行身份验证?

或者有没有办法在不使用 Gsuite API 的情况下查询组,只需从 GCP 中调用一些?

4

2 回答 2

0

我从谷歌得到的答案:

  1. 不,如果不为 SA 生成用于模拟的私钥,这是不可能的。
  2. 不,获取组的正确方法是查询 Gsuite 的 API。
于 2020-02-13T01:25:44.090 回答
0

您应该阅读官方 GCP 文档页面上的下一篇文章

这是一个如何将服务帐户绑定到 VM 的示例

gcloud compute instances create example-vm \
--service-account my-sa@my-project.iam.gserviceaccount.com \
--scopes https://www.googleapis.com/auth/admin.directory.group.readonly
于 2020-02-06T09:59:34.010 回答