I am working on a PII de-identification project and using google cloud's data loss prevention api.
Use case: To encrypt a field with cloud KMS key.
- Created a dlp-deidentification template, here is the snippet:
{
"deidentify_template":{
"display_name":"deidentification_encryption",
"description":"deidentification_encryption",
"deidentify_config":{
"record_transformations":{
"field_transformations":[
{
"fields":[
{
"name":"password"
}
],
"primitive_transformation":{
"crypto_hash_config": {
"crypto_key": {
"kms_wrapped": {
"wrapped_key": "[base64 encoded]",
"crypto_key_name": "kms-key-resource-name"
}
}
}
}
Saved the template as JSON file.
When I am trying to built the template using
python Api
, I am getting following error:
TypeError: Cannot set google.privacy.dlp.v2.KmsWrappedCryptoKey.wrapped_key [base64-encoded]: [base64-encoded] has type <class 'str'>, but expected one of: (<class 'bytes'>,) for field KmsWrappedCryptoKey
How we can write bytes in json? Not sure about the feasibility
Workaround I used:
- Created a template with transient crypto key:
"cryptoKey": {
"transient": {
"name": "ola-32"
}
}
}
- In the DLP UI modified the template configuration.
- Changed the transformation for password field to KMS wrapped crypto key.
- Added the resource name and the KMS generated key.
- Its working fine, tested the template.
Additional observation:
- I did a API call to check the configuration, after i added the KMS keys using UI, i saw the wrapped key like this:
Its not possible to use wrapped key in this format in json as per my knowledge.
Is there a way to use KMS keys using templates saved as json?
Yes you should be able to use a KMSWrapped key in a template. You can do this using JSON and calling the API or via the Cloud Console UI here.
It's possible that the error you are getting is due to the key being wrapped in the wrong format.
I just went through these steps and got a successfully working DLP
deidentify_template
with aKMSWrappedKey
.To create a wrapped key you can try the following steps:
sample KMS call:
This should produce output like
name
field into the DLPcryptoKeyName
but drop the last part/cryptoKeyVersions/1
and copy what's in theciphertext
value into the DLPwrappedKey
field.Example:
Save your template and try it out. You can test it in the API tester here or in the Cloud DLP Console template tester here (just click on the template that you made and then the
Test
tab).Below is a full JSON example for creating a template. You would just need to run this under your project with your project as a parent id and need to ensure that your key resource ID matches yours. Here I use a keyring called
keyring1
and a key calledkey1
in a project calledproject-test-123
:Note: this is a randomly generated 128 bit key that has been wrapped using KMS. Please don't use this actual key in any production systems or to protect any data since it's posted publicly here.