LoginSignup
4
1

More than 1 year has passed since last update.

[CircleCIのセキュリティインシデント対応] Railsのmaster keyをローテーション(再生成)する

Last updated at Posted at 2023-01-06

大した情報ではないですが、
circle ciでRailsを使っている人への注意喚起になればと思います。

話の発端

2023/04/01 CircleCIで管理しているセキュア情報が漏洩したかもらしい

環境

  • Ruby on Rails: 7.0.2
  • Ruby: 3.1
  • ruby:3.1.0 をベースイメージにしたコンテナ内で作業

再生成していく

今回は例として、production用のcredentialsを再生成します。

① 現状のcredentialsをメモする

# 現状のcredentialsをメモ帳などにコピー
$ rails credentials:show -e production
db:                        
  username: hoge               
  password: huga

② credentialファイルとmaster.keyを削除する

# 念の為バックアップしておく 
$ cp -r config/credentials config/credentials_bkup

# keyファイルとcredentialファイルを削除
$ rm config/credentials/production.key
$ rm config/credentials/production.yml.enc 

③ credentialファイルとmaster.keyを生成して書き込む

# EDITOR=vim rails credentials:editで新しいcredentialファイルとkeyを生成し、
# 先ほどメモした情報を貼り付ける
$ EDITOR=vim rails credentials:edit -e production
db:                        
  username: hoge               
  password: huga


# 保存をして、vimを閉じると以下のようなメッセージが出れば成功
adding config/credentials/production.key to store the encryption key: xxxxxxxxxxxxxxxxxxxx

Save this in a password manager your team can access.

If you lose the key, no one, including you, can access anything encrypted with it.

      create  config/credentials/production.key

④ CircleCIの情報を更新

RAILS_MASTER_KEY の環境変数で保存しているはずなので書き換える

正常に複合できるか確認

Rails.application.credentials.configを表示できれば成功です。
 余計な空白などないか確認しておきましょう。

$ RAILS_ENV=production rails c
Loading development environment (Rails 7.0.2)

irb(main):001:0> Rails.application.credentials.config  # 確認
=> 
{:db=>{:username=>"hoge", :password=>"huga" }}

参考

4
1
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
4
1