Python 如何将对google app engine flask端点的访问限制为仅访问应用程序代码(或app engine服务帐户)

Python 如何将对google app engine flask端点的访问限制为仅访问应用程序代码(或app engine服务帐户),python,google-app-engine,flask,authorization,service-accounts,Python,Google App Engine,Flask,Authorization,Service Accounts,目前正在使用Python3.7和flask框架在AppEngine标准环境中构建应用程序。我需要安排一些任务,这将需要应用程序定期运行几个敏感端点 我想限制应用程序本身对这些端点的访问,防止非管理员用户访问这些端点。在Python 2版本的app engine中,可以在app.yaml文件中指定login:admin,如下所示: # app.yaml for google app engine standard env python 2 handlers: - url: /this_is

目前正在使用Python3.7和flask框架在AppEngine标准环境中构建应用程序。我需要安排一些任务,这将需要应用程序定期运行几个敏感端点

我想限制应用程序本身对这些端点的访问,防止非管理员用户访问这些端点。在Python 2版本的app engine中,可以在app.yaml文件中指定login:admin,如下所示:

# app.yaml for google app engine standard env python 2

handlers:

  - url: /this_is/my_protected/endpoint
    script: main.app
    login: admin

然而,在应用程序引擎环境的Python3.7化身中,这已经不可能了

我知道可能需要在我的flask应用程序的main.py文件中进行身份验证,但我不确定从何处开始。我已经让firebase auth工作,并且该应用程序正在为几个面向用户的端点验证用户。但是,我不确定如何验证我自己的应用程序引擎应用程序或可能的服务帐户以运行它自己的几个端点。我试过检查文档,但它们要么是稀疏的,要么就是我根本找不到我需要的信息


有没有一种简单的方法可以做到这一点?

正如一篇评论中所建议的,这是我的简化版?解决方案,使谷歌应用程序引擎中的特定烧瓶端点只能由应用程序代码或应用程序引擎服务帐户访问。答案基于与和相关的文档

基本上,我们编写了一个decorator来验证消息头中的X-Appengine-Cron:true是否表示端点是由您的代码而不是远程用户调用的。如果未找到标头,则不运行受保护的函数

# python
# main.py

from flask import Flask, request, redirect, render_template

app = Flask(__name__)

# Define the decorator to protect your end points
def validate_cron_header(protected_function):
    def cron_header_validator_wrapper(*args, **kwargs):
        # https://cloud.google.com/appengine/docs/standard/python3/scheduling-jobs-with-cron-yaml#validating_cron_requests
        header = request.headers.get('X-Appengine-Cron')
        # If you are validating a TASK request from a TASK QUEUE instead of a CRON request, then use 'X-Appengine-TaskName' instead of 'X-Appengine-Cron'
        # example:
        # header = request.headers.get('X-Appengine-TaskName')
        # Other possible headers to check can be found here: https://cloud.google.com/tasks/docs/creating-appengine-handlers#reading_app_engine_task_request_headers

        # If the header does not exist, then don't run the protected function
        if not header:
            # here you can raise an error, redirect to a page, etc.
            return redirect("/")

        # Run and return the protected function
        return protected_function(*args, **kwargs)

    # The line below is necessary to allow the use of the wrapper on multiple endpoints
    # https://stackoverflow.com/a/42254713
    cron_header_validator_wrapper.__name__ = protected_function.__name__
    return cron_header_validator_wrapper


@app.route("/example/protected/handler")
@validate_cron_header
def a_protected_handler():
    # Run your code here
    your_response_or_error_etc = "text"
    return your_response_or_error_etc


@app.route("/yet/another/example/protected/handler/<myvar>")
@validate_cron_header
def another_protected_handler(some_var=None):
    # Run your code here
    return render_template("my_sample_template", some_var=some_var)

看,我已经找到了文档的相关部分。似乎可以通过检查Cron请求的http头中的X-Appengine-Cron:true来实现。如果您认为这可以回答问题并帮助他人,您应该将其作为社区wiki答案发布,以防这有助于其他用户。使用评论中的新信息,此问题已经得到了回答