代码:
contents = encryptedWebhookSecret[0].toString();
console.log(typeof contents);
console.log(contents);
const formattedName = kmsClient.cryptoKeyPath(PROJECT, 'global', KEYRING, KEY);
const kmsDecryptRequest = {
name: formattedName,
ciphertext: contents //encryptedWebhookSecret
};
console.log("Decrypting webhook secret...");
return kmsClient.decrypt(kmsDecryptRequest);
encryptedWebhookSecret 是使用 @google-cloud/storage 客户端的 download() 操作的结果。这将返回一个 [Buffer],我将其转换为一个字符串。我记录了加密的字符串,它的值是正确的。gsutil
我可以使用从命令行下载加密的秘密并且gcloud kms decrypt
工作正常。
这个错误似乎是在说字符串编码不正确(不应该是 utf8 吗?)。
PROJECT、KEYRING 和 KEY 的值已经过双重和三重检查并且是正确的。
错误:
ERROR: Error: invalid encoding at Error (native) at Object.decode (/user_code/node_modules/@google-cloud/kms/node_modules/@protobufjs/base64/index.js:105:19) at Type.DecryptRequest$fromObject [as fromObject] (eval at Codegen (/user_code/node_modules/@google-cloud/kms/node_modules/@protobufjs/codegen/index.js:50:33), <anonymous>:12:15) at Type.fromObject (/user_code/node_modules/@google-cloud/kms/node_modules/protobufjs/src/type.js:538:25) at serialize (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/protobuf_js_6_common.js:70:23) at Object.final_requester.sendMessage (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/client_interceptors.js:802:37) at InterceptingCall._callNext (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/client_interceptors.js:418:43) at InterceptingCall.sendMessage (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/client_interceptors.js:460:8) at InterceptingCall._callNext (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/client_interceptors.js:424:12) at InterceptingCall.sendMessage (/user_code/node_modules/@google-cloud/kms/node_modules/grpc/src/client_interceptors.js:460:8)
编辑:当我尝试使用 base64 编码时,我得到“TypeError: Key must be a buffer at TypeError (native) at new Hmac (crypto.js:93:16) at Object.Hmac (crypto.js:91:12)在isRequestValid (/user_code/index.js:81:8) 在decryptWebhookSecret.then (/user_code/index.js:119:21)”。