3

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:

enter image description here

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?

Arnab Mukherjee
  • 190
  • 3
  • 18

1 Answers1

3

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 a KMSWrappedKey.

To create a wrapped key you can try the following steps:

  1. Create a KMS Key Ring and Key. You will use this later to wrap your de-identification key.
  2. Create an 128/192/256 encryption key to use as your DLP de-identification key.
  3. Base64 encode this key from step #2.
  4. Wrap/encrypt this base64 encoded key from step #3 with the KMS key from step #1.

sample KMS call:

curl "https://cloudkms.googleapis.com/v1/projects/<project-id>/locations/global/keyRings/<key-ring-id>/cryptoKeys/<key-id>:encrypt" \
  --request "POST" \
  --header "Authorization:Bearer $(gcloud auth application-default print-access-token)" \
  --header "content-type: application/json" \
  --data "{\"plaintext\": \"<your base64 encoded key>\"}"

This should produce output like

{
  "name": "projects/<project-id>/locations/global/keyRings/<key-ring-id>/cryptoKeys/<key-id>/cryptoKeyVersions/1",
  "ciphertext": "<cipher text>",
  "ciphertextCrc32c": "<some number>"
}
  1. Copy what is in the name field into the DLP cryptoKeyName but drop the last part /cryptoKeyVersions/1 and copy what's in the ciphertext value into the DLP wrappedKey field.

Example:

...
        "crypto_hash_config": {
          "crypto_key": {
              "kmsWrapped": {
                "wrappedKey": "CiQA4yqJRKIrMRQCdYdsSHIhqGthDuuxnhBOLN512drs6f59tt4SOQAwcYzUXvT1tJQmHHhqycGMj/lB+UPkmIb7j+QcIGxtQuMbuqG2xdRC8WVMQ9MFJ9tuOO6vxJqaVw==",
                "cryptoKeyName": "projects/<project-id>/locations/global/keyRings/<key-ring-id>/cryptoKeys/<key-id>"
              }
          }
        }
  1. 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).

  2. 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 called key1 in a project called project-test-123:

{
  "deidentifyTemplate": {
    "deidentifyConfig": {
      "infoTypeTransformations": {
        "transformations": [
          {
            "primitiveTransformation": {
              "cryptoHashConfig": {
                "cryptoKey": {
                  "kmsWrapped": {
                    "cryptoKeyName": "projects/project-test-123/locations/global/keyRings/keyring1/cryptoKeys/key1",
                    "wrappedKey": "CiQA4yqJRKIrMRQCdYdsSHIhqGthDuuxnhBOLN512drs6f59tt4SOQAwcYzUXvT1tJQmHHhqycGMj/lB+UPkmIb7j+QcIGxtQuMbuqG2xdRC8WVMQ9MFJ9tuOO6vxJqaVw=="
                  }
                }
              }
            }
          }
        ]
      }
    }
  },
  "templateId": "test1"
}

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.

Scott Ellis
  • 116
  • 3
  • Hey @Scott: I am getting the same error, how you are creating a 128 bit encryption key ? I was using openssl `openssl rand -base64 16`. Same key is working if i follow the workaround. If format is wrong, it should not work both ways. – Arnab Mukherjee Dec 12 '20 at 04:52
  • Workaround i used: Created a simple transient encryption. Went to the UI modified it to KMS key encryption(Added the resource name and KMS generated key). Its working fine. Let me know if you need more details. – Arnab Mukherjee Dec 12 '20 at 04:59
  • Edited the question with more details of work around. When the same key is used in json file its failing but its working using UI. – Arnab Mukherjee Dec 12 '20 at 05:16
  • Even when i create the template using UI its working. _I am seeing issue when i am trying to create a template using API call from saved json file._ – Arnab Mukherjee Dec 12 '20 at 05:25
  • can you respond to it? – Arnab Mukherjee Dec 14 '20 at 14:41
  • @ArnabMukherjee I updated the answer to have a sample JSON request to create a `deidentifyTemplate`. Does that work for you? – Scott Ellis Dec 14 '20 at 19:05
  • _how you are creating the template ? using API calls from local or UI?_ Issue is, from UI things are good. When creating template from saved json using API(local python code), i am facing issue. Let me know if you need more insights. – Arnab Mukherjee Dec 15 '20 at 06:46
  • 1
    I tested this JSON to create templates with a CURL command or directly in the API Explorer/tester here: https://cloud.google.com/dlp/docs/reference/rest/v2/projects.deidentifyTemplates/create?apix=true re: Python, is it possible that your Python code is base64 encoding things twice or doing some other kind of byte handling? – Scott Ellis Dec 17 '20 at 17:58
  • you might be correct, some gocha is happening with python api – Arnab Mukherjee Dec 18 '20 at 02:12