可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm building a monitoring tool based on AWS Lambda. Given a set of metrics, the Lambdas should be able to send SMS using Twilio API. To be able to use the API, Twilio provide an account SID and an auth token.
How and where should I store these secrets?
I'm currently thinking to use AWS KMS but there might be other better solutions.
回答1:
Here is what I've come up with. I'm using AWS KMS to encrypt my secrets into a file that I upload with the code to AWS Lambda. I then decrypt it when I need to use them.
Here are the steps to follow.
First create a KMS key. You can find documentation here: http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html
Then encrypt your secret and put the result into a file. This can be achieved from the CLI with:
aws kms encrypt --key-id some_key_id --plaintext "This is the scret you want to encrypt" --query CiphertextBlob --output text | base64 -D > ./encrypted-secret
You then need to upload this file as part of the Lambda. You can decrypt and use the secret in the Lambda as follow.
var fs = require('fs'); var AWS = require('aws-sdk'); var kms = new AWS.KMS({region:'eu-west-1'}); var secretPath = './encrypted-secret'; var encryptedSecret = fs.readFileSync(secretPath); var params = { CiphertextBlob: encryptedSecret }; kms.decrypt(params, function(err, data) { if (err) console.log(err, err.stack); else { var decryptedSecret = data['Plaintext'].toString(); console.log(decryptedSecret); } });
I hope you'll find this useful.
回答2:
As of AWS Lambda support for NodeJS 4.3, the correct answer is to use Environment Variables. This feature integrates with AWS KMS, so you can use your own master keys to encrypt the secrets.
回答3:
There is a blueprint for a Nodejs Lambda function that starts off with decrypting an api key from kms. It provides an easy way to decrypt using a promise interface. It also gives you the role permissions that you need to give the lambda function in order to access kms. The blue print can be found by searching for "algorithmia-blueprint"
回答4:
Well...that's what KMS was made for :) And certainly more secure than storing your tokens in plaintext in the Lambda function or delegating to a third-party service.
If you go down this route, check out this blog post for an existing usage example to get up and running faster. In particular, you will need to add the following to your Lambda execution role policy:
"kms:Decrypt", "kms:DescribeKey", "kms:GetKeyPolicy",
The rest of the code for the above example is a bit convoluted; you should really only need describeKey()
in this case.
回答5:
Whatever you choose to do, you should use a tool like GitMonkey to monitor your code repositories and make sure your keys aren't committed or pushed to them.