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

Initializer fails with ArgumentError: Missing secret_key_base for 'test_data' environment, set this string with bin/rails credentials:edit #2

Closed
mdoel opened this issue Apr 30, 2021 · 3 comments

Comments

@mdoel
Copy link

mdoel commented Apr 30, 2021

It seems that the test_data:install task wants to initialize the new test_data environment. If you are in a repo that has a config/credentials.yml.enc but you are unable to edit that with rails credentials:edit (i.e. because you don't have a config/master.key or haven't set RAILS_MASTER_KEY, the environment creation throws an exception.

@searls
Copy link
Member

searls commented Jul 6, 2021

Thanks for the report @mdoel -- sorry for taking my time in looking into this. Looks like it's still an issue as of 49768a2.

To reproduce:

$ bin/rails credentials:edit
# save the file
$ mv config/master.key someplace/else
$ bin/rake test_data:install
$ RAILS_ENV=test_data bin/rails server

Will yield this error on each request:

2021-07-06 09:03:45 -0400 Rack app ("GET /" - (127.0.0.1)): #<ArgumentError: Missing `secret_key_base` for 'test_data' environment, set this string with `bin/rails credentials:edit`>

This doesn't happen to development, indicating that it's treated differently for some reason.

@searls
Copy link
Member

searls commented Jul 6, 2021

Yeah, sure enough:

    def secret_key_base
      if Rails.env.development? || Rails.env.test?
        secrets.secret_key_base ||= generate_development_secret
      else
        validate_secret_key_base(
          ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
        )
      end
    end

@searls
Copy link
Member

searls commented Jul 6, 2021

If we were to slam this lovely private-method call into config/environments/test_data.rb, it would work:

Rails.application.secrets.secret_key_base ||= Rails.application.send(:generate_development_secret)

The number of places in which Rails hardcodes Rails.env.development? || Rails.env.test? or similar is frustrating, given its support for arbitrarily-many environments (the undocumented working assumption appears to be that all non-development/test environments are production-like)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants