【发布时间】: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