Skip to content

deep symbolize keys on secrets.yml#26929

Merged
guilleiguaran merged 1 commit into
rails:masterfrom
elorest:is_deep_symbolize_secrets
Nov 21, 2016
Merged

deep symbolize keys on secrets.yml#26929
guilleiguaran merged 1 commit into
rails:masterfrom
elorest:is_deep_symbolize_secrets

Conversation

@elorest

@elorest elorest commented Oct 29, 2016

Copy link
Copy Markdown
Contributor

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
Copy Markdown

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

sgrif commented Nov 1, 2016

Copy link
Copy Markdown
Contributor

Can you add a test case for this?

@elorest

elorest commented Nov 1, 2016

Copy link
Copy Markdown
Contributor Author

@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

sgrif commented Nov 1, 2016

Copy link
Copy Markdown
Contributor

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

@elorest

elorest commented Nov 1, 2016

Copy link
Copy Markdown
Contributor Author

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

@elorest

elorest commented Nov 2, 2016

Copy link
Copy Markdown
Contributor Author

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.

@rafaelfranca rafaelfranca left a comment

Copy link
Copy Markdown
Member

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

elorest commented Nov 21, 2016

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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

elorest commented Nov 21, 2016

Copy link
Copy Markdown
Contributor Author

@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

elorest commented Nov 21, 2016

Copy link
Copy Markdown
Contributor Author

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

@guilleiguaran

Copy link
Copy Markdown
Member

Failures in Travis aren't related to this PR

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

Copy link
Copy Markdown
Member

Thank you very much for this 👏

@kirs

kirs commented Nov 22, 2016

Copy link
Copy Markdown
Member

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
Copy Markdown
Member

@kirs: yes, absolutely makes sense

@rafaelfranca

Copy link
Copy Markdown
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

elorest commented Nov 22, 2016

Copy link
Copy Markdown
Contributor Author

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

printercu commented Apr 21, 2017

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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