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

本快速入門導覽課程說明如何使用 Sensitive Data Protection,將文字內容中的機密資料去識別化和重新識別。過程中,系統會引導您使用 Cloud Key Management Service 建立包裝金鑰。您需要在去識別化和重新識別化要求中使用這個金鑰。

本快速入門導覽課程所述的程序稱為「假名化」(或「權杖化」)。在這個過程中,Sensitive Data Protection 會使用加密金鑰將私密文字轉換 (去識別化) 為權杖。如要還原 (重新識別) 該文字,您需要去識別化期間使用的加密金鑰和權杖。

Sensitive Data Protection 支援可逆和不可逆的加密方法。如要重新識別內容,請選擇可逆方法。

本文所述的加密方法稱為「使用 AES-SIV 的確定性加密」,其中 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.

    • 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 要求會失敗。

    如要查看支援的地點清單,請參閱「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\"}"
    

    更改下列內容:

    • PROJECT_ID專案 ID
    • BASE64_ENCODED_AES_KEY步驟 2 中傳回的 base64 編碼字串。

    您從 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 視窗工具列上的「Open Editor」(開啟編輯器)

    如要將文字內容中的機密資料去識別化,請按照下列步驟操作:

    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

    撤銷憑證

    Optional: Revoke credentials from the gcloud CLI.

    gcloud auth revoke

    後續步驟