Secret Manager 提供 REST API 和 gRPC API,可直接或在應用程式中使用及管理密鑰。本頁說明如何啟用 Secret Manager API,以及如何設定 Google Cloud 專案,以便首次使用 Secret Manager。
熟悉 Secret Manager 後,建議使用獨立的 Google Cloud 專案。刪除專案時,也會一併刪除測試期間建立的所有資源,包括可計費資源。
如果您在安裝 Cloud Code 的 IDE 中開發應用程式,Secret Manager 會整合到擴充功能中。也就是說,您不必離開 IDE,就能建立、查看、更新及使用密鑰。如要進一步瞭解如何搭配使用 Secret Manager 與 Cloud Code,請參閱您偏好 IDE 的密鑰管理指南:VS Code、IntelliJ 或 Cloud Shell 編輯器。
事前準備
-
In the Google Cloud console, go to the project selector page.
-
Select or create a Google Cloud project.
啟用 Secret Manager API
控制台
前往 Google Cloud 控制台的「Secret Manager API」頁面。
確認頁面頂端的專案選取器中顯示專案名稱。
如果沒有看到專案名稱,請按一下專案選取器,然後選取專案。
- 按一下「啟用」。
gcloud
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
- 如要在 gcloud 工作階段中設定專案,請執行
gcloud config set project
Google Cloud 指令。將 PROJECT_ID 替換為專案 ID。 Google Cloudgcloud config set project PROJECT_ID
- 如要啟用 Secret Manager API,請執行
gcloud services enable
指令:gcloud services enable secretmanager.googleapis.com
- 如要確認 Secret Manager API 是否已啟用,請執行
gcloud services list
指令:gcloud services list --enabled
確認 Secret Manager API 列在已啟用 API 的清單中。
設定 Secret Manager 使用區域端點
如要建立區域性密鑰,請設定 Secret Manager 使用區域端點。區域端點是要求端點,只有在受影響的資源位於端點指定的位置時,才會允許要求繼續執行。使用區域端點可讓您執行工作負載時,符合資料落地和資料主權規定。
地區端點採用下列格式:
SERVICE_NAME.LOCATION.rep.googleapis.com
如要使用區域端點,您必須根據存取 Secret Manager 服務的方式,更新設定並提供區域端點位址。
gcloud
如要將 Google Cloud CLI 設為使用區域端點,請完成下列步驟:
確認您使用的是 Google Cloud CLI 402.0.0 以上版本。
將
api_endpoint_overrides/secretmanager
屬性設為要使用的地區端點:gcloud config set api_endpoint_overrides/secretmanager https://secretmanager.LOCATION.rep.googleapis.com/
將 LOCATION 替換為支援的 Google Cloud 位置名稱,例如
me-central2
。
REST
如要使用 API 連線至 Secret Manager 服務,請將一般 API 端點 URL (https://secretmanager.googleapis.com/v1/
) 替換為要使用的特定區域端點。地區端點採用下列格式:
https://secretmanager.LOCATION.rep.googleapis.com/v1/
將 LOCATION 替換為支援的 Google Cloud 位置名稱,例如 me-central2
。
如要查看支援的位置,請參閱「Secret Manager 位置」。
設定角色和權限
如要取得設定 Secret Manager 所需的權限,請要求管理員為您授予專案的 Secret Manager 管理員 (roles/secretmanager.admin
) 身分與存取權管理角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。
如要進一步瞭解 Secret Manager 的存取權控管機制,請參閱「使用 IAM 控管存取權」。