Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't load app environment when editing credentials #34789

Merged
merged 1 commit into from Jan 24, 2019
Merged

Don't load app environment when editing credentials #34789

merged 1 commit into from Jan 24, 2019

Conversation

christos
Copy link
Contributor

Summary

While evaluating multi-environment credentials, I came across a problem trying to create a new environment for my app.

Once you have created your first environment (e.g. development) you might start adding configuration in initialisers that tries to access encrypted credentials like so:

# config/initializers/sidekiq.rb
Sidekiq::Web.set :session_secret, Rails.application.credentials.secret_key_base!

That works fine when running rails credentials:edit --environment development

If you now decide to create a staging environment, doing rails credentials:edit --environment staging will fail with something similar to ...ordered_options.rb:49:in 'method_missing': :secret_key_base is blank (KeyError)

A great many gems that access 3rd party APIs use an initialiser for configuring credentials so the above situation is quite likely to occur.

I worked around the issue by creating a new empty Rails app, running rails credentials:edit --environment staging in it, and copying across the generated credential files.

This PR attempts to fix the issue by not loading the application environment for rails credentials:* commands.

I've added a test but I am not sure that it is correct (or sufficient) as I struggled to fully understand the railties test harness. Happy to try another way to test this.

This avoids missing key exceptions caused by code that tries to read the credentials before they have been added to the encrypted file, for example when editing the credentials for a new environment.
@rails-bot rails-bot bot added the railties label Dec 25, 2018
@kaspth kaspth merged commit 5f7d599 into rails:master Jan 24, 2019
@kaspth
Copy link
Contributor

kaspth commented Jan 24, 2019

Lovely fix, thank you!

kaspth added a commit that referenced this pull request Jan 24, 2019
…redentials

Don't load app environment when editing credentials
@kaspth
Copy link
Contributor

kaspth commented Jan 24, 2019

Backported to 5-2-stable @ 4b6a24c

@christos christos deleted the fix-editing-new-environment-credentials branch April 26, 2019 14:54
jonathanhefner added a commit to jonathanhefner/rails that referenced this pull request Jan 25, 2023
This commit changes the credentials commands (e.g. `bin/rails
credentials:edit`) to load `config/environments/#{Rails.env}.rb`.  Thus,
`config.credentials.content_path` and `config.credentials.key_path` can
be set in `config/environments/*.rb`, in addition to the currently
supported `config/application.rb`.

The `load_environment_config` initializer, which is run via
`Rails.application.initialize!`, is responsible for loading the
appropriate `config/environments/*.rb` file.

Normally, when booting an app, `Rails.application.initialize!` is called
without arguments by `config/environment.rb`, which is loaded via
`Rails.application.require_environment!`.  Doing so runs all
initializers.

Running all initializers is problematic for credentials commands because
(1) initializers might try to access resources that aren't available
(e.g. a production database), and (2) initializers might try to read
credentials values that have not yet been set in the current
environment's credentials (see rails#34789).

Thus, the credentials commands call `Rails.application.initialize!`
directly with a dummy group argument, so that initializers in the `:all`
group -- including `load_environment_config` -- are run, but not
initializers in the default group, such as
`active_record.initialize_database` and `load_config_initializers`.

Closes rails#40778.

Co-authored-by Brian Thoman <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants