pg_rls 0.0.2.6.6 → 0.0.2.6.7

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: 9d98899addad2a4073a53c1d4fde443400f610241e98d99a66374dff394b540f
4
- data.tar.gz: 1c00e5b91791beb5c2bcd6f16bcb6952a707662a4004d64f680792af6e97d06f
3
+ metadata.gz: 6783ec3471dcba6dbad4e13ab33933d5f02e4ea336956f1a88d568a0ac4f7943
4
+ data.tar.gz: 72d651c7863a7fc8de7df9441b849b58894e37833334d445df814aad402cf8e1
5
5
  SHA512:
6
- metadata.gz: 8f32e29c43be16ff844c7208cb28c45fbc0835076030bf792c0e31b7564513d8c96834a2bc3d84f4c13bd914f8855f60333f529d343bb41d24e66d73f5444e10
7
- data.tar.gz: 29f4c7a3ad8194b8a80611c07e8965b6329e1e7b5831b01e3dddde17e0c00b5630345b2d7d6e7d15decab07c7c4d9c5ba9602f88bf3745b084163a7d641f890f
6
+ metadata.gz: ef6b9d2c986f307fe286e7289fd6059a1255f029c1bc4c88d47da7547d5e2a30d1e43fa0bf7161dfbd307e78f996527f015800fc9c1ce44c80ecb2e7720f36fb
7
+ data.tar.gz: 15f5f316a234d82bbc6712ba78aaa33edc329ac7e7027d228fddd9aa6ea750a66c2585921e31b83d48e4906ae5ed36169ad57954c49e3ae52dc9158cc17dee34
@@ -26,4 +26,19 @@ PgRls.setup do |config|
26
26
  ## data structure across many project, Solo mode create a hidden tenant table
27
27
  ## which is autopopulated on each request
28
28
  # config.solo_mode = true
29
+
30
+ ##
31
+ ## After installing the PgRls gem, you can add the `PgRls::Middleware::SetResetConnection`
32
+ ## middleware to your Rails application to secure all database connections using Row Level Security.
33
+ ## To add the middleware using the `middleware.use` method, add the following
34
+ ## lines to your `config/application.rb` file:
35
+ ## require 'pg_rls/middleware/set_reset_connection'
36
+ ## config.middleware.use PgRls::Middleware::SetResetConnection
37
+ ## Note: Be sure to add the `PgRls::Middleware::SetResetConnection` middleware after any
38
+ ## middleware that sets up the Rails session, since the RLS middleware depends
39
+ ## on the presence of the session to work correctly.
40
+ ## Additionally, you will need to manually require the `PgRls::Middleware::SetResetConnection`
41
+ ## file in your application, as shown in the first line of the code snippet above.
42
+ # config.session_key = '_hub_sessions'
43
+ # config.session_prefix = '_session_id:2::'
29
44
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgRls
4
+ module Middleware
5
+ # Set RLS if sessions present.
6
+ class SetResetConnection
7
+ def initialize(app)
8
+ @app = app
9
+ end
10
+
11
+ def call(env)
12
+ tenant = load_tenant_thought_session(env)
13
+
14
+ return @app.call(env) if tenant.blank?
15
+
16
+ PgRls::Tenant.with_tenant(tenant) { @app.call(env) }
17
+ rescue ActiveRecord::RecordNotFound => e
18
+ raise e unless rails_active_storage_request?(env)
19
+
20
+ [404, { 'Content-Type' => 'text/plain' }, ['Could not find asset']]
21
+ end
22
+
23
+ def load_session_cookie_value(env)
24
+ cookie_string = env['HTTP_COOKIE']
25
+ return if cookie_string.nil?
26
+
27
+ cookie_regex = /#{PgRls.session_key}=([^;]+)/
28
+ match = cookie_regex.match(cookie_string)
29
+ match[1] if match
30
+ end
31
+
32
+ def load_tenant_thought_session(env)
33
+ cookie = load_session_cookie_value(env)
34
+
35
+ return if cookie.blank?
36
+
37
+ sessions = Rails.cache.read("#{PgRls.session_prefix}#{Digest::SHA256.hexdigest(cookie)}")
38
+ sessions['_tenant']
39
+ end
40
+
41
+ def rails_active_storage_request?(env)
42
+ env['PATH_INFO'].start_with?('/rails/active_storage/')
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/pg_rls/tenant.rb CHANGED
@@ -25,9 +25,9 @@ module PgRls
25
25
  raise e
26
26
  end
27
27
 
28
- def find_each(&block)
28
+ def find_each(&)
29
29
  PgRls.main_model.find_each do |tenant|
30
- with_tenant(tenant, &block)
30
+ with_tenant(tenant, &)
31
31
  end
32
32
  end
33
33
 
@@ -41,12 +41,12 @@ module PgRls
41
41
 
42
42
  def fetch
43
43
  fetch!
44
- rescue ActiveRecord::StatementInvalid
45
- 'no tenant is selected'
44
+ rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound
45
+ nil
46
46
  end
47
47
 
48
48
  def fetch!
49
- @fetch ||= PgRls.main_model.find_by!(
49
+ @tenant ||= PgRls.main_model.find_by!(
50
50
  tenant_id: PgRls.connection_class.connection.execute(
51
51
  "SELECT current_setting('rls.tenant_id')"
52
52
  ).getvalue(0, 0)
@@ -63,7 +63,6 @@ module PgRls
63
63
  end
64
64
 
65
65
  def reset_rls!
66
- @fetch = nil
67
66
  @tenant = nil
68
67
  PgRls.connection_class.connection.execute('RESET rls.tenant_id')
69
68
  end
@@ -80,7 +79,7 @@ module PgRls
80
79
 
81
80
  connection_adapter.connection.transaction do
82
81
  connection_adapter.connection.execute(format('SET rls.tenant_id = %s',
83
- connection_adapter.connection.quote(tenant.tenant_id)))
82
+ connection_adapter.connection.quote(tenant.tenant_id)))
84
83
  end
85
84
 
86
85
  tenant
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PgRls
4
- VERSION = '0.0.2.6.6'
4
+ VERSION = '0.0.2.6.7'
5
5
  end
data/lib/pg_rls.rb CHANGED
@@ -148,6 +148,12 @@ module PgRls
148
148
  mattr_accessor :test_inline_tenant
149
149
  @@test_inline_tenant = false
150
150
 
151
+ mattr_accessor :session_key
152
+ @@session_key = '_hub_sessions'
153
+
154
+ mattr_accessor :session_prefix
155
+ @@session_prefix = '_session_id:2::'
156
+
151
157
  mattr_accessor :search_methods
152
158
  @@search_methods = %i[subdomain id tenant_id]
153
159
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_rls
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.6.6
4
+ version: 0.0.2.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Laloush
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-02-17 00:00:00.000000000 Z
11
+ date: 2023-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -65,6 +65,7 @@ files:
65
65
  - lib/pg_rls/database/tasks/admin_database.rake
66
66
  - lib/pg_rls/errors/tenant_not_found.rb
67
67
  - lib/pg_rls/middleware.rb
68
+ - lib/pg_rls/middleware/set_reset_connection.rb
68
69
  - lib/pg_rls/middleware/sidekiq.rb
69
70
  - lib/pg_rls/middleware/sidekiq/client.rb
70
71
  - lib/pg_rls/middleware/sidekiq/server.rb