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

Add support for rails credentials #34

Merged
merged 6 commits into from
Feb 15, 2024

Conversation

svanhesteren
Copy link
Contributor

@svanhesteren svanhesteren commented Jan 31, 2024

From rails 7.2 on the secrets mechanism will be deprecated in favor of rails credentials and from rails 7.1 on it will show warnings about it, so time to add support for it in here.

This includes updates to rails 7.1.3, ruby 3.2.2 and rspec 3.16.

Copy link

@julik julik left a comment

Choose a reason for hiding this comment

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

Good to go but we probably should make this a tiny bit less magical 😄

@@ -9,7 +9,13 @@ class Railtie < Rails::Railtie
PRIVATE_KEY_ENV_VAR = "EJSON_PRIVATE_KEY"

config.before_configuration do
secrets_files.each do |file|
secrets_or_credentials = if Rails.version.start_with?("7.2") || Dir.glob(Rails.root.join("config", "credentials.*")).any?
Copy link

Choose a reason for hiding this comment

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

What happens if both files are present? As in - do we need to force the user to have both, or to choose one or the other? can we conditionally assign both, and let credentials take over (with a deep merge, for example)? Or we could tell the user that they need to rename the file and it will get picked up automatically? because there are Rails versions which have both (like we have now)

Copy link

Choose a reason for hiding this comment

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

Also: this check is too magicall as well, as it would pick up any, say, config.bak.rb file as an indication that the application is using credentials - while it might not be the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added a more explicit check for both kinds. From rails 7.2 when secret files are deprecated we only look for credential files here. Before that credential files have prio over secrets, but secrets will still work of course.

Rails.root.join("config", "secrets.#{ext}"),
Rails.root.join("config", "secrets.#{Rails.env}.#{ext}")
Rails.root.join("config", "#{secrets_or_credentials}.#{ext}"),
Rails.root.join("config", "#{secrets_or_credentials}.#{Rails.env}.#{ext}")
]
end.flatten
Copy link

Choose a reason for hiding this comment

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

nit: likely flat_map can be used here

EYAML::SUPPORTED_EXTENSIONS.map do |ext|
[
Rails.root.join("config", "secrets.#{ext}"),
Rails.root.join("config", "secrets.#{Rails.env}.#{ext}")
Rails.root.join("config", "#{secrets_or_credentials}.#{ext}"),
Copy link

Choose a reason for hiding this comment

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

nit: maybe hint that these files will be read and merged from the least specific to the more specific one? also, should there be, say, credentials.production.eyaml and credentials.eyml the latter will overwrite everything introduced by credentials.production.eyaml even though it is more specific (specific to an environment). So to play this safe it would be better to do the product the other way around - first "envless" variants for all extensions, then "enved" variants for all the extensions. Still a bit magical but will be more logical IMO.

@@ -9,6 +9,10 @@ def secrets_class
ActiveSupport::OrderedOptions
end

def credentials_class
Copy link

Choose a reason for hiding this comment

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

we positive that on all our Rails versions the secrets/credentials thingies exposed from the config are OrderedOptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am seeing various resources online indicating that rails uses OrderedOptions for secrets/credentials dating back to rails 4 even, so I am betting on the fact that this will not cause issues. If so, I'm happy to receive an issue/pr about it. :)

So we prioritize credential files over secret files and only look for
credential files from rails 7.2 onwards.
Copy link

@julik julik left a comment

Choose a reason for hiding this comment

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

🚀

@svanhesteren svanhesteren force-pushed the add-support-for-rails-credentials branch from 07db351 to 51b3269 Compare February 14, 2024 17:26
@svanhesteren svanhesteren merged commit 053679a into main Feb 15, 2024
1 check passed
@svanhesteren svanhesteren deleted the add-support-for-rails-credentials branch February 15, 2024 10:49
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

Successfully merging this pull request may close these issues.

2 participants