Google 云 http 认证 function-to-function
Google cloud http authentication function-to-function
我有一个名为 source
的云函数,它正在调用另一个名为 target
.
的函数
根据 google 在 https://cloud.google.com/functions/docs/securing/authenticating 上提供的文档,我完成了以下操作:
- 使用附加角色
roles/cloudfunctions.invoker
的服务帐户设置 source
的身份
- 使用相应的服务帐户及其角色设置
target
的权限
- 在
invoker
源代码中,我从具有适当受众的元数据服务器请求一个 jwt 令牌作为 target
函数 url(对令牌的调用是一个成功)并通过 target
. 的 http 调用的 header 传递它
但是,调用目标会导致 401
响应:
Error: Unauthorized Your client does not have permission to the requested URL /target
我在这里错过了什么?
我能够成功完成您所描述的场景;在这里,我分享我的代码,以便它可以阐明一些问题。出于调试目的,我在源函数中包含了一些打印语句。
main.py
import requests
REGION = 'us-central1'
PROJECT_ID = 'project123'
RECEIVING_FUNCTION = 'targetFunction123'
function_url = f'https://us-central1-project123.cloudfunctions.net/targetFunction123'
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 source(request):
print(REGION, PROJECT_ID, RECEIVING_FUNCTION)
print('Token full url: ',token_full_url)
token_response = requests.get(token_full_url, headers=token_headers)
jwt = token_response.text
print('JWT: ',jwt)
function_headers = {'Authorization': f'bearer {jwt}'}
function_response = requests.get(function_url, headers=function_headers)
print('Result =',function_response.text)
return 'ok'
def target(request):
return 'Target function called from source function'
requirements.txt
requests
这是我在被调用函数和调用函数上执行的 gcloud 命令。
调用函数:
gcloud functions deploy targetFunction123 --region=us-central1 --entry-point=target --runtime=python37 --memory=128MB --trigger-http --verbosity=debug
gcloud functions add-iam-policy-binding targetFunction123 --member='serviceAccount:serviceaccount123@project123.iam.gserviceaccount.com' --role='roles/cloudfunctions.invoker'
调用函数:
gcloud functions deploy sourceFunction123 --region=us-central1 --allow-unauthenticated --entry-point=source --runtime=python37 --memory=128MB --trigger-http --service-account=serviceaccount123@project123.iam.gserviceaccount.com --verbosity=debug
#Command to add the invoker role to serviceaccount123
gcloud projects add-iam-policy-binding project123 --member=serviceAccount:serviceaccount123@project123.iam.gserviceaccount.com --role='roles/cloudfunctions.invoker'
#Command to list all the roles associated with my serviceaccount123 (also for debugging)
gcloud projects get-iam-policy project123 --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:serviceaccount123@project123.iam.gserviceaccount.com"
被调用函数返回的结果将打印在调用函数的stackdriver日志中。
我希望你觉得这有用。
我有一个名为 source
的云函数,它正在调用另一个名为 target
.
根据 google 在 https://cloud.google.com/functions/docs/securing/authenticating 上提供的文档,我完成了以下操作:
- 使用附加角色
roles/cloudfunctions.invoker
的服务帐户设置source
的身份 - 使用相应的服务帐户及其角色设置
target
的权限 - 在
invoker
源代码中,我从具有适当受众的元数据服务器请求一个 jwt 令牌作为target
函数 url(对令牌的调用是一个成功)并通过target
. 的 http 调用的 header 传递它
但是,调用目标会导致 401
响应:
Error: Unauthorized Your client does not have permission to the requested URL /target
我在这里错过了什么?
我能够成功完成您所描述的场景;在这里,我分享我的代码,以便它可以阐明一些问题。出于调试目的,我在源函数中包含了一些打印语句。
main.py
import requests
REGION = 'us-central1'
PROJECT_ID = 'project123'
RECEIVING_FUNCTION = 'targetFunction123'
function_url = f'https://us-central1-project123.cloudfunctions.net/targetFunction123'
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 source(request):
print(REGION, PROJECT_ID, RECEIVING_FUNCTION)
print('Token full url: ',token_full_url)
token_response = requests.get(token_full_url, headers=token_headers)
jwt = token_response.text
print('JWT: ',jwt)
function_headers = {'Authorization': f'bearer {jwt}'}
function_response = requests.get(function_url, headers=function_headers)
print('Result =',function_response.text)
return 'ok'
def target(request):
return 'Target function called from source function'
requirements.txt
requests
这是我在被调用函数和调用函数上执行的 gcloud 命令。
调用函数:
gcloud functions deploy targetFunction123 --region=us-central1 --entry-point=target --runtime=python37 --memory=128MB --trigger-http --verbosity=debug
gcloud functions add-iam-policy-binding targetFunction123 --member='serviceAccount:serviceaccount123@project123.iam.gserviceaccount.com' --role='roles/cloudfunctions.invoker'
调用函数:
gcloud functions deploy sourceFunction123 --region=us-central1 --allow-unauthenticated --entry-point=source --runtime=python37 --memory=128MB --trigger-http --service-account=serviceaccount123@project123.iam.gserviceaccount.com --verbosity=debug
#Command to add the invoker role to serviceaccount123
gcloud projects add-iam-policy-binding project123 --member=serviceAccount:serviceaccount123@project123.iam.gserviceaccount.com --role='roles/cloudfunctions.invoker'
#Command to list all the roles associated with my serviceaccount123 (also for debugging)
gcloud projects get-iam-policy project123 --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:serviceaccount123@project123.iam.gserviceaccount.com"
被调用函数返回的结果将打印在调用函数的stackdriver日志中。
我希望你觉得这有用。