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

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

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

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

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

您可以在 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.

    • 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. Verify that billing is enabled for your Google Cloud project.

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

    gcloud services enable dlp.googleapis.com cloudkms.googleapis.com
  8. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/dlp.user

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  9. Install the Google Cloud CLI.

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

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

    gcloud init
  12. Create or select a Google Cloud project.

    • 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. Verify that billing is enabled for your Google Cloud project.

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

    gcloud services enable dlp.googleapis.com cloudkms.googleapis.com
  15. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/dlp.user

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, myemail@example.com.
    • ROLE: the IAM role that you grant to your user account.
  16. 第 1 步:创建密钥环和密钥

    在开始此步骤之前,请先确定您希望 Sensitive Data Protection 处理去标识化和重标识请求的位置。创建 Cloud KMS 密钥时,您必须将其存储在 global 位置或用于 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                                                                                   PURPOSE          ALGORITHM                    PROTECTION_LEVEL  LABELS  PRIMARY_ID  PRIMARY_STATE
      projects/PROJECT_ID/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key  ENCRYPT_DECRYPT  GOOGLE_SYMMETRIC_ENCRYPTION  SOFTWARE                  1           ENABLED
      

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

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

    第 2 步:创建 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=
      

    第 3 步:使用 Cloud KMS 密钥封装 AES 密钥

    本部分介绍如何使用第 1 步中创建的 Cloud KMS 密钥来封装第 2 步中创建的 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/PROJECT_ID/locations/global/keyRings/dlp-keyring/cryptoKeys/dlp-key/cryptoKeyVersions/1",
      "ciphertext": "CiQAYuuIGo5DVaqdE0YLioWxEhC8LbTmq7Uy2G3qOJlZB7WXBw0SSQAjdwP8ZusZJ3Kr8GD9W0vaFPMDksmHEo6nTDaW/j5sSYpHa1ym2JHk+lUgkC3Zw5bXhfCNOkpXUdHGZKou1893O8BDby/82HY=",
      "ciphertextCrc32c": "901327763",
      "protectionLevel": "SOFTWARE"
    }
    

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

    记下您获得的响应中的 ciphertext 值。这是封装的密钥。

    第 4 步:向 DLP API 发送去标识化请求

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

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

    • 您在第 1 步创建的 Cloud KMS 密钥的完整资源名称。
    • 您在第 3 步创建的封装密钥。

    此部分要求您将示例请求保存为 JSON 文件。如果您使用的是 Cloud Shell,则可以使用 Cloud Shell 编辑器创建该文件。如需启动编辑器,请点击 Cloud Shell 窗口工具栏上的 打开编辑器

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

    1. 创建包含以下文本的 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"
            }
          ]
        }
      }
      

      请替换以下内容:

      • PROJECT_ID:您的项目的 ID。
      • WRAPPED_KEY:您在第 3 步创建的封装密钥。

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

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

    2. 将该文件另存为 deidentify-request.json

    3. 使用 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/PROJECT_ID/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 的令牌。要重标识此内容,您必须在重标识请求中传递整个令牌。

    第 5 步:向 DLP API 发送重标识请求

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

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

    • 您在第 1 步创建的 Cloud KMS 密钥的完整资源名称。
    • 您在第 3 步创建的封装密钥。
    • 您在第 4 步收到的令牌。

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

    1. 创建包含以下文本的 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."
        }
      }
      

      请替换以下内容:

      • PROJECT_ID:您的项目的 ID。
      • WRAPPED_KEY:您在第 3 步创建的封装密钥。
      • TOKEN:您在第 4 步收到的令牌,例如 EMAIL_ADDRESS_TOKEN(52):AVAx2eIEnIQP5jbNEr2j9wLOAd5m4kpSBR/0jjjGdAOmryzZbE/q

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

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

    2. 将该文件另存为 reidentify-request.json

    3. 使用 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/PROJECT_ID/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 替换为要销毁的版本的编号。

    删除项目

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

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    撤消凭据

  17. Optional: Revoke credentials from the gcloud CLI.

    gcloud auth revoke
  18. 后续步骤