【问题标题】:Google Cloud Function 403 for internal authenticated requests用于内部认证请求的 Google Cloud Function 403
【发布时间】:2020-12-01 21:47:26
【问题描述】:

我正在从我的 GCP 项目中调用云函数。

当功能配置为仅允许内部流量时,我收到 403(权限被拒绝),请参阅 https://cloud.google.com/functions/docs/networking/network-settings#ingress_settings

当移除入口控制没有问题时,该函数以状态 200 响应。 该功能不允许允许未经身份验证的访问,IAM 策略已配置

https://cloud.google.com/functions/docs/securing/authenticating#function-to-function为例:

# main.py
import requests

# TODO<developer>: set these values
# REGION = None
# PROJECT_ID = None

RECEIVING_FUNCTION = 'hello-get'

# Constants for setting up metadata server request
# See https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
function_url = f'https://{REGION}-{PROJECT_ID}.cloudfunctions.net/{RECEIVING_FUNCTION}'

metadata_server_url = \
    'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience='
token_full_url = metadata_server_url + function_url
token_headers = {'Metadata-Flavor': 'Google'}


def hello_trigger(request):
    token_response = requests.get(token_full_url, headers=token_headers)
    jwt = token_response.text

    function_headers = {'Authorization': f'bearer {jwt}'}
    function_response = requests.get(function_url, headers=function_headers)

    function_response.raise_for_status()

    return function_response.text


def hello_get(req):
    return 'Hello there...'

使用所需的入口设置部署函数和触发函数:

gcloud functions deploy hello-get --trigger-http --entry-point hello_get --runtime python37 --ingress-settings internal-only
gcloud functions deploy hello-trigger --trigger-http --entry-point hello_trigger --runtime python37 --ingress-settings all --allow-unauthenticated

调用 hello-trigger 返回 403。

更改hello-get 的入口可以解决问题:

gcloud functions deploy hello-get --trigger-http --entry-point hello_get --runtime python37 --ingress-settings all

现在调用 hello-trigger 返回 200。

用于 Cloud Functions 的服务帐户被赋予此设置的 Functions Invoker 角色。

【问题讨论】:

  • Google Cloud Functions 不是您的 VPC 中的资源。这意味着当配置internal-only 时,您的触发函数无法调用您的 hello-get 函数。请注意“仅来自同一项目中的 VPC 网络的请求”的措辞。 Cloud Functions 位于 Google 的网络中。
  • @JohnHanley 谢谢。你能举一个我能够调用hello-get的设置示例吗?如果你不介意把它拼出来,我显然很难理解这里的网络细节。
  • 您可以从 Compute Engine 实例调用该函数。对于函数调用,使用授权而不是网络。 cloud.google.com/functions/docs/securing/authenticating

标签: google-cloud-platform google-cloud-functions cloud-security


【解决方案1】:

当您将入口流量设置为仅限内部时,仅接受来自您的 VPC 或来自 VPC SC(服务控制)的流量。

在这里,在您的触发函数中,您不是来自您的 vpc,而是来自另一个 vpc(一个无服务器 VPC,由 Google 管理,部署云函数的地方)。因此,不遵守入口设置,您会得到 403。

因此,为此您有 2 个解决方案:

  1. 仅使用 IAM 服务来过滤谁可以调用或不能调用您的函数,并通过 ingress=all 让您的函数“公开”。 (约翰在他的第二条评论中提出的解决方案)。这已经是高级别的安全性了。

但是,有时,出于监管原因(或出于旧式安全团队设计),网络控制是首选。

  1. 如果您想通过您的 VPC,您需要

像这样,您的触发函数的所有传出流量都将通过无服务器 VPC 连接器,因此,流量在尝试到达您的“入口内部”云函数之前先在您的 VPC 中路由。并且会被接受。


如果您的函数使用 ingress=all 设置,那么任何人都可以通过互联网访问它。

但是,如果您不公开该功能,我的意思是,授权给未经身份验证的用户,则只有有效的请求(通过角色 cloudfunctions.invoker 进行身份验证和授权)将被处理您的云函数

事实上,任何 Google 服务名称 GFE: Google Front End 都有一个公共层。该层负责许多事情(在 HTTPS 中公开您的服务,管理您的证书,丢弃 DDoS 攻击 OSI 第 4 层,...),检查身份验证标头和针对 IAM 服务的授权检查。

因此,在第 4 层受到 DDoS 攻击的情况下,GFE 默认过滤这些攻击。在第 7 层攻击的情况下,只允许授权请求(有效),您只需为它们付费。 GFE 执行的过滤器是免费的。

【讨论】:

  • 感谢您的解释!追问:缺乏入口控制是否会增加 DDoS 攻击的脆弱性?
  • 也许@JohnHanley 会告诉你
  • 我回答你购买编辑我的答案。 @JohnHanley 是一个安全大师(和怪物!),我相信他将能够添加一些聪明的补充!
猜你喜欢
  • 1970-01-01
  • 2019-06-11
  • 2019-01-18
  • 2021-07-13
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 2021-04-05
  • 1970-01-01
相关资源
最近更新 更多