在 Rails 6 上定义 secret_key_base 的正确方法是什么?
What's the correct way of defining secret_key_base on Rails 6?
既然我们拥有每个环境的凭据,那么在 Rails 6 上定义 secret_key_base
的正确方法是什么?
我的环境有变量 SECRET_KEY_BASE
,但 Rails 没有选择它。我尝试在 config\credentials\production.yml.enc
中定义 secret_key_base
但它对 Rails.application.credentials.secret_key_base
没有影响
我知道config/secrets.yml
和
staging:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
有效,但是,这是 Rails 6 方式吗?
几天前我已经尝试解决这个问题。
我学到了什么:
第一次尝试
我尝试在每个环境中使用凭据
$ EDITOR=nano rails credentials:edit --environment development
$ EDITOR=nano rails credentials:edit --environment staging
$ EDITOR=nano rails credentials:edit --environment production
我的 creds 文件和密钥被放置在 config/credentials
.
我直接在那里设置必要的变量。这是可用的解决方案,但是当我们的开发人员想要使用 helm
配置时,我们在 Kubernetes 集群上的部署遇到了问题。因此,预定义凭据不适用于这种情况。
第二次尝试
之后,我尝试在我的凭据文件中使用 ENV 变量。
不幸的是,它也不起作用:
secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>
最后的尝试
最后,我使用默认配置优雅地降级到 gem config
,当您将每个环境设置放在那里时:
config/settings.yml
config/settings/development.yml
config/settings/production.yml
config/settings/test.yml
而我的 settings.yml
文件仅包含 ENV 变量,如下所示:
secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>
db:
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
pool: <%= ENV['DB_POOL'] %>
user: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: <%= ENV['DB_DATABASE'] %>
...
这是可行的解决方案,但似乎是倒退。
据我所知,我们不能以任何简单方式在凭据中使用 ENV-vars。
在 Rails 6 中访问和检查 secret_key_base
的正确方法不再是:~
Rails.application.credentials.secret_key_base
现在是:
Rails.application.secret_key_base
我不确定这是 Rails 6 还是一直这样。在查看此方法及其实现时,这一点变得非常清楚:
# The secret_key_base is used as the input secret to the application's key generator, which in turn
# is used to create all MessageVerifiers/MessageEncryptors, including the ones that sign and encrypt cookies.
#
# In development and test, this is randomly generated and stored in a
# temporary file in <tt>tmp/development_secret.txt</tt>.
#
# In all other environments, we look for it first in ENV["SECRET_KEY_BASE"],
# then credentials.secret_key_base, and finally secrets.secret_key_base. For most applications,
# the correct place to store it is in the encrypted credentials file.
def secret_key_base
if Rails.env.development? || Rails.env.test?
secrets.secret_key_base ||= generate_development_secret
else
validate_secret_key_base(
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
)
end
end
开发模式和测试模式都有自己的密钥库生成和存储方式。对于其他一切,它会按顺序从环境、凭据或机密中获取。
Docker 开发中的用户可能会在 entrypoint.sh
:
中考虑这一点
if [ "$RAILS_ENV" = "development" ]; then
printf $SECRET_KEY_BASE > ./tmp/development_secret.txt
fi
既然我们拥有每个环境的凭据,那么在 Rails 6 上定义 secret_key_base
的正确方法是什么?
我的环境有变量 SECRET_KEY_BASE
,但 Rails 没有选择它。我尝试在 config\credentials\production.yml.enc
中定义 secret_key_base
但它对 Rails.application.credentials.secret_key_base
我知道config/secrets.yml
和
staging:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
有效,但是,这是 Rails 6 方式吗?
几天前我已经尝试解决这个问题。
我学到了什么:
第一次尝试
我尝试在每个环境中使用凭据
$ EDITOR=nano rails credentials:edit --environment development
$ EDITOR=nano rails credentials:edit --environment staging
$ EDITOR=nano rails credentials:edit --environment production
我的 creds 文件和密钥被放置在 config/credentials
.
我直接在那里设置必要的变量。这是可用的解决方案,但是当我们的开发人员想要使用 helm
配置时,我们在 Kubernetes 集群上的部署遇到了问题。因此,预定义凭据不适用于这种情况。
第二次尝试
之后,我尝试在我的凭据文件中使用 ENV 变量。 不幸的是,它也不起作用:
secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>
最后的尝试
最后,我使用默认配置优雅地降级到 gem config
,当您将每个环境设置放在那里时:
config/settings.yml
config/settings/development.yml
config/settings/production.yml
config/settings/test.yml
而我的 settings.yml
文件仅包含 ENV 变量,如下所示:
secret_key_base: <%= ENV['SECRET_KEY_BASE'] %>
db:
host: <%= ENV['DB_HOST'] %>
port: <%= ENV['DB_PORT'] %>
pool: <%= ENV['DB_POOL'] %>
user: <%= ENV['DB_USER'] %>
password: <%= ENV['DB_PASSWORD'] %>
database: <%= ENV['DB_DATABASE'] %>
...
这是可行的解决方案,但似乎是倒退。
据我所知,我们不能以任何简单方式在凭据中使用 ENV-vars。
在 Rails 6 中访问和检查 secret_key_base
的正确方法不再是:~
Rails.application.credentials.secret_key_base
现在是:
Rails.application.secret_key_base
我不确定这是 Rails 6 还是一直这样。在查看此方法及其实现时,这一点变得非常清楚:
# The secret_key_base is used as the input secret to the application's key generator, which in turn
# is used to create all MessageVerifiers/MessageEncryptors, including the ones that sign and encrypt cookies.
#
# In development and test, this is randomly generated and stored in a
# temporary file in <tt>tmp/development_secret.txt</tt>.
#
# In all other environments, we look for it first in ENV["SECRET_KEY_BASE"],
# then credentials.secret_key_base, and finally secrets.secret_key_base. For most applications,
# the correct place to store it is in the encrypted credentials file.
def secret_key_base
if Rails.env.development? || Rails.env.test?
secrets.secret_key_base ||= generate_development_secret
else
validate_secret_key_base(
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
)
end
end
开发模式和测试模式都有自己的密钥库生成和存储方式。对于其他一切,它会按顺序从环境、凭据或机密中获取。
Docker 开发中的用户可能会在 entrypoint.sh
:
if [ "$RAILS_ENV" = "development" ]; then
printf $SECRET_KEY_BASE > ./tmp/development_secret.txt
fi