簡體   English   中英

GCP API 雲功能網關默認服務帳戶身份驗證:“您的客戶端無權訪問請求的 URL”

[英]GCP API Gateway to Cloud Functions default service account authentication: "Your client does not have permission to the requested URL""

我正在嘗試在 Cloud Functions 前面使用 API 網關,但當 Cloud Functions 不可公開訪問時收到 401 響應。

我將網關設置為使用默認的 AppEngine 服務帳戶身份 ( project-id @appspot.gserviceaccount.com),並為該服務帳戶授予 IAM 中的Cloud Functions Invoker角色。 我還可以在 Cloud Functions Permissions 選項卡下的調用者中看到它。 當調用網關端點時,我收到一個 401 響應,其中包含格式錯誤的 HTML 負載,內容為“您的客戶端沒有請求的 URL [url]”的權限。 不幸的是,網關日志也沒有指出問題所在。 當我公開 Cloud Function 並等待幾分鍾時,調用成功。

我已多次閱讀本指南的“保護后端服務”部分,但看不出我遺漏了什么。

ps:這不是 JWT 問題,那部分效果很好

找到問題的根源:這是內部 JWT 中的aud聲明。 我想利用 Cloud Functions 的靈活可尋址性,例如,稱為a的 function 處理以a/...開頭的每個請求。 在我的示例中,function 被稱為hello ,我想從網關調用它作為/hello/example

The internal JWT sets the Audience claim to the address given in the OpenAPI specs ( https://...cloudfunctions.net/hello/example ), which does not exactly match the base URL of the Cloud Function (which is only https://...cloudfunctions.net/hello )。 因此,Cloud Functions 授權方拒絕 JWT,即使 Audience 是一個有效的 URL,實際上由這個 function 提供服務。

顯而易見的解決方案是在從 API 網關調用 Cloud Functions 時不使用變量后綴。

順便說一句,你可以使用 jwt_audience。

將 jwt_audience 設置為 https://...cloudfunctions.net/hello

請參閱https://cloud.google.com/endpoints/docs/openapi/openapi-extensions#jwt_audience_disable_auth

暫無
暫無

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

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