Rails:如何修复 "Missing secret_key_base for 'production' environment"

Rails: How to fix "Missing secret_key_base for 'production' environment"

我根本无法理解这条消息:

Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit` (ArgumentError)

我有 Rails 5.2.0,并且 运行

EDITOR=vim rails credentials:edit

里面:

production:
   secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx

保存并在终端中:

RAILS_ENV=production rails c

我错过了什么吗?我重新启动了服务器并遇到了同样的问题,但在开发模式下没有问题。

保持默认 secrets.yml 文件

# config/secrets.yml
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  aws_secret: abcde
  some_password: abcdex

development:
  secret_key_base: static_secret_key
  aws_secret: abcde

test:
  secret_key_base: static_test_secret_key


#not_indented: key for all env in once
secret_key_base: global_key_for_all_env
RAILS_ENV=production SECRET_KEY_BASE=production_test_key rails c

如果使用 Rails 5.2.0,请添加到下面的生产环境中,检查此 LINK

config.require_master_key = true    #config/environments/production.rb

Rails 5.2.0 需要一个额外的生产环境阶段:

config.require_master_key = true    # in config/environments/production.rb

没有它,Rails 仍然会退回到遗留 secret.yml 机制(目前)。

Engine Yard 的 Christopher Rigor has written a concise post on it。相关作品:

Reading the Credentials

If you want to use the credentials in the production environment, add the following to config/environments/production.rb

config.require_master_key = true

很好的阅读,也能看到上下两面。

注意:@TomDogg 发现,Rails 5.2.1 似乎又有所不同,因此此答案可能仅适用于 5.2.0。

凭据文件中没有 production: development:test: 环境标签。此 DHH 的 post 中的更多信息:https://github.com/rails/rails/pull/30067

所以直接写

secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx

请不要将主密钥与秘密密钥库混淆。主密钥用于打开凭据加密文件。

切换回以前的秘密系统不应该是解决方案,也不是公认的答案。

config/credentials.yml.enc:

development:
  some_username: XXXXXXXXX
  some_password: YYYYYYYYY

test:
  some_username: XXXXXXXXX
  some_password: YYYYYYYYY

production:
  some_username: XXXXXXXXX
  some_password: YYYYYYYYY

secret_key_base: ZZZZZZZZZ
# `secret_key_base:` must NOT be indented !
# It must be put at the very start of a new line.
# There is also no need for it in development or test environment,
#   since there are no attacks to be expected.

还要确保您遵守所有 YAML 缩进规则(即仅限 2 个空格),否则我将导致加载此文件失败且无提示。

我在生产中处理 Rails 5.2 应用程序时遇到了同样的问题。

我已经设置了其他东西。对我来说,问题不是 secret_key_base 设置不正确,而是因为 将环境名称作为常规参数传递已弃用

rails c RAILS_ENV=production

如果您从顶部仔细查看生成的错误日志,您会看到:

DEPRECATION WARNING: Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead. (called from at bin/rails:9)

要运行 不同环境中的rails 控制台,请使用-e 选项,如下所示:

rails console -e production

注意:在secrets.yml文件中设置secret_key_base是不安全的,因为这不是存储密钥的安全方式,请使用加密 credential.yml 文件和 master key 解密。

就这些了。

希望对您有所帮助

Secret_key_base 设置不正确。 这是一个没有得到足够重视的已知问题:https://github.com/rails/rails/issues/32947

生成密钥:

EDITOR=vim rails credentials:edit

记录密钥。 保存在 config/master.key.

SECRET_KEY_BASE=`cat config/master.key` bin/rails assets:precompile

这就是我得出的解决方案。我真的不喜欢我如何被迫通过环境变量来放置它。如果有人有更多关于 master.key 如何工作的信息提请我注意,请发表评论。

避免将 secret_key_base 放在环境标签下。放在上面。

这是错误的:

production:
   secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx
   some_other_key: xxx

试试这个:

secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx
production:
   some_other_key: xxx

我 运行 在使用 Dockerfile 将我的 rails 应用程序部署到 dokku 时遇到了这个问题。我的解决方案:

文件config/secrets.yml引用了一个环境变量:

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

我需要使用 dokku 命令行设置这个变量(直接在服务器上,或者在我的开发机器上使用 dokku-cli gem):

dokku config:set SECRET_KEY_BASE=blalbalblablahblablah