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

deep symbolize keys on secrets.yml #26929

Merged
merged 1 commit into from
Nov 21, 2016

Conversation

elorest
Copy link
Contributor

@elorest elorest commented Oct 29, 2016

Summary

Changed symbolize_keys to deep_symbolize_keys where secrets.yml is loaded in. This allows secret files to contain more complex information without all child keys being strings while parent keys are symbols.

{:smtp_settings=>{"address"=>"smtp.mailgun.org", "port"=>587, "domain"=>"example.com", "authentication"=>"plain", "enable_starttls_auto"=>true, "user_name"=>"postmaster@mailgun.example.com", "password"=>"12d3f673e0a83c97045eb4e2d10ebc8a4"}}

becomes this

{:smtp_settings=>{:address=>"smtp.mailgun.org", :port=>587, :domain=>"example.com", :authentication=>"plain", :enable_starttls_auto=>true, :user_name=>"postmaster@mailgun.example.com", :password=>"12d3f673e0a83c97045eb4e2d10ebc8a4"}}

Source yaml.

development:
  smtp_settings:
    address: "smtp.mailgun.org"
    port: 587
    domain: "example.com"
    authentication: "plain"
    enable_starttls_auto: true
    user_name: "postmaster@mailgun.example.com"
    password: "12d3f673e0a83c97045eb4e2d10ebc8a4"

Benchmarks

To do benchmarks I wrote a rake task which loaded the environment, printed the secrets and exited.

With symbolize_keys:
2.36 real 1.94 user 0.55 sys
2.38 real 1.93 user 0.51 sys

With deep_symbolize_keys:
2.37 real 1.91 user 0.51 sys
2.31 real 1.89 user 0.49 sys

As you can see there doesn't seem to be a negative effect in load time for this change. I wasn't able to find where the secrets.yml loading was being tested, but if someone could point that out to me I'll add tests as well.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @sgrif (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@sgrif
Copy link
Contributor

sgrif commented Nov 1, 2016

Can you add a test case for this?

@elorest
Copy link
Contributor Author

elorest commented Nov 1, 2016

@sgrif I would love too. Could you point me to where tests for secret.yml loading are currently being done? Would this be the correct location? https://github.com/rails/rails/blob/master/railties/test/application/configuration_test.rb#L446

@sgrif
Copy link
Contributor

sgrif commented Nov 1, 2016

Yes, those tests which are creating .secrets.yml and asserting the result seems like a fine place.

@elorest
Copy link
Contributor Author

elorest commented Nov 1, 2016

Thanks. I'll add those after after work today.

@elorest
Copy link
Contributor Author

elorest commented Nov 2, 2016

Tests have been added. Other than just consistency I feel deep serializing works better because it allows you to setup configurations with many options by just passing the hash in. For instance
config.action_mailer.smtp_settings = Rails.application.secrets.smtp_settings in application.rb only works if keys are symbols.

Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Could you add a CHANGELOG entry and squash your commits? Even that is small it is a breaking change.

@elorest elorest force-pushed the is_deep_symbolize_secrets branch 2 times, most recently from 9f78466 to 0066f57 Compare November 21, 2016 18:12
@elorest
Copy link
Contributor Author

elorest commented Nov 21, 2016

@maclover7 I did as you requested but got an conflict on CHANGELOG.md so I merged in master but can't seem to squash the merge. Also weirdly after I merged in master, travis checks aren't passing... Any suggestions?

@maclover7
Copy link
Contributor

@elorest hmm, do you have the latest master locally? I believe you should be able to squash down to one commit via git rebase -i master

@elorest elorest force-pushed the is_deep_symbolize_secrets branch from c1f3fad to 7102c6c Compare November 21, 2016 23:13
@elorest
Copy link
Contributor Author

elorest commented Nov 21, 2016

@maclover7 I created this pull request from my branch is_deep_symbolize_keys. rebase -i isn't working like normal. It brings up thousands of commits. So I just switch back to master, cherry-picked my commit and then force pushed it back to my is_deep_symbolize_secrets on github. Should be good now.

@elorest
Copy link
Contributor Author

elorest commented Nov 21, 2016

@maclover7 Any idea why travis doesn't pass since I merged in master?

@guilleiguaran
Copy link
Member

Failures in Travis aren't related to this PR

@guilleiguaran guilleiguaran merged commit 6cd6586 into rails:master Nov 21, 2016
@guilleiguaran
Copy link
Member

Thank you very much for this 👏

@kirs
Copy link
Member

kirs commented Nov 22, 2016

Does this preserve backward compatibility if I usedapp.secrets.smtp_settings['password']? Looks like it doesn't 😬
Maybe it's worth using HashWithIndifferentAccess here?

@guilleiguaran
Copy link
Member

@kirs: yes, absolutely makes sense

@rafaelfranca
Copy link
Member

I don't think it is needed. Like I said in the comment above it is fine to have this small backwards incompatible change as it is in the CHANGELOG.

@elorest
Copy link
Contributor Author

elorest commented Nov 22, 2016

I considered doing that, but using HashWithIndifferentAccess did slow down my benchmark of loading and printing by a small amount. @kirs Default smtp settings for rails expect symbols when you mass assign them so config.smtp_settings = app.secrets.smtp_settings is now possible.

@printercu
Copy link
Contributor

printercu commented Apr 21, 2017

@elorest @rafaelfranca This change breaks some gems. There is much more_indifferent access_ on every request when accessing params, don't think this would be a problem to make secrets work the same way.

@rafaelfranca
Copy link
Member

Added a note in the upgrade guide 0541a0d

rafaelfranca added a commit that referenced this pull request Apr 21, 2017
This is a small breaking change that we chose to make in 5.1 since the
fix can be done with a search and replace tool.
rafaelfranca added a commit that referenced this pull request Apr 21, 2017
This is a small breaking change that we chose to make in 5.1 since the
fix can be done with a search and replace tool.
abicky added a commit to abicky/yaml_vault that referenced this pull request Dec 15, 2017
In Rails 5.1, nested keys are also symbolized
cf. rails/rails#26929
abicky added a commit to abicky/yaml_vault that referenced this pull request Dec 15, 2017
In Rails 5.1, nested keys are also symbolized
cf. rails/rails#26929
abicky added a commit to abicky/yaml_vault that referenced this pull request Dec 15, 2017
In Rails 5.1, nested keys are also symbolized
cf. rails/rails#26929
abicky added a commit to abicky/yaml_vault that referenced this pull request Dec 15, 2017
In Rails 5.1, nested keys are also symbolized
cf. rails/rails#26929
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.

8 participants