-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set secret_key_base early enough for rails processes
We don't use secret_key_base like vanilla rails. We implemented secrets through MiqPassword before any of this existed in rails. Our only things currently going through rails secrets perhaps is the provider vcr cassette information. Rails 7.1 asserts secret_key_base is now set and set during rails boot fairly early. So, now, we can't only set this for puma based workers, but all rails processes. We also, must move this logic to be done earlier during boot. It was found that after eager load allows us to leverage autoload and access the database, both are required in order to fetch the secrets from the database. Move over the logic for rails console so it can use a dummy secret key base. Move the tests to a vmdb/initializer test. Co-Authored-By: Jason Frey <fryguy9@gmail.com>
- Loading branch information
Showing
8 changed files
with
56 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
RSpec.describe Vmdb::Initializer do | ||
describe ".init_secret_token" do | ||
before do | ||
@token = Rails.application.secret_key_base | ||
end | ||
|
||
after do | ||
Rails.application.config.secret_key_base = @token | ||
Rails.application.secrets = nil | ||
end | ||
|
||
it "defaults to MiqDatabase session_secret_token" do | ||
MiqDatabase.seed | ||
Rails.application.config.secret_key_base = nil | ||
Rails.application.secrets = nil | ||
|
||
described_class.init_secret_token | ||
expect(Rails.application.secret_key_base).to eq(MiqDatabase.first.session_secret_token) | ||
expect(Rails.application.config.secret_key_base).to eq(MiqDatabase.first.session_secret_token) | ||
end | ||
|
||
it "uses random hex when MiqDatabase isn't seeded" do | ||
Rails.application.config.secret_key_base = nil | ||
Rails.application.secrets = nil | ||
|
||
described_class.init_secret_token | ||
expect(Rails.application.secret_key_base).to match(/^\h{128}$/) | ||
expect(Rails.application.config.secret_key_base).to match(/^\h{128}$/) | ||
end | ||
|
||
it "does not reset secrets when token already configured" do | ||
existing_value = SecureRandom.hex(64) | ||
Rails.application.config.secret_key_base = existing_value | ||
Rails.application.secrets = nil | ||
|
||
described_class.init_secret_token | ||
expect(Rails.application.secret_key_base).to eq(existing_value) | ||
expect(Rails.application.config.secret_key_base).to eq(existing_value) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters