Ensures Customer Supplied Encryption Key is enabled on disks. Google encrypts all disks at rest by default. By using CSEK only authorized team members with the keys can access the disk. Anyone else, including Google, cannot access the disk data.
To remediate the misconfiguration “Customer Supplied Encryption Key Should Be Enabled For Disks” for GCP using GCP console, please follow the below steps:
Open the GCP Console and select the project for which you want to enable Customer Supplied Encryption Key.
In the left navigation menu, select “Compute Engine” and then select “Disks”.
Select the disk for which you want to enable Customer Supplied Encryption Key.
Click on “Edit” at the top of the page.
In the Encryption section, select “Customer-supplied encryption key”.
Enter the 256-bit encryption key in the “Key” field.
Click on “Save” to save the changes.
Once you have completed the above steps, the disk will be encrypted using the customer-supplied encryption key. It is recommended to create a backup of the encryption key and store it in a secure location, as it will be required to access the data on the disk.
Replace [DISK_NAME] with the name of the disk that you want to check and [ZONE] with the zone in which the disk is located.
If the output of the previous command is “False”, then the customer-supplied encryption key is not enabled for the disk. To enable it, run the following command:
Replace [DISK_NAME] with the name of the disk that you want to encrypt and [ZONE] with the zone in which the disk is located. Replace [USER_EMAIL] with the email address of the user who will be able to encrypt and decrypt the disk.
After running the above command, the customer-supplied encryption key will be enabled for the disk. You can verify it by running the following command:
The output of the above command should be “True”, indicating that the customer-supplied encryption key is enabled for the disk.
By following the above steps, you can remediate the “Customer Supplied Encryption Key Should Be Enabled For Disks” misconfiguration in GCP using GCP CLI.
Using Python
To remediate the misconfiguration “Customer Supplied Encryption Key Should Be Enabled For Disks” in GCP using Python, you can follow the below steps:Step 1: Install the required packages
Step 5: Create a function to enable customer-supplied encryption key for disks
Copy
Ask AI
def enable_csek(project_id, zone, instance_name, disk_name, encryption_key): """ This function enables customer-supplied encryption key for a disk in a GCP VM instance. """ # Create the compute client compute_client = compute_v1.InstancesClient(credentials=credentials) # Get the instance resource URL instance_url = f'/projects/{project_id}/zones/{zone}/instances/{instance_name}' # Get the disk resource URL disk_url = f'/compute/v1/projects/{project_id}/zones/{zone}/disks/{disk_name}' # Create the disk encryption key resource disk_encryption_key_resource = { "rawKey": encryption_key, "rsaEncryptedKey": None, "kmsKeyName": None } # Create the disk resource with the customer-supplied encryption key disk_resource = { "sourceDisk": disk_url, "diskEncryptionKey": disk_encryption_key_resource, "autoDelete": False, "boot": False, "interface": "SCSI", "mode": "READ_WRITE", "autoResize": False, "sizeGb": None, "type": None } # Create the update mask update_mask = 'diskEncryptionKey' # Update the instance to enable customer-supplied encryption key for the disk operation = compute_client.attach_disk(instance=instance_url, disk=disk_resource, update_mask=update_mask) # Wait for the operation to complete operation.result() print(f'Successfully enabled customer-supplied encryption key for disk {disk_name} in instance {instance_name}.')
Step 6: Call the function to enable customer-supplied encryption key for the disk