[英]Decrypting multiple env. variables in AWS Lambda
我需要在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 SDK是否包含一個允許我一次解密多個值的函數。 如果不這樣做,有沒有辦法優雅地將這些調用鏈接在一起,這樣他們就不會占用我這個簡單功能的~75行?
您可以使用promises來實現此目的。 請參閱下面的示例,以通過KMS解密用戶名和密碼。 您可以根據需要向decryptPromises
數組添加盡可能多的其他解密承諾:
const AWS = require('aws-sdk'); const encrypted = { username: process.env.username, password: process.env.password }; let decrypted = {}; function processEvent(event, context, callback) { //do work } exports.handler = (event, context, callback) => { if ( decrypted.username && decrypted.password ) { processEvent(event, context, callback); } else { const kms = new AWS.KMS(); const decryptPromises = [ 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(event, context, callback); }).catch( err => { console.log('Decrypt error:', err); return callback(err); }); } };
您可以在SDK文檔的Support for Promises中找到有關如何為AWS SDK實現promises的更多信息。
我創建了一個類來解密亞馬遜lambda中的變量。 它使用async await而不是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.`; };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.