rails_multisite 2.4.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c423a9ad1f1506b7278fdad487f740236f6241a5cfced260e135cc4392f0f24d
4
- data.tar.gz: 3da1e820fedb5a8ec1c7c34c41668cf62d8891bab765f554e54186688fe27464
3
+ metadata.gz: c629d2dc8da503bfa856a639b4462108d15f776554dca35dd2d02545bc0e2793
4
+ data.tar.gz: a8199f54a9af9b8fea60e9c772d845944925499067d3ce29d362ab4ad68f13fe
5
5
  SHA512:
6
- metadata.gz: 68717b4c14ccf6586de353e1e455aa948e931a498fcb5816008f5f6655e6d8c896793ef635532cc056073f917f7ea6e7205e7f5bc9b4a5a581e574cee76cd47b
7
- data.tar.gz: 68779aedd6acb814e8401226ef67ad9636abf3ca71c954cef89226199fd754a34f78c84c5340af2bc2216f9e560d410867837a3fd3d221c53b08449ed60ceb68
6
+ metadata.gz: 14ea81350cad5b028a1d6558525d350b1dac376168249cb2fdcce148055fe7b86a53b3e4472815567f626025420dfd40156b84a0f6eb80ff714725df3a20d4bc
7
+ data.tar.gz: b68b07d983452e6b1d785f6e927450cfe761983123408fcc0e7cb4589c4be2d4855a0351964ce497a278c17c5ffcc378a57bbfb55bfdf8a4059466f438d03646
data/README.md CHANGED
@@ -95,7 +95,7 @@ RAILS_DB=db_one rails console
95
95
 
96
96
  ### CDN origin support
97
97
 
98
- To avoid needing to configure many origins you can consider using `RailsMultisite::ConnectionManagement.asset_hostname`
98
+ To avoid needing to configure many origins you can consider using `RailsMultisite::ConnectionManagement.asset_hostnames`
99
99
 
100
100
  When configured, requests to `asset_hostname`?__ws=another.host.name will be re-routed to the correct site. Cookies will
101
101
  be stripped on all incoming requests.
@@ -103,7 +103,7 @@ be stripped on all incoming requests.
103
103
  Example:
104
104
 
105
105
  - Multisite serves `sub.example.com` and `assets.example.com`
106
- - `RailsMultisite::ConnectionManagement.asset_hostname = 'assets.example.com'`
106
+ - `RailsMultisite::ConnectionManagement.asset_hostnames = ['assets.example.com']`
107
107
  - Requests to `https://assets.example.com/route/?__ws=sub.example.com` will be routed to the `sub.example.com`
108
108
 
109
109
 
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class ConnectionManagement
5
+ class ConnectionSpecification
6
+ class << self
7
+ def current
8
+ ActiveRecord::Base.connection_pool.spec
9
+ end
10
+
11
+ def db_spec_cache(configs)
12
+ resolve_configs = configs
13
+ # rails 6 needs to use a proper object for the resolver
14
+ if defined?(ActiveRecord::DatabaseConfigurations)
15
+ resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
16
+ end
17
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(resolve_configs)
18
+ configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.to_h
19
+ end
20
+
21
+ def default
22
+ ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
23
+ .new(ActiveRecord::Base.configurations)
24
+ .spec(Rails.env.to_sym)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class ConnectionManagement
5
+ class ConnectionSpecification
6
+ class << self
7
+ def current
8
+ new(ActiveRecord::Base.connection_pool.db_config)
9
+ end
10
+
11
+ def db_spec_cache(configs)
12
+ resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
13
+ configs.map { |k, _| [k, new(resolve_configs.resolve(k.to_sym))] }.to_h
14
+ end
15
+
16
+ def default
17
+ new(ActiveRecord::Base.configurations.resolve(Rails.env.to_sym))
18
+ end
19
+ end
20
+
21
+ attr_reader :spec
22
+
23
+ def initialize(spec)
24
+ @spec = spec
25
+ end
26
+
27
+ def name
28
+ spec.env_name
29
+ end
30
+
31
+ def to_hash
32
+ spec.configuration_hash
33
+ end
34
+ alias config to_hash
35
+ end
36
+ end
37
+ end
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
+ if Rails.version >= '6.1'
4
+ require 'rails_multisite/connection_management/rails_61_compat'
5
+ else
6
+ require 'rails_multisite/connection_management/rails_60_compat'
7
+ end
8
+
3
9
  module RailsMultisite
4
10
  class ConnectionManagement
5
-
6
11
  DEFAULT = 'default'
7
- SPEC_KLASS = ActiveRecord::ConnectionAdapters::ConnectionSpecification
8
12
 
9
13
  def self.default_config_filename
10
14
  File.absolute_path(Rails.root.to_s + "/config/multisite.yml")
@@ -35,12 +39,12 @@ module RailsMultisite
35
39
  end
36
40
  end
37
41
 
38
- def self.asset_hostname
39
- @asset_hostname
42
+ def self.asset_hostnames
43
+ @asset_hostnames
40
44
  end
41
45
 
42
- def self.asset_hostname=(h)
43
- @asset_hostname = h
46
+ def self.asset_hostnames=(h)
47
+ @asset_hostnames = h
44
48
  end
45
49
 
46
50
  def self.config_filename
@@ -104,16 +108,11 @@ module RailsMultisite
104
108
  end
105
109
 
106
110
  def self.current_hostname
107
- spec = @instance.connection_spec(db: self.current_db) if @instance
108
- spec ||= ActiveRecord::Base.connection_pool.spec
109
- config = spec.config
110
- config[:host_names].nil? ? config[:host] : config[:host_names].first
111
+ current_db_hostnames.first
111
112
  end
112
113
 
113
114
  def self.current_db_hostnames
114
- spec = @instance.connection_spec(db: self.current_db) if @instance
115
- spec ||= ActiveRecord::Base.connection_pool.spec
116
- config = spec.config
115
+ config = (@instance&.connection_spec(db: current_db) || ConnectionSpecification.current).config
117
116
  config[:host_names].nil? ? [config[:host]] : config[:host_names]
118
117
  end
119
118
 
@@ -121,7 +120,7 @@ module RailsMultisite
121
120
  if @instance
122
121
  @instance.connection_spec(opts)
123
122
  else
124
- ActiveRecord::Base.connection_pool.spec
123
+ ConnectionSpecification.current
125
124
  end
126
125
  end
127
126
 
@@ -170,7 +169,7 @@ module RailsMultisite
170
169
  end
171
170
 
172
171
  @db_spec_cache = {}
173
- @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
172
+ @default_spec = ConnectionSpecification.default
174
173
  @default_connection_handler = ActiveRecord::Base.connection_handler
175
174
 
176
175
  @reload_mutex = Mutex.new
@@ -181,7 +180,7 @@ module RailsMultisite
181
180
  def load_config!
182
181
  configs = YAML::load(File.open(@config_filename))
183
182
 
184
- no_prepared_statements = ActiveRecord::Base.configurations[Rails.env]["prepared_statements"] == false
183
+ no_prepared_statements = @default_spec.config[:prepared_statements] == false
185
184
 
186
185
  configs.each do |k, v|
187
186
  raise ArgumentError.new("Please do not name any db default!") if k == DEFAULT
@@ -189,17 +188,8 @@ module RailsMultisite
189
188
  v[:prepared_statements] = false if no_prepared_statements
190
189
  end
191
190
 
192
- resolve_configs = configs
193
-
194
- # rails 6 needs to use a proper object for the resolver
195
- if defined?(ActiveRecord::DatabaseConfigurations)
196
- resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
197
- end
198
-
199
- resolver = SPEC_KLASS::Resolver.new(resolve_configs)
200
-
201
191
  # Build a hash of db name => spec
202
- new_db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
192
+ new_db_spec_cache = ConnectionSpecification.db_spec_cache(configs)
203
193
  new_db_spec_cache.each do |k, v|
204
194
  # If spec already existed, use the old version
205
195
  if v&.to_hash == @db_spec_cache[k]&.to_hash
@@ -217,7 +207,7 @@ module RailsMultisite
217
207
  end
218
208
 
219
209
  # Add the default hostnames as well
220
- ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
210
+ @default_spec.config[:host_names].each do |host|
221
211
  new_host_spec_cache[host] = @default_spec
222
212
  end
223
213
 
@@ -365,7 +355,7 @@ module RailsMultisite
365
355
  end
366
356
 
367
357
  def current_db
368
- ActiveRecord::Base.connection_pool.spec.config[:db_key] || DEFAULT
358
+ ConnectionSpecification.current.config[:db_key] || DEFAULT
369
359
  end
370
360
 
371
361
  def current_hostname
@@ -380,7 +370,7 @@ module RailsMultisite
380
370
  request = Rack::Request.new(env)
381
371
 
382
372
  host =
383
- if request['__ws'] && request.host == self.class.asset_hostname
373
+ if request['__ws'] && self.class.asset_hostnames&.include?(request.host)
384
374
  request.cookies.clear
385
375
  request['__ws']
386
376
  else
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class CookieSalt
5
+ COOKIE_SALT_KEYS = [
6
+ "action_dispatch.signed_cookie_salt",
7
+ "action_dispatch.encrypted_cookie_salt",
8
+ "action_dispatch.encrypted_signed_cookie_salt",
9
+ "action_dispatch.authenticated_encrypted_cookie_salt"
10
+ ]
11
+
12
+ def self.update_cookie_salts(env:, host:)
13
+ COOKIE_SALT_KEYS.each { |key| env[key] = "#{env[key]} #{host}" }
14
+ end
15
+ end
16
+ end
@@ -22,6 +22,7 @@ module RailsMultisite
22
22
 
23
23
  ActiveRecord::Base.connection_handler.clear_active_connections!
24
24
  ConnectionManagement.establish_connection(host: host, db: db)
25
+ CookieSalt.update_cookie_salts(env: env, host: host)
25
26
  @app.call(env)
26
27
  ensure
27
28
  ActiveRecord::Base.connection_handler.clear_active_connections!
@@ -9,9 +9,14 @@ module RailsMultisite
9
9
  initializer "RailsMultisite.init" do |app|
10
10
  app.config.multisite = false
11
11
 
12
- config_file = ConnectionManagement.default_config_filename
12
+ config_file =
13
+ app.config.respond_to?(:multisite_config_path) &&
14
+ app.config.multisite_config_path.presence
15
+
16
+ config_file ||= ConnectionManagement.default_config_filename
17
+
13
18
  if File.exist?(config_file)
14
- ConnectionManagement.config_filename = ConnectionManagement.default_config_filename
19
+ ConnectionManagement.config_filename = config_file
15
20
  app.config.multisite = true
16
21
  Rails.logger.formatter = RailsMultisite::Formatter.new
17
22
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.4.0"
4
+ VERSION = "4.0.0"
5
5
  end
@@ -7,3 +7,4 @@ require 'rails_multisite/railtie'
7
7
  require 'rails_multisite/formatter'
8
8
  require 'rails_multisite/connection_management'
9
9
  require 'rails_multisite/middleware'
10
+ require 'rails_multisite/cookie_salt'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_multisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-15 00:00:00.000000000 Z
11
+ date: 2021-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -61,6 +61,9 @@ files:
61
61
  - README.md
62
62
  - lib/rails_multisite.rb
63
63
  - lib/rails_multisite/connection_management.rb
64
+ - lib/rails_multisite/connection_management/rails_60_compat.rb
65
+ - lib/rails_multisite/connection_management/rails_61_compat.rb
66
+ - lib/rails_multisite/cookie_salt.rb
64
67
  - lib/rails_multisite/formatter.rb
65
68
  - lib/rails_multisite/middleware.rb
66
69
  - lib/rails_multisite/railtie.rb
@@ -85,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
90
  requirements: []
88
- rubygems_version: 3.0.3
91
+ rubygems_version: 3.1.6
89
92
  signing_key:
90
93
  specification_version: 4
91
94
  summary: Multi tenancy support for Rails