背景
透過 Identity and Access Management 自訂角色,您可以為使用者帳戶和服務帳戶授予精細的權限。當新權限開放使用時,自訂角色是由您的機構進行維護,這點與 Google 負責維護及更新的預先定義角色不同。
Cloud Deployment Manager 能讓自訂角色的設定及維護工作更加容易。您可以使用 Deployment Manager 來描述使用設定檔的角色,然後將設定檔簽入到您選擇的原始碼控管系統中。只要將 Deployment Manager 與原始碼控管搭配使用,就能更輕鬆地為以下項目達成「config-as-code」的效果:
- 隨著時間的變化追蹤自訂角色
- 推動自訂角色通過推出階段的測試及驗證
- 將角色在機構中的部署方式自動化
事前準備
-
Enable the IAM and Cloud Deployment Manager APIs.
透過 Deployment Manager 維護自訂角色的情境
請參考以下情境,這些情境說明透過 Deployment Manager 維護自訂角色的優點:
- 控制新權限的存取:您的某個自訂角色具有 Google Cloud 服務的權限。服務增加測試版功能及公佈新權限時,您的自訂角色不會自動繼承這些新權限,這些權限必須以手動方式新增。在新的權限加入自訂角色之前,機構中的使用者無法存取 Beta 版功能。您可以使用 Deployment Manager 的設定檔和原始碼控管來協助稽核現有權限、根據需要更新自定義角色,以及維護修訂版本記錄。
- 角色生命週期管理:您擁有新的自訂角色,並已指派
ALPHA
推出階段給這個角色。自訂角色經過測試和驗證後,就可以使用 Deployment Manager 來更新自訂角色的設定,並將其推進到BETA
版測試階段,不需透過Google Cloud console。 - 瞭解自訂角色採用:原始碼控管中存有越多的機構自訂角色定義,就能更輕鬆地共用、透過應用模式學習,並對多個專案施加存取權控管。
瞭解 Deployment Manager 設定檔
Deployment Manager 使用三種不同的檔案類型來說明部署作業,各種類型如下所述:
- 設定為 YAML 檔案,用於說明資源和相關屬性。對於身分與存取權管理 (IAM) 自訂角色,每個 YAML 檔案都會指定一或多個自訂角色的所有屬性,例如角色名稱、說明及權限。
- 結構定義為特定資源與相關預設值指定所有可能的屬性。對於 IAM 自訂角色,專案層級和機構層級自訂角色都有適用的結構定義。
- 範本為 Jinja 或 Python 檔案,可讓您將設定拆分成可重複使用的部分,藉此加強設定。對於 IAM 自訂角色,系統提供兩種範本:其中一個適用於專案層級自訂角色,另一個適用於機構層級自訂角色。
如要進一步瞭解這些類型,請參閱 Deployment Manager 基礎知識一文。
事前準備
如果您不熟悉 Deployment Manager,請先完成快速入門導覽課程,然後按照下列步驟操作:前往 Google Cloud 控制台的「IAM」(身分與存取權管理) 頁面。
從主體清單中找出名稱為「Google APIs service agent」(Google API 服務代理程式) 的主體。
如要編輯服務帳戶的角色,請按一下「編輯」
按鈕,然後新增「角色」>「角色管理員」角色。按一下 [儲存] 以套用這個角色。
- 將這個 GitHub 存放區的內容下載到本機電腦中。
- 在終端機視窗中,導覽到已下載存放區內的
examples/v2/iam_custom_role/jinja
目錄,其中包含 project_custom_role.yaml 檔案與相關聯的 Jinja 檔案。 執行下列指令以部署範例自訂角色,請用自己的部署作業名稱取代 your-deployment-name 預留位置值:
gcloud deployment-manager deployments create \ your-deployment-name --config project_custom_role.yaml
等待作業完成,屆時系統會顯示部署指紋和進度指標,如下所示:
The fingerprint of the deployment is PUo2fmKdoFdJqiLViHjT3Q== Waiting for create [operation-1513228867042-560460d157ad1-b4ababb8-c6a7dddc]...done.
如果作業成功,您會看到以下狀態訊息:
Create operation operation-1513228867042-560460d157ad1-b4ababb8-c6a7dddc completed successfully. NAME TYPE STATE ERRORS INTENT custom-role gcp-types/iam-v1:projects.roles COMPLETED []
在終端機視窗中,執行以下指令:
gcloud deployment-manager deployments describe your-deployment-name
您會看到部署相關說明,包括部署狀態、錯誤等完整資訊。
fingerprint: oaJoGesEFYvsAldP2bm5jQ== id: '3222872306422524501' insertTime: '2018-01-29T13:40:10.822-08:00' manifest: manifest-1517262010855 name: your-deployment-name operation: endTime: '2018-01-29T13:40:26.500-08:00' name: operation-1517262010573-563f1172be0c9-ce5f0242-239fe1af operationType: insert progress: 100 startTime: '2018-01-29T13:40:11.535-08:00' status: DONE user: id-7201362145@my-example-project.iam.gserviceaccount.com NAME TYPE STATE INTENT custom-role gcp-types/iam-v1:projects.roles COMPLETED
您也可以擷取部署作業的資訊清單,查看自訂角色設定的詳細資訊。在上述步驟 2 中
describe
指令的輸出內容中,記下manifest
ID。複製這個 ID (包括manifest-
前置字元),並用來取代下列預留位置值:gcloud deployment-manager manifests describe your-manifest-id \ --deployment your-deployment-name
資訊清單包含有關自訂角色部署的詳細資訊,包括來源 project_custom_role.yaml 和 project_custom_role.jinja 檔案的原始內容。請特別留意靠近資訊清單底部的
resources
區塊,這個區塊總結了自訂角色的相關細節:... resources: - name: custom-role properties: description: My description. includedPermissions: - iam.roles.get - iam.roles.list roleId: aCustomRole stage: ALPHA title: My Title resources: - name: custom-role type: gcp-types/iam-v1:projects.roles type: project_custom_role.jinja ...
前往 Google Cloud 控制台的「IAM」(身分與存取權管理) 頁面。
選取專案並按一下 [開啟]。
按一下左側選單中的 [角色]。
按一下清單中新建立的自訂角色名稱,操作畫面應會類似下方螢幕擷圖:
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Verify that billing is enabled for your Google Cloud project.
Enable the Deployment Manager and IAM APIs.
安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:
gcloud init
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Verify that billing is enabled for your Google Cloud project.
Enable the Deployment Manager and IAM APIs.
安裝 Google Cloud CLI。 安裝完成後,執行下列指令初始化 Google Cloud CLI:
gcloud init
如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI。
完成這些事前準備步驟後,您就能搭配使用 Deployment Manager 與 IAM 以維護自訂角色。
向 Google API 服務帳戶授予權限
如要使用 Deployment Manager 維護自訂角色,您必須先將相關權限授予 Google API 服務帳戶。根據預設,系統會為每個機構和專案建立這個帳戶。
現在 Google API 服務帳戶已經取得維護自訂角色的權限,您可以透過 Deployment Manager 部署自訂角色範例。
將自訂角色部署到您的專案
如上節所述,Deployment Manager 會使用一組設定檔來描述部署作業。關於 IAM,請參閱專案自訂角色的以下兩個範例檔:
project_custom_role.yaml
imports: - path: project_custom_role.jinja resources: - name: custom-role type: project_custom_role.jinja properties: roleId: myCustomRole title: My Title description: My description. includedPermissions: - iam.roles.get - iam.roles.list
這個 YAML 檔案會指定名為 custom-role
的自訂角色屬性,該角色會將權限授予 iam.roles.get
和 iam.roles.list
。請注意,這個檔案會參照 .jinja 檔案兩次;這些參照會使用 Jinja 範本提供的值,如下所述。
project_custom_role.jinja
# Copyright 2017 Google LLC. All rights reserved. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. resources: - name: custom-role type: gcp-types/iam-v1:projects.roles properties: parent: projects/{{ env["project"] }} roleId: {{ properties["roleId"] }} role: title: {{ properties["title"] }} description: {{ properties["description"] }} stage: {{ properties["stage"] }} includedPermissions: {{ properties["includedPermissions"] }}
這個 Jinja 檔案提供了自訂角色的範本,以儘可能減少在許多角色之間複製相同屬性的情況。如果相對應的 YAML 檔案沒有指定各個屬性的值,系統則會使用 .schema 檔案插入預設值。
如何透過 Deployment Manager 部署這個自訂角色:
查看您已部署的自訂角色
如何查看 Deployment Manager 中的自訂角色:
如要在 Google Cloud 控制台中查看自訂角色,請按照下列步驟操作: