BigQuery by default encrypts the data as rest by employing Envelope Encryption using Google managed cryptographic keys. This is seamless and does not require any additional input from the user. For greater control over the encryption, customer-managed encryption keys (CMEK) can be used as encryption key management solution for BigQuery Data Sets. Setting a Default Customer-managed encryption key (CMEK) for a data set ensure any tables created in future will use the specified CMEK if none other is provided.
Replace <KMS_KEY_ID> with the ID of the KMS key that you want to use as the default CMEK for the dataset and <DATASET_NAME> with the name of the dataset that you identified in step 3.
Verify that the default CMEK has been set for the dataset by running the following command:
Copy
Ask AI
bq show <DATASET_NAME>
This will display the details of the dataset, including the default CMEK that has been set.
Repeat steps 3 to 5 for all the BigQuery datasets in the project to ensure that the default CMEK is specified for all the datasets.
By following the above steps, you can remediate the misconfiguration “Ensure Default CMEK Is Specified For BigQuery Data Sets” for GCP using GCP CLI.
Using Python
To remediate the misconfiguration “Ensure Default CMEK Is Specified For BigQuery Data Sets” in GCP using Python, you can follow the below steps:
First, you need to create a Key Management Service (KMS) key ring and key in the same region as your BigQuery dataset.
Copy
Ask AI
from google.cloud import kms_v1from google.cloud.kms_v1 import enumsclient = kms_v1.KeyManagementServiceClient()# Set the parent location of the key ringparent = client.location_path(project_id, location)# Set the key ring ID and key IDkey_ring_id = 'my-key-ring'key_id = 'my-key'# Create the key ringkey_ring = client.create_key_ring(parent, key_ring_id, {})# Create the keykey = client.create_crypto_key(key_ring.name, key_id, enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT, {})
Next, you need to set the default encryption key for your BigQuery dataset using the KMS key you created in step 1.
Copy
Ask AI
from google.cloud import bigqueryclient = bigquery.Client()# Set the dataset IDdataset_id = 'my-dataset'# Set the encryption configuration using the KMS keyencryption_config = bigquery.EncryptionConfiguration( kms_key_name=f"projects/{project_id}/locations/{location}/keyRings/{key_ring_id}/cryptoKeys/{key_id}")# Set the default encryption configuration for the datasetdataset = client.get_dataset(dataset_id)dataset.encryption_configuration = encryption_configclient.update_dataset(dataset, ["encryption_configuration"])
Finally, you need to verify that the default encryption key is set for your BigQuery dataset.
Copy
Ask AI
# Get the dataset and verify the encryption configurationdataset = client.get_dataset(dataset_id)print(f"Encryption configuration: {dataset.encryption_configuration}")
By following these steps, you can remediate the misconfiguration “Ensure Default CMEK Is Specified For BigQuery Data Sets” in GCP using Python.