使用 Apache Spark 的托管服务时,集群和作业数据存储在与集群中的 Compute Engine 虚拟机相关联的永久性磁盘上,以及 Cloud Storage 暂存存储桶中。默认情况下,此永久性磁盘和存储桶数据均采用 Google 生成的数据加密密钥 (DEK) 和密钥加密密钥 (KEK) 进行了加密。
如果您希望控制和管理密钥加密密钥 (KEK),可以使用客户管理的加密密钥 (CMEK)(Google 会继续控制数据加密密钥 (DEK))。如需详细了解 Google 数据加密密钥,请参阅静态加密。
CMEK 集群数据加密
您可以使用客户管理的加密密钥 (CMEK) 来加密以下集群数据:
- 连接到 Managed Service for Apache Spark 集群虚拟机的永久性磁盘上的数据
- 提交到集群的作业参数数据,例如通过 Spark SQL 作业提交的查询字符串
- 集群元数据、作业驱动程序输出以及写入 Managed Service for Apache Spark 集群暂存存储桶的其他数据
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
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.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
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 密钥保护资源所需的权限,请让您的管理员为以下每个服务账号(Compute Engine 服务代理服务账号、Cloud Storage 服务代理服务账号和 Managed Service for Apache Spark 服务代理服务账号)授予项目的 Cloud KMS CryptoKey Encrypter/Decrypter (
roles/cloudkms.cryptoKeyEncrypterDecrypter) IAM 角色。使用 Google Cloud CLI 将 Cloud KMS CryptoKey Encrypter/Decrypter 角色分配给 Managed Service for Apache Spark Service Agent 服务账号的示例:
gcloud projects add-iam-policy-binding KMS_PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@dataproc-accounts.iam.gserviceaccount.com \ --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 Service Agent 角色未附加到 Managed Service for Apache Spark Service Agent 服务账号,请将
serviceusage.services.use权限添加到附加到 Managed Service for Apache Spark Service Agent 服务账号的自定义角色。
创建使用 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](/dataproc/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命令验证密钥设置。在gcePdKmsKeyName和kmsKey上设置密钥资源 ID,以便使用您的密钥对集群的永久性磁盘和作业参数数据进行加密。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 暂存存储桶的集群元数据、作业驱动程序和其他输出数据,请执行以下操作:
- 使用 CMEK 创建自有存储桶。将密钥添加到存储桶时,请使用您在第 1 步创建的密钥。
- 在创建集群时,将存储桶名称传递给
--bucket标志。
gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --bucket=CMEK_BUCKET_NAME \ other arguments ...如果作业使用存储桶参数,您还可以将启用 CMEK 的存储桶传递给“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
- 如需使用您的密钥对集群虚拟机的永久性磁盘数据进行加密,请在 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 ... - 如需使用您的密钥加密集群虚拟机的永久性磁盘数据和作业参数数据,请在 cluster.create 请求中包含
Cluster.EncryptionConfig.kmsKey字段。如需查看使用--kms-key字段加密的作业类型和实参列表,请参阅 Cluster.EncryptionConfig.kmsKey。您可以使用 gcloud CLI 的
dataproc clusters describe命令验证密钥设置。在gcePdKmsKeyName和kmsKey上设置密钥资源 ID,以便使用您的密钥对集群的永久性磁盘和作业参数数据进行加密。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 暂存存储桶的集群元数据、作业驱动程序和其他输出数据,请执行以下操作:
- 使用 CMEK 创建自有存储桶。将密钥添加到存储桶时,请使用您在第 1 步创建的密钥。
- 将存储桶名称传递给 ClusterConfig.configBucket 字段,作为 cluster.create 请求的一部分。
gcloud dataproc clusters create CLUSTER_NAME \ --region=REGION \ --bucket=CMEK_BUCKET_NAME \ other arguments ...如果作业使用存储桶参数,您还可以将启用 CMEK 的存储桶传递给“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 Service Agent 服务账号。您可以使用 gcloud CLI 来分配角色:gcloud projects add-iam-policy-binding KMS_PROJECT_ID \ --member serviceAccount:service-PROJECT_NUMBER@dataproc-accounts.iam.gserviceaccount.com \ --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 Service Agent 角色未附加到 Managed Service for Apache Spark Service Agent 服务账号,请将
serviceusage.services.use权限添加到附加到 Managed Service for Apache Spark Service Agent 服务账号的自定义角色。如果 Managed Service for Apache Spark Service Agent 角色已附加到 Managed Service for Apache Spark Service Agent 服务账号,则可以跳过此步骤。
您可以使用 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 中。您可以通过查看日志标签页检查失败的集群的错误。