簡體   English   中英

通過雲端點基於 API 密鑰限制對雲 function 的訪問的問題

[英]An issue with restricting access to a cloud function based on API key through Cloud Endpoints

我嘗試通過在 openapi.yaml 中定義 API KEY 來限制對 Google 雲 function 的訪問

schemes:
  - https
produces:
  - application/json
security:
  - api_key: []
securityDefinitions:
  api_key:
    type: "apiKey"
    name: "mot"
    in: "query"  
paths:
  /:
    get:
      summary: Greet a user
      operationId: hello

部署步驟:

1. gcloud run deploy apikeytst1 --image="gcr.io/endpoints-release/endpoints-runtime-serverless:2" --allow-unauthenticated --platform managed --project=xxxx
2. gcloud endpoints services deploy apikeytst.yaml --project xxxx
3. ./gcloud_build_image -s apikeytst1-yyyyyyyyy-ew.a.run.app -c 2020-10-08r0 -p xxxx
4. gcloud run deploy apikeytst1 --image="gcr.io/xxxxx/endpoints-runtime-serverless:apikeytst1-yyyyyyyyyy-ew.a.run.app-2020-10-08r0" --allow-unauthenticated --platform managed  --project=xxxx

但是任何人都可以在沒有密鑰的情況下為 function 開具發票。

看來您在設置 Swagger 規范時遵循了谷歌提供的信息 它可能與您的雲端點部署或權限有關。 我建議您再次仔細完成所有雲端點步驟go。 如果您正確地將雲端點部署為雲 function 的包裝器,您應該無法直接調用雲 function。 相反,使用帶有 API 鍵的雲端點端點作為查詢參數。 一個關鍵步驟是從您的雲 function 中刪除 allUsers 調用者權限並添加以下內容:

gcloud functions add-iam-policy-binding FUNCTION_NAME \
   --region FUNCTION_REGION \
   --member "serviceAccount:ESP_PROJECT_NUMBER-compute@developer.gserviceaccount.com" \
   --role "roles/cloudfunctions.invoker" \
   --project FUNCTIONS_PROJECT_ID

Google 要求第一個密鑰必須是名稱為“key”或“api_key”的密鑰。 之后(在第二個等位置),我們可以添加任意名稱的鍵。

暫無
暫無

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

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