如何手动访问在Rails 7的credentials.yml.enc中添加的环境变量?

huangapple go评论56阅读模式
英文:

How can I access environment variables manually added to credentials.yml.enc in Rails 7?

问题

我正在使用Rails 7,并能够使用命令Rails.application.secret_key_base获取secret_key_base,但是当我尝试获取credentials.yml.enc中保存的另一个变量,比如Rails.application.new_key时,我会收到错误消息。

请问如何使用命令EDITOR='code --wait' rails credentials:edit获取我手动添加到credentials.yml.enc文件中的变量?

这是我的credentials.yml.enc文件的样子:

# aws:
#   access_key_id: 123
#   secret_access_key: 345

# 用作Rails中所有MessageVerifiers的基本密钥,包括保护cookie的密钥。
secret_key_base: dkud
new_key: qwerty

我还尝试了命令Rails.application.credentials,它可以用于secret_key_base,但对于credentials.yml.enc中的其他变量则不起作用。

英文:

I'm using Rails 7 and able to get the secret_key_base with the command Rails.application.secret_key_base but when I try get another variable that I saved in the credentials.yml.enc like Rails.application.new_key I get error.

Please how can I get variable that I manuelly added to the credentials.yml.enc file using the command EDITOR='code --wait' rails credentials:edit

Here is what my credentials.yml.enc file look like:

# aws:
#   access_key_id: 123
#   secret_access_key: 345

# Used as the base secret for all MessageVerifiers in Rails, including the one protecting cookies.
secret_key_base: dkud
new_key: qwerty

I also try the command Rails.application.credentials it works with the secret_key_base but not with other variables in the credentials.yml.enc

答案1

得分: 1

凭证文件中保存的机密信息可通过 Rails.application.credentials 访问,您需要在命令中添加 .credentials

irb(main):001:0> Rails.application.credentials.new_key
=> "qwerty"
irb(main):002:0> Rails.application.credentials[:new_key]
=> "qwerty"

默认情况下,只能使用 Rails.application 检索 secret_key_base

指南中了解更多有关机密信息的信息。

英文:

Secrets kept in the credentials file are accessible via Rails.application.credentials, you are missing .credentials to your command:

irb(main):001:0> Rails.application.credentials.new_key
=> "qwerty"
irb(main):002:0> Rails.application.credentials[:new_key]
=> "qwerty"

By default only secret_key_base can be retrieved using Rails.application

Learn more on secrets in the guide.

huangapple
  • 本文由 发表于 2023年5月28日 20:37:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定