【问题标题】:What service account permissions should I set for secure flexible app engine -> cloud function communication我应该为安全灵活的应用程序引擎设置哪些服务帐户权限 -> 云功能通信
【发布时间】:2019-11-30 09:47:23
【问题描述】:

我在确定需要将某些角色分配给哪个服务帐户时遇到问题。

我有一个 NodeJS 应用程序在我的灵活应用程序引擎环境中运行。 我有一个 hello-world python3.7 HTTP 云函数。

我想从我的应用引擎向我的云功能发出 GET 请求。

当 allUser 成员在 hello-world 云函数上被赋予 Cloud Function Invoker 角色时,一切正常。

但现在我想保护我的云功能端点,以便只有我的灵活应用引擎可以访问它。

我删除了 allUser 成员,当应用引擎尝试调用时,如预期的那样,我得到了 403。

现在我将 @appspot.gserviceaccount.com 和 @gae-api-prod.google.com.iam.gserviceaccount.com 成员添加到 hello-world 云函数,并赋予他们 Cloud Function Invoker 角色。

我希望灵活的应用引擎现在能够调用 hello-world 云函数,因为我赋予它 Cloud Function Invoker 角色。

但我不断收到 403 错误。

应用引擎灵活使用什么服务帐户来调用云函数 API?

【问题讨论】:

  • 你在正确的轨道上。接下来,从元数据服务器请求一个身份令牌,并将该令牌包含在“授权:承载 ID_TOKEN”HTTP 标头中,以向 Cloud Functions 发出请求。 cloud.google.com/compute/docs/instances/…
  • 注意:基于身份的授权不需要为服务帐户分配任何角色。服务帐号的身份用于授权,而不是分配给服务帐号的角色。

标签: google-app-engine google-cloud-platform


【解决方案1】:

约翰·汉利是正确的,

当使用 GCP 库执行操作时(例如 google-cloud-firestore),执行函数将使用底层服务帐户权限来执行这些操作。

在对云函数 URL 执行手动 HTTP 请求时,您必须从元数据服务器获取令牌以正确验证您的请求。

def generate_token() -> str:
    """Generate a Google-signed OAuth ID token"""
    token_request_url: str = f'http://metadata/computeMetadata/v1/instance/service- 
    accounts/default/identity?audience={TARGET_URL}'
    token_request_headers: dict = {'Metadata-Flavor': 'Google'}

    token_response = requests.get(token_request_url, headers=token_request_headers)
    return token_response.content.decode("utf-8")

def do_request():
    token: str = generate_token()

    headers: dict = {
        'Content-type': 'application/json',
        'Accept': 'application/json',
        'Authorization': f'Bearer {token}'
    }

    requests.post(url=TARGET_URL, json=data, headers=headers)

【讨论】:

    【解决方案2】:

    为了将云功能连接到服务帐户,需要进行一些设置:

    • 启用所需的 API
    • 启用服务帐号
    • 充当用户服务帐号

    默认服务帐号创建云功能,有时不具备所有权限。

    您可以在这里找到更多信息:

    https://cloud.google.com/functions/docs/securing/

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 2010-10-05
      • 2012-03-26
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多