我应该在哪里存储我的Node.js应用程序的密钥?

我真的很困难,我应该如何隐藏我的钥匙。

我需要隐藏的两个密钥是secrets.crypto和secrets.jwt …我打算使用Elastic Beanstalk在AWS上托pipe我的应用程序。

此外,我不知道在哪里我会把我的钥匙,以访问像我的Dynamodb和我的S3桶。

exports.generateToken = (type, user) => { if (!_.isString(type)) { return undefined; } try { //Turn the json object of the current user's id and the type of token into a string var stringData = JSON.stringify({ _id: user._id, type: type }); //Take the json string and encrypt it with a secret then turn it back into a string var encryptedData = cryptojs.AES.encrypt(stringData, secrets.crypto).toString(); //Take the encryptedData and turn it into a token with a secret var token = jwt.sign({ token: encryptedData }, secrets.jwt); return token; } catch(e) { return undefined; } }; 

在Elastic Beanstalk中,我相信存储像这样的键的首选方法是通过环境variables。 您可以使用命令eb setenv key=value来设置环境variables。 关于这方面的更多信息。

要访问您提到的有关访问DynamoDB和S3的AWS API,您根本不会使用密钥。 为此,您可以将IAM实例configuration文件分配给由Elastic Beanstalk创build的EC2服务器。 这是在这里logging 。

为开发,生产等所有设备创build一个configuration文件,并将所有密钥init添加到您想要的任意位置。

 config.json file { "development": { "Secret1": "Your Secret Here", "Secret2": "Your Secret Here", "db":{ //development database settings here } }, "production": { "Secret1": "Your Secret Here", "Secret2": "Your Secret Here", "db":{ //development database settings here } } } var config = require('./config.json'); config.Secret1;