簡體   English   中英

從 Python 2 升級到 Python 3 Google App Engine

[英]Upgrading from Python 2 to Python 3 Google App Engine

我想將我的應用程序引擎 python 版本從 Python 2 升級到 Python 3。但是在第二代應用程序引擎中,我們不能使用 app.yaml 中的處理程序中的登錄字段來使應用程序引擎中的某些頁面只能由管理員訪問。

根據谷歌的指導方針,建議如下:不支持登錄字段。 使用 Cloud Identity and Access Management 進行用戶管理。

我不知道如何使用身份和訪問管理來控制登錄訪問?

現在,您必須在代碼中使用 Cloud IAM Client Librares 才能提供訪問權限。 您可以在此處的 Python 3 中找到如何使用它的示例。

大多數 Google Cloud Platform 中的用戶身份驗證與 Python 2 App Engine 方法有很大不同。 您可以在您的應用程序中以多種方式完全實現用戶登錄以進行此限制,但您不能僅在.yaml文件中設置一個屬性來執行此操作。

或者,嘗試將您的管理功能全部放在單獨的 App Engine 服務中,然后使用 IAP(身份識別代理)將對該服務的訪問限制為所需用戶。 這個頁面應該有幫助。 這是我編寫的一個代碼實驗室,其中包含使用 IAP 保護整個 App Engine 應用程序的詳細步驟,而不僅僅是一項服務。

您是否嘗試讓管理員用戶可以實際使用的僅管理員端點? 或者您是否嘗試使用僅用於運行 cron 作業和/或排隊任務的管理員端點?

如果是前者(即具有管理員人員將實際查看的頁面/處理程序),那么此處的文檔可能就是您要查找的內容。 不幸的是,正如我在應用引擎文檔中注意到的那樣,您可能不得不閱讀“理論”一頁又一頁,而永遠看不到您可以嘗試實際使用的示例代碼。 然而,我的猜測是,您最終可能會編寫一個裝飾器來檢查用戶授權和身份驗證,如下所示。

如果您只是試圖限制對端點的訪問以保護正在運行的 cron 作業和排隊任務,那么您可能正在尋找這個這個解決方案。 基本上,您編寫一個裝飾器來驗證端點/處理程序是否被 cron 作業或任務隊列調用。 這是應該很好運行的工作代碼:

# 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)

自問這個問題以來,情況發生了變化,因此提供了更新的答案

2022年3月現在

  1. 您仍然可以在 app.yaml 文件中使用login:required ,它會強制訪問者使用他們的 gmail 帳戶登錄。

  2. Python 3 現在支持users API (參見公告),這意味着如果頁面受login: required保護,您現在可以在路由處理程序上調用is_current_user_admin()以確認它是您的管理員

  3. 即使您不想使用users API ,您仍然可以通過檢查以下任何標頭X-Appengine-User-Email, X-Appengine-User-Id來獲取登錄用戶的詳細信息(對於受login:required保護的頁面) X-Appengine-User-Email, X-Appengine-User-Id 您可以參考我其他 SO 問題的回復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM