【问题标题】:Google cloud deploy function with "allow unauthenticated" in python谷歌云部署功能在python中“允许未经身份验证”
【发布时间】:2020-07-31 06:40:54
【问题描述】:

我正在使用 Python 从另一个 Cloud Function 部署 Google Cloud Function。请参阅下面的代码:

import requests
import json


def make_func(request):


    # Get the access token from the metadata server
    metadata_server_token_url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token?scopes=https://www.googleapis.com/auth/cloud-platform'
    token_request_headers = {'Metadata-Flavor': 'Google'}
    token_response = requests.get(metadata_server_token_url, headers=token_request_headers)
    token_response_decoded = token_response.content.decode("utf-8")
    jwt = json.loads(token_response_decoded)['access_token']

    # Use the api to create the function
    response = requests.post('https://cloudfunctions.googleapis.com/v1/projects/myproject/locations/us-central1/functions',
                               json={"name":"projects/my-project/locations/us-central1/functions/funct","runtime":"python37","sourceArchiveUrl":"gs://bucket/main.zip","entryPoint":"hello_world","httpsTrigger": {} },
                               headers={'Accept': 'application/json', 
                                        'Content-Type': 'application/json',
                                        'Authorization': 'Bearer {}'.format(jwt)} )   
    if response:
         return 'Success! Function Created'
    else:
         return str(response.json())  

但是,此功能不会自动启用“允许未经身份验证”。因此,不允许来自外部的请求。在部署新功能时,如何更改我的 Python 代码以添加此功能?

谢谢

【问题讨论】:

    标签: python authentication google-cloud-platform google-cloud-functions


    【解决方案1】:

    您还需要为allUsers 成员赋予Cloud Functions Invoker 角色:

    from googleapiclient.discovery import build
    service = build('cloudfunctions', 'v1')
    
    project_id = ...
    location_id = ...
    function_id = ...
    resource = f'projects/{project_id}/locations/{location_id}/functions/{function_id}'
    
    set_iam_policy_request_body = {
        'policy': {
            "bindings": [
                {
                  "role": "roles/cloudfunctions.invoker",
                  "members": ["allUsers"],
                },
            ],
        },
    }
    
    request = service.projects().locations().functions().setIamPolicy(
        resource=resource,
        body=set_iam_policy_request_body,
    )
    response = request.execute()
    
    print(response)
    

    这使用google-api-python-client 包。

    【讨论】:

    • 你知道我如何在 Python 中做到这一点吗?
    • 谢谢。但这是在项目中创建角色对吗?如何将其绑定到我创建的函数?
    • 用一个完整的例子更新了我的答案。您也可以使用此 API 服务来部署函数,而不是手动获取令牌并创建 HTTP 请求:cloud.google.com/functions/docs/reference/rest/v1/…
    • @SiemPeters,不,它绑定在函数上。查看资源名称projects/{project_id}/locations/{location_id}/functions/{function_id}。不是项目的所有功能,只有在定义位置有function_id的这一项
    【解决方案2】:

    除了达斯汀的回答,你必须知道--allow-unauthenticated 是为了方便开发人员。在引擎盖下它执行 2 件事

    • 以私有模式部署您的函数
    • 将 allUsers 添加为具有 Cloudfunction.invoker 角色的成员
    gcloud functions add-iam-policy-binding --member=allUsers --role=roles/cloudfunctions.invoker function-1
    

    因此,确实,请使用 google-cloud-iam 库来执行此操作。

    此外,您当前的代码不起作用,因为您使用访问令牌访问 Cloud Function。

    • 确实,您有一个授权错误 (401) -> 您提供了一个授权标头,但它不是授权的。
    • 如果没有标头,您会收到 403 错误 -> 未经身份验证。

    无论如何,您需要有一个签名的身份令牌。你有描述和python代码sn -p here

    【讨论】:

    • 谢谢。你知道如何在 Python 中进行策略绑定吗?
    • 感谢@Dustin-Ingram
    猜你喜欢
    • 2021-03-10
    • 2020-11-27
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2019-01-30
    • 2017-08-15
    相关资源
    最近更新 更多