LoginSignup
29
24

More than 5 years have passed since last update.

Rails5.1をproduction環境で起動する際の秘密鍵設定

Posted at

Rails5.1をproduction環境で動かすための秘密鍵設定ダイジェスト版

  1. $ rails secrets:setup → 一番最初の行の秘密鍵をメモれ.Rails実行時にRAILS_MASTER_KEYとして渡せ.
  2. $ rails secret → 上記の秘密鍵より更に長い鍵が出る.コピれ.
  3. $ EDITOR=vim rails secrets:edit を実行しろ.vimが起動するので下記を入力して保存しろ.
production:
  secret_key_base: 2番めに出てきてコピった長い鍵

本番(production)環境でrails serverしてつまづいた経緯など

何も考えずにdevelopment環境で開発してきた.しかしいよいよproduction環境の準備もしなくては,ということで 先人のQiita記事 を参考にとりあえず動かしてみた.
$ rails server --environment production

なにもせずにやるとエラーが出ます🤔

An unhandled lowlevel error occurred. The application logs may have details.
とブラウザに出ておこられる。

まさにまさに.Rails5.1で導入された
$ rails secrets:setup
を実行してみてじゃあこれでよいのか?と思ったらエラーメッセージがまったく変わらない.
ライブラリの中を追いかけていってみたところ,要するにそれだけじゃあいけなかった,ということがわかった.
ところで上記を実行すると,以下のような出力が得られる.

Adding config/secrets.yml.key to store the encryption key: a1e98ed29c40d7453a06bebeb815c0f3
Save this in a password manager your team can access.
If you lose the key, no one, including you, can access any encrypted secrets.
      create  config/secrets.yml.key
Adding config/secrets.yml.enc to store secrets that needs to be encrypted.
      create  config/secrets.yml.enc
For now the file contains this but it's been encrypted with the generated key:
# See `secrets.yml` for tips on generating suitable keys.
# production:
#  external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289…
You can edit encrypted secrets with `bin/rails secrets:edit`.
Add this to your config/environments/production.rb:
config.read_encrypted_secrets = true

上記の最初の行で出力された秘密鍵(a1e98ed29c40d7453a06bebeb815c0f3)が大事で,これが全体の秘密鍵となり config/secrets.yml.key に出力される.秘匿しなければならない情報.Gitでコミットしちゃダメ,絶対.
ただしこのファイルは .gitignore に自動的に追加されるので普通はコミットすることはない.

Railsの実行時にはconfig/secrets.yml.keyを外部からコピーしてくるか,それとも環境変数RAILS_MASTER_KEYにこの秘密鍵を指定するかのいずれかが必要となる.

で,実はここまでは前半部分で秘密情報にかける鍵の設定をしたに過ぎない.その秘密情報というのが,config/secrets.yml.enc で,前述の秘密鍵により暗号化されたymlファイルで内容は↓こんな感じ.

secrets.yml.enc
PWuauKAmdO3Cv2cVx+qqY8cxH3gF/xwRooeoccT8zjd/RypgQOtxPMGx9Coq1eXrw+1wEubcvs6RreKnWlJ
+rrSH1LJjti8dWvveQxvhVlliF/dHJl0nTKBFoq/VTlk6OZhsejga0TJqXygKrCDOeLZXFRoGfsxKsRRByl
/9gmeVpqmy0TJDVWOS0jSK+g==--dEPT19vek1oKHNWd--8OKW3ZEmKMFEpAc7/8X5oQ=

このファイルを編集するためにはこうする.(エディタとしてvimを使う場合)
$ EDITOR=vim rails secrets:edit
すると何も設定していないファイルの中身はこんな感じ

# See `secrets.yml` for tips on generating suitable keys.
# production:
#  external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289…

そして上記では何も指定されていないが,実際には production のところに secret_key_base を指定しなければならない.これはCookieの暗号化などに使用される秘密鍵で,自分で考えても良いがRailsが生成してくれる鍵を使うのがおすすめ.
具体的には,まず rails secret で鍵を生成しておいて,

$ rails secret
d7755ec06a476332ec0c5083e6a824eb60552704f136383d9...

それを利用して以下のように暗号化された秘密情報ファイルに secret_key_base を保存する

$ EDITOR=vim rails secrets:edit
production:
  secret_key_base: d7755ec06a476332ec0c5083e6a824eb60552704f136383d9...

これで秘密鍵関係のエラーは出なくなる.どうせなら最初の段階でそこまでやってくれれば親切なのだが.

ちなみに secret_key_base 以外にもいろいろと秘密情報を保存することができる.たとえば,データベース用のパスワードの場合,以下のようになる.

production:
  secret_key_base: d7755ec06a476332ec0c5083e6a824eb60552704f136383d9...
  postgresql_password: c0c5083e6a824eb6

そしてここで指定した情報は,Rails.application.secrets.postgresql_password のようにして参照することができる.たとえば,

config/database.yml
...
production:
  adapter: postgresql
  username: appname
  password: <%= Rails.application.secrets.postgresql_password %>
...
29
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
24