使用 Managed Service for Apache Spark 時,叢集和工作資料會儲存在與叢集中 Compute Engine VM 相關聯的永久磁碟,以及 Cloud Storage 暫存 bucket。根據預設,這個永久磁碟和 bucket 資料會使用 Google 產生的資料加密金鑰 (DEK) 和金鑰加密金鑰 (KEK) 加密。
如要控管及管理金鑰加密金鑰 (KEK),可以使用客戶自行管理的加密金鑰 (CMEK) (Google 仍會控管資料加密金鑰 (DEK))。如要進一步瞭解 Google 資料加密金鑰,請參閱「靜態資料加密」。
CMEK 叢集資料加密
您可以使用客戶自行管理的加密金鑰 (CMEK),加密下列叢集資料:
- 附加至 Managed Service for Apache Spark 叢集 VM 的永久磁碟資料
- 提交至叢集的工作引數資料,例如與 Spark SQL 工作一併提交的查詢字串
- 叢集中繼資料、工作驅動程式輸出內容,以及寫入 Managed Service for Apache Spark 叢集暫存 bucket 的其他資料
事前準備
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc, Cloud Key Management Service, Compute Engine, and Cloud Storage APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
建立金鑰
如要使用 CMEK 保護 Managed Service for Apache Spark 資源,可以自動或手動建立金鑰。
自動建立金鑰
使用 Autokey 自動佈建及指派 CMEK。Autokey 會在建立資源時,視需求產生金鑰環和金鑰。服務代理會在加密和解密作業中使用金鑰。如有需要,Autokey 會建立代理,並授予必要的 Identity and Access Management (IAM) 角色。詳情請參閱「Autokey 總覽」。
手動建立金鑰
請按照下列步驟,手動建立用於 CMEK 加密叢集資料的金鑰:
使用 Cloud KMS 建立一或多個金鑰。資源名稱 (也稱為金鑰的資源 ID) 會在後續步驟中使用,其結構如下:
金鑰 (CMEK) 必須與加密資源位於相同位置。 舉例來說,用於加密projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME
us-central1區域中資源的 CMEK 也必須位於us-central1區域。為確保下列各服務帳戶 (Compute Engine 服務代理服務帳戶、Cloud Storage 服務代理服務帳戶和 Managed Service for Apache Spark 服務代理服務帳戶) 具備必要權限,可使用 Cloud KMS 金鑰保護資源,請要求管理員將專案中的 Cloud KMS CryptoKey 加密者/解密者 (
roles/cloudkms.cryptoKeyEncrypterDecrypter) IAM 角色授予下列各服務帳戶:Compute Engine 服務代理服務帳戶、Cloud Storage 服務代理服務帳戶和 Managed Service for Apache Spark 服務代理服務帳戶。使用 Google Cloud CLI,將 Cloud KMS CryptoKey Encrypter/Decrypter 角色指派給 Managed Service for Apache Spark 服務代理服務帳戶的範例:
gcloud projects add-iam-policy-binding KMS_PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@dataproc-accounts. \ --role roles/cloudkms.cryptoKeyEncrypterDecrypter
更改下列內容:
KMS_PROJECT_ID:包含 Cloud KMS 金鑰的 Google Cloud 專案 ID。PROJECT_NUMBER:執行 Managed Service for Apache Spark 資源的 Google Cloud 專案編號 (非專案 ID)。如果 Managed Service for Apache Spark 服務代理角色未附加至 Managed Service for Apache Spark 服務代理服務帳戶,請將
serviceusage.services.use權限新增至附加至 Managed Service for Apache Spark 服務代理服務帳戶的自訂角色。
建立使用 CMEK 的叢集
建立 Managed Service for Apache Spark 叢集時,請傳遞金鑰的資源 ID。
gcloud CLI
- 如要使用金鑰加密叢集永久磁碟資料,請在建立叢集時,將金鑰的資源 ID 傳遞至
--gce-pd-kms-key旗標。gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --gce-pd-kms-key='projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME' \ other arguments ...您可以透過
gcloud指令列工具驗證金鑰設定。gcloud dataproc clusters describe CLUSTER_NAME \ --region=REGION指令輸出片段:
... configBucket: dataproc- ... encryptionConfig: gcePdKmsKeyName: projects/project-id/locations/region/keyRings/key-ring-name/cryptoKeys/key-name ... - 如要使用金鑰加密叢集永久磁碟資料和工作引數資料,請在建立叢集時,將金鑰的資源 ID 傳遞至
--kms-key旗標。如要查看使用 `--kms-key` 旗標加密的工作類型和引數清單,請參閱 [Cluster.EncryptionConfig.kmsKey](/managed-spark/docs/reference/rest/v1/ClusterConfig#EncryptionConfig.FIELDS.kms_key)。gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --kms-key='projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME' \ other arguments ...您可以使用 gcloud CLI
dataproc clusters describe指令驗證金鑰設定。金鑰資源 ID 已設為gcePdKmsKeyName和kmsKey,以便使用金鑰加密叢集永久磁碟和工作引數資料。gcloud dataproc clusters describe CLUSTER_NAME \ --region=REGION指令輸出片段:
... configBucket: dataproc- ... encryptionConfig: gcePdKmsKeyName: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME kmsKey: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME ... - 如要加密叢集中繼資料、工作驅動程式,以及寫入 Cloud Storage 中 Managed Service for Apache Spark 暫存 bucket 的其他輸出資料,請按照下列步驟操作:
- 使用 CMEK 建立自己的 bucket。將金鑰新增至 bucket 時,請使用您在步驟 1 建立的金鑰。
- 建立叢集時,請將 bucket 名稱傳遞至
--bucket旗標。
gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --bucket=CMEK_BUCKET_NAME \ other arguments ...如果工作接受 bucket 引數,您也可以將啟用 CMEK 的 bucket 傳遞至 `gcloud dataproc jobs submit` 指令,如下列 `cmek-bucket` 範例所示:
gcloud dataproc jobs submit pyspark gs://cmek-bucket/wordcount.py \ --region=region \ --cluster=cluster-name \ -- gs://cmek-bucket/shakespeare.txt gs://cmek-bucket/counts
REST API
- 如要使用金鑰加密叢集 VM 永久磁碟資料,請在 cluster.create 要求中加入 ClusterConfig.EncryptionConfig.gcePdKmsKeyName 欄位。
您可以使用 gcloud CLI
dataproc clusters describe指令驗證金鑰設定。gcloud dataproc clusters describe CLUSTER_NAME \ --region=REGION指令輸出片段:
... configBucket: dataproc- ... encryptionConfig: gcePdKmsKeyName: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME ... - 如要使用金鑰加密叢集 VM 永久磁碟資料和工作引數資料,請在 cluster.create 要求中加入
Cluster.EncryptionConfig.kmsKey欄位。如需使用--kms-key欄位加密的工作類型和引數清單,請參閱 Cluster.EncryptionConfig.kmsKey。您可以使用 gcloud CLI
dataproc clusters describe指令驗證金鑰設定。金鑰資源 ID 已設為gcePdKmsKeyName和kmsKey,以便使用金鑰加密叢集永久磁碟和工作引數資料。gcloud dataproc clusters describe CLUSTER_NAME \ --region=REGION指令輸出片段:
... configBucket: dataproc- ... encryptionConfig: gcePdKmsKeyName: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME kmsKey: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME ... - 如要加密叢集中繼資料、工作驅動程式,以及寫入 Cloud Storage 中 Managed Service for Apache Spark 暫存 bucket 的其他輸出資料,請按照下列步驟操作:
- 使用 CMEK 建立自己的 bucket。將金鑰新增至 bucket 時,請使用您在步驟 1 建立的金鑰。
- 將 bucket 名稱傳遞至 ClusterConfig.configBucket 欄位,做為 cluster.create 要求的一部分。
gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --bucket=CMEK_BUCKET_NAME \ other arguments ...如果工作接受 bucket 引數,您也可以將啟用 CMEK 的 bucket 傳遞至 `gcloud dataproc jobs submit` 指令,如下列 `cmek-bucket` 範例所示:
gcloud dataproc jobs submit pyspark gs://cmek-bucket/wordcount.py \ --region=region \ --cluster=cluster-name \ -- gs://cmek-bucket/shakespeare.txt gs://cmek-bucket/counts
搭配使用 CMEK 與工作流程範本資料
Managed Service for Apache Spark 工作流程範本工作引數資料 (例如 Spark SQL 工作的查詢字串) 可使用 CMEK 加密。請按照本節的步驟 1、2 和 3,將 CMEK 與 Managed Service for Apache Spark 工作流程範本搭配使用。如要查看啟用這項功能時,使用 CMEK 加密的工作流程範本工作類型和引數清單,請參閱 WorkflowTemplate.EncryptionConfig.kmsKey。
- 使用 Cloud KMS 建立金鑰。金鑰的資源名稱會在後續步驟中使用,其建構方式如下:
projects/project-id/locations/region/keyRings/key-ring-name/cryptoKeys/key-name
如要讓 Managed Service for Apache Spark 服務帳戶使用您的金鑰,請按照下列步驟操作:
將 Cloud KMS
CryptoKey Encrypter/Decrypter角色指派給 Managed Service for Apache Spark 服務代理服務帳戶。您可以使用 gcloud CLI 指派角色:gcloud projects add-iam-policy-binding KMS_PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@dataproc-accounts. \ --role roles/cloudkms.cryptoKeyEncrypterDecrypter
更改下列內容:
KMS_PROJECT_ID:執行 Cloud KMS 的 Google Cloud 專案 ID。這個專案也可以是執行 Managed Service for Apache Spark 資源的專案。
PROJECT_NUMBER:執行 Managed Service for Apache Spark 資源的 Google Cloud 專案編號 (非專案 ID)。
在執行 Managed Service for Apache Spark 資源的專案中,啟用 Cloud KMS API。
如果 Managed Service for Apache Spark 服務代理角色未附加至 Managed Service for Apache Spark 服務代理服務帳戶,請將
serviceusage.services.use權限新增至附加至 Managed Service for Apache Spark 服務代理服務帳戶的自訂角色。如果 Managed Service for Apache Spark 服務代理角色已附加至 Managed Service for Apache Spark 服務代理服務帳戶,則可略過這個步驟。
您可以使用 gcloud CLI 或 Dataproc API,在工作流程中設定步驟 1 建立的金鑰。在工作流程上設定金鑰後,系統會使用該金鑰,加密 WorkflowTemplate.EncryptionConfig.kmsKey 中列出的任何工作類型和引數的所有工作流程工作引數和查詢。
gcloud CLI
使用 gcloud dataproc workflow-templates create 指令建立工作流程範本時,將金鑰的資源 ID 傳遞至
--kms-key旗標。範例:
您可以透過gcloud dataproc workflow-templates create my-template-name \ --region=region \ --kms-key='projects/project-id/locations/region/keyRings/key-ring-name/cryptoKeys/key-name' \ other arguments ...
gcloud指令列工具驗證金鑰設定。gcloud dataproc workflow-templates describe TEMPLATE_NAME \ --region=REGION
... id: my-template-name encryptionConfig: kmsKey: projects/PROJECT_ID/locations/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME ...
REST API
在 workflowTemplates.create 要求中,使用 WorkflowTemplate.EncryptionConfig.kmsKey。
您可以發出 workflowTemplates.get 要求,驗證金鑰設定。傳回的 JSON 包含
kmsKey:... "id": "my-template-name", "encryptionConfig": { "kmsKey": "projects/project-id/locations/region/keyRings/key-ring-name/cryptoKeys/key-name" },
Cloud External Key Manager
Cloud External Key Manager (Cloud EKM) 可讓您使用支援的外部金鑰管理合作夥伴管理的金鑰,保護 Managed Service for Apache Spark 資料。在 Managed Service for Apache Spark 中使用 Cloud EKM 的步驟,與設定 CMEK 金鑰的步驟相同,但有以下差異:金鑰會指向外部代管金鑰的 URI (請參閱 Cloud EKM 總覽)。
Cloud EKM 錯誤
使用 Cloud EKM 時,如果輸入內容、Cloud EKM、外部金鑰管理合作夥伴系統,或 Cloud EKM 與外部系統之間的通訊發生錯誤,叢集建立作業可能會失敗。如果您使用 REST API 或 Google Cloud 控制台,錯誤會記錄在 Cloud Logging 中。您可以在「View Log」(查看記錄) 分頁中查看失敗叢集的錯誤。