【问题标题】:Where should I store my secret keys for my Node.js app?我应该在哪里存储我的 Node.js 应用程序的密钥?
【发布时间】:2017-05-30 06:21:22
【问题描述】:

我真的很纠结如何隐藏我的钥匙。

我需要隐藏的两个密钥是 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;
    }
};

【问题讨论】:

    标签: node.js amazon-web-services amazon-s3 amazon-dynamodb amazon-elastic-beanstalk


    【解决方案1】:

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

    对于访问 AWS API(您在访问 DynamoDB 和 S3 时提到),您根本不会使用密钥。为此,您需要将 IAM 实例配置文件分配给 Elastic Beanstalk 创建的 EC2 服务器。这记录在here

    【讨论】:

    • 这也是一种“安全的方式”
    • 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;
    

    【讨论】:

    • 但我会在生产期间将该文件部署到我的项目中吗?
    • 是的,该文件应该在服务器上。您不想将该文件放在服务器上吗?该文件或机密不会在网络上传播,因此将文件保存在服务器上是安全的
    • 这不是将密钥以纯文本形式放在服务器上吗?
    • 将亚马逊AWS的访问限制在允许的ips和负载均衡器上。你的钥匙是安全的
    • 如果解密技术也在服务器上,加密密钥不是没有意义吗?我正在考虑将我的密钥放入 AWS 环境变量中。这也行吗?
    猜你喜欢
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 2018-06-27
    相关资源
    最近更新 更多