首页 > 解决方案 > Bigquery Javascript UDF 使用存储和 kms 库

问题描述

我正在尝试开发用于 AES 解密的 Bigquery Javascript UDF。用于 AES 加密的密钥被加密并存储在 GCS 中。我开发了一个 Javascript 代码,它将执行以下步骤:

  1. 从 GCS 读取具有 AES 密钥加密值的文件
  2. 使用 kms 密钥环和 kms 密钥解密 AES 密钥值
  3. 使用此密钥进行 AES 解密。

我正在使用以下语句获取存储和 kms 库:

const Storage = require('@google-cloud/storage'); const kms = require('@google-cloud/kms');

当我必须从 Bigquery UDF 调用相同的功能时,我将如何确保这些库可用?(我不想在 Bigquery UDF 中硬编码 AES 密钥)

我在 Bigquery UDF 定义中看到了 [OPTIONS (library = library_array)] 的选项,但我不确定存储和 kms 集成需要哪些特定的 .js 文件?

代码片段

const Storage = require('@google-cloud/storage');
const storage = new Storage.Storage();
const kms = require('@google-cloud/kms');
const client = new kms.KeyManagementServiceClient();
bucketName ="gs://testbucket"
const keyFile = storage.bucket(bucketName).file("key.enc");
'use strict';

  async function decrypt(ciphertext){
   const name=<replace with crypto-key-path>;
   const [result] = await client.decrypt({name, ciphertext});
   return Buffer.from(result.plaintext, 'base64').toString();
  }
   var key=saltFile.download(function(err, contents) { 
     key=decrypt(contents);
     key.then(function (value) {
            key = value.trim();
            console.log(value);     
        });
     return key;
   })

谢谢你,阿努

标签: node.jsencryptiongoogle-cloud-platformgoogle-bigquery

解决方案


  • 要回答这个问题:您可以将密钥存储在 GCS 文件中,并通过将该文件作为附加 .js 导入来读取它。

但:

  • BigQuery UDF 无法调用外部 API,因此即使“@google-cloud/storage”导入成功,它也无法运行和执行额外的 API 调用。

推荐阅读