01 To set up and manage Customer-Managed Keys (CMKs), you must create a key ring first. A KMS key ring is a grouping of cryptographic keys made available for organizational purposes in a specific Google Cloud location. Run kms keyrings create command (Windows/macOS/Linux) to create a new Cloud KMS key ring in the specified location (must match the BigQuery dataset location):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
gcloud kms keyrings create cc-ml-project-key-ring
--location=us
--project=cc-ml-project-112233
--format="table(name)"
02 The command output should return the identifier (name) of the newly created key ring:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
NAME
projects/cc-ml-project-112233/locations/us/keyRings/cc-ml-project-key-ring
03 Run kms keys create command (Windows/macOS/Linux) to create a new Cloud KMS Customer-Managed Key (CMK) within the KMS key ring created at the previous steps:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
gcloud kms keys create cc-bigquery-cmk
--location=us
--keyring=cc-ml-project-key-ring
--purpose=encryption
--protection-level=software
--rotation-period=90d
--next-rotation-time=2020-10-10T12:00:00.0000Z
--format="table(name)"
04 The command output should return the name of the new Customer-Managed Key (CMK):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
NAME
projects/cc-ml-project-112233/locations/us/keyRings/cc-ml-project-key-ring/cryptoKeys/cc-bigquery-cmk
05 Run projects add-iam-policy-binding command (Windows/macOS/Linux) to assign the Cloud KMS "CryptoKey Encrypter/Decrypter" role to the BigQuery system service account. Replace <kms-project-id>
with the ID of the Google Cloud project where the Customer-Managed Keys are provisioned, and replace <project-number>
with the project number (not the project ID) of the Google Cloud project that is running your BigQuery datasets:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
gcloud projects add-iam-policy-binding <kms-project-id>
--member serviceAccount:bq-<project-number>
@bigquery-encryption.iam.gserviceaccount.com
--role roles/cloudkms.cryptoKeyEncrypterDecrypter
06 The command output should return the updated IAM policy (YAML format):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
Updated IAM policy for project <kms-project-id>.
bindings:
- members:
- serviceAccount:bq-<project-number>@bigquery-encryption.iam.gserviceaccount.com
role: roles/cloudkms.cryptoKeyEncrypterDecrypter
- members:
- serviceAccount:bq-<project-number>@bigquery-encryption.iam.gserviceaccount.com
- user:admin@cloudconformity.com
role: roles/owner
etag: abcdabcdabcd
version: 1
07 Run bq cp command (using bq tool) using the name of the new Cloud KMS Customer-Managed Key (CMK) as value for the --destination_kms_key parameter, to copy the selected BigQuery dataset table, protected by default encryption into the original (source) table, protected by the specified Customer-Managed Key (CMK):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
bq cp -f
--destination_kms_key projects/cc-ml-project-112233/locations/global/keyRings/cc-ml-project-key-ring/cryptoKeys/cc-bigquery-cmk cc_analytics_dataset.cc_pageviews_table cc_analytics_dataset.cc_pageviews_table
08 The output should return the bq cp command request status:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
Waiting on bqjob_abcdabcdabcdabcd_012342123412341234 ... (0s) Current status: DONE
Table 'cc-ml-project-112233:cc_analytics_dataset.cc_pageviews_table' successfully copied to 'cc-ml-project-112233:cc_analytics_dataset.cc_pageviews_table'
09 Repeat step no. 7 and 8 to enable encryption at rest with Customer-Managed Keys (CMKs) for other BigQuery tables created for the selected dataset.
10 Repeat steps no. 7 – 9 for each BigQuery dataset available in the selected GCP project.
11 Repeat steps no. 1 – 10 for each project deployed within your Google Cloud account.