【问题标题】:Accessing a Cloud Run service from another project从另一个项目访问 Cloud Run 服务
【发布时间】:2019-08-19 16:10:26
【问题描述】:

我可以使用来自该项目的 ServiceAccount 从另一个项目访问 CloudRun api 吗?

我有一个项目 A,其中托管了 CloudRun 服务 (ServiceA)。 我有另一个项目 B,我需要使用服务帐户 (SvcAcccountB) 从中调用 ServiceA。

但每次我尝试使用 SvcAccountB 调用 ServiceA 时,它都会失败并显示 403。没有防火墙问题,正如我在 ServiceA 的日志中看到的那样,调用被拒绝了。 我已将“CloudRun Invoker”权限授予 SvcAccountB。

如果我在项目 A 中有服务帐户 (svcAccountA),则对 ServiceA 的调用工作正常。

我还缺少什么以便 svcAccountB 可以调用 ServiceA?

【问题讨论】:

  • 是的,您可以将 CloudRun Invoker 分配给任何有效的 Google 帐户电子邮件。我一直这样做。您的问题没有显示您如何调用 Cloud Run。确保您使用的是身份令牌而不是访问令牌。
  • 这就是问题所在,错误的身份验证。
  • 您能解释一下您调用的完整元数据/identity URL 是什么吗?即?audience= 值等是什么?我怀疑你那里有问题。如果?audience= 无效,你仍然会得到一个令牌,但它不起作用。
  • 我得到了这个工作。发布详细信息作为答案。 John Hanley 的网站帮助了 jhanley.com

标签: google-cloud-platform google-cloud-run


【解决方案1】:

您需要使用服务帐户向 CloudRun 服务发布 3 个步骤:-

  1. 使用您的服务帐户 json 文件获取 JWT 令牌 这是一个示例声明:- Iat: now, // expires after 'expiraryLength' seconds. Exp: now + expiryLength, // Iss must match 'issuer' in the security configuration in your // swagger spec (e.g. service account email). It can be any string. Iss: saEmail, // Aud must be either your Endpoints service name, or match the value // specified as the 'x-google-audience' in the OpenAPI document. Aud: "https://www.googleapis.com/oauth2/v4/token", // Sub and Email should match the service account's email address. Sub: saEmail, PrivateClaims: map[string]interface{}{"target_audience": audience}, 其中,受众是您的 CloudRun 服务的 url。

  2. 传递此 JWT 令牌以获取访问令牌 这里有更多细节:https://developers.google.com/identity/protocols/OAuth2ServiceAccount

  3. 然后将访问令牌发送到您的 CloudRun 服务。

【讨论】:

    猜你喜欢
    • 2019-11-06
    • 2021-04-14
    • 2021-04-29
    • 1970-01-01
    • 2013-03-28
    • 2022-12-28
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多