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

Wrap ActiveRecord::Base with ActiveSupport.on_load #199

Merged

Conversation

nipe0324
Copy link
Contributor

@nipe0324 nipe0324 commented May 30, 2023

This PR try to fix #198

Cause

Before: Rails initialization order is NOT good.

First. Set ActiveRecord::Base.filter_attributes which is empty array.

# https://github.com/rails/rails/blob/v7.0.5/activerecord/lib/active_record/railtie.rb#L318-L322
initializer "active_record.set_filter_attributes" do
  ActiveSupport.on_load(:active_record) do
    self.filter_attributes += Rails.application.config.filter_parameters
  end
end

Second. Initialize Rails.application.config.filter_parameters

# My Rails Application
# config/initializers/filter_parameter_logging.rb
Rails.application.config.filter_parameters += [
  :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]

After: Rails initialization order is good.

First. Initialize Rails.application.config.filter_parameters

# My Rails Application
# config/initializers/filter_parameter_logging.rb
Rails.application.config.filter_parameters += [
  :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]

Second. Set ActiveRecord::Base.filter_attributes which is NOT empty array.

# https://github.com/rails/rails/blob/v7.0.5/activerecord/lib/active_record/railtie.rb#L318-L322
initializer "active_record.set_filter_attributes" do
  ActiveSupport.on_load(:active_record) do
    self.filter_attributes += Rails.application.config.filter_parameters
  end
end

Test

Use fixed gem

# Gemfile
gem 'activerecord-multi-tenant', github: 'nipe0324/activerecord-multi-tenant', branch: 'wrap_active_support_on_load'

Run bundle install. And, bin/rails c and filter_parameters works!!

Loading development environment (Rails 7.0.5)
irb(main):001:0> Rails.application.config.filter_parameters
=> [:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn]
irb(main):002:0> Customer.inspection_filter
=> 
#<ActiveSupport::ParameterFilter:0x0000000111df2188
 @filters=[:passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn],
 @mask="[FILTERED]">
irb(main):003:0> Customer.new(name: 'name', token: 'some-token')
=> #<Customer:0x0000000111e9b878 id: nil, name: "name", token: "[FILTERED]", created_at: nil, updated_at: nil>

@codecov
Copy link

codecov bot commented May 30, 2023

Codecov Report

Merging #199 (c89b21d) into master (076db75) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #199   +/-   ##
=======================================
  Coverage   82.61%   82.61%           
=======================================
  Files          14       14           
  Lines         719      719           
=======================================
  Hits          594      594           
  Misses        125      125           
Impacted Files Coverage Δ
lib/activerecord-multi-tenant/query_rewriter.rb 92.30% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@gurkanindibay
Copy link
Contributor

@nipe0324 thanks for your contribution.Could you add a test that covers this scenario?
Thanks

@nipe0324
Copy link
Contributor Author

nipe0324 commented Jun 3, 2023

OK. Please give me a few days to add a test that covers this scenario. Thank you.

@nipe0324
Copy link
Contributor Author

nipe0324 commented Jun 4, 2023

@microsoft-github-policy-service agree

@gurkanindibay
Copy link
Contributor

Hi @nipe0324

I think you need a test for filters since this fix is about filters. By adding tests for filters

  1. We will have a live documentation for filter support
  2. We will make sure that our code base will support filter in the future
  3. We can make sure that current PR fixes the issue

Thanks for your contribution

Copy link
Contributor

@gurkanindibay gurkanindibay left a comment

Choose a reason for hiding this comment

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

Unit tests are required

@gurkanindibay
Copy link
Contributor

@nipe0324 Just giving a starting point, I prepared a prompt in ChatGPT. I didn't test it but I think test should be like below
https://chat.openai.com/share/ad170578-c628-4182-bc14-b4d46a2b1648

@nipe0324 nipe0324 force-pushed the wrap_active_support_on_load branch from 02bc01a to 3b257b3 Compare June 4, 2023 12:58
@nipe0324 nipe0324 changed the title Wrap ActiveRecord::Base with ActiveSupport.on_load [wip] Wrap ActiveRecord::Base with ActiveSupport.on_load Jun 4, 2023
@nipe0324 nipe0324 force-pushed the wrap_active_support_on_load branch from 3b257b3 to 98809c7 Compare June 4, 2023 14:03
@nipe0324 nipe0324 changed the title [wip] Wrap ActiveRecord::Base with ActiveSupport.on_load Wrap ActiveRecord::Base with ActiveSupport.on_load Jun 4, 2023
require 'activerecord_multi_tenant'

MultiTenantTest::Application.config.secret_token = 'x' * 40
MultiTenantTest::Application.config.secret_key_base = 'y' * 40
MultiTenantTest::Application.config.filter_parameters = [:password]
Copy link
Contributor Author

@nipe0324 nipe0324 Jun 4, 2023

Choose a reason for hiding this comment

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

This code should execute before ActiveRecord::Base called. So, I moved to here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment explaining this necessity? Could you refer the test in the comment as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I commented.

@nipe0324
Copy link
Contributor Author

nipe0324 commented Jun 4, 2023

@gurkanindibay Thanks a lot. I try to add filter test. Could you please confirm?

require 'activerecord_multi_tenant'

MultiTenantTest::Application.config.secret_token = 'x' * 40
MultiTenantTest::Application.config.secret_key_base = 'y' * 40
MultiTenantTest::Application.config.filter_parameters = [:password]
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a comment explaining this necessity? Could you refer the test in the comment as well?

end

MultiTenantTest::Application.config.secret_token = 'x' * 40
MultiTenantTest::Application.config.secret_key_base = 'y' * 40
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This 2 lines are not used. So I removed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @nipe0324 for your contribution :)

@gurkanindibay gurkanindibay merged commit 7b6bb2f into citusdata:master Jun 5, 2023
41 checks passed
@nipe0324 nipe0324 deleted the wrap_active_support_on_load branch July 12, 2023 03:32
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.

Rails.application.config.filter_parameters does not work with ActiveRecord and activerecord-multi-tenant gem
3 participants