对敏感数据进行去标识化和重标识

本文档介绍如何使用敏感数据保护对文本内容中的敏感数据进行去标识化和重标识化。在此过程中,您将学习如何使用 Cloud Key Management Service 创建封装密钥。您需要在去标识化和重标识请求中使用此密钥。

本文档介绍的过程称为“假名化”(或“令牌化”)。在此过程中,Sensitive Data Protection 使用加密密钥将敏感文本转换为令牌(去标识化)。为了恢复(重标识)该文本,您需要去标识化期间使用的加密密钥以及令牌。

Sensitive Data Protection 支持可逆和不可逆的加密方法。为了重标识内容,您需要选择一种可逆转的方法。

此处描述的加密方法称为使用 AES-SIV 的确定性加密(合成初始化矢量模式下的高级加密标准)。我们建议使用此方法,因为在 Sensitive Data Protection 支持的所有可逆加密方法中,此方法可提供最高级别的安全。

您可以在 10 到 20 分钟内完成本文档中的步骤,但不包括准备工作步骤。

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  4. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  5. Create or select 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Sensitive Data Protection and Cloud KMS APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dlp.googleapis.com cloudkms.googleapis.com
  9. Install the Google Cloud CLI.

  10. 如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. Create or select 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 the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Sensitive Data Protection and Cloud KMS APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dlp.googleapis.com cloudkms.googleapis.com
  16. 所需的角色

    如需获得创建封装的 AES 密钥、对敏感数据进行去标识化处理并重新标识化所需的权限,请让管理员为您授予项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您也可以通过自定义角色或其他预定义角色来获取所需的权限。

    创建密钥环和密钥

    在开始此步骤之前,请先确定您希望 Sensitive Data Protection 处理去标识化和重标识请求的位置。创建 Cloud KMS 密钥时,您必须将其存储在 global 位置或用于 Sensitive Data Protection 请求的同一区域中。否则,Sensitive Data Protection 请求将失败。

    您可以在 Sensitive Data Protection 位置中找到受支持位置的列表。记下所选区域的名称(例如 us-west1)。

    此步骤使用 global 作为所有 API 请求的位置。如果要使用其他区域,请将 global 替换为区域名称。

    1. 创建密钥环:

      gcloud kms keyrings create "dlp-keyring" \
          --location "global"
      
    2. 创建密钥:

      gcloud kms keys create "dlp-key" \
          --location "global" \
          --keyring "dlp-keyring" \
          --purpose "encryption"
      
    3. 列出密钥环和密钥:

      gcloud kms keys list \
          --location "global" \
          --keyring "dlp-keyring"
      

      您将获得以下输出:

      NAME: projects/<var>PROJECT_ID</var>/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key
      PURPOSE: ENCRYPT_DECRYPT
      ALGORITHM: GOOGLE_SYMMETRIC_ENCRYPTION
      PROTECTION_LEVEL: SOFTWARE
      LABELS:
      PRIMARY_ID: 1
      PRIMARY_STATE: ENABLED
      

      在此输出中,PROJECT_ID 是您的项目 ID。

      NAME 的值是您的 Cloud KMS 密钥的完整资源名称。请记下此值,去标识化和重标识请求将需要它。

    创建 base64 编码的 AES 密钥

    本部分介绍如何创建高级加密标准 (AES) 密钥并采用 base64 格式进行编码。

    1. 创建 128 位、192 位或 256 位 AES 密钥。以下命令使用 openssl 在当前目录中创建一个 256 位密钥:

      openssl rand -out "./aes_key.bin" 32
      

      文件 aes_key.bin 已添加到当前目录。

    2. 将 AES 密钥编码为 base64 字符串:

      base64 -i ./aes_key.bin
      

      您将看到类似于以下内容的输出:

      uEDo6/yKx+zCg2cZ1DBwpwvzMVNk/c+jWs7OwpkMc/s=
      

    使用 Cloud KMS 密钥封装 AES 密钥

    本部分介绍如何使用创建密钥环和密钥中创建的 Cloud KMS 密钥来封装创建 base64 编码的 AES 密钥中创建的 base64 编码的 AES 密钥。

    如需封装 AES 密钥,请使用 curl 将以下请求发送至 Cloud KMS API projects.locations.keyRings.cryptoKeys.encrypt 方法:

    curl "https://cloudkms.googleapis.com/v1/projects/PROJECT_ID/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key:encrypt" \
        --request "POST" \
        --header "Authorization:Bearer $(gcloud auth application-default print-access-token)" \
        --header "content-type: application/json" \
        --data "{\"plaintext\": \"BASE64_ENCODED_AES_KEY\"}"
    

    替换以下内容:

    您从 Cloud KMS 获得的响应类似于以下 JSON:

    {
      "name": "projects/<var>PROJECT_ID</var>/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key/cryptoKeyVersions/1",
      "ciphertext": "CiQAYuuIGo5DVaqdE0YLioWxEhC8LbTmq7Uy2G3qOJlZB7WXBw0SSQAjdwP8ZusZJ3Kr8GD9W0vaFPMDksmHEo6nTDaW/j5sSYpHa1ym2JHk+lUgkC3Zw5bXhfCNOkpXUdHGZKou1893O8BDby/82HY=",
      "ciphertextCrc32c": "901327763",
      "protectionLevel": "SOFTWARE"
    }
    

    在此输出中,PROJECT_ID 是您的项目 ID。

    请记下响应中 ciphertext 的值。 这是封装的密钥。

    向 DLP API 发送去标识化请求

    本部分介绍如何对文本内容中的敏感数据进行去标识化。

    如需完成本任务,您需要以下各项:

    您必须将示例请求保存在 JSON 文件中。如果您使用 Cloud Shell,请使用 Cloud Shell 编辑器创建该文件。如需启动编辑器,请点击 Cloud Shell 工具栏上的 打开编辑器

    如需对文本内容中的敏感数据进行去标识化,请按以下步骤操作:

    1. 创建一个名为 deidentify-request.json 的 JSON 请求文件,其中包含以下文本。

      {
        "item": {
          "value": "My name is Alicia Abernathy, and my email address is aabernathy@example.com."
        },
        "deidentifyConfig": {
          "infoTypeTransformations": {
            "transformations": [
              {
                "infoTypes": [
                  {
                    "name": "EMAIL_ADDRESS"
                  }
                ],
                "primitiveTransformation": {
                  "cryptoDeterministicConfig": {
                    "cryptoKey": {
                      "kmsWrapped": {
                        "cryptoKeyName": "projects/PROJECT_ID/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key",
                        "wrappedKey": "WRAPPED_KEY"
                      }
                    },
                    "surrogateInfoType": {
                      "name": "EMAIL_ADDRESS_TOKEN"
                    }
                  }
                }
              }
            ]
          }
        },
        "inspectConfig": {
          "infoTypes": [
            {
              "name": "EMAIL_ADDRESS"
            }
          ]
        }
      }
      

      替换以下内容:

      确保生成的 cryptoKeyName 值构成 Cloud KMS 密钥的完整资源名称。

      如需详细了解此 JSON 请求的组成部分,请参阅 projects.locations.content.deidentify。完成此任务后,请尝试对此请求使用不同的输入。您可以按本文所述使用 curl。或者,您可以使用该 API 参考文档页面上的试用此方法 下的 API Explorer。

    2. 使用 curl 发出 projects.locations.content.deidentify 请求:

      curl -s \
          -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
          -H "Content-Type: application/json" \
          https://dlp.googleapis.com/v2/projects/PROJECT_ID/locations/global/content:deidentify \
          -d @deidentify-request.json
      

      PROJECT_ID 替换为您的项目 ID。

      如需将文件名传递给 curl,请使用 -d 选项(用于数据),并在文件名前面加上 @ 符号。此文件必须位于您运行 curl 命令所在的目录中。

      您从敏感数据保护服务获得的响应类似于以下 JSON:

      {
        "item": {
          "value": "My name is Alicia Abernathy, and my email address is EMAIL_ADDRESS_TOKEN(52):AVAx2eIEnIQP5jbNEr2j9wLOAd5m4kpSBR/0jjjGdAOmryzZbE/q."
        },
        "overview": {
          "transformedBytes": "22",
          "transformationSummaries": [
            {
              "infoType": {
                "name": "EMAIL_ADDRESS"
              },
              "transformation": {
                "cryptoDeterministicConfig": {
                  "cryptoKey": {
                    "kmsWrapped": {
                      "wrappedKey": "CiQAYuuIGo5DVaqdE0YLioWxEhC8LbTmq7Uy2G3qOJlZB7WXBw0SSQAjdwP8ZusZJ3Kr8GD9W0vaFPMDksmHEo6nTDaW/j5sSYpHa1ym2JHk+lUgkC3Zw5bXhfCNOkpXUdHGZKou1893O8BDby/82HY=",
                      "cryptoKeyName": "projects/<var>PROJECT_ID</var>/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key"
                    }
                  },
                  "surrogateInfoType": {
                    "name": "EMAIL_ADDRESS_TOKEN"
                  }
                }
              },
              "results": [
                {
                  "count": "1",
                  "code": "SUCCESS"
                }
              ],
              "transformedBytes": "22"
            }
          ]
        }
      }
      

      item 字段中,电子邮件地址会被替换为类似 EMAIL_ADDRESS_TOKEN(52):AVAx2eIEnIQP5jbNEr2j9wLOAd5m4kpSBR/0jjjGdAOmryzZbE/q 的令牌。记下响应中令牌的值。如需重标识已去标识化的内容,您可以在重标识请求中传递整个令牌。

    向 DLP API 发送重标识请求

    本部分介绍如何重标识文本内容中的令牌化数据。

    如需完成本任务,您需要以下各项:

    如需对令牌化内容进行重标识,请按以下步骤操作:

    1. 创建一个名为 reidentify-request.json 的 JSON 请求文件,其中包含以下文本。

      {
        "reidentifyConfig":{
          "infoTypeTransformations":{
            "transformations":[
              {
                "infoTypes":[
                  {
                    "name":"EMAIL_ADDRESS_TOKEN"
                  }
                ],
                "primitiveTransformation":{
                  "cryptoDeterministicConfig":{
                    "cryptoKey":{
                    "kmsWrapped": {
                      "cryptoKeyName": "projects/PROJECT_ID/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key",
                      "wrappedKey": "WRAPPED_KEY"
                    }
                  },
                    "surrogateInfoType":{
                      "name":"EMAIL_ADDRESS_TOKEN"
                    }
                  }
                }
              }
            ]
          }
        },
        "inspectConfig":{
          "customInfoTypes":[
            {
              "infoType":{
                "name":"EMAIL_ADDRESS_TOKEN"
              },
              "surrogateType":{
      
              }
            }
          ]
        },
        "item":{
          "value": "My name is Alicia Abernathy, and my email address is TOKEN."
        }
      }
      

      替换以下内容:

      确保生成的 cryptoKeyName 值构成 Cloud KMS 密钥的完整资源名称。

      如需详细了解此 JSON 请求的组成部分,请参阅 projects.locations.content.reidentify。完成此任务后,请尝试对此请求使用不同的输入。您可以按本文所述使用 curl。或者,您可以使用该 API 参考文档页面上的试用此方法下的 API Explorer。

    2. 使用 curl 发出 projects.locations.content.reidentify 请求:

      curl -s \
          -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
          -H "Content-Type: application/json" \
          https://dlp.googleapis.com/v2/projects/PROJECT_ID/locations/global/content:reidentify \
          -d @reidentify-request.json
      

      PROJECT_ID 替换为您的项目 ID。

      如需将文件名传递给 curl,请使用 -d 选项(用于数据),并在文件名前面加上 @ 符号。此文件必须位于您运行 curl 命令所在的目录中。

      您从敏感数据保护服务获得的响应类似于以下 JSON:

      {
        "item": {
          "value": "My name is Alicia Abernathy, and my email address is aabernathy@example.com."
        },
        "overview": {
          "transformedBytes": "70",
          "transformationSummaries": [
            {
              "infoType": {
                "name": "EMAIL_ADDRESS"
              },
              "transformation": {
                "cryptoDeterministicConfig": {
                  "cryptoKey": {
                    "kmsWrapped": {
                      "wrappedKey": "CiQAYuuIGo5DVaqdE0YLioWxEhC8LbTmq7Uy2G3qOJlZB7WXBw0SSQAjdwP8ZusZJ3Kr8GD9W0vaFPMDksmHEo6nTDaW/j5sSYpHa1ym2JHk+lUgkC3Zw5bXhfCNOkpXUdHGZKou1893O8BDby/82HY=",
                      "cryptoKeyName": "projects/<var>PROJECT_ID</var>/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key"
                    }
                  },
                  "surrogateInfoType": {
                    "name": "EMAIL_ADDRESS_TOKEN"
                  }
                }
              },
              "results": [
                {
                  "count": "1",
                  "code": "SUCCESS"
                }
              ],
              "transformedBytes": "70"
            }
          ]
        }
      }
      

      item 字段中,电子邮件地址令牌会被替换为原始文本中的实际电子邮件地址。

      您现在已使用确定性加密对文本内容中的敏感数据进行了去标识化和重标识处理。

    清理

    为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。

    销毁密钥版本

    如果您不想再使用在本任务中创建的密钥,请销毁其版本。

    列出您的密钥可用的版本:

    gcloud kms keys versions list \
        --location "global" \
        --keyring "dlp-keyring" \
        --key "dlp-key"
    

    如需销毁版本,请运行以下命令:

    gcloud kms keys versions destroy KEY_VERSION \
        --location "global" \
        --keyring "dlp-keyring" \
        --key "dlp-key"
    

    KEY_VERSION 替换为要销毁的版本的编号,例如 1

    删除项目

    如果您为此任务创建了一个新项目,那么防止产生额外费用的最简单方法是删除该项目。

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    撤消凭据

    Optional: Revoke credentials from the gcloud CLI.

    gcloud auth revoke

    后续步骤