3

我正在做一个 PII 去识别项目并使用谷歌云的数据丢失防护 api。

用例:使用云 KMS 密钥加密字段。

  • 创建了一个 dlp-deidentification 模板,这里是片段:
{
  "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"
                    }
              }
              }
            }
  • 将模板保存为 JSON 文件。

  • 当我尝试使用 构建模板python Api时,出现以下错误:

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

我们如何在 json 中写入字节?不确定可行性

我使用的解决方法:

  • 使用临时加密密钥创建了一个模板:
                      "cryptoKey": {
                        "transient": {
                            "name": "ola-32"
                      }
                    }
                }
  • 在 DLP UI 中修改了模板配置。
  • 将密码字段的转换更改为 KMS 包装的加密密钥。
  • 添加了资源名称和 KMS 生成的密钥。
  • 它工作正常,测试了模板。

补充观察:

  • 我做了一个 API 调用来检查配置,在我使用 UI 添加 KMS 密钥后,我看到了这样的包装密钥:

在此处输入图像描述

据我所知,它不可能在 json 中使用这种格式的包装密钥。

有没有办法使用保存为 json 的模板来使用 KMS 密钥?

4

1 回答 1

3

是的,您应该能够在模板中使用 KMSWrapped 密钥。您可以使用 JSON 并调用 API 或通过此处的Cloud Console UI 执行此操作

您遇到的错误可能是由于密钥以错误的格式包装。

我刚刚完成了这些步骤,并获得了一个deidentify_template带有KMSWrappedKey.

要创建包装密钥,您可以尝试以下步骤:

  1. 创建KMS 密钥环和密钥。稍后您将使用它来包装您的去识别密钥。
  2. 创建一个 128/192/256 加密密钥以用作您的 DLP 去识别密钥。
  3. Base64 对步骤 #2 中的密钥进行编码。
  4. 使用步骤 1 中的 KMS 密钥包装/加密步骤 3 中的 base64 编码密钥。

示例KMS调用:

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>\"}"

这应该产生类似的输出

{
  "name": "projects/<project-id>/locations/global/keyRings/<key-ring-id>/cryptoKeys/<key-id>/cryptoKeyVersions/1",
  "ciphertext": "<cipher text>",
  "ciphertextCrc32c": "<some number>"
}
  1. name字段中的内容复制到 DLP中cryptoKeyName,但删除最后一部分/cryptoKeyVersions/1并将ciphertext值中的内容复制到 DLPwrappedKey字段中。

例子:

...
        "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. 保存您的模板并尝试一下。您可以在此处的API 测试器中或在此处的Cloud DLP 控制台模板测试器中对其进行测试(只需单击您创建的模板,然后单击Test选项卡)。

  2. 下面是创建模板的完整 JSON 示例。您只需要在您的项目下以您的项目作为父 ID 运行它,并且需要确保您的关键资源 ID 与您的匹配。在这里,我使用了一个名为的密钥环keyring1和一个名为key1的项目中的密钥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"
}

注意:这是使用 KMS 包装的随机生成的 128 位密钥。请不要在任何生产系统中使用此实际密钥或保护任何数据,因为它已在此处公开发布。

于 2020-12-12T00:51:25.803 回答