提交 Rails' credentials.yml.enc 文件安全吗?

Is it safe to commit Rails' credentials.yml.enc file?

我刚刚创建了一个新的 Rails 项目,它附带了这个 credentials.yml.enc 文件。

公开提交安全吗?

什么David Heinemeier Hansson said here:

These secrets are not supposed to withstand any sort of attack in test or development.

据我所知,你不应该在这里保留该死的秘密凭据,然后最好在 public 中发布。

It's only in production (and derivative environments, like exposed betas) where the secret actually needs to be secret. So we can simply insert this secret into the new flat credentials.yml.enc file.

最后他提到:

Note: We should just keep Rails.secrets and friends around. The Rails.credentials setup would be a new, concurrent approach. All new apps would use it, but we wouldn't need to screw existing apps.

希望对您有所帮助。更多请关注this.

credentials.yml.enc 为什么不呢?这是加密文件,没有密钥就没有任何信息。

但是master.key你们一定要保密!!!它可能会解密您的文件。

终于明白了。 请先阅读此 https://blog.saeloun.com/2019/10/10/rails-6-adds-support-for-multi-environment-credentials.html

对于testdevelopment环境,你可以简单地去掉master.key,你会发现rails s效果很好。 您可以 运行 rails console,然后 运行 Rails.application.credentials.config 来查看该值。

但是如果你那里master.key和运行rails s错了,你就会报错

但是如果你去掉了master.key,你会发现rails s -e production不起作用。

如果 master.key 的值正确,您可以 运行 EDITOR=vim rails credentials:edit 进行编辑。

如果您没有正确的 master.key 值,当您 运行 EDITOR=vim rails credentials:edit, 它将为您生成一个新的 master.key 但不幸的是 master.key 是错误的。 这是合理的,因为它使 credentials.yml.enc 无法解密,除非您已经得到正确的 master.key.

因此您可以删除 credentials.yml.encmaster.key 以及 运行 EDITOR=vim rails credentials:edit 以生成新的一对。 但在你这样做之前,你应该删除 master.key 和 运行 rails console,然后 运行 Rails.application.credentials.config 到 了解 运行ning EDITOR=vim rails credentials:edit.

时需要设置的值

production env 中的所有 Rails 实例应该具有相同的 credentials.yml.encmaster.key.

所以你应该在源代码中保留 credentials.yml.enc

您可以将 credentials.yml.enc 文件推送到生产环境。只需删除 master.key。它们旨在被推向生产。但是,如果您对此持怀疑态度,请将其保存到某个本地服务器,并在部署时生成代码以提取文件和 master.key。如果您愿意,可以使用 Capistrano 任务来完成此操作。