Running a rails server in production locally (InvalidMessage error)
For Rails 6, I had a multi-environment credentials setup.
One for development, staging, and production.
The master.key works for the main credentials.yml
file
The other environments have there own key, so for staging we used the production.key in place of the RAILS_MASTER_KEY
config envs on heroku and that fixed it for me.
Okay I got it working finally.
I simply deleted my master.key
and credentials.yml.enc
files and then ran
bin/rails credentials:edit
Which created new files. After that everything worked fine.
I don't really understand why it works though. Can anyone give a good explanation for this?
It appears your solution of removing the master.key
and credentials.yml.enc
indicates you are running Rails 5.2. This setup changed from a similar encrypted secrets.yml.enc
file used in Rails 5.1.
The goal is to allow committing secret keys (AWS, Rails' secrect_key_base
) to a project's code repository. These would typically be set with ENV variables. Now collaborators need only share the master.key
that was generated to decrypt and modify or read the contents of credentials.yml.enc
.
When you removed both the master.key
and credentials.yml.enc
files, rails generated a new pair, now you were able to decrypt credentials.yml.enc
and this file was initialized with a new Rails secret_key_base
value needed to avoid the ActiveSupport::MessageEncryptor::InvalidMessage
. If you track down the source of that message, it's likely referencing the Rails credentials secret key base: Rails.application.credentials.secret_key_base
.
These are nice write ups on the topic:
https://medium.com/cedarcode/rails-5-2-credentials-9b3324851336
https://www.engineyard.com/blog/rails-encrypted-credentials-on-rails-5.2