pg_rls 0.0.2.6.5 → 0.0.2.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5f40a0bb6bad0d0b90f53d41c806c63aaafcf889151d91242ed16b6eb590babf
4
- data.tar.gz: 27c5acfd0cad8367fe6328d2dc744bbfa5b5c4b7b39c1c30d1b700cd4f4b1f0e
3
+ metadata.gz: 6783ec3471dcba6dbad4e13ab33933d5f02e4ea336956f1a88d568a0ac4f7943
4
+ data.tar.gz: 72d651c7863a7fc8de7df9441b849b58894e37833334d445df814aad402cf8e1
5
5
  SHA512:
6
- metadata.gz: d0002c973722db8b26d7ce59f2196a0bbfc2a7f771d5066c0b7d05f499ef5d36e97a828564fd0be6b1976ccc03f86f0505db0b971a49ba8f7cb78f15b46c8351
7
- data.tar.gz: f8a722d17ee0282b9e91df0c27b7ece20fff16387d7bffa80eb1b7032e1562b0c16a2ab42f3ee2171068aefc04bf0fef9a2ca116727742d1202927a520fdfeda
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
@@ -1,12 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # OVERIDE RAILS TASK
4
- PG_RLS_TASKS = ['db:grant_usage', 'db:test:grant_usage']
5
-
6
4
  Rake::TaskManager.class_eval do
7
5
  def alias_task(fq_name)
8
6
  new_name = "#{fq_name}:original"
9
- @tasks[new_name] = @tasks.delete(fq_name) unless @tasks[new_name].nil?
7
+ @tasks[new_name] = @tasks.delete(fq_name) unless @tasks[fq_name].nil?
10
8
  end
11
9
  end
12
10
 
@@ -114,12 +112,30 @@ namespace :db do
114
112
  end
115
113
  end
116
114
 
115
+ override_task create: :load_config do
116
+ admin_connection_test_db do
117
+ Rake::Task['db:test:create:original'].invoke
118
+ end
119
+ end
120
+
121
+ override_task drop: :load_config do
122
+ admin_connection_test_db do
123
+ Rake::Task['db:test:drop:original'].invoke
124
+ end
125
+ end
126
+
117
127
  override_task prepare: :load_config do
118
128
  admin_connection_test_db do
119
129
  Rake::Task['db:test:prepare:original'].invoke
120
130
  end
121
131
  end
122
132
 
133
+ override_task setup: :load_config do
134
+ admin_connection_test_db do
135
+ Rake::Task['db:test:setup:original'].invoke
136
+ end
137
+ end
138
+
123
139
  override_task purge: :load_config do
124
140
  admin_connection_test_db do
125
141
  Rake::Task['db:test:purge:original'].invoke
@@ -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.5'
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.5
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