Rails4でsecrets.ymlでハマった。

まぁ完全にミスだったんですが、secrets.ymlにproduction環境のsecret_key_baseを設定するのは環境変数SECRET_KEY_BASEを使うのに、一度SSHでログインして設定して満足してしまって、永続化するのを忘れていました。それでcapistranoでデプロイしたら環境変数ないから止まってしまったというね…(開発環境だったからよかった)

config/secrets.yml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
# Be sure to restart your server when you modify this file.
 
# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
 
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rake secret` to generate a secure secret key.
 
# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.
 
development:
  secret_key_base: *************************************************************************************************************
test:
  secret_key_base: *************************************************************************************************************
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

どうするのがいいかと考えたのと相談した結果、/etc/profile.d/以下に設定ファイルを書くのが楽なんじゃないか?ということで、今回はそうしてみた。

/etc/profile.d/rails.sh
1
export SECRET_KEY_BASE=*************************************************************************************************************

一度unicornを終了させてからcapistranoでデプロイして確認したら動くようになりました。
ただ、これだとアプリ単位で定義しようと思ったら、こうせざるを得なくなります。

/etc/profile.d/rails.sh
1
2
export FOO_SECRET_KEY_BASE=*************************************************************************************************************
export BAR_SECRET_KEY_BASE=*************************************************************************************************************
fooアプリのsecrets.yml
1
2
production:
  secret_key_base: <%= ENV["FOO_SECRET_KEY_BASE"] %>
barアプリのsecrets.yml
1
2
production:
  secret_key_base: <%= ENV["BAR_SECRET_KEY_BASE"] %>

本当にいいのかわからなかったので社内で相談してみたところ、まぁ昔ならいざ知らず、現在は1サーバー1アプリケーションになりつつあるし、そんなに気にしなくていいんじゃね?という意見もあり、それもそうだなぁと思いました。
あとは、dotenvというgemがあるから(Rails用のdotenv-railsもある)、それを使うのがいいんじゃないかという話もでました。dotenv-railsはまだ試してないのですが、概要を読んだところ、よさそうでした。後で試してみたいと思います。


タグ Ruby, Ruby on Rails | パーマリンク.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です