activerecord-rls 0.1.0
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/.devcontainer/Dockerfile +24 -0
- data/.devcontainer/compose.yaml +42 -0
- data/.devcontainer/devcontainer.json +22 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +511 -0
- data/Rakefile +12 -0
- data/gemfiles/common.gemfile +12 -0
- data/gemfiles/rails_7.2.gemfile +9 -0
- data/gemfiles/rails_8.0.gemfile +9 -0
- data/gemfiles/rails_8.1.gemfile +9 -0
- data/lib/active_record/rls/command_recorders/default_privileges.rb +25 -0
- data/lib/active_record/rls/command_recorders/policies.rb +38 -0
- data/lib/active_record/rls/command_recorders/privileges.rb +25 -0
- data/lib/active_record/rls/command_recorders/rls_policies.rb +41 -0
- data/lib/active_record/rls/command_recorders/roles.rb +25 -0
- data/lib/active_record/rls/migration_methods.rb +45 -0
- data/lib/active_record/rls/model.rb +54 -0
- data/lib/active_record/rls/schema_dumper.rb +186 -0
- data/lib/active_record/rls/statements/default_privileges.rb +77 -0
- data/lib/active_record/rls/statements/policies.rb +125 -0
- data/lib/active_record/rls/statements/privileges.rb +233 -0
- data/lib/active_record/rls/statements/quoting.rb +60 -0
- data/lib/active_record/rls/statements/rls_policies.rb +50 -0
- data/lib/active_record/rls/statements/roles.rb +155 -0
- data/lib/active_record/rls/statements/runtime_variables.rb +60 -0
- data/lib/active_record/rls/test_databases.rb +15 -0
- data/lib/active_record/rls/version.rb +7 -0
- data/lib/active_record/rls.rb +77 -0
- metadata +135 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module CommandRecorders
|
|
6
|
+
module DefaultPrivileges
|
|
7
|
+
def grant_default_privileges_to(*args)
|
|
8
|
+
record(:grant_default_privileges_to, args)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def revoke_default_privileges_from(*args)
|
|
12
|
+
record(:revoke_default_privileges_from, args)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def invert_grant_default_privileges_to(args)
|
|
16
|
+
[ :revoke_default_privileges_from, args ]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def invert_revoke_default_privileges_from(args)
|
|
20
|
+
[ :grant_default_privileges_to, args ]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module CommandRecorders
|
|
6
|
+
module Policies
|
|
7
|
+
def create_policy(*args, &block)
|
|
8
|
+
record(:create_policy, args, &block)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def drop_policy(*args, &block)
|
|
12
|
+
record(:drop_policy, args, &block)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def rename_policy(*args)
|
|
16
|
+
record(:rename_policy, args)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def alter_policy(*args, &block)
|
|
20
|
+
record(:alter_policy, args, &block)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def invert_create_policy(args, &block)
|
|
24
|
+
[ :drop_policy, args, block ]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def invert_drop_policy(args, &block)
|
|
28
|
+
[ :create_policy, args, block ]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def invert_rename_policy(args)
|
|
32
|
+
args[1], args[2] = args[2], args[1] # Swap names
|
|
33
|
+
[ :rename_policy, args ]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module CommandRecorders
|
|
6
|
+
module Privileges
|
|
7
|
+
def grant_privileges_to(*args)
|
|
8
|
+
record(:grant_privileges_to, args)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def revoke_privileges_from(*args)
|
|
12
|
+
record(:revoke_privileges_from, args)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def invert_grant_privileges_to(args)
|
|
16
|
+
[ :revoke_privileges_from, args ]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def invert_revoke_privileges_from(args)
|
|
20
|
+
[ :grant_privileges_to, args ]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module CommandRecorders
|
|
6
|
+
module RLSPolicies
|
|
7
|
+
def enable_rls(*args)
|
|
8
|
+
record(:enable_rls, args)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def disable_rls(*args)
|
|
12
|
+
record(:disable_rls, args)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def enable_force_rls(*args)
|
|
16
|
+
record(:enable_force_rls, args)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def disable_force_rls(*args)
|
|
20
|
+
record(:disable_force_rls, args)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def invert_enable_rls(args)
|
|
24
|
+
[ :disable_rls, args ]
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def invert_disable_rls(args)
|
|
28
|
+
[ :enable_rls, args ]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def invert_enable_force_rls(args)
|
|
32
|
+
[ :disable_force_rls, args ]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def invert_disable_force_rls(args)
|
|
36
|
+
[ :enable_force_rls, args ]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module CommandRecorders
|
|
6
|
+
module Roles
|
|
7
|
+
def create_role(*args)
|
|
8
|
+
record(:create_role, args)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def drop_role(*args)
|
|
12
|
+
record(:drop_role, args)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def invert_create_role(args)
|
|
16
|
+
[ :drop_role, args ]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def invert_drop_role(args)
|
|
20
|
+
[ :create_role, args ]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
# ActiveRecord::Migration#method_missing treats the first argument of any
|
|
6
|
+
# method it forwards as a table name: it is converted to a string and
|
|
7
|
+
# table_name_prefix/table_name_suffix are added. These methods take role
|
|
8
|
+
# names first, so they are defined on migrations directly and forward their
|
|
9
|
+
# arguments unchanged. Everything else matches method_missing: the call is
|
|
10
|
+
# logged with say_with_time and sent through the migration's execution
|
|
11
|
+
# strategy, which is the command recorder while a migration is reverted.
|
|
12
|
+
module MigrationMethods
|
|
13
|
+
ROLE_METHODS = %i[
|
|
14
|
+
create_role
|
|
15
|
+
alter_role
|
|
16
|
+
create_or_alter_role
|
|
17
|
+
drop_role
|
|
18
|
+
grant_privileges_to
|
|
19
|
+
revoke_privileges_from
|
|
20
|
+
grant_default_privileges_to
|
|
21
|
+
revoke_default_privileges_from
|
|
22
|
+
].freeze
|
|
23
|
+
|
|
24
|
+
# Defined with def (not define_method) so ruby2_keywords reliably keeps
|
|
25
|
+
# trailing options flagged as keywords, as Migration#method_missing does.
|
|
26
|
+
ROLE_METHODS.each do |method|
|
|
27
|
+
module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
28
|
+
def #{method}(*arguments, &block)
|
|
29
|
+
say_with_time "#{method}(\#{format_arguments(arguments)})" do
|
|
30
|
+
rls_execution_target.send(:#{method}, *arguments, &block)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
ruby2_keywords(:#{method})
|
|
34
|
+
RUBY
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# Rails 7.1+ routes migration commands through an execution strategy.
|
|
40
|
+
def rls_execution_target
|
|
41
|
+
respond_to?(:execution_strategy, true) ? send(:execution_strategy) : connection
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
# Include in a model to set the model's id as a PostgreSQL runtime variable
|
|
6
|
+
# that RLS policies can read, e.g. current_setting('rls.user_id', true).
|
|
7
|
+
#
|
|
8
|
+
# class User < ApplicationRecord
|
|
9
|
+
# include ActiveRecord::RLS::Model
|
|
10
|
+
# end
|
|
11
|
+
#
|
|
12
|
+
# user.with_rls_runtime_variable { Post.all } # rls.user_id = user.id
|
|
13
|
+
module Model
|
|
14
|
+
extend ActiveSupport::Concern
|
|
15
|
+
|
|
16
|
+
class_methods do
|
|
17
|
+
def rls_runtime_variable_name
|
|
18
|
+
"#{rls_runtime_variable_namespace}.#{name.underscore.gsub "/", "_"}_id"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def rls_runtime_variable_namespace
|
|
22
|
+
RLS.default_runtime_variable_namespace
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# The variable has to be set on the connection the thread's queries
|
|
26
|
+
# run on. lease_connection holds that connection for the thread,
|
|
27
|
+
# whereas with_connection could return it to the pool.
|
|
28
|
+
def rls_connection
|
|
29
|
+
lease_connection
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# scope is :local (the default, lasts until the end of the current
|
|
34
|
+
# transaction) or :session.
|
|
35
|
+
def set_rls_runtime_variable(**options)
|
|
36
|
+
self.class.rls_connection.set_runtime_variable self.class.rls_runtime_variable_name, to_rls_runtime_variable,
|
|
37
|
+
**options
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def with_rls_runtime_variable(**options, &block)
|
|
41
|
+
self.class.rls_connection.with_runtime_variable self.class.rls_runtime_variable_name, to_rls_runtime_variable,
|
|
42
|
+
**options, &block
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def reset_rls_runtime_variable(**options)
|
|
46
|
+
self.class.rls_connection.reset_runtime_variable self.class.rls_runtime_variable_name, **options
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_rls_runtime_variable
|
|
50
|
+
public_send self.class.primary_key
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module SchemaDumper
|
|
6
|
+
# Hooked into trailer rather than tables: the PostgreSQL dumper calls
|
|
7
|
+
# tables once per dumped schema (Rails 8.1+), which would repeat these
|
|
8
|
+
# sections. trailer runs exactly once, after all tables are dumped.
|
|
9
|
+
def trailer(stream)
|
|
10
|
+
roles(stream)
|
|
11
|
+
default_privileges(stream)
|
|
12
|
+
privileges(stream)
|
|
13
|
+
table_policies(stream)
|
|
14
|
+
table_rls_configurations(stream)
|
|
15
|
+
super
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def roles(stream)
|
|
19
|
+
roles = @connection.all_roles
|
|
20
|
+
return unless roles.any?
|
|
21
|
+
|
|
22
|
+
stream.puts
|
|
23
|
+
stream.puts " # Custom roles defined in this database."
|
|
24
|
+
roles.each do |role|
|
|
25
|
+
if role.password
|
|
26
|
+
stream.puts " # User '#{role.name}' password should be set in one of the following"
|
|
27
|
+
stream.puts " # - ENV[${Rails.env}_DB_ROLE_#{role.name.upcase}_PASSWORD]"
|
|
28
|
+
stream.puts " # - ENV[DB_ROLE_#{role.name.upcase}_PASSWORD]"
|
|
29
|
+
stream.puts " # - Rails.application.credentials.database.#{role.name}.password"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
stream.puts " create_or_alter_role(#{role.name.inspect}, super_user: #{role.super_user}, " \
|
|
33
|
+
"inherit: #{role.inherit}, create_role: #{role.create_role}, create_db: #{role.create_db}, " \
|
|
34
|
+
"replication: #{role.replication}, bypass_rls: #{role.bypass_rls}, password: #{role.password})"
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def default_privileges(stream)
|
|
39
|
+
default_privileges = @connection.all_default_privileges schema: rls_dump_schemas
|
|
40
|
+
return unless default_privileges.any?
|
|
41
|
+
|
|
42
|
+
stream.puts
|
|
43
|
+
stream.puts " # Custom privileges defined in this database."
|
|
44
|
+
stream.puts " # Note that default ACL privileges may not work with other database engines. " \
|
|
45
|
+
"Be careful if changing database."
|
|
46
|
+
|
|
47
|
+
default_privileges.each do |privilege|
|
|
48
|
+
options = []
|
|
49
|
+
# Default privileges belong to the role creating the objects, so it has to be restored too.
|
|
50
|
+
options << "target_role: #{privilege.target_role.inspect}" if privilege.target_role
|
|
51
|
+
options << "schema: #{privilege.schema.inspect}" if privilege.schema
|
|
52
|
+
|
|
53
|
+
arguments = [ privilege.role.inspect, privilege.privileges.inspect, privilege.object_type.inspect, *options ]
|
|
54
|
+
stream.puts " grant_default_privileges_to(#{arguments.join(", ")})"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def privileges(stream)
|
|
59
|
+
privileges = @connection.all_privileges(schema: rls_dump_schemas).reject do |privilege|
|
|
60
|
+
rls_skip_privilege?(privilege)
|
|
61
|
+
end
|
|
62
|
+
return unless privileges.any?
|
|
63
|
+
|
|
64
|
+
stream.puts
|
|
65
|
+
stream.puts " # User privileges defined in this database."
|
|
66
|
+
stream.puts " # Note that not all user privileges may work with other database engines. " \
|
|
67
|
+
"Be careful if changing database."
|
|
68
|
+
|
|
69
|
+
privileges.each do |privilege|
|
|
70
|
+
options = []
|
|
71
|
+
options << "columns: #{privilege.columns.inspect}" if privilege.columns.present?
|
|
72
|
+
options << "schema: #{privilege.schema.inspect}" if privilege.schema
|
|
73
|
+
|
|
74
|
+
arguments = [
|
|
75
|
+
privilege.role.inspect,
|
|
76
|
+
(privilege.privileges || []).inspect,
|
|
77
|
+
privilege.object_type ? privilege.object_type.inspect : "nil",
|
|
78
|
+
rls_privilege_object(privilege)
|
|
79
|
+
]
|
|
80
|
+
arguments << "{ #{options.join(", ")} }" if options.any?
|
|
81
|
+
|
|
82
|
+
stream.puts " grant_privileges_to(#{arguments.join(", ")})"
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def table_policies(stream)
|
|
87
|
+
policies = @connection.all_policies(schema: rls_dump_schemas).reject do |policy|
|
|
88
|
+
rls_ignored_table?(policy.table_name)
|
|
89
|
+
end
|
|
90
|
+
return unless policies.any?
|
|
91
|
+
|
|
92
|
+
stream.puts
|
|
93
|
+
stream.puts " # Custom table policies defined in this database."
|
|
94
|
+
stream.puts " # Note that policies may not work with other database engines. Be careful if changing database."
|
|
95
|
+
|
|
96
|
+
policies.each_with_index do |policy, index|
|
|
97
|
+
table_name = dump_table_name(policy.schema, policy.table_name)
|
|
98
|
+
|
|
99
|
+
stream.puts unless index.zero?
|
|
100
|
+
stream.puts " create_policy(#{table_name.inspect}, name: #{policy.name.inspect}, " \
|
|
101
|
+
"permissive: #{policy.permissive}, roles: #{policy.roles.inspect}, " \
|
|
102
|
+
"command: #{policy.command.inspect}) do |p|"
|
|
103
|
+
# inspect escapes quotes and interpolation so expressions like ("userId" = 1) stay valid Ruby.
|
|
104
|
+
stream.puts " p.using(#{strip_outer_parentheses(policy.using).inspect})" if policy.using
|
|
105
|
+
stream.puts " p.check(#{strip_outer_parentheses(policy.check).inspect})" if policy.check
|
|
106
|
+
stream.puts " end"
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def table_rls_configurations(stream)
|
|
111
|
+
policies = @connection.all_rls_policies(schema: rls_dump_schemas).reject do |policy|
|
|
112
|
+
rls_ignored_table?(policy.table_name)
|
|
113
|
+
end
|
|
114
|
+
return unless policies.any?
|
|
115
|
+
|
|
116
|
+
stream.puts
|
|
117
|
+
stream.puts " # Table Row Level Security policies defined in this database."
|
|
118
|
+
stream.puts " # Note that RLS may not work with other database engines. Be careful if changing database."
|
|
119
|
+
|
|
120
|
+
policies.each do |policy|
|
|
121
|
+
table_name = dump_table_name(policy.schema, policy.table_name)
|
|
122
|
+
|
|
123
|
+
stream.puts " enable_rls(#{table_name.inspect})" if policy.enabled
|
|
124
|
+
stream.puts " enable_force_rls(#{table_name.inspect})" if policy.force
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
private
|
|
129
|
+
|
|
130
|
+
# Rails 8.1+ sets @dump_schemas on the PostgreSQL schema dumper. Older
|
|
131
|
+
# versions only dump the current schema.
|
|
132
|
+
def rls_dump_schemas
|
|
133
|
+
@dump_schemas.presence || [ @connection.current_schema ]
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Uses Rails' own ignore check, which supports both strings and regular
|
|
137
|
+
# expressions in ActiveRecord::SchemaDumper.ignore_tables.
|
|
138
|
+
def rls_ignored_table?(table_name)
|
|
139
|
+
if respond_to?(:ignored?, true)
|
|
140
|
+
ignored?(table_name)
|
|
141
|
+
else
|
|
142
|
+
Array(@ignore_tables).any? { |ignored| ignored === table_name }
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def rls_skip_privilege?(privilege)
|
|
147
|
+
case privilege.object_type
|
|
148
|
+
when "TABLE"
|
|
149
|
+
rls_ignored_table?(privilege.object)
|
|
150
|
+
when "DATABASE"
|
|
151
|
+
# pg_database is shared by the whole cluster; only the database being
|
|
152
|
+
# dumped belongs in its schema.
|
|
153
|
+
privilege.object != @connection.current_database
|
|
154
|
+
else
|
|
155
|
+
false
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def rls_privilege_object(privilege)
|
|
160
|
+
if privilege.use_arguments?
|
|
161
|
+
{ privilege.object => privilege.arguments }.inspect
|
|
162
|
+
elsif privilege.object_type == "DATABASE"
|
|
163
|
+
# Resolved when the schema is loaded, so it works for any database name.
|
|
164
|
+
"current_database"
|
|
165
|
+
elsif %w[TABLE SEQUENCE].include?(privilege.object_type)
|
|
166
|
+
dump_table_name(privilege.schema, privilege.object).inspect
|
|
167
|
+
elsif privilege.object
|
|
168
|
+
privilege.object.inspect
|
|
169
|
+
else
|
|
170
|
+
"nil"
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def strip_outer_parentheses(expression)
|
|
175
|
+
expression.sub(/\A\((.*)\)\z/m, "\\1")
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Tables in the public schema are dumped without a schema so existing
|
|
179
|
+
# dumps are unchanged. Tables in other schemas are schema qualified so
|
|
180
|
+
# they are not resolved through the search_path when the schema is loaded.
|
|
181
|
+
def dump_table_name(schema, table_name)
|
|
182
|
+
schema.blank? || schema == "public" ? table_name : "#{schema}.#{table_name}"
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module Statements
|
|
6
|
+
module DefaultPrivileges
|
|
7
|
+
include Quoting
|
|
8
|
+
|
|
9
|
+
# target_role is the role whose newly created objects receive the privileges
|
|
10
|
+
# (ALTER DEFAULT PRIVILEGES FOR ROLE <target_role>).
|
|
11
|
+
DefaultPrivilege = Struct.new(:object_type, :schema, :role, :privileges, :target_role)
|
|
12
|
+
|
|
13
|
+
def all_default_privileges(schema: schema_names)
|
|
14
|
+
default_privileges = execute <<~SQL
|
|
15
|
+
SELECT CASE
|
|
16
|
+
WHEN pg_default_acl.defaclobjtype = 'r' THEN 'TABLES'
|
|
17
|
+
WHEN pg_default_acl.defaclobjtype = 'S' THEN 'SEQUENCES'
|
|
18
|
+
WHEN pg_default_acl.defaclobjtype = 'f' THEN 'FUNCTIONS'
|
|
19
|
+
WHEN pg_default_acl.defaclobjtype = 'T' THEN 'TYPES'
|
|
20
|
+
WHEN pg_default_acl.defaclobjtype = 'n' THEN 'SCHEMAS'
|
|
21
|
+
WHEN pg_default_acl.defaclobjtype = 'L' THEN 'LARGE OBJECTS'
|
|
22
|
+
END AS object_type,
|
|
23
|
+
pg_namespace.nspname AS schema,
|
|
24
|
+
CASE
|
|
25
|
+
WHEN a.grantee = 0 THEN 'public'
|
|
26
|
+
ELSE a.grantee::regrole::varchar
|
|
27
|
+
END AS role,
|
|
28
|
+
array_agg(DISTINCT a.privilege_type) AS privileges,
|
|
29
|
+
pg_default_acl.defaclrole::regrole::varchar AS target_role
|
|
30
|
+
FROM pg_catalog.pg_default_acl
|
|
31
|
+
LEFT JOIN pg_catalog.pg_namespace ON pg_default_acl.defaclnamespace = pg_namespace.oid
|
|
32
|
+
JOIN aclexplode(pg_default_acl.defaclacl) AS a ON true
|
|
33
|
+
WHERE (pg_namespace.nspname IS NULL OR#{" "}
|
|
34
|
+
pg_namespace.nspname = ANY (#{quote_text_array schema}))
|
|
35
|
+
AND a.grantee::regrole::text NOT LIKE 'pg_%'
|
|
36
|
+
AND a.grantee::regrole::text NOT IN (
|
|
37
|
+
SELECT applicable_roles.grantee
|
|
38
|
+
FROM information_schema.applicable_roles
|
|
39
|
+
WHERE applicable_roles.role_name = 'pg_database_owner'
|
|
40
|
+
)
|
|
41
|
+
GROUP BY pg_default_acl.defaclrole, pg_default_acl.defaclobjtype, pg_namespace.nspname, a.grantee
|
|
42
|
+
SQL
|
|
43
|
+
|
|
44
|
+
default_privileges.map do |privilege|
|
|
45
|
+
DefaultPrivilege.new(
|
|
46
|
+
privilege["object_type"],
|
|
47
|
+
privilege["schema"],
|
|
48
|
+
privilege["role"],
|
|
49
|
+
PG::TextDecoder::Array.new.decode(privilege["privileges"]),
|
|
50
|
+
privilege["target_role"]
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def grant_default_privileges_to(role, privileges, object_type, options = {})
|
|
56
|
+
execute build_default_privileges_sql(:grant, role, privileges, object_type, options)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def revoke_default_privileges_from(role, privileges, object_type, options = {})
|
|
60
|
+
execute build_default_privileges_sql(:revoke, role, privileges, object_type, options)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def build_default_privileges_sql(action, role, privileges, object_type, options = {})
|
|
66
|
+
sql = "ALTER DEFAULT PRIVILEGES"
|
|
67
|
+
sql += " FOR ROLE #{quote_role_names options[:target_role]}" if options[:target_role]
|
|
68
|
+
sql += " IN SCHEMA #{quote_identifiers options[:schema]}" if options[:schema]
|
|
69
|
+
sql += " #{action.to_s.upcase} #{sql_keywords privileges, "privilege"}"
|
|
70
|
+
sql += " ON #{sql_keywords object_type.to_s.pluralize, "object type"}"
|
|
71
|
+
|
|
72
|
+
sql + " #{action == :revoke ? "FROM" : "TO"} #{quote_role_names role}"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveRecord
|
|
4
|
+
module RLS
|
|
5
|
+
module Statements
|
|
6
|
+
module Policies
|
|
7
|
+
include Quoting
|
|
8
|
+
|
|
9
|
+
SYSTEM_POLICY_ROLES = Quoting::SPECIAL_ROLES
|
|
10
|
+
|
|
11
|
+
Policy = Struct.new(:table_name, :name, :permissive, :roles, :command, :using, :check, :schema)
|
|
12
|
+
|
|
13
|
+
class PolicyBuilder
|
|
14
|
+
attr_reader :using_expression, :check_expression
|
|
15
|
+
|
|
16
|
+
def using(expression = nil)
|
|
17
|
+
@using_expression = block_given? ? yield : expression
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def check(expression = nil)
|
|
21
|
+
@check_expression = block_given? ? yield : expression
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def all_policies(schema: schema_names, ignore: [])
|
|
26
|
+
policies = execute <<~SQL
|
|
27
|
+
SELECT pg_policies.tablename AS table_name,
|
|
28
|
+
pg_policies.policyname AS name,
|
|
29
|
+
pg_policies.permissive = 'PERMISSIVE' AS permissive,
|
|
30
|
+
pg_policies.roles AS roles,
|
|
31
|
+
pg_policies.cmd AS command,
|
|
32
|
+
pg_policies.qual AS using,
|
|
33
|
+
pg_policies.with_check AS check,
|
|
34
|
+
pg_policies.schemaname AS schema
|
|
35
|
+
FROM pg_catalog.pg_policies
|
|
36
|
+
WHERE pg_policies.schemaname = ANY (#{quote_text_array schema})
|
|
37
|
+
AND NOT pg_policies.tablename = ANY (#{quote_text_array ignore})
|
|
38
|
+
ORDER BY pg_policies.schemaname, pg_policies.tablename, pg_policies.policyname
|
|
39
|
+
SQL
|
|
40
|
+
|
|
41
|
+
policies.map do |policy|
|
|
42
|
+
Policy.new(
|
|
43
|
+
policy["table_name"],
|
|
44
|
+
policy["name"],
|
|
45
|
+
policy["permissive"],
|
|
46
|
+
PG::TextDecoder::Array.new.decode(policy["roles"]),
|
|
47
|
+
policy["command"],
|
|
48
|
+
policy["using"],
|
|
49
|
+
policy["check"],
|
|
50
|
+
policy["schema"]
|
|
51
|
+
)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def create_policy(table, options = {})
|
|
56
|
+
builder = PolicyBuilder.new
|
|
57
|
+
yield builder if block_given?
|
|
58
|
+
|
|
59
|
+
execute build_policy_sql(:create, table, options, builder)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def drop_policy(table, options = {})
|
|
63
|
+
roles = normalize_policy_roles(options[:roles])
|
|
64
|
+
name = options.fetch(:name) { default_policy_name table, roles }
|
|
65
|
+
|
|
66
|
+
execute "DROP POLICY IF EXISTS #{quote_column_name name} ON #{quote_table_name table}"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def rename_policy(table, name, new_name)
|
|
70
|
+
execute "ALTER POLICY #{quote_column_name name} ON #{quote_table_name table} " \
|
|
71
|
+
"RENAME TO #{quote_column_name new_name}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def alter_policy(table, options = {})
|
|
75
|
+
unsupported = options.keys & %i[permissive command]
|
|
76
|
+
if unsupported.any?
|
|
77
|
+
raise ArgumentError,
|
|
78
|
+
"ALTER POLICY cannot change #{unsupported.join(", ")}; drop and recreate the policy instead"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
builder = PolicyBuilder.new
|
|
82
|
+
yield builder if block_given?
|
|
83
|
+
|
|
84
|
+
execute build_policy_sql(:alter, table, options, builder)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def build_policy_sql(action, table, options, builder)
|
|
90
|
+
roles = normalize_policy_roles(options[:roles])
|
|
91
|
+
name = options.fetch(:name) { default_policy_name table, roles }
|
|
92
|
+
sql = "#{action.to_s.upcase} POLICY #{quote_column_name name} ON #{quote_table_name table}"
|
|
93
|
+
|
|
94
|
+
# AS and FOR can only be set when the policy is created.
|
|
95
|
+
if action == :create
|
|
96
|
+
permissive = options.fetch(:permissive, true)
|
|
97
|
+
command = options.fetch(:command, "ALL").to_s.upcase
|
|
98
|
+
|
|
99
|
+
sql += " AS #{permissive ? "PERMISSIVE" : "RESTRICTIVE"}"
|
|
100
|
+
sql += " FOR #{command}"
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
sql += " TO #{quote_role_names roles}" if roles
|
|
104
|
+
sql += " USING (#{builder.using_expression})" if builder.using_expression
|
|
105
|
+
sql += " WITH CHECK (#{builder.check_expression})" if builder.check_expression
|
|
106
|
+
sql
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def normalize_policy_roles(roles)
|
|
110
|
+
roles = Array(roles).compact_blank.map do |role|
|
|
111
|
+
upcase_role = role.upcase
|
|
112
|
+
|
|
113
|
+
SYSTEM_POLICY_ROLES.include?(upcase_role) ? upcase_role : role
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
roles.empty? ? nil : roles
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def default_policy_name(table, roles)
|
|
120
|
+
(Array(table) + Array(roles) + Array(:policy)).compact_blank.map(&:to_s).join("_")
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|