Rails 7 adds method calls for nested secrets

Ashik Salman

Ashik Salman

June 9, 2021

This blog is part of our  Rails 7 series.

Rails stores secrets in config/credentials.yml.enc, which is encrypted and cannot be edited directly. You can read more about credentials management here: Rails security guide.

Rails 7 allows access to nested encrypted secrets (credentials) by method calls. We can easily access the nested secrets present in the credentials YAML file like we've accessed top-level secrets previously:

# config/credentials.yml.enc

secret_key_base: "47327396e32dc8ac825760bb31f079225c5c0"
aws:
  access_key_id: "A6AMOGVNQKCWLNQ"
  secret_access_key: "jfm6b9530tPu/h8v93W4TkUJN+b/ZMKkG"
=> Rails.application.credentials.aws
=> {:access_key_id=>"A6AMOGVNQKCWLNQ", :secret_access_key=>"jfm6b9530tPu/h8v93W4TkUJN+b/ZMKkG"}

Before Rails 7

=> Rails.application.credentials.aws[:access_key_id]
=> "A6AMOGVNQKCWLNQ"

=> Rails.application.credentials.aws.access_key_id
=> NoMethodError (undefined method `access_key_id' for #<Hash:0x00007fb1adb0cca8>)

After Rails 7

=> Rails.application.credentials.aws.access_key_id
=> "A6AMOGVNQKCWLNQ"

Check out this pull request for more details.

If this blog was helpful, check out our full blog archive.

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.