Search code examples
google-cloud-platformgoogle-cloud-kms

Rotating keys and reactive reencypt data


I want to introduce key rotation to my system but for that reencryption is needed. It would be nice to do it reactively on some event, trigger etc., but I can't find anything like that at google documentation.

After a rotate event, I want to reencrypt data with the new key and destroy the old one.

Any ideas, how to achieve this goal?


Solution

  • When you rotate an encryption key (or when you enable scheduled rotation on a key), Cloud KMS does not automatically delete the old key version material. You can still decrypt data previously encrypted with the old key unless you manually disable/destroy that key version. You can read more about this in detail in the Cloud KMS Key rotation documentation.

    While you may have business requirements, it's not a Cloud KMS requirement that you re-encrypt old data with the new key version material.

    • New data will be encrypted with the new key
    • Old data will be decrypted with the old key

    At the time of this writing, Cloud KMS does not publish an event when a key is rotated. If you have a business requirement to re-encrypt all existing data with the new key, you could do one of the following:

    Use Cloud Scheduler

    Write a Cloud Function connected to Cloud Scheduler that invokes on a periodic basis. For example, if your keys rotate every 72 hours, you could schedule the cloud function to run every 24 hours. Happy to provide some sample code if that would help, but the OP didn't specifically ask for code.

    Long-poll

    Write a long-running function that polls the KMS API to check if the Primary crypto key has changed, and trigger your re-encryption when change is detected.