將機密資料去識別化及重新識別化

本文說明如何使用 Sensitive Data Protection,將文字內容中的機密資料去識別化和重新識別。過程中,系統會引導您使用 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 參考資料頁面中,使用「Try this method」(試用這個方法) 下方的 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 參考資料頁面中,使用「Try this method」(試用這個方法) 下方的 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

    後續步驟