谷歌云功能身份验证.获取标识令牌授权持有者标头卷曲



根据cron风格的部署设置一个发布/订阅,以调用一个谷歌函数,该函数将检查新数据,然后将其推送到管道中。 此管道的一部分需要提交具有采用标识令牌的授权标头的 curl 调用。我还没有找到生成此标识令牌的好方法。

我目前尝试将云功能的所有者更改为具有跨存储/数据标签/云功能权限的服务帐户,并且我还使用了存储的凭据文件(即access.json) 与私钥。我有一个环境变量集(GOOGLE_APPLICATION_CREDENTIALS),它指向这个私钥,并尝试通过$(gcloud auth application-default print-access-token)在谷歌云函数中提取一个身份令牌 - 这将返回一个空字符串,没有错误。

# I have tried something very similar to this
command = "echo $(gcloud auth application-default print-access-token)"
p = subprocess.Popen(command, shell=True, 
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.wait()
out = p.communicate()
print("OUT_CODE: ", out)

我只想使用正确获取的令牌提交此 curl 命令。

command = "GOOGLE_APPLICATION_CREDENTIALS=/user_code/dl_access.json bash -c 'gcloud auth activate-service-account --key-file=/user_code/dl_access.json; echo $(gcloud auth application-default print-access-token)'"
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
p.wait()
out, err = p.communicate()
auth = out.decode().rstrip()
print("OUT_CODE: ", out, err)
command = "curl -X POST "
command += '-H "Authorization: Bearer $(gcloud auth application-default print-access-token)" '
command += '-H "Content-Type: application/json" '
command += 'https://datalabeling.googleapis.com/v1beta1/projects/'
command += '{}/datasets/{}/image:label '.format(PROJECT_ID, dataset.name.split("/")[-1])
command += "-d '{"
command += '"basicConfig": {'
command += '"labelGroup": "{}", '.format("test_label_group")
command += '"instruction": "{}", '.format("projects/cv/instructions/5cd5da11_0sdfgsdfgsdfg2c0b8eb8")
command += '"annotatedDatasetDisplayName": "{}", '.format(dataset.display_name)
command += '"replica_count": 3 '
command += '}, '
command += '"feature": "BOUNDING_BOX", '
command += '"boundingPolyConfig": { '
command += '"annotationSpecSet": "{}", '.format(
"projects/cv/annotationSpecSets/_b3b1_005csdfgc6_0000_297e1a11439bdc")
command += '}, '
command += "}' "
print(command)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
p.wait()
out, err = p.communicate()
print("out:", out)
print("err:", err)

由于Authorization: Bearer <ID_Token>ID_Token的空字符串,上述操作失败。

您的云函数已沙盒化,无法执行系统调用。请记住,您处于无服务器模式,您不知道底层服务器存在什么问题:它自己的问题是什么?是否安装了 gcloud 和 curl?哪个版本?....

因此,您必须编写代码并进行 Python 调用。签出用于数据标记的库。 如果您更喜欢直接调用 API,还可以从函数到函数代码中获得灵感。

不要在 Cloud Functions 中使用 shell 脚本、外部命令等。

下面是在云函数中运行时如何获取 OAuth 2.0 身份令牌的示例。在实际代码中,您需要将"受众"值更改为所调用的服务所需的任何值。如果调用此函数,它将在浏览器中显示"成功"或"失败"。此代码还演示了如何使用 Python 请求库发出 HTTP 请求。使用此库,而不是尝试执行程序 CURL。

import requests
import json
def entry(request):
id_token = requestIdentityToken('http://www.example.com')
if id_token is not None:
print('ID Token', id_token)
return f'SUCCESS'
else:
return f'FAILURE'
def requestIdentityToken(audience=None):
host = 'http://metadata.google.internal'
header = {'Metadata-Flavor': 'Google'}
if audience is None:
audience = 'http://example.com'
url = '{}/computeMetadata/v1/instance/service-accounts/default/identity?audience={}'.format(host, audience)
try:
r = requests.get(url=url, headers=header)
if r.status_code < 200 or r.status_code >= 300:
print('Error:', r.reason)
return None
return r.text
except Exception as e:
print(str(e))
return None

部署此函数的示例命令:

gcloud functions deploy requestIdentityToken --runtime python37 --trigger-http --entry-point entry

此命令将"打印"身份令牌,您将在此函数的堆栈驱动程序日志中找到该令牌。

附加信息:

  • 云函数标识
  • OpenID Connect
  • 云函数部署

最新更新