Skip to content

feat: add rails credentials support #355

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from

Conversation

noxasch
Copy link

@noxasch noxasch commented Mar 8, 2024

Added rails credentials support with config flag addressing #68

@noxasch noxasch force-pushed the feat/add-rails-credentials-support branch from a24c238 to 61810ff Compare March 8, 2024 20:09
@pkuczynski pkuczynski requested review from cjlarose and BuonOmo March 9, 2024 20:52
@pkuczynski pkuczynski added this to the Next milestone Mar 9, 2024
@noxasch
Copy link
Author

noxasch commented Mar 14, 2024

I'm currently having trouble with different ruby version in the test, any clue ?

@cjlarose
Copy link
Member

Not 100% sure what's going on with the tests on CI. Tests pass for me locally. I suspect it has something to do with Rails 7.0 or 7.1 because we don't run tests for those Rails versions when running the test suite for Ruby 2.7, jruby, or truffleruby.

I merged in a change that address the deprecation warnings for fixture_path in Rails 7.1 to help clear up the test output. Hoping that will help clear things up 🤞

@noxasch noxasch force-pushed the feat/add-rails-credentials-support branch 9 times, most recently from 075d284 to ef7ccc8 Compare March 16, 2024 12:17
@noxasch
Copy link
Author

noxasch commented Mar 16, 2024

Update:

Rebased to latest master

I look around and found the solution for rails 7.1 fail test. For Rails 7.1 and above it seems we need to use ActiveSupport::EncryptedConfiguration config method instead. But prior 7.1, Rails.credentials.to_h and Rails.secret.to_h will work just fine.

All test should pass now.

@noxasch noxasch requested a review from cjlarose March 16, 2024 12:18
Copy link
Member

@cjlarose cjlarose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! Requested a few changes

lib/config.rb Outdated
@@ -48,6 +49,14 @@ def self.load_files(*sources)

config.add_source!(Sources::EnvSource.new(ENV)) if Config.use_env

if defined?(::Rails::Railtie) && Config.use_rails_credentials
if Rails.version < '7.1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the version numbers are represented as strings here, this comparison can lead to unexpected results. For example, if Rails.version is 10.0.0, then '10.0.0' < '7.1' == true

To compare version numbers correctly, I think we need to either use something like

if [Rails::VERSION::MAJOR, Rails::VERSION::MINOR] < [7, 1]

or

if Gem::Version.new(Rails.version) < Gem::Version.new('7.1')

lib/config.rb Outdated
if Rails.version < '7.1'
config.add_source!(Sources::HashSource.new(secret: Rails.application.secrets.to_h.deep_stringify_keys))
else
config.add_source!(Sources::HashSource.new(secret: Rails.application.credentials.config))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Rails.application.credentials.config returns a hash with symbol keys. I think for merging to work correctly, we need to .deep_stringify_keys here.

lib/config.rb Outdated
@@ -48,6 +49,14 @@ def self.load_files(*sources)

config.add_source!(Sources::EnvSource.new(ENV)) if Config.use_env
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's load credentials before the environment. My expectation is usually that env vars should "win" against every other configuration source.

@noxasch noxasch force-pushed the feat/add-rails-credentials-support branch 8 times, most recently from 36b06f6 to f67fadf Compare March 17, 2024 14:52
@noxasch
Copy link
Author

noxasch commented Mar 17, 2024

Update:

  1. Addressed the changes and refactor the code.
  2. Update the crendentails to include the aws patter below to be more realistic
aws:
  secret_access_key: '123456'

Apparently we don't need to check for rails version, just need to require the master_key in test environment and for rails5.2 untiil 6.1 need test.key and master.key, otherwise it somehow ignore the RAILS_MASTER_KEY in environment variable

@noxasch noxasch requested a review from cjlarose March 18, 2024 00:08
@pkuczynski pkuczynski requested a review from toncid July 8, 2025 09:08
@pkuczynski
Copy link
Member

@noxasch are you still interested in moving forward with this PR?

@noxasch
Copy link
Author

noxasch commented Jul 8, 2025

Hi @pkuczynski , sure. im happy. would you like me to take a look at the fail test ?

@pkuczynski
Copy link
Member

Hi @pkuczynski , sure. im happy. would you like me to take a look at the fail test ?

Great! It would be a shame to waste so much work you put in this already.

I succesfully fixed most of the failing tests in #371, except JRuby. It would be great if you could help me fix remaining one...

@pkuczynski pkuczynski changed the title Add rails credentials support feat: add rails credentials support Jul 9, 2025
@pkuczynski pkuczynski modified the milestones: 5.6.0, Next Jul 11, 2025
@noxasch
Copy link
Author

noxasch commented Jul 11, 2025

@pkuczynski I see all the test pass already, guess you fix all the test ?

@pkuczynski
Copy link
Member

pkuczynski commented Jul 11, 2025

Yes, was able to fix the tests thanks to @Nuzair46 help. Now you need to resolve conflicts and we should be good to go @noxasch

@pkuczynski pkuczynski requested review from fredwu and Nuzair46 and removed request for toncid July 11, 2025 20:30
@Nuzair46
Copy link
Member

Nuzair46 commented Jul 12, 2025

Doubt: Rails credentials doesn't evaluate ERB anymore in the credentials file unlike secrets.yml before. I mean doing something like this
before:

token: <%= ENV['TOKEN'] %>

With Rails credentials now

token: 'must_be_hardcoded_as_file_will_be_encrytped'

Does making it load with this gem bring back this functionality? Must be nice if it does and would be better to document it somewhere/test against it. I think it might be working as you are using add_source to load a Hash source. So as long as Rails will encrypt and decrypt the file, it should work fine

@noxasch
Copy link
Author

noxasch commented Jul 12, 2025

@Nuzair46 not sure what you mean by that. What this does it will read rails credentials merge it with existing config from your settings.yml and if you set use_env, then your setting that use ENV will take preceding over other including in the credentials.

@Nuzair46
Copy link
Member

@noxasch I was wondering if this pr enables evaluating the credentials file.
So previously without credentials in rails, we could write the file like this

api_key: '12345'
api_key_env: ENV['API_KEY'] # this will be evaluated and load the variable provided in the env

But after Rails credentials, this won't work anymore. And I have seen people wanting this feature like being able to have some env variable load from vault or other env managers. I thought this change to config gem will also make this possible, but it seems like it doesn't.
This is a test done on this branch to see if it works.

tokyo(dev)> Rails.application.credentials.api_key
=> "12345"
tokyo(dev)> Rails.application.credentials.api_key_env
=> "<%= ENV[\"API_KEY\"] %>"
tokyo(dev)> exit
╭─Red@Red ~/Miqor/Tokyo ‹main●› 
╰─$ rails c
Loading development environment (Rails 8.0.2)
tokyo(dev)> Settings.api_key
=> "12345"
tokyo(dev)> Settings.api_key_env
=> "<%= ENV[\"API_KEY\"] %>"
tokyo(dev)> exit
╭─Red@Red ~/Miqor/Tokyo ‹main●› 
╰─$ API_KEY=123 rails c
Loading development environment (Rails 8.0.2)
tokyo(dev)> Settings.api_key_env
=> "<%= ENV[\"API_KEY\"] %>"
tokyo(dev)> Settings.api_key
=> "12345"
tokyo(dev)> Rails.application.credentials.api_key_env
=> "<%= ENV[\"API_KEY\"] %>"

Nuzair46
Nuzair46 previously approved these changes Jul 13, 2025
@Nuzair46 Nuzair46 requested a review from pkuczynski July 13, 2025 04:12
@noxasch
Copy link
Author

noxasch commented Jul 13, 2025

@Nuzair46 Understand. I supposed that is how it previously work that it only change if the setting is nil, if we want to achieve this should be a different change require there.

@Nuzair46
Copy link
Member

@Nuzair46 Understand. I supposed that is how it previously work that it only change if the setting is nil, if we want to achieve this should be a different change require there.

Yea it will require us to evaluate the env and then attach it. But I think it might be out of scope for this repo.

@pkuczynski
Copy link
Member

@noxasch there is only one comment left from @toncid and if we resolve it I am happy to merge and release

@pkuczynski pkuczynski requested a review from toncid July 15, 2025 11:28
@Nuzair46 Nuzair46 self-requested a review July 15, 2025 12:07
@Nuzair46 Nuzair46 dismissed their stale review July 15, 2025 12:08

Condition in lib/config.rb should be addressed

@Nuzair46
Copy link
Member

@noxasch Can you address the review comments.

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

Successfully merging this pull request may close these issues.

6 participants