Leon's Blogging

Coding blogging for hackers.

Rails 5.2 Credentials

| Comments

rails5.2 開始 config/secrets.yml 已經被移除了,取而代之的是 credentials

rails5.2 會新增兩個檔案

預設會有 config/credentials.yml.enc 執行 EDITOR="vim" rails credentials:edit 就會產生 config/master.key

如果無法順利執行的話可以刪掉 config/credentials.yml.enc 在執行 EDITOR="vim" rails credentials:edit 一次

  • config/credentials.yml.enc is an encrypted file that will contain all your secret credentials,因為加密過可以很放心的推到 github

  • config/master.key is a file containing your encryption key

mater.key 是用來加解密 credentials.yml.enc,記得不要上到 github,要放到 .gitignore

1
2
3
# .gitignore
# Ignore master key for decrypting credentials and more.
/config/master.key
1
2
# 可以看說明
rails credentials:help

因為經過加密,所以打開 credentials.yml.enc 會發現一串亂碼

1
2
# credentials.yml.enc
17NEkGq/xkDO...

如果要編輯檔案需要另外執行解密,才能夠進行編輯

1
2
# EDITOR 可以改用其他編輯器 subl code 都可以
EDITOR=vim rails credentials:edit
1
2
3
4
5
6
# 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: 008174812dc5a309a0de...

離開後就會自動儲存

1
2
# 這時候就會用 master.key 進行加密
New credentials encrypted and saved.

讀取檔案

1
2
3
4
5
Rails.application.credentials.dig(:secret_key_base)
Rails.application.credentials[:secret_key_base]
Rails.application.credentials.aws[:access_key_id]
Rails.application.credentials.aws[:secret_access_key]
Rails.application.credentials.secret_key_base

並且向其他 yml 檔案一樣,建立一在 share 的資料夾中,或是新增一個環境變數 RAILS_MASTER_KEY

Environments

如果想像之前一樣,不同環境有不同的 key,可以設定成跟之前的 secret.yml 一樣

1
2
3
4
5
6
7
8
development:
  aws:
    access_key_id: 123
    secret_access_key: 345
production:
  aws:
    access_key_id: 321
    secret_access_key: 543
1
Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]

Production

如果在 production 環境,要做以下設定

1
2
3
4
#config/environments/production.rb
...
config.require_master_key = true
...

在 docker 中 create

1
docker-compose run --rm -e RAILS_ENV=development -e EDITOR=vim backend bin/rails credentials:edit

參考文件

Comments