LoginSignup
18
23

More than 5 years have passed since last update.

【備忘録】credentials.yml.encにdatabase設定を保存する

Last updated at Posted at 2018-06-05

はじめに

Rails5.2からcredentials.yml.encに機密情報を格納させることができるようになったので、databaseの設定を保存させてみました。

credentials.yml.enc

credentials.yml.encにdatabaseの設定を記述する

# credentials.yml.encをviで開く
$ EDITOR="vi" bin/rails credentials:edit
credentials.yml.enc
db:
  host:     xxxxx
  database: xxxxx
  username: xxxxx
  password: xxxxx

database.yml

database.ymlcredentials.yml.encに指定した値を利用するように設定する

config/database.yml
# 省略
production:
  <<: *default
  # 省略
  host:     <%= Rails.application.credentials.db[:host] %>
  database: <%= Rails.application.credentials.db[:database] %>
  username: <%= Rails.application.credentials.db[:username] %>
  password: <%= Rails.application.credentials.db[:password] %>

所感/宿題

  • master.keyを総当たりされると復号できてしまうので、publicリポジトリではやらないほうがいい
  • データベースが1つだけの環境であれば、credentials.yml.encに格納させておくのが楽ちん
  • データベースが複数ある環境での運用方法がわからない
18
23
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
18
23