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

Con*_*orB 7 amazon-s3 amazon-web-services node.js amazon-dynamodb amazon-elastic-beanstalk

我真的很挣扎如何隐藏我的钥匙.

我需要隐藏的两个密钥是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;
    }
};
Run Code Online (Sandbox Code Playgroud)

Mar*_*k B 6

在Elastic Beanstalk中,我认为存储这样的密钥的首选方法是通过环境变量.您可以使用该命令eb setenv key=value设置环境变量.关于这方面更多的信息在这里.

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

  • `eb setenv`调用将通过SSL连接将密钥名称和值发送到AWS API,因此这是设置值的安全方法.您也可以通过我认为的AWS Web控制台执行相同的操作.只要您限制对AWS账户的访问权限,它就是"安全的".如果您希望围绕这些值获得更多安全性,可以使用AWS Key Management Service(KMS)进行调查.您必须构建一些支持才能将该服务用于您的应用程序,因为它没有与Elastic Beanstalk紧密集成. (3认同)