【问题标题】:Decrypting multiple env. variables in AWS Lambda解密多个环境。 AWS Lambda 中的变量
【发布时间】:2019-01-03 23:22:29
【问题描述】:

我有许多需要在 AWS Lambda 函数中解密的加密环境变量。他们给出了一个示例代码,但我不想为我需要解密的每个值运行一大块:

const AWS = require('aws-sdk');

const encrypted = process.env['my_password'];
let decrypted;


function processEvent(event, context, callback) {
    // TODO handle the event here
}

exports.handler = (event, context, callback) => {
    if (decrypted) {
        processEvent(event, context, callback);
    } else {
        // Decrypt code should run once and variables stored outside of the function
        // handler so that these are decrypted once per container
        const kms = new AWS.KMS();
        kms.decrypt({ CiphertextBlob: new Buffer(encrypted, 'base64') }, (err, data) => {
            if (err) {
                console.log('Decrypt error:', err);
                return callback(err);
            }
            decrypted = data.Plaintext.toString('ascii');
            processEvent(event, context, callback);
        });
    }
};

我想知道 AWS 开发工具包是否包含一个可以让我一次解密多个值的函数。如果做不到这一点,有没有办法优雅地将这些调用链接在一起,这样它们就不会占用我原本简单的函数的大约 75 行?

【问题讨论】:

    标签: node.js amazon-web-services


    【解决方案1】:

    您可以使用 Promise 来实现这一点。有关通过 KMS 解密用户名和密码的示例,请参见下面的示例。您可以根据需要向decryptPromises 数组添加任意数量的额外解密承诺:

    常量 AWS = 要求('aws-sdk'); 常量加密 = { 用户名:process.env.username, 密码:process.env.password }; 让解密 = {}; 函数过程事件(事件,上下文,回调){ //做工作 } export.handler = (事件、上下文、回调) => { 如果(解密的用户名 && 解密的密码){ processEvent(事件,上下文,回调); } 别的 { const kms = new AWS.KMS(); 常量解密承诺 = [ kms.decrypt( { CiphertextBlob: new Buffer(encrypted.username, 'base64') } ).promise(), kms.decrypt( { CiphertextBlob: new Buffer(encrypted.password, 'base64') } ).promise() ]; Promise.all(decryptPromises).then(data => { decrypted.username = data[0].Plaintext.toString('ascii'); decrypted.password = data[1].Plaintext.toString('ascii'); processEvent(事件,上下文,回调); }).catch( 错误 => { console.log('解密错误:', err); 返回回调(错误); }); } };

    您可以在Support for Promises in the SDK 文档中找到有关如何为 AWS 开发工具包实现承诺的更多信息。

    【讨论】:

      【解决方案2】:

      我创建了一个类来解密 amazon lambda 中的变量。它使用异步等待而不是 Promises.all。您不需要导入 lodash 库。您可以修改波纹管类以不使用它(改用 forEach)。

      var _ = require('lodash/core');
      const AWS = require('aws-sdk');
      class EnvVarsDecryptor {
        constructor(encryptedVariables) {
          this.encryptedVariables = encryptedVariables;
          this.decrypted = {};
        }
        isDecrypted() {
          return _.every(this.encryptedVariables, (e) => this.decrypted[e] != undefined && this.decrypted[e] != null);
        }
        async decryptVars() {
          const kms = new AWS.KMS();
          try {
            for ( let index = 0; index <  this.encryptedVariables.length; index++) {
              const encrypted = this.encryptedVariables[index];
              const data = await kms.decrypt({CiphertextBlob: new Buffer(process.env[encrypted], 'base64') }).promise();
              this.decrypted[encrypted] = data.Plaintext.toString('ascii');
            }
          } catch( e) {
            console.error(e);
          }
          return this.decrypted;
        }
      }
      module.exports = EnvVarsDecryptor;
      

      这是一个说明如何使用该函数的示例:

      exports.handler = async (event) => {
        if (!decryptor.isDecrypted()) {
              await decryptor.decryptVars();
        }
        console.log(decryptor.decrypted);
        return `Successfully processed ${event.Records.length} messages.`;
      };
      

      【讨论】:

      • 先生有没有机会展示使用 forEach 的替代实现......无论出于何种原因,我都无法弄清楚如何将它换成您在此处提供的 lodash 实现的示例。 (感谢您分享顺便说一句)。
      猜你喜欢
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 2019-12-22
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多