啟用 Cloud KMS Autokey

本頁說明如何在資源資料夾中啟用及設定 Cloud KMS Autokey。如要進一步瞭解 Autokey,請參閱「Autokey 總覽」。本頁面的步驟應由安全性管理員完成。

事前準備

您必須具備下列條件,才能啟用 Cloud KMS Autokey:

  • 機構資源,其中包含要啟用 Autokey 的資料夾。如果您沒有要啟用 Autokey 的資料夾,可以建立新的資源資料夾。在這個資料夾中啟用 Autokey 後,資料夾內的所有資源專案都會啟用 Autokey。
  • 如果您有要使用 Autokey 的資源專案,但這些專案不在啟用 Autokey 的資料夾中,您可以將現有的資源專案移至新資料夾

必要的角色

如要取得啟用及設定 Autokey 所需的權限,請管理員授予您機構或資料夾的下列 IAM 角色:

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這些預先定義的角色具備啟用及設定 Autokey 所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

如要啟用及設定 Autokey,您必須具備下列權限:

  • cloudkms.autokeyConfigs.*
  • cloudkms.projects.showEffectiveAutokeyConfig
  • resourcemanager.folders.get
  • resourcemanager.folders.getIamPolicy
  • resourcemanager.folders.setIamPolicy
  • billing.resourceAssociations.create

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

決定如何啟用 Autokey

您可以使用 Terraform 進行必要的設定變更,將 Autokey 納入基礎架構即程式碼策略。如果您想使用 Terraform 啟用 Autokey,請參閱本頁的「使用 Terraform 啟用 Autokey」一節。如果您不想使用 Terraform,請先按照下一節中的操作說明進行。

設定金鑰專案

建議您建立新的金鑰專案,用於容納 Autokey 建立的 Cloud KMS 資源。您應在機構資源中建立金鑰專案。如果您已建立要用於 Autokey 建立的金鑰的金鑰專案,可以略過「建立金鑰專案」一節,並繼續閱讀本頁的「設定 Autokey 金鑰專案」一節。

您可以在要啟用 Autokey 的資料夾中建立金鑰專案。您不應在主要專案中建立其他資源。如果您嘗試在金鑰專案中建立由 Autokey 保護的資源,Autokey 會拒絕建立新金鑰的要求。

如果您日後想遷移至 Assured Workloads,請在受這些金鑰保護的資源所在的資料夾中建立金鑰專案。

如果貴機構使用 constraints/gcp.restrictCmekCryptoKeyProjects 機構政策限制,確保所有 CMEK 均來自指定的關鍵專案,則必須將關鍵專案加入允許專案清單。如要進一步瞭解 CMEK 機構政策,請參閱「CMEK 機構政策」。

建立金鑰專案

控制台

  1. 前往 Google Cloud 控制台的「管理資源」頁面。

    前往「管理資源」

  2. 在「Select organization」部分,選取要建立專案的機構資源。
  3. 按一下 [Create Project]
  4. 在隨即顯示的「New project」視窗中,輸入專案名稱並選取帳單帳戶。專案名稱只能由英文字母、數字、單引號、連字號、空格或驚嘆號組成,而且長度必須介於 4 至 30 個字元之間。
  5. 在「Location」(位置) 中,選取要做為主要專案父項的資源。
  6. 按一下「Create」,即可完成專案建立程序。

gcloud

  • 建立新專案:

    gcloud projects create PROJECT_ID \
        --PARENT_TYPE=PARENT_ID
    

    更改下列內容:

    • PROJECT_ID:包含金鑰環的專案 ID。
    • PARENT_TYPE:您要建立新金鑰專案的資源類型。輸入 organization 即可在特定機構下建立新的關鍵專案,輸入 folder 則可在特定資料夾下建立新的關鍵專案。
    • PARENT_ID:您要建立金鑰專案的機構或資料夾 ID。

準備 Autokey 金鑰專案

控制台

  1. 在金鑰專案中啟用 Cloud KMS API。

    啟用 API

  2. 如果您使用的是新的金鑰專案,請授予 Cloud KMS 管理員權限。請重複執行下列步驟,將 Cloud KMS 管理員角色授予自己和其他 Cloud KMS 管理員使用者:

    1. 前往 Google Cloud 控制台的「IAM」頁面。

      前往「身分與存取權管理」頁面

    2. 選取金鑰專案。

    3. 按一下 「授予存取權」,然後輸入使用者的電子郵件地址。

    4. 選取「Cloud KMS 管理員」角色。

    5. 按一下 [儲存]

gcloud

  1. 在金鑰專案中啟用 Cloud KMS API:

    gcloud services enable cloudkms.googleapis.com
    
  2. 授予金鑰專案的 Cloud KMS 管理員權限。重複執行下列指令,將 roles/cloudkms.admin 角色授予自己和其他 Cloud KMS 管理員使用者:

    gcloud projects add-iam-policy-binding PROJECT_NUMBER \
        --role=roles/cloudkms.admin \
        --member=user:KEY_ADMIN_EMAIL
    

    更改下列內容:

    • PROJECT_NUMBER:主要專案的專案編號。
    • KEY_ADMIN_EMAIL:負責管理 Cloud KMS 金鑰的使用者電子郵件地址。

在資源資料夾上啟用 Cloud KMS Autokey

控制台

  1. 前往 Google Cloud 控制台的「KMS 控制項」頁面。

    前往 KMS 控制項

  2. 在內容挑選器中,選取要啟用 Autokey 的資料夾。

  3. 按一下「啟用」

  4. 選取主要專案,然後按一下「提交」

    系統會顯示訊息,確認已在資料夾上啟用 Cloud KMS Autokey。

API

為要啟用 Autokey 的資料夾建立 AutokeyConfig

curl "https://cloudkms.googleapis.com/v1/folders/FOLDER_ID/autokeyConfig?updateMask=keyProject" \
    --request "PATCH" \
    --header "authorization: Bearer TOKEN" \
    --header "content-type: application/json" \
    --data '{"key_project": "projects/PROJECT_ID"}'

更改下列內容:

  • FOLDER_ID:要啟用 Autokey 的資料夾 ID。
  • PROJECT_ID:主鍵專案的 ID。

設定 Cloud KMS 服務代理人

金鑰專案的 Cloud KMS 服務代理程式會代表 Cloud KMS 管理員,在建立資源時建立金鑰並套用 IAM 政策繫結。Cloud KMS 服務代理人必須具備 Cloud KMS 管理員權限,才能建立及指派金鑰。

  1. 建立 Cloud KMS 服務代理人:

    gcloud beta services identity create --service=cloudkms.googleapis.com \
        --project=PROJECT_NUMBER
    

    PROJECT_NUMBER 替換為主要專案的專案編號。

    輸出結果會與下列內容相似:

    Service identity created: service-PROJECT_NUMBER@gcp-sa-ekms.iam.gserviceaccount.com
    

    指令的輸出內容會指出已建立 Cloud EKM 服務帳戶 (含 gcp-sa-ekms 子網域)。不過,這項指令也會建立 Cloud KMS 服務代理程式 (含 gcp-sa-cloudkms 子網域),也就是您稍後在這些操作說明中使用的服務代理程式。

  2. 將 Cloud KMS 管理員權限授予服務代理人:

    gcloud projects add-iam-policy-binding PROJECT_NUMBER \
        --role=roles/cloudkms.admin \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudkms.iam.gserviceaccount.com
    

    PROJECT_NUMBER 替換為主要專案的專案編號。

授予 Autokey 使用者角色

您必須先授予開發人員必要的角色,他們才能使用 Autokey。您可以在資料夾層級或專案層級授予角色。這個角色可讓開發人員在該資料夾或專案中建立資源時,向 Cloud KMS 服務代理要求金鑰。

請選擇下列其中一項或兩者皆執行:

  • 在資料夾層級授予 roles/cloudkms.autokeyUser 角色:

    gcloud resource-manager folders add-iam-policy-binding \
        FOLDER_ID --role=roles/cloudkms.autokeyUser \
        --member=user:USER_EMAIL
    

    更改下列內容:

    • FOLDER_ID:要啟用 Autokey 的資料夾 ID。
    • USER_EMAIL:您要授予使用 Autokey 權限的使用者電子郵件地址。
  • 在專案層級授予 roles/cloudkms.autokeyUser 角色:

    gcloud projects add-iam-policy-binding PROJECT_ID \
        --role=roles/cloudkms.autokeyUser \
        --member=user:USER_EMAIL
    

    更改下列內容:

    • PROJECT_ID:資源專案的 ID。
    • USER_EMAIL:您要授予使用 Autokey 權限的使用者電子郵件地址。

Autokey 開發人員現在可以視需要建立金鑰。如要瞭解如何使用 Autokey 按需建立的金鑰,建立受保護的資源,請參閱「使用 Autokey 建立受保護的資源」。

使用 Terraform 啟用 Autokey

下列 Terraform 範例會自動執行下列設定步驟:

  • 建立資源資料夾
  • 建立金鑰專案
  • 授予使用者權限
  • 設定 Cloud KMS 服務代理人
  • 啟用 Autokey

您必須在資源資料夾中個別建立資源專案。

variable "organization_ID" {
  description = "Your Google Cloud Org ID"
  type        = string
  default     = "ORGANIZATION_ID"
}

variable "billing_account" {
  description = "Your Google Cloud Billing Account ID"
  type        = string
  default     = "BILLING_ACCOUNT_ID"
}

/* List the users who should have the authority to enable and configure
   Autokey at a folder level */
variable "autokey_folder_admins" {
  type    = list(string)
  default = [AUTOKEY_ADMIN_USER_IDS]
}

/* List the users who should have the authority to protect their resources
   with Autokey */
variable "autokey_folder_users" {
  type    = list(string)
  default = [AUTOKEY_DEVELOPER_USER_IDS]
}

/* List the users who should have the authority to manage crypto operations in
   the Autokey key project */
variable "autokey_project_kms_admins" {
  type    = list(string)
  default = [KEY_PROJECT_ADMIN_USER_IDS]
}

/* The project ID to use for the key project. The project ID must be 6 to 30
   characters with lowercase letters, digits, hyphens. The project ID must start
   with a letter. Trailing hyphens are prohibited */
variable "key_management_project_ID" {
  description = "Sets the project ID for the Key Management Project. This project will contain the Key Rings and Keys generated by Cloud KMS Autokey"
  type        = string
  default     = "KEY_PROJECT_ID"
}

# Create a new folder
resource "google_folder" "autokey_folder" {
  parent       = "organizations/${var.organization_ID}"
  display_name = "autokey_folder"
}

# Set permissions for key admins to use Autokey in this folder
resource "google_folder_iam_binding" "autokey_folder_admin" {
  folder  = google_folder.autokey_folder.name
  role    = "roles/cloudkms.autokeyAdmin"
  members = var.autokey_folder_admins
}

# Set permissions for users to protect resources with Autokey in this folder
resource "google_folder_iam_binding" "autokey_folder_users" {
  folder  = google_folder.autokey_folder.name
  role    = "roles/cloudkms.autokeyUser"
  members = var.autokey_folder_users
}

# Create a key project to store keys created by Autokey
 resource "google_project" "key_management_project" {
  project_id      = var.key_management_project_ID
  name            = var.key_management_project_ID
  billing_account = var.billing_account
  folder_id       = google_folder.autokey_folder.name
}

output "project_number" {
  value = google_project.key_management_project.number
}

# Grant role for Cloud KMS admins to use Autokey in the key project
resource "google_project_iam_binding" "autokey_project_admin" {
  project    = google_project.key_management_project.project_id
  role       = "roles/cloudkms.admin"
  members    = var.autokey_project_kms_admins
  depends_on = [ google_project.key_management_project ]
}

# Enable the Cloud KMS API in the key project
resource "google_project_service" "enable_api" {
  service                    = "cloudkms.googleapis.com"
  project                    = google_project.key_management_project.project_id
  disable_on_destroy         = false
  disable_dependent_services = false
  depends_on                 = [google_project.key_management_project]
}

# Create Cloud KMS service agent
resource "google_project_service_identity" "KMS_Service_Agent" {
  provider   = google-beta
  service    = "cloudkms.googleapis.com"
  project    = google_project.key_management_project.project_id
  depends_on = [google_project.key_management_project]
}

/* Grant role for the Cloud KMS service agent to use delegated
   Cloud KMS admin permissions */
resource "google_project_iam_member" "autokey_project_admin" {
  project = google_project.key_management_project.project_id
  role    = "roles/cloudkms.admin"
  member  = "serviceAccount:service-${google_project.key_management_project.number}@gcp-sa-cloudkms.iam.gserviceaccount.com"
}

/* Enable AutokeyConfig in this folder */
resource "google_kms_autokey_config" "autokey_config" {
  provider    = google-beta
  folder      = google_folder.autokey_folder.folder_id
  key_project = "projects/${google_project.key_management_project.project_id}"
}

更改下列內容:

  • BILLING_ACCOUNT_ID:您的 Google Cloud 帳單帳戶 ID。帳單帳戶 ID 是由 18 個英數字元組成,並以連字號分隔,例如 010101-F0FFF0-10XX01
  • AUTOKEY_ADMIN_USER_IDS:應具備 roles/cloudkms.autokeyAdmin 角色的使用者電子郵件地址清單,例如 "Ariel@example.com", "Charlie@example.com"
  • AUTOKEY_DEVELOPER_USER_IDS:應具備 roles/cloudkms.autokeyUser 角色的使用者電子郵件地址清單,例如 "Kalani@example.com", "Mahan@example.com"
  • KEY_PROJECT_ADMIN_USER_IDS:應具備 roles/cloudkms.admin 角色的使用者電子郵件地址清單,例如 "Sasha@example.com", "Nur@example.com"
  • KEY_PROJECT_ID:用於鍵專案的 ID,例如 autokey-key-project

強制使用 Autokey

如要強制在資料夾中使用 Autokey,您可以結合 IAM 存取控管機制和 CMEK 組織政策。這項功能會移除 Cloud KMS 服務代理人以外的使用者金鑰建立權限,然後要求所有資源都必須使用 Autokey 金鑰專案的 CMEK 進行保護。

如要在資料夾中強制使用 Autokey,請完成下列步驟:

  1. 移除在金鑰專案中手動建立金鑰的存取權。如果無法手動建立金鑰,則只能在這個專案中建立 Autokey 建立的金鑰。如要進一步瞭解如何控管存取權,請參閱「使用 IAM 控管存取權」。

  2. 在資料夾上設定組織政策,要求資源必須使用 constraints/gcp.restrictNonCmekServices 限制的 CMEK 保護。詳情請參閱「要求使用 CMEK 保護機制」。

  3. 在資料夾上設定機構政策,要求用於 CMEK 的金鑰必須來自使用 constraints/gcp.restrictCmekCryptoKeyProjects 限制的 Autokey 金鑰專案。詳情請參閱「限制 CMEK 使用 Cloud KMS 金鑰」。

停用 Autokey

Cloud KMS Autokey 是在資料夾層級啟用和停用。可為資料夾啟用 Autokey 的角色,也能為該資料夾停用 Autokey。如要在資料夾上停用 Autokey,您必須清除 AutokeyConfig,才能移除資料夾與 Autokey 金鑰專案之間的關聯。

移除資料夾的 Autokey 設定後,Cloud KMS 服務代理就無法再為開發人員在資料夾中建立資源時建立金鑰。移除資料夾和金鑰專案之間的連結會停用資料夾中的 Autokey,但建議您一併移除 roles/cloudkms.autokeyAdminroles/cloudkms.autokeyUser 角色的 IAM 繫結。

停用 Autokey 不會影響金鑰專案中的現有金鑰。您可以繼續使用這些金鑰來保護資源。

清除 AutokeyConfig

控制台

  1. 前往 Google Cloud 控制台的「KMS 控制項」頁面。

    前往 KMS 控制項

  2. 在內容挑選器中,選取要停用 Autokey 的資料夾。

  3. 按一下「停用」

    系統會顯示訊息,提示您確認是否要停用 Autokey。

  4. 如要停用 Autokey,請按一下「確認」

    系統會顯示訊息,確認已在資料夾中停用 Cloud KMS Autokey。

API

取消勾選要停用 Autokey 的資料夾:AutokeyConfig

curl "https://cloudkms.googleapis.com/v1/folders/FOLDER_ID/autokeyConfig?updateMask=keyProject" \
    --request "PATCH" \
    --header "authorization: Bearer TOKEN" \
    --header "content-type: application/json" \
    --data '{}'

更改下列內容:

  • FOLDER_ID:要停用 Autokey 的資料夾 ID。

撤銷 Autokey 角色

  1. 選用:撤銷 roles/cloudkms.autokeyAdmin 角色:

    gcloud resource-manager folders remove-iam-policy-binding \
        FOLDER_ID --role=roles/cloudkms.autokeyAdmin \
        --member=user:USER_EMAIL
    

    更改下列內容:

    • FOLDER_ID:您停用自動鍵的資料夾 ID。
    • USER_EMAIL:您要撤銷其管理 Autokey 權限的使用者電子郵件地址。
  2. 選用:在資料夾層級撤銷 roles/cloudkms.autokeyUser 角色:

    gcloud resource-manager folders remove-iam-policy-binding \
        FOLDER_ID --role=roles/cloudkms.autokeyUser \
        --member=user:USER_EMAIL
    

    更改下列內容:

    • FOLDER_ID:停用自動鍵的資料夾 ID。
    • USER_EMAIL:您要撤銷使用 Autokey 權限的使用者電子郵件地址。
  3. 選用:在專案層級撤銷 roles/cloudkms.autokeyUser 角色:

    gcloud projects remove-iam-policy-binding RESOURCE_PROJECT_NUMBER \
        --role=roles/cloudkms.autokeyUser \
        --member=user:USER_EMAIL
    

    更改下列內容:

    • RESOURCE_PROJECT_NUMBER:在停用 Autokey 的資料夾中,資源專案的專案編號。
    • USER_EMAIL:您要撤銷使用 Autokey 權限的使用者電子郵件地址。
  4. 選用:如果您不打算繼續使用 Autokey 的金鑰專案來管理其他資料夾,請撤銷 Cloud KMS 服務代理的 roles/cloudkms.admin 角色:

    gcloud projects remove-iam-policy-binding KEY_PROJECT_NUMBER \
        --role=roles/cloudkms.admin \
        --member=serviceAccount:service-KEY_PROJECT_NUMBER@gcp-sa-cloudkms.iam.gserviceaccount.com
    

    KEY_PROJECT_NUMBER 替換為主要專案的數字 ID。

  5. 選用:如果您不打算繼續使用金鑰專案中建立的金鑰,請撤銷 Cloud KMS 管理員的 roles/cloudkms.admin 角色:

    gcloud projects remove-iam-policy-binding KEY_PROJECT_NUMBER \
        --role=roles/cloudkms.admin \
        --member=user:KEY_ADMIN_EMAIL
    

    更改下列內容:

    • KEY_PROJECT_NUMBER:主要專案的專案編號。
    • USER_EMAIL:您要撤銷使用 Autokey 權限的使用者電子郵件地址。

後續步驟