7

我真的很挣扎,应该如何隐藏我的钥匙。我应该在哪里存储我的Node.js应用程序的密钥?

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

此外,我不知道在哪里放置我的密钥以访问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; 
    } 
}; 

回答

5

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

要访问您提及的有关访问DynamoDB和S3的AWS API,您根本不会使用密钥。为此,您可以将IAM实例配置文件分配给由Elastic Beanstalk创建的EC2服务器。这记录在here

+0

这也是一种“安全方式” – ConnorB

+3

eb setenv调用将通过SSL连接将密钥名称和值发送到AWS API,因此它是设置值的安全方式。我相信您也可以通过AWS Web控制台执行相同的操作。只要您限制访问您的AWS账户,它就是“安全”的。如果您希望获得更多有关这些值的安全性,可以使用AWS Key Management Service(KMS)进行研究。您将不得不在构建应用程序时使用该服务,因为它与Elastic Beanstalk没有紧密集成。 –

-2

为开发,生产等所有设备创建一个配置文件,并添加所有密钥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; 
+0

但我会在生产过程中使用我的项目部署该文件吗? – ConnorB

+0

是的,该文件应该在服务器上。你想不要把该文件放在服务器上?该文件或秘密不会在网络上旅行,所以它的安全保存在服务器上的文件 –

+0

不会在服务器上以纯文本格式输入密钥吗? – ConnorB

相关问题