Cases where Rails fails to start due to an invalid config/master.key and how to deal with it

Problems that occur

When I try to launch a Rails app that I brought locally with git clone with rails s or rails c, the following error occurs.

$ rails s
=> Booting Puma
=> Rails 6.1.0 application starting in development 
=> Run `bin/rails server --help` for more startup options
Exiting
/Users/jnito/.rbenv/versions/3.0.0/lib/ruby/gems/3.0.0/gems/activesupport-6.1.0/lib/active_support/message_encryptor.rb:203:in `rescue in _decrypt': ActiveSupport::MessageEncryptor::InvalidMessage (ActiveSupport::MessageEncryptor::InvalidMessage)

Cause of error

Rails won't start with an exception because the key for config/master.key doesn't match the key for config/credentials.yml.enc.

A little more detail

config/master.key is usually a file that is added to .gitignore and is not subject to source code control. Therefore, git clone does not copy config/master.key locally.

In that state, if you want to edit credentials.yml.enc and execute a command like EDITOR = "vi" bin/rails credentials: edit, you will see the following message and config/master. .key is newly created. (In addition, you cannot edit credentials.yml.enc)

$ EDITOR="vi" bin/rails credentials:edit
Adding config/master.key to store the encryption key: 6eb4ad(The following is omitted)

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/master.key

Couldn't decrypt config/credentials.yml.enc. Perhaps you passed the wrong key?

However, the newly created config/master.key is not the key of the config/credentials.yml.enc that has been git cloned, so decryption fails when Rails starts and an exception (ActiveSupport :: MessageEncryptor: : InvalidMessage) occurs.

Workaround

How to deal with it depends on the situation.

If credentials.yml.enc was used before git clone

Since the key sharing method should be decided within the development team, place the appropriate config/master.key locally according to the operation rules.

If credentials.yml.enc has never been used

If you delete the automatically created config/master.key, Rails will start.

If you don't want to use credentials.yml.enc in the future, you can delete the whole config/credentials.yml.enc.

Conversely, if you plan to use credentials.yml.enc (= you will be the first user in your team), recreate config/credentials.yml.enc and then credentials /master.key needs to be properly managed and shared within the team. (However, if you can get the appropriate config/master.key from somewhere, you don't need to recreate it.)

#Existing credentials.yml.Remove enc
$ rm config/credentials.yml.enc

# credentials.yml.enc and master.Create a new key and edit it
$ EDITOR="vi" bin/rails credentials:edit
Adding config/master.key to store the encryption key: 917c31e316061bcdd909754a56897f61

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/master.key

File encrypted and saved.

Operation check environment

Recommended Posts

Cases where Rails fails to start due to an invalid config/master.key and how to deal with it
How to build API with GraphQL and Rails
[Rails] How to build an environment with Docker
I'm making an Android app and I'm stuck with errors and how to solve it
How to push an app developed with Rails to Github
How to make an almost static page with rails
[Rails] How to deal with URL changes after render
Common problems with WSL and how to deal with them
[Docker + Rails] How to deal with Rails server startup failure
How to specify db when creating an app with rails
How to deal with different versions of rbenv and Ruby
How to build an environment with Docker, which is the minimum required to start a Rails application
[Rails / RSpec] How to deal with element has zero size error
[Rails] How to introduce kaminari with Slim and change the design
How to get along with Rails
How to start Camunda with Docker
How to make an app with a plugin mechanism [C # and Java]
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
How to deal with FATAL: role "admin0" does not exist and PG :: ConnectionBad: FATAL: role "admin0" does not exist when executing rails db: create
[Android] How to deal with dark themes
Note: [Docker] How to start and stop
How to deal with Precompiling assets failed.
How to build Rails 6 environment with Docker
What to do when is invalid because it does not start with a'-'
Downgrade an existing app created with rails 5.2.4 to 5.1.6
Rails6.0 ~ How to create an eco-friendly development environment
[Rails] How to easily implement numbers with pull-down
[Rails] How to get success and error messages
Rails scope anti-patterns and how to eliminate them
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (5)
[Ruby] 5 errors that tend to occur when scraping with Selenium and how to deal with them
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (3)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (2)
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (1)
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Rails AWS Docker] Build an existing Ruby on Rails + MySQL application with Docker and deploy it on AWS (4)
How to deal with errors in Rails s could not find a JavaScript runtime.