从另一个项目访问Cloud Run服务

时间:2019-08-19 16:10:26

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

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

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

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

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

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

1 个答案:

答案 0 :(得分:0)

您需要使用服务帐户将三个步骤发布到CloudRun服务:-

  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服务。

相关问题