【问题标题】:Enable APIs using serviceusage API with a service account使用 serviceusage API 和服务帐户启用 API
【发布时间】:2019-05-08 10:56:39
【问题描述】:

我想为客户端创建 GCP 的自动部署。

为了做到这一点,我打开了一个页面让他们使用 google 登录,然后启用了 IAM API 和 Service Usage API。

然后我创建了一个服务帐户,我想从现在开始使用它来按需启用其他所需的 API,而不是一次性启用。

当我尝试启用 cloudkms API 时,我得到了

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://serviceusage.googleapis.com/v1/projects/x-y-z/services/cloudkms.googleapis.com?alt=json returned "The caller does not have permission"

我尝试使用根据创建服务帐户的响应创建的服务帐户凭据 (google.auth.jwt.Credentials),并添加了所有必需的权限。我不想将角色所有者授予服务帐号,因为我希望该帐号拥有尽可能少的权限。

当我尝试使用用户权限获取 cloudkms API 的状态时,它可以工作。

我已经看到一些解决方案解决了我需要为服务帐户创建凭据的问题:https://console.developers.google.com/apis/credentials,但我也确实需要以编程方式执行此操作。

我的代码:

credentials = jwt.Credentials.from_service_account_file(service_account_info['email'] + '.json', audience="https://www.googleapis.com/auth/cloud-platform")
# credentials = GoogleCredentials.get_application_default() - it works with this
service_usage = googleapiclient.discovery.build('serviceusage', 'v1', credentials=credentials)
service_usage.services().get(name="projects/<project_id>/services/cloudkms.googleapis.com").execute()

上面提到了错误。

【问题讨论】:

    标签: python authentication google-cloud-platform google-cloud-iam google-auth-library


    【解决方案1】:

    您需要 Cloud IAM 权限 serviceusage.services.enable 才能启用服务。根据您需要的功能,例如列表服务,您需要serviceusage.services.list

    通常您添加角色roles/serviceusage.serviceUsageAdmin,其中包括以下权限:

    • serviceusage.services.get
    • serviceusage.services.list
    • serviceusage.services.enable
    • serviceusage.services.disable

    【讨论】:

    • 我其实是用了roles/owner,加到policy里面,把service account放在下面,还是不行。
    【解决方案2】:
    1. 转到 IAM
    2. 编辑选定的用户
    3. 添加新角色
    4. 输入 服务使用管理员
    5. 保存

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2019-01-23
      • 2021-09-30
      • 2013-05-25
      • 2022-01-15
      • 2019-11-06
      • 1970-01-01
      相关资源
      最近更新 更多