Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KMS: Clean up base64 logic in the encrypt and decrypt functions. (#1074)
The use of base64 is essentially an implementation detail of the Cloud KMS REST API: it is required only so that arbitrary binary data can be included in a JSON string, which only allows Unicode characters. Therefore, the "encrypt" sample function should decode the base64-encoded ciphertext before writing the file. Similarly, "decrypt" should not assume that an input file is base64-encoded, but should perform the base64-encoding itself before sending the encrypted data to KMS. This aligns with how the "gcloud kms encrypt" and "gcloud kms decrypt" commands behave. See https://stackoverflow.com/q/45699472 for an example of user confusion caused by the mismatch.
- Loading branch information
e0f957c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a heads-up that this breaks existing implementations that folks might have such as (basically the sample code):
The
_decrypt
would need to change to (as done above):While I understand this change and the confusion it caused this particular user mentioned on SO, it actually left me in a loop for a period of time as I was re-reviewing what changed and attempting to find anything that had changed in my codebase and then eventually stumbled here.
I didn't expect to pull an update to this repository and have a breaking change to encrypting new key material when I had not changed any of my code.
Just a heads-up. To be honest, I would have preferred a compatibility mode for a bit with some messaging and eventual deprecation. There isn't a ton of documentation on KMS in the wild and this repository was my goto when first building a couple applications utilizing KMS. Given the lack of usage and documentation a little more hand-holding here would have been appreciated.
e0f957c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand, are you saying you're using our sample code repository directly in your app? We have no way of versioning the samples so it's difficult for us to communicate changes (and there are some weeks with upwards of 100 changes here). Generally our samples do not constitute production ready code and are for illustrative purposes only.
That said - this sample change didn't introduce any breaking changes at the service or client library level - you can easily just check out an older version of this sample and everything should work as before.
e0f957c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, that would be crazy! :)
I am using
snippets.py
(actually a wrapper around it to make invocation quick/easy) to encrypt secrets and upload them to GCS.When I pulled this update in and ran
snippets.py
, the data that was produced, received a similar error to the author because my_decrypt
function above had not been updated, thus the breaking change.Let me know if that isn't clear and I can provide more details.
Cheers!