解密AWS KMS密钥时出现Node.js异步问题 [英] Nodejs async issue while decrypting aws kms keys

查看:143
本文介绍了解密AWS KMS密钥时出现Node.js异步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在node6中有一个lambda函数,它具有5个用aws kms加密的env变量.我有以下方法,该方法需要一个加密密钥并返回一个解密密钥.

I have a lambda function in node6 which has 5 env variables all encrypted with aws kms. I have the following method which takes a encrypted key and returns a decrypted key.

function decryptKMS(encryptedKey) {
console.log('inside decryptkms');
 const kms = new AWS.KMS();
    kms.decrypt({ CiphertextBlob: new Buffer(encryptedKey, 'base64') }, (err, data) => {
        if (err) {
            console.log('Decrypt error:', err);
            return callback(err);
        }
        var result = data.Plaintext.toString('ascii');
        return result;
});
}

在我的处理程序中,我正在这样做以获取解密的密钥.

And in my handler I'm doing this to get my decrypted keys.

decryptedkey1 = decryptKMS(encryptedkey1);
decryptedkey2 = decryptKMS(encryptedkey2);
decryptedkey3 = decryptKMS(encryptedkey3);
decryptedkey4 = decryptKMS(encryptedkey4);
decryptedkey5 = decryptKMS(encryptedkey5);

但是,由于节点是异步的,因此该功能在解密密钥之前移至了下一步.无论如何,我可以对所有组合的密钥使用节点承诺,还是可以从kms一次解密多个密钥?

But, since node is async, the function moved to the next step before decrypting the keys. Is there anyway I can use node promises for all the keys combined, or is there any way to decrypt multiple keys at once from kms?

推荐答案

承诺您的decryptKMS并与Promise.all

function decryptKMS(key) {
  return new Promise((resolve, reject) => {
    const kms = new AWS.KMS()

    kms.decrypt({}, (err, data) => {
      if(err) {
        reject(err)
      }
      else {
        resolve(data.Plaintext.toString('ascii'))
      }
    }) 
  })
}

const keys = [encryptedkey1, encryptedkey2, encryptedkey3]

Promise.all(keys.map(decryptKMS))
  .then(([decryptedkey1, decryptedkey2, decryptedkey3]) => {
    // use decryptedkeyN here 
  })
  .catch(console.log)

这篇关于解密AWS KMS密钥时出现Node.js异步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆