Migrating symmetric encryption keys in Google Cloud KMS when transitioning a Cloud SQL database from one project to another presents challenges due to strict security policies that prevent key material export. Because symmetric key material cannot be extracted, a direct migration is impossible. Instead, the process requires creating a new functionally equivalent key in the destination project and securely re-encrypting data.
When moving a Cloud SQL database from Project A to Project B, where Project A is set for decommissioning, the first step is to identify the key properties in Project A. Using the gcloud CLI, the key’s algorithm, protection level (software or HSM), and location should be noted to ensure an identical key can be created in Project B.
gcloud kms keys list --location=<location> --keyring=<keyring-name-A> --project=<project-A-id>
Once the key details are confirmed, a new keyring and symmetric key must be generated in Project B with identical attributes.
# Create the keyring (if it doesn't exist)
gcloud kms keyrings create <keyring-name-B> \
--location=<location> \
--project=<project-B-id>
# Create a new symmetric key
gcloud kms keys create <key-name-B> \
--location=<location> \
--keyring=<keyring-name-B> \
--project=<project-B-id> \
--purpose=encryption \
--default-algorithm=GOOGLE_SYMMETRIC_ENCRYPTION \
--protection-level=software # or HSM if the original key was HSM
After creating the new key, application configurations must be updated to reference the new key’s resource ID in Project B. The most secure approach is to decrypt existing data using Project A’s key and immediately re-encrypt it using Project B’s key before storing it in the new database. This ensures data remains secure and eliminates dependency on Project A.
In scenarios where immediate re-encryption is not feasible, a temporary solution is to grant Project B’s service account decryption access to Project A’s KMS key, specifically the roles/cloudkms.cryptoKeyDecrypter role. This allows Project B to continue decrypting data with the original key until full migration is complete. However, this approach should be used cautiously, ensuring that Project A’s permissions are revoked as soon as possible after data is re-encrypted with the new key.
gcloud kms keys add-iam-policy-binding <key-name-A> \
--location=<location> \
--keyring=<keyring-name-A> \
--project=<project-A-id> \
--member="serviceAccount:<service-account-of-project-B>" \
--role="roles/cloudkms.cryptoKeyDecrypter"
To enhance security and maintain compliance, it is advisable to enable key rotation on the new key in Project B and configure Cloud KMS audit logs for monitoring.
gcloud kms keys update <key-name-B> \
--location=<location> \
--keyring=<keyring-name-B> \
--project=<project-B-id> \
--rotation-period=90d # Adjust rotation period as needed
Before decommissioning Project A, thorough testing should be conducted to confirm that applications can decrypt data using the new key without disruption.
Ultimately, while symmetric key migration is not directly possible, recreating an identical key and securely re-encrypting data ensures a seamless transition without compromising security. By following best practices such as least privilege access, thorough testing, and structured key rotation, organizations can execute a smooth migration while maintaining data integrity and security.