云函数权限(从其他云函数调用时为403) [英] Cloud Function Permissions (403 when invoking from another cloud function)

查看:21
本文介绍了云函数权限(从其他云函数调用时为403)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个云函数(CF1)调用云函数(CF2)。但是,我一直得了403分。

我的问题是找出哪个服务账号是CF2的"云函数调用者"。

我假设CF1的云函数服务代理是必需的,但这并不起作用。

推荐答案

来自Documentation

接收函数

首先,您需要将接收函数配置为接受调用函数的请求:

将云函数调用者(Roles/cloudfunctions.invoker)角色授予接收函数上的调用函数标识。默认情况下,此标识为PROJECT_ID@apppot.gserviceaccount.com

使用gcloud函数add-iam-policy-binding命令:

gcloud functions add-iam-policy-binding RECEIVING_FUNCTION 
  --member='serviceAccount:CALLING_FUNCTION_IDENTITY' 
  --role='roles/cloudfunctions.invoker'

其中Receiving_Function是接收函数,Call_Function_Identity是调用函数标识。


调用函数

在调用函数中,您需要:

  1. 创建Google签名的OAuth ID令牌,并将受众(AUD)设置为接收函数的URL。
  2. 将ID令牌包括在函数请求的Authorization:Beader ID_Token标头中。
# Requests is already installed, no need to add it to requirements.txt
import requests

def calling_function(request):
  # Make sure to replace variables with appropriate values
  receiving_function_url = 'https://REGION-PROJECT.cloudfunctions.net/RECEIVING_FUNCTION'

  # Set up metadata server request
  # See https://cloud.google.com/compute/docs/instances/verifying-instance-identity#request_signature
  metadata_server_token_url = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience='

  token_request_url = metadata_server_token_url + receiving_function_url
  token_request_headers = {'Metadata-Flavor': 'Google'}

  # Fetch the token
  token_response = requests.get(token_request_url, headers=token_request_headers)
  jwt = token_response.content.decode("utf-8")

  # Provide the token in the request to the receiving function
  receiving_function_headers = {'Authorization': f'bearer {jwt}'}
  function_response = requests.get(receiving_function_url, headers=receiving_function_headers)

  return function_response.content

可以找到更多信息here

这篇关于云函数权限(从其他云函数调用时为403)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆