日常

ケ・セラ・セラ

encrypted_secrets についてのメモ

v5.1.0.rc2 を使ってみていた。encrypted_secrets について確認したことをいくつかメモしておこうと思います。

まあ主に使い方はここ railties/lib/rails/commands/secrets/USAGE を見れば書かれているのですが、簡単に補足を加えつつ日本語でいきます。

encrypted secrets を使うにはまず、secrets:setup します。以下のようになります。

$ rails secrets:setup
Adding config/secrets.yml.key to store the encryption key: 40dc6a0dcf24059ce1edf48b7a242518

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

Ignoring config/secrets.yml.key so it won't end up in Git history:

      append  .gitignore

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
$

config/secrets.yml.key に secret key が、 その key で暗号化されたファイルが config/secrets.yml.enc です。

また、secrets:setup により config/secrets.yml.key は、.gitignore に追加されています。

secrets:edit で、$EDITOR を使い secret file の編集ができます。

secret file を読み込む為には、config.read_encrypted_secrets = true します。config/environments/production.rb には最初から設定されていますね。

railties/lib/rails/secrets.rb を見ると、暗号化は default では AES-128-GCM でします。以下の p-r でそう変わっています。

https://github.com/rails/rails/pull/28139