本教學課程說明如何使用遠端函式和 Sensitive Data Protection,在查詢 BigQuery 資料表時將資料去識別化。這種做法有助於清除即時查詢結果,盡量減少對分析不需要的資料的存取權。
本教學課程示範如何加密及解密傳輸中的資料。如要瞭解如何使用 Sensitive Data Protection 加密靜態資料,請參閱「儲存空間中私密資料的去識別化」一文。
本教學課程的目標對象為負責資料安全、資料處理或資料分析的人員。本指南假設您熟悉資料處理和資料隱私權,不一定要是專家。本指南也假設您能執行基本的 Cloud Shell 和 SQL 指令碼。
本教學課程會使用以 SQL 為基礎的函式、BigQuery、遠端函式、Cloud Run 和 Sensitive Data Protection。
加密等去識別化技術會模糊處理資料中的原始機密識別資訊。這些技術可協助您對原始的機密識別資訊進行模糊處理,藉此降低資料處理風險,同時保留資料的實用性,方便您執行資料彙整或分析作業。
企業可能基於政策或法規要求,只能在雲端資料倉儲中儲存去識別化資料。此外,他們可能需要有效率地重新識別去識別化資料,以產生報表。
為盡量降低處理大量機密資料的風險,您可以透過自動化資料轉換管道建立去識別化資料集。您可以按照本教學課程,使用 SQL 查詢取代該管道,只進行重新識別化,或同時進行去識別化和重新識別化。本教學課程說明如何使用 Cloud Run 上託管的中央服務,執行去識別化和重新識別化作業。您可以在整個機構使用這項中央服務,不必設定或維護 Dataflow 叢集。
Sensitive Data Protection 會檢查資料集中的機密資訊,並據此分類資料集。Sensitive Data Protection 內建超過 200 個分類器,稱為 infoTypes。如要使用 Cloud Data Loss Prevention API 對資料去識別化,需要資料管道和應用程式。本教學課程旨在協助資料分析師、工程師或科學家透過 SQL 函式達成相同結果。
完成本教學課程後,您就能編寫類似下列的查詢,查詢結果中的私密資料會經過去識別化和重新識別化。
SELECT
pii_column,
fns.dlp_freetext_encrypt(pii_column) AS dlp_encrypted,
fns.dlp_freetext_decrypt(fns.dlp_freetext_encrypt(pii_column)) AS dlp_decrypted
FROM
UNNEST(
[
'My name is John Doe. My email is john.doe@example.com']) AS pii_column
輸出結果會與下列內容相似:
列 | pii_column |
dlp_encrypted |
dlp_decrypted |
---|---|---|---|
1 |
My name is John Doe. My email is john.doe@example.com |
My name is John Doe. My email is
BQ_TRF_EMAIL(40):AQy6lGvwKR+AiiRqJpEr+nBzZUzOcjXkXamUugU= |
My name is John Doe. My email is john.doe@example.com |
架構
下圖顯示本教學課程如何使用 BigQuery 做為資料倉儲、Sensitive Data Protection 服務來去識別及重新識別資料,以及 Cloud Run 來代管遠端函式。
目標
- 部署 Cloud Run 服務,提供 Sensitive Data Protection 的去識別化功能。
- 建立使用 Sensitive Data Protection 去識別範本的 BigQuery 遠端函式。
- 使用 SQL 查詢驗證 BigQuery 中的資料加密。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, BigQuery, BigQuery Connection API, Cloud Build, Cloud Data Loss Prevention API, Cloud Key Management Service, Cloud Run, Container Registry, Identity and Access Management, Resource Manager, Secret Manager, and Service Usage APIs:
gcloud services enable artifactregistry.googleapis.com
bigquery.googleapis.com bigqueryconnection.googleapis.com cloudbuild.googleapis.com cloudkms.googleapis.com cloudresourcemanager.googleapis.com containerregistry.googleapis.com dlp.googleapis.com iam.googleapis.com run.googleapis.com secretmanager.googleapis.com serviceusage.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Artifact Registry, BigQuery, BigQuery Connection API, Cloud Build, Cloud Data Loss Prevention API, Cloud Key Management Service, Cloud Run, Container Registry, Identity and Access Management, Resource Manager, Secret Manager, and Service Usage APIs:
gcloud services enable artifactregistry.googleapis.com
bigquery.googleapis.com bigqueryconnection.googleapis.com cloudbuild.googleapis.com cloudkms.googleapis.com cloudresourcemanager.googleapis.com containerregistry.googleapis.com dlp.googleapis.com iam.googleapis.com run.googleapis.com secretmanager.googleapis.com serviceusage.googleapis.com
準備環境
在 Cloud Shell 中,複製來源存放區:
git clone https://github.com/GoogleCloudPlatform/bigquery-dlp-remote-function.git
前往本教學課程的目錄:
cd bigquery-dlp-remote-function/
使用指令碼部署資源
如要使用部署指令碼,但不想進行自訂,請按照下列步驟操作。如要自訂部署作業,請略過本節,改為參閱「手動部署自訂解決方案」。
設定 PROJECT_ID 和 REGION 欄位的值:
# Project ID of the Google Cloud project PROJECT_ID="PROJECT_ID" # Google Cloud region to use for deployment of resources # Refer to https://cloud.google.com/about/locations REGION="REGION"
更改下列內容:
- PROJECT_ID:本教學課程的專案 ID。
- REGION:您要儲存及處理資料的區域,例如
us-west1
。提供區域,而非可用區。
選用:如果您想使用檢查範本,請將 DLP_INSPECT_TEMPLATE 欄位設為該檢查範本的完整資源名稱。檢查範本必須與您在「REGION」欄位中設定的區域相同。
請確認檢查範本包含去識別化範本中使用的所有 infoType。
如果略過這個步驟,Sensitive Data Protection 會使用系統預設的 infoType 偵測工具檢查資料。
DLP_INSPECT_TEMPLATE="DLP_INSPECT_TEMPLATE"
將 DLP_INSPECT_TEMPLATE 替換為檢查範本的完整資源名稱,例如
projects/PROJECT_ID/locations/REGION/inspectTemplates/TEMPLATE_ID
。使用應用程式預設憑證進行驗證:
gcloud auth application-default login && \ gcloud auth application-default set-quota-project "${PROJECT_ID}"
初始化並執行 Terraform 指令碼,建立所有資源:
terraform init && \ terraform apply \ -var "project_id=${PROJECT_ID}" \ -var "region=${REGION}" \ -var "dlp_inspect_template_full_path=${DLP_INSPECT_TEMPLATE}"
系統會顯示 Terraform 將執行的所有動作。 查看動作。如要繼續,請輸入
yes
。確認資料可以加密及解密。
手動部署自訂解決方案
如要自訂部署作業,請按照下列步驟操作。如要使用提供的部署指令碼,但不想進行自訂或手動操作,請改為參閱「使用指令碼部署資源」。
設定環境變數
在 Cloud Shell 中設定下列環境變數:
PROJECT_ID="PROJECT_ID"
REGION="REGION"
CLOUD_RUN_SERVICE_NAME="CLOUD-RUN-SERVICE-NAME"
ARTIFACT_REGISTRY_NAME="ARTIFACT-DOCKER-REGISTRY-NAME"
更改下列內容:
- PROJECT_ID:本教學課程的專案 ID。
- REGION:您要儲存及處理資料的區域,例如
us-west1
。提供區域,而非可用區。 - CLOUD_RUN_SERVICE_NAME:新 Cloud Run 服務的名稱。最多可輸入 15 個半形字元。
- ARTIFACT_REGISTRY_NAME:新 Artifact Registry 的名稱,用於儲存容器映像檔。
為 Cloud Run 服務建立服務帳戶
建立服務帳戶:
RUNNER_SA_NAME="${CLOUD_RUN_SERVICE_NAME}-runner" RUNNER_SA_EMAIL="${RUNNER_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com" gcloud iam service-accounts create "${RUNNER_SA_NAME}" \ --project="${PROJECT_ID}" \ --description "Runner for BigQuery remote function execution" \ --display-name "${RUNNER_SA_NAME}"
授予 Sensitive Data Protection 的必要角色。
授予 DLP 讀取者角色:
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ --member="serviceAccount:${RUNNER_SA_EMAIL}" \ --role='roles/dlp.reader'
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \ --member="serviceAccount:${RUNNER_SA_EMAIL}" \ --role='roles/dlp.user'
部署 Cloud Run 服務
如要部署應用程式,請按照下列步驟操作:
選用:您可以變更環境變數或更新
src/main/resources/aes.properties
檔案,藉此變更預設值。建立 Artifact Registry 存放區,用於儲存函式的容器映像檔:
gcloud artifacts repositories create "${ARTIFACT_REGISTRY_NAME}" \ --repository-format=docker \ --location="${REGION}" \ --description="Container images repository for BigQuery Functions" \ --project="${PROJECT_ID}"
使用 Cloud Build 編譯應用程式並部署至 Cloud Run:
gcloud builds submit \ --project ${PROJECT_ID} \ --substitutions=_CONTAINER_IMAGE_NAME="${REGION}-docker.pkg.dev/${PROJECT_ID}/${ARTIFACT_REGISTRY_NAME}/${CLOUD_RUN_SERVICE_NAME}:latest" \ --machine-type=e2-highcpu-8 && \ gcloud beta run deploy ${CLOUD_RUN_SERVICE_NAME} \ --image="${REGION}-docker.pkg.dev/${PROJECT_ID}/${ARTIFACT_REGISTRY_NAME}/${CLOUD_RUN_SERVICE_NAME}:latest" \ --execution-environment=gen2 \ --platform=managed \ --region="${REGION}" \ --service-account="${RUNNER_SA_EMAIL}" \ --cpu=4 \ --memory=8Gi \ --no-allow-unauthenticated \ --project ${PROJECT_ID} \ --update-env-vars=PROJECT_ID=${PROJECT_ID}
輸出結果的結尾會與下列內容相似:
ID: 403a276e-b0c6-41f3-aaed-f0ec9f9cedba CREATE_TIME: 2023-02-04T01:52:15+00:00 DURATION: 1M59S SOURCE: gs://PROJECT_ID_cloudbuild/source/1675475534.124241-9c43787f64e04cfd9e4a1979d3324fe0.tgz IMAGES: gcr.io/PROJECT_ID/CLOUD_RUN_SERVICE_NAME (+1 more) STATUS: SUCCESS Deploying container to Cloud Run service [CLOUD_RUN_SERVICE_NAME] in project [PROJECT_ID] region [REGION] OK Deploying new service... Done. OK Creating Revision... Revision deployment finished. Checking container heal th. OK Routing traffic... Done. Service [CLOUD_RUN_SERVICE_NAME] revision [CLOUD_RUN_SERVICE_NAME-00001-tat] has been deployed and is serving 100 percent of traffic. Service URL: https://CLOUD_RUN_SERVICE_NAME-j2bpjx2xoq-uw.a.run.app
擷取 Cloud Run 網址,並儲存至環境變數:
RUN_URL="$(gcloud run services describe ${CLOUD_RUN_SERVICE_NAME} --region \ ${REGION} --project ${PROJECT_ID} --format="get(status.address.url)")"
建立 Sensitive Data Protection 去識別化範本
Sensitive Data Protection 去識別化範本可協助您儲存去識別化設定,以便在多項作業和資料來源中重複使用。
這個步驟會使用 sample_dlp_deid_config.json
檔案,其中包含去識別化範本範例。
在 Cloud Shell 中建立範本:
DEID_TEMPLATE=$(curl -X POST \
-H "Authorization: Bearer `gcloud auth print-access-token`" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Goog-User-Project: ${PROJECT_ID}" \
--data-binary "@sample_dlp_deid_config.json" \
"https://dlp.googleapis.com/v2/projects/${PROJECT_ID}/locations/${REGION}/deidentifyTemplates")
DEID_TEMPLATE_NAME="$(echo ${DEID_TEMPLATE} | jq -r '.name')"
Google 建議您在實際的敏感工作負載上執行 Sensitive Data Protection 加密時,使用包裝金鑰。為進行示範,本教學課程使用未包裝的金鑰。如要進一步瞭解如何建立包裝金鑰,並在去識別化和重新識別要求中使用,請參閱將機密資料去識別化和重新識別。
建立 BigQuery 與 Cloud Run 的連線
在 Cloud Shell 中,建立 BigQuery 連線來存取 Cloud Run:
bq mk --connection \ --display_name='External transform function connection' \ --connection_type=CLOUD_RESOURCE \ --project_id="${PROJECT_ID}" \ --location="${REGION}" \ ext-${CLOUD_RUN_SERVICE_NAME}
找出並設定用於連線的 BigQuery 服務帳戶:
CONNECTION_SA="$(bq --project_id ${PROJECT_ID} --format json show \ --connection ${PROJECT_ID}.${REGION}.ext-${CLOUD_RUN_SERVICE_NAME} \ | jq -r '.cloudResource.serviceAccountId')"
將 Cloud Run 叫用者角色授予服務帳戶:
gcloud projects add-iam-policy-binding ${PROJECT_ID} \ --member="serviceAccount:${CONNECTION_SA}" \ --role='roles/run.invoker'
建立遠端函式的 BigQuery 資料集
定義遠端函式的 BigQuery 資料集:
BQ_FUNCTION_DATASET="fns"
如果資料集不存在,請建立資料集:
bq mk --dataset \ --project_id ${PROJECT_ID} \ --location ${REGION} \ ${BQ_FUNCTION_DATASET}
建立 Sensitive Data Protection 遠端函式
選用:如果您有想使用的檢查範本,請將 DLP_INSPECT_TEMPLATE 變數設為該檢查範本的完整資源名稱。檢查範本必須位於您在 REGION 環境變數中設定的相同區域。
請確認檢查範本包含去識別化範本中使用的所有 infoType。
如果略過這個步驟,Sensitive Data Protection 會使用系統預設的 infoType 偵測工具檢查資料。
DLP_INSPECT_TEMPLATE="DLP_INSPECT_TEMPLATE"
將 DLP_INSPECT_TEMPLATE 替換為檢查範本的完整資源名稱,例如
projects/PROJECT_ID/locations/REGION/inspectTemplates/TEMPLATE_ID
。建立 Sensitive Data Protection 去識別化函式:
bq query --project_id ${PROJECT_ID} \ --use_legacy_sql=false \ "CREATE OR REPLACE FUNCTION ${BQ_FUNCTION_DATASET}.dlp_freetext_encrypt(v STRING) RETURNS STRING REMOTE WITH CONNECTION \`${PROJECT_ID}.${REGION}.ext-${CLOUD_RUN_SERVICE_NAME}\` OPTIONS (endpoint = '${RUN_URL}', user_defined_context = [('mode', 'deidentify'),('algo','dlp'),('dlp-deid-template','${DEID_TEMPLATE_NAME}'),('dlp-inspect-template', '${DLP_INSPECT_TEMPLATE}')]);"
建立 Sensitive Data Protection 重新識別函式:
bq query --project_id ${PROJECT_ID} \ --use_legacy_sql=false \ "CREATE OR REPLACE FUNCTION ${BQ_FUNCTION_DATASET}.dlp_freetext_decrypt(v STRING) RETURNS STRING REMOTE WITH CONNECTION \`${PROJECT_ID}.${REGION}.ext-${CLOUD_RUN_SERVICE_NAME}\` OPTIONS (endpoint = '${RUN_URL}', user_defined_context = [('mode', 'reidentify'),('algo','dlp'),('dlp-deid-template','${DEID_TEMPLATE_NAME}'),('dlp-inspect-template', '${DLP_INSPECT_TEMPLATE}')]);"
驗證去識別化和重新識別化
如要驗證解決方案是否會去識別化及重新識別資料,請按照下列步驟操作:
控制台
前往 Google Cloud 控制台的「BigQuery」頁面。
BigQuery 會開啟您最近存取過的專案。
如要開啟查詢編輯器,請按一下
「撰寫新查詢」。輸入下列查詢:
SELECT pii_column, fns.dlp_freetext_encrypt(pii_column) AS dlp_encrypted, fns.dlp_freetext_decrypt(fns.dlp_freetext_encrypt(pii_column)) AS dlp_decrypted FROM UNNEST( [ 'My name is John Doe. My email is john.doe@example.com', 'Some non PII data', '650-253-0000', 'some script with simple number 1234']) AS pii_column
按一下「執行」。
bq
設定資料集的環境變數:
BQ_FUNCTION_DATASET="fns"
執行查詢:
bq query --project_id ${PROJECT_ID} \ --use_legacy_sql=false \ " SELECT pii_column, ${BQ_FUNCTION_DATASET}.dlp_freetext_encrypt(pii_column) AS dlp_encrypted, ${BQ_FUNCTION_DATASET}.dlp_freetext_decrypt(${BQ_FUNCTION_DATASET}.dlp_freetext_encrypt(pii_column)) AS dlp_decrypted FROM UNNEST( [ 'My name is John Doe. My email is john.doe@example.com', 'Some non PII data', '650-253-0000', 'some script with simple number 1234']) AS pii_column"
輸出結果會與下列內容相似:
列 | pii_column |
dlp_encrypted |
dlp_decrypted |
---|---|---|---|
1 |
My name is John Doe. My email is john.doe@example.com |
My name is John Doe. My email is
BQ_TRF_EMAIL(40):AQy6lGvwKR+AiiRqJpEr+nBzZUzOcjXkXamUugU= |
My name is John Doe. My email is john.doe@example.com |
2 |
Some non PII data |
Some non PII data |
Some non PII data |
3 |
650-253-0000 |
BQ_TRF_PH(40):AeKpGU5KBXaTyecCun7dv1hHht5w5Q2PTpvkRC4= |
650-253-0000 |
4 |
some script with simple number 1234 |
some script with simple number 1234 |
some script with simple number 1234 |
注意事項
根據自身需求調整本教學課程時,請考量下列事項:
- 去識別化和重新識別程序會透過 Cloud Run 服務處理。根據運算需求,佈建 Cloud Run CPU 和記憶體。詳情請參閱 Cloud Run 的 CPU 限制和記憶體限制。
- 使用 Sensitive Data Protection 時,請考量用量限制和控管費用的建議。
為控管費用和總 Sensitive Data Protection 配額用量,請將傳遞至 Sensitive Data Protection 遠端函式的項目限制為 10,000 個以下。這項解決方案可自動批次處理要求,妥善處理下列敏感資料防護要求限制:
- 資料表值數上限:50,000
- 預設要求大小上限:0.5 MB
查詢的最終和篩選結果應傳遞至 Sensitive Data Protection 函式,而非來源。
在這個解決方案中,
pii_column
資料欄中的每個值都是一個項目,例如My name is John Doe. My email is john.doe@example.com
就是一個項目。請確認 BigQuery 資料集、Cloud Run 服務和 Sensitive Data Protection 範本位於相同的雲端區域。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
後續步驟
- 進一步瞭解 BigQuery 遠端函式。
- 瞭解如何將機密資料去識別化。
- 逐步完成範例,瞭解如何建立包裝金鑰、將內容代碼化,以及重新識別代碼化內容。
- 瞭解如何使用 Sensitive Data Protection,對大規模資料集中的 PII 執行去識別化和重新識別作業。
- 進一步瞭解 Cloud KMS。