如何在使用sails.js时安全地存储连接string

我在config \ adapters.js中有一个数据库连接string,如下所示。 我不想以纯文本离开连接string。 确保这些信息的最佳方式是什么,服务可以调用并连接到数据库?

module.exports.adapters = { // If you leave the adapter config unspecified // in a model definition, 'default' will be used. 'default': 'mssql', mssql: { connectionString: 'Driver={SQL Server Native Client 10.0};Server=foo.domain.com;Database=RODDY;Uid=jsmith;Pwd=nw0ow21', module: 'sails-mssql' } }; 

所有其他答案都是很好的答案,这里是另一种方法。

设置一个名为类似SQL_STRING的环境variables

这假设你在Ubuntu的..

 #edit /etc/profile or ~/.profile vim /etc/profile; #add this line to it. export SQL_STRING=<insert your sql string> 

现在在你的config/local.js文件中添加这个connectionString:

 connectionString: process.env.SQL_STRING 

这将加载连接string作为环境variables。 这将做两件事,

  1. 你现在可以提交local.js到你的local.js ,如果需要的话,无论你在哪里部署应用程序,它都会inheritanceenvvariables的设置。 这对于开发环境等是非常好的。

  2. 从应用程序文件中删除安全的数据,所以没有人可以偷看它们,他们必须做一些挖掘才能得到实际的数据。

我目前在sailsjs应用程序的生产环境中执行此操作,我有一个特殊的sails用户,它具有一个~/.profile ,其中包含我所有的MAILGUN,db和其他凭证数据,都被定义为环境variables。 这个用户受到sudo,ssh的访问限制,并被locking只能访问一个或两个文件夹。

有效地,唯一能够看到这些环境variables的人是root用户。

模糊连接凭证的问题是,您的应用程序需要访问与数据库进行通信,并最终需要以明文forms访问它。 Web应用程序开发人员有一个共识,那就是把它存储在你的filesytsem中。 如果攻击者可以访问你的文件系统,你的系统已经被严重破坏了。

作为一个networking应用程序,由于其暴露于世界本身就有攻击的风险。 您可以遵循安全,注重安全的做法来帮助pipe理风险:

  • 在您的数据库中创build一个用户帐号,只需要它所需的权限,仅此而已。 您不应该使用超级特权帐户(如root)与您的应用程序交谈
  • 禁止外部访问您的数据库
  • 保持定期备份异地(另一台服务器,我使用s3桶)
  • 使用防火墙来阻止所有端口,除了需要访问的外部端口(通常是http,https,ssh)
  • 禁用FTP和所有以明文forms发送数据的协议
  • 为您的人员帐户使用强密码,并为您的Web应用程序使用的帐户使用长时间随机生成的密码。

这份清单并不详尽,有更多的事情可以做,但这是一个很好的起点。

如果你想要的是将你的适配器连接凭证从版本控制中隐藏起来,sails.js允许你在config/local.js定义你的适配器configuration,默认情况下,它被添加到你的.gitignore中,所以不会被检入GIT中[1]。 查看sails.js部署指南,了解更多关于使用local.js [2]的信息。 如部署指南中所示,您可以使用local.js,如下所示:

 // Local configuration // // Included in the .gitignore by default, // this is where you include configuration overrides for your local system // or for a production deployment. // // For example, to use port 80 on the local machine, override the `port` config module.exports = { port: 80, environment: 'production', adapters: { mssql: { connectionString: 'Driver={SQL Server Native Client 10.0};Server=foo.domain.com;Database=RODDY;Uid=jsmith;Pwd=nw0ow21' } } } 

尽pipe如此,这不会阻止有权访问您的服务器的人访问此信息。 如果你担心这一点,那么@SamT给出的build议非常好。

  • [1]: http : //sailsjs.org/#!documentation/config.local
  • [2]: http : //sailsjs.org/#!documentation/deployment