pg_rls 0.0.1.alpha
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +5 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +182 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/pg_rls/schema/down_statements.rb +48 -0
- data/lib/pg_rls/schema/statements.rb +44 -0
- data/lib/pg_rls/schema/up_statements.rb +85 -0
- data/lib/pg_rls/tenant/tenant.rb +23 -0
- data/lib/pg_rls/version.rb +5 -0
- data/lib/pg_rls.rb +55 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5ed56d6024c7658ebdf54879615273e29b9337f178b27a56352d2b3ba831b53d
|
4
|
+
data.tar.gz: 10ed3f8ae3350ef016a0138fcee0dfaa875c93f399ec3bca3a258f3f6f8d1639
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7bc62b85e6065bcfd687de04809efffd295a47b66f9ef72afa8f236a7b2750888ebc55df8e8b371c674a879d18204338328c5684835ca6fb09bb2d6456985302
|
7
|
+
data.tar.gz: 28ab96d49303876ed85573439140695db0adf475f4b2f2253d99c5c99f6b2069eda679c10b87c9275bd7e40b8cd2c4981cf5e6614b61cb8417f9e9ec431b7c23
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at daniel.laloush@influitive.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pg_rls (0.0.1.alpha)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actioncable (6.1.4.1)
|
10
|
+
actionpack (= 6.1.4.1)
|
11
|
+
activesupport (= 6.1.4.1)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailbox (6.1.4.1)
|
15
|
+
actionpack (= 6.1.4.1)
|
16
|
+
activejob (= 6.1.4.1)
|
17
|
+
activerecord (= 6.1.4.1)
|
18
|
+
activestorage (= 6.1.4.1)
|
19
|
+
activesupport (= 6.1.4.1)
|
20
|
+
mail (>= 2.7.1)
|
21
|
+
actionmailer (6.1.4.1)
|
22
|
+
actionpack (= 6.1.4.1)
|
23
|
+
actionview (= 6.1.4.1)
|
24
|
+
activejob (= 6.1.4.1)
|
25
|
+
activesupport (= 6.1.4.1)
|
26
|
+
mail (~> 2.5, >= 2.5.4)
|
27
|
+
rails-dom-testing (~> 2.0)
|
28
|
+
actionpack (6.1.4.1)
|
29
|
+
actionview (= 6.1.4.1)
|
30
|
+
activesupport (= 6.1.4.1)
|
31
|
+
rack (~> 2.0, >= 2.0.9)
|
32
|
+
rack-test (>= 0.6.3)
|
33
|
+
rails-dom-testing (~> 2.0)
|
34
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
35
|
+
actiontext (6.1.4.1)
|
36
|
+
actionpack (= 6.1.4.1)
|
37
|
+
activerecord (= 6.1.4.1)
|
38
|
+
activestorage (= 6.1.4.1)
|
39
|
+
activesupport (= 6.1.4.1)
|
40
|
+
nokogiri (>= 1.8.5)
|
41
|
+
actionview (6.1.4.1)
|
42
|
+
activesupport (= 6.1.4.1)
|
43
|
+
builder (~> 3.1)
|
44
|
+
erubi (~> 1.4)
|
45
|
+
rails-dom-testing (~> 2.0)
|
46
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
47
|
+
activejob (6.1.4.1)
|
48
|
+
activesupport (= 6.1.4.1)
|
49
|
+
globalid (>= 0.3.6)
|
50
|
+
activemodel (6.1.4.1)
|
51
|
+
activesupport (= 6.1.4.1)
|
52
|
+
activerecord (6.1.4.1)
|
53
|
+
activemodel (= 6.1.4.1)
|
54
|
+
activesupport (= 6.1.4.1)
|
55
|
+
activestorage (6.1.4.1)
|
56
|
+
actionpack (= 6.1.4.1)
|
57
|
+
activejob (= 6.1.4.1)
|
58
|
+
activerecord (= 6.1.4.1)
|
59
|
+
activesupport (= 6.1.4.1)
|
60
|
+
marcel (~> 1.0.0)
|
61
|
+
mini_mime (>= 1.1.0)
|
62
|
+
activesupport (6.1.4.1)
|
63
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
64
|
+
i18n (>= 1.6, < 2)
|
65
|
+
minitest (>= 5.1)
|
66
|
+
tzinfo (~> 2.0)
|
67
|
+
zeitwerk (~> 2.3)
|
68
|
+
ast (2.4.2)
|
69
|
+
builder (3.2.4)
|
70
|
+
concurrent-ruby (1.1.9)
|
71
|
+
crass (1.0.6)
|
72
|
+
diff-lcs (1.4.4)
|
73
|
+
erubi (1.10.0)
|
74
|
+
globalid (0.5.2)
|
75
|
+
activesupport (>= 5.0)
|
76
|
+
i18n (1.8.10)
|
77
|
+
concurrent-ruby (~> 1.0)
|
78
|
+
loofah (2.12.0)
|
79
|
+
crass (~> 1.0.2)
|
80
|
+
nokogiri (>= 1.5.9)
|
81
|
+
mail (2.7.1)
|
82
|
+
mini_mime (>= 0.1.1)
|
83
|
+
marcel (1.0.2)
|
84
|
+
method_source (1.0.0)
|
85
|
+
mini_mime (1.1.1)
|
86
|
+
minitest (5.14.4)
|
87
|
+
nio4r (2.5.8)
|
88
|
+
nokogiri (1.12.5-x86_64-linux)
|
89
|
+
racc (~> 1.4)
|
90
|
+
parallel (1.21.0)
|
91
|
+
parser (3.0.2.0)
|
92
|
+
ast (~> 2.4.1)
|
93
|
+
racc (1.5.2)
|
94
|
+
rack (2.2.3)
|
95
|
+
rack-test (1.1.0)
|
96
|
+
rack (>= 1.0, < 3)
|
97
|
+
rails (6.1.4.1)
|
98
|
+
actioncable (= 6.1.4.1)
|
99
|
+
actionmailbox (= 6.1.4.1)
|
100
|
+
actionmailer (= 6.1.4.1)
|
101
|
+
actionpack (= 6.1.4.1)
|
102
|
+
actiontext (= 6.1.4.1)
|
103
|
+
actionview (= 6.1.4.1)
|
104
|
+
activejob (= 6.1.4.1)
|
105
|
+
activemodel (= 6.1.4.1)
|
106
|
+
activerecord (= 6.1.4.1)
|
107
|
+
activestorage (= 6.1.4.1)
|
108
|
+
activesupport (= 6.1.4.1)
|
109
|
+
bundler (>= 1.15.0)
|
110
|
+
railties (= 6.1.4.1)
|
111
|
+
sprockets-rails (>= 2.0.0)
|
112
|
+
rails-dom-testing (2.0.3)
|
113
|
+
activesupport (>= 4.2.0)
|
114
|
+
nokogiri (>= 1.6)
|
115
|
+
rails-html-sanitizer (1.4.2)
|
116
|
+
loofah (~> 2.3)
|
117
|
+
railties (6.1.4.1)
|
118
|
+
actionpack (= 6.1.4.1)
|
119
|
+
activesupport (= 6.1.4.1)
|
120
|
+
method_source
|
121
|
+
rake (>= 0.13)
|
122
|
+
thor (~> 1.0)
|
123
|
+
rainbow (3.0.0)
|
124
|
+
rake (13.0.6)
|
125
|
+
regexp_parser (2.1.1)
|
126
|
+
rexml (3.2.5)
|
127
|
+
rspec (3.10.0)
|
128
|
+
rspec-core (~> 3.10.0)
|
129
|
+
rspec-expectations (~> 3.10.0)
|
130
|
+
rspec-mocks (~> 3.10.0)
|
131
|
+
rspec-core (3.10.1)
|
132
|
+
rspec-support (~> 3.10.0)
|
133
|
+
rspec-expectations (3.10.1)
|
134
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
135
|
+
rspec-support (~> 3.10.0)
|
136
|
+
rspec-mocks (3.10.2)
|
137
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
138
|
+
rspec-support (~> 3.10.0)
|
139
|
+
rspec-support (3.10.2)
|
140
|
+
rubocop (1.22.1)
|
141
|
+
parallel (~> 1.10)
|
142
|
+
parser (>= 3.0.0.0)
|
143
|
+
rainbow (>= 2.2.2, < 4.0)
|
144
|
+
regexp_parser (>= 1.8, < 3.0)
|
145
|
+
rexml
|
146
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
147
|
+
ruby-progressbar (~> 1.7)
|
148
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
149
|
+
rubocop-ast (1.12.0)
|
150
|
+
parser (>= 3.0.1.1)
|
151
|
+
ruby-progressbar (1.11.0)
|
152
|
+
sprockets (4.0.2)
|
153
|
+
concurrent-ruby (~> 1.0)
|
154
|
+
rack (> 1, < 3)
|
155
|
+
sprockets-rails (3.2.2)
|
156
|
+
actionpack (>= 4.0)
|
157
|
+
activesupport (>= 4.0)
|
158
|
+
sprockets (>= 3.0.0)
|
159
|
+
thor (1.1.0)
|
160
|
+
tzinfo (2.0.4)
|
161
|
+
concurrent-ruby (~> 1.0)
|
162
|
+
unicode-display_width (2.1.0)
|
163
|
+
websocket-driver (0.7.5)
|
164
|
+
websocket-extensions (>= 0.1.0)
|
165
|
+
websocket-extensions (0.1.5)
|
166
|
+
zeitwerk (2.4.2)
|
167
|
+
|
168
|
+
PLATFORMS
|
169
|
+
x86_64-linux
|
170
|
+
|
171
|
+
DEPENDENCIES
|
172
|
+
pg_rls!
|
173
|
+
rails (~> 6.1.4, >= 6.1.4.1)
|
174
|
+
rake
|
175
|
+
rspec
|
176
|
+
rubocop
|
177
|
+
|
178
|
+
RUBY VERSION
|
179
|
+
ruby 3.0.2p107
|
180
|
+
|
181
|
+
BUNDLED WITH
|
182
|
+
2.2.27
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Daniel Laloush
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# PgRls
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pg_rls`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'pg_rls'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pg_rls
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pg_rls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/pg_rls/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the PgRls project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pg_rls/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'pg_rls'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PgRls
|
4
|
+
module Schema
|
5
|
+
# Down Schema Statements
|
6
|
+
module DownStatements
|
7
|
+
def drop_rls_user(name = :app_user)
|
8
|
+
ActiveRecord::Migration.execute "DROP USER #{name};"
|
9
|
+
end
|
10
|
+
|
11
|
+
def drop_rls_blocking_function
|
12
|
+
ActiveRecord::Migration.execute 'DROP FUNCTION IF EXISTS id_safe_guard ();'
|
13
|
+
end
|
14
|
+
|
15
|
+
def drop_rls_setter_function
|
16
|
+
ActiveRecord::Migration.execute 'DROP FUNCTION IF EXISTS tenant_id_setter ();'
|
17
|
+
end
|
18
|
+
|
19
|
+
def detach_blocking_function(table_name)
|
20
|
+
ActiveRecord::Migration.execute <<-SQL
|
21
|
+
DROP TRIGGER IF EXISTS id_safe_guard
|
22
|
+
ON #{table_name};
|
23
|
+
SQL
|
24
|
+
end
|
25
|
+
|
26
|
+
def detach_trigger_function(table_name)
|
27
|
+
ActiveRecord::Migration.execute <<-SQL
|
28
|
+
DROP TRIGGER IF EXISTS tenant_id_setter
|
29
|
+
ON #{table_name};
|
30
|
+
SQL
|
31
|
+
end
|
32
|
+
|
33
|
+
def drop_rls_column(table_name)
|
34
|
+
ActiveRecord::Migration.execute <<-SQL
|
35
|
+
ALTER TABLE #{table_name}
|
36
|
+
DROP COLUMN IF EXISTS tenant_id;
|
37
|
+
SQL
|
38
|
+
end
|
39
|
+
|
40
|
+
def drop_rls_policy(table_name, user = :app_user)
|
41
|
+
ActiveRecord::Migration.execute <<-SQL
|
42
|
+
DROP POLICY #{table_name}_#{user} ON #{table_name};
|
43
|
+
ALTER TABLE #{table_name} DISABLE ROW LEVEL SECURITY;
|
44
|
+
SQL
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'down_statements'
|
4
|
+
require_relative 'up_statements'
|
5
|
+
|
6
|
+
module PgRls
|
7
|
+
module Schema
|
8
|
+
# Schema Statements
|
9
|
+
module Statements
|
10
|
+
include UpStatements
|
11
|
+
include DownStatements
|
12
|
+
|
13
|
+
def create_rls_tenant_table(table_name, **options, &block)
|
14
|
+
create_rls_user(password: PgRls.database_configuration['password'])
|
15
|
+
create_rls_setter_function
|
16
|
+
create_rls_blocking_function
|
17
|
+
create_table(table_name, **options, &block)
|
18
|
+
add_rls_column_to_tenant_table(table_name)
|
19
|
+
append_blocking_function(table_name)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_rls_table(table_name, **options, &block)
|
23
|
+
create_table(table_name, **options, &block)
|
24
|
+
add_rls_column(table_name)
|
25
|
+
create_rls_policy(table_name)
|
26
|
+
append_trigger_function(table_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def drop_rls_tenant_table(table_name)
|
30
|
+
drop_rls_user
|
31
|
+
drop_rls_setter_function
|
32
|
+
detach_blocking_function(table_name)
|
33
|
+
drop_table(table_name)
|
34
|
+
drop_rls_blocking_function
|
35
|
+
end
|
36
|
+
|
37
|
+
def drop_rls_table(table_name)
|
38
|
+
detach_trigger_function(table_name)
|
39
|
+
drop_rls_policy(table_name)
|
40
|
+
drop_table(table_name)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PgRls
|
4
|
+
module Schema
|
5
|
+
# Up Schema Statements
|
6
|
+
module UpStatements
|
7
|
+
def create_rls_user(name: :app_user, password: 'password')
|
8
|
+
PgRls.execute <<-SQL
|
9
|
+
DROP ROLE IF EXISTS #{name};
|
10
|
+
CREATE USER #{name} WITH PASSWORD '#{password}';
|
11
|
+
GRANT ALL PRIVILEGES ON TABLE schema_migrations TO #{name};
|
12
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA public
|
13
|
+
GRANT SELECT, INSERT, UPDATE, DELETE
|
14
|
+
ON TABLES TO #{name};
|
15
|
+
SQL
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_rls_blocking_function
|
19
|
+
ActiveRecord::Migration.execute <<-SQL
|
20
|
+
CREATE OR REPLACE FUNCTION id_safe_guard ()
|
21
|
+
RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
22
|
+
BEGIN
|
23
|
+
RAISE EXCEPTION 'This column is guarded due to tenancy dependency';
|
24
|
+
END $$;
|
25
|
+
SQL
|
26
|
+
end
|
27
|
+
|
28
|
+
def create_rls_setter_function
|
29
|
+
ActiveRecord::Migration.execute <<-SQL
|
30
|
+
CREATE OR REPLACE FUNCTION tenant_id_setter ()
|
31
|
+
RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
32
|
+
BEGIN
|
33
|
+
new.tenant_id:= (current_setting('rls.tenant_id'));
|
34
|
+
RETURN new;
|
35
|
+
END $$;
|
36
|
+
SQL
|
37
|
+
end
|
38
|
+
|
39
|
+
def append_blocking_function(table_name)
|
40
|
+
ActiveRecord::Migration.execute <<-SQL
|
41
|
+
CREATE TRIGGER id_safe_guard
|
42
|
+
BEFORE UPDATE OF id ON #{table_name}
|
43
|
+
FOR EACH ROW EXECUTE PROCEDURE id_safe_guard();
|
44
|
+
SQL
|
45
|
+
end
|
46
|
+
|
47
|
+
def append_trigger_function(table_name)
|
48
|
+
ActiveRecord::Migration.execute <<-SQL
|
49
|
+
CREATE TRIGGER tenant_id_setter
|
50
|
+
BEFORE INSERT OR UPDATE ON #{table_name}
|
51
|
+
FOR EACH ROW EXECUTE PROCEDURE tenant_id_setter();
|
52
|
+
SQL
|
53
|
+
end
|
54
|
+
|
55
|
+
def add_rls_column_to_tenant_table(table_name)
|
56
|
+
ActiveRecord::Migration.execute <<-SQL
|
57
|
+
ALTER TABLE #{table_name}
|
58
|
+
ADD COLUMN IF NOT EXISTS
|
59
|
+
tenant_id uuid UNIQUE DEFAULT gen_random_uuid();
|
60
|
+
SQL
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_rls_column(table_name)
|
64
|
+
ActiveRecord::Migration.execute <<-SQL
|
65
|
+
ALTER TABLE #{table_name}
|
66
|
+
ADD COLUMN IF NOT EXISTS tenant_id uuid,
|
67
|
+
ADD CONSTRAINT fk_companies
|
68
|
+
FOREIGN KEY (tenant_id)
|
69
|
+
REFERENCES companies(tenant_id)
|
70
|
+
ON DELETE CASCADE;
|
71
|
+
SQL
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_rls_policy(table_name, user = :app_user)
|
75
|
+
ActiveRecord::Migration.execute <<-SQL
|
76
|
+
ALTER TABLE #{table_name} ENABLE ROW LEVEL SECURITY;
|
77
|
+
CREATE POLICY #{table_name}_#{user}
|
78
|
+
ON #{table_name}
|
79
|
+
TO #{user}
|
80
|
+
USING (tenant_id = NULLIF(current_setting('rls.tenant_id', TRUE), '')::uuid);
|
81
|
+
SQL
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PgRls
|
4
|
+
# Tenant Controller
|
5
|
+
module Tenant
|
6
|
+
class << self
|
7
|
+
SET_COMPANY_ID_SQL = 'SET rls.tenant_id = %s'
|
8
|
+
def switch(resource)
|
9
|
+
connection_adapter = PgRls.establish_new_connection
|
10
|
+
tenant = tenant_by_subdomain_uuid_or_tenant_id(resource)
|
11
|
+
connection_adapter.connection.execute(format('SET rls.tenant_id = %s', connection_adapter.connection.quote(tenant.tenant_id)))
|
12
|
+
puts "connected to #{resource}"
|
13
|
+
rescue StandardError => e
|
14
|
+
puts 'connection was not made'
|
15
|
+
puts e
|
16
|
+
end
|
17
|
+
|
18
|
+
def tenant_by_subdomain_uuid_or_tenant_id(resource)
|
19
|
+
Company.find_by_subdomain(resource) || Company.find_by_id(resource) || Company.find_by_tenant_id(resource)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/pg_rls.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
require 'forwardable'
|
5
|
+
require_relative 'pg_rls/version'
|
6
|
+
require_relative 'pg_rls/schema/statements'
|
7
|
+
require_relative 'pg_rls/tenant/tenant'
|
8
|
+
|
9
|
+
# PostgreSQL Row Level Security
|
10
|
+
module PgRls
|
11
|
+
class Error < StandardError; end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
extend Forwardable
|
15
|
+
|
16
|
+
WRITER_METHODS = %i[].freeze
|
17
|
+
READER_METHODS = %i[connection_class database_configuration execute].freeze
|
18
|
+
DELEGATORS_METHODS = %i[connection_class database_configuration execute].freeze
|
19
|
+
|
20
|
+
attr_writer(*WRITER_METHODS)
|
21
|
+
attr_reader(*READER_METHODS)
|
22
|
+
|
23
|
+
def_delegators(*DELEGATORS_METHODS)
|
24
|
+
# Your code goes here...
|
25
|
+
def setup
|
26
|
+
yield self
|
27
|
+
end
|
28
|
+
|
29
|
+
def database_connection_file
|
30
|
+
file = File.read(Rails.root.join('config', 'database.yml'))
|
31
|
+
|
32
|
+
YAML.safe_load(ERB.new(file).result, aliases: true)
|
33
|
+
end
|
34
|
+
|
35
|
+
def connection_class
|
36
|
+
@connection_class ||= ActiveRecord::Base
|
37
|
+
end
|
38
|
+
|
39
|
+
def establish_new_connection
|
40
|
+
connection_class.establish_connection(
|
41
|
+
connection_class.connection_config.dup.tap { |n| n[:username] = 'app_user' }
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
def execute(query)
|
46
|
+
@execute = ActiveRecord::Migration.execute(query)
|
47
|
+
end
|
48
|
+
|
49
|
+
def database_configuration
|
50
|
+
@database_configuration ||= database_connection_file[Rails.env].tap do |config|
|
51
|
+
config['username'] = 'app_user'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_rls
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Laloush
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Write a longer description or delete this line.
|
14
|
+
email:
|
15
|
+
- daniel.laloush@influitive.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- ".rubocop.yml"
|
22
|
+
- CHANGELOG.md
|
23
|
+
- CODE_OF_CONDUCT.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- bin/console
|
30
|
+
- bin/setup
|
31
|
+
- lib/pg_rls.rb
|
32
|
+
- lib/pg_rls/schema/down_statements.rb
|
33
|
+
- lib/pg_rls/schema/statements.rb
|
34
|
+
- lib/pg_rls/schema/up_statements.rb
|
35
|
+
- lib/pg_rls/tenant/tenant.rb
|
36
|
+
- lib/pg_rls/version.rb
|
37
|
+
homepage: https://github.com/Dandush03/pg_rls
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.0.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.1
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.2.27
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Write a short summary, because RubyGems requires one.
|
60
|
+
test_files: []
|