File: acl.sql

package info (click to toggle)
postgresql-periods 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,084 kB
  • sloc: sql: 9,744; ansic: 548; makefile: 30; sh: 1
file content (231 lines) | stat: -rw-r--r-- 9,846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
SELECT CASE
    WHEN setting::integer >= 170000 THEN '17 ..'::text
    WHEN setting::integer >= 110000 THEN '11 .. 16'
    WHEN setting::integer >= 90600 THEN '9.6 .. 10'
    ELSE '.. 9.5' END
FROM pg_settings WHERE name = 'server_version_num';

/* Tests for access control on the history tables */

CREATE ROLE periods_acl_1;
CREATE ROLE periods_acl_2;
CREATE ROLE periods_acl_3;

/* OWNER */

-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_owners AS
    SELECT c.relnamespace::regnamespace AS schema_name,
           c.relname AS object_name,
           CASE c.relkind
               WHEN 'r' THEN 'table'
               WHEN 'v' THEN 'view'
           END AS object_type,
           c.relowner::regrole AS owner
    FROM pg_class AS c
    WHERE c.relnamespace = 'public'::regnamespace
      AND c.relname = ANY (ARRAY['owner_test', 'owner_test_history', 'owner_test_with_history', 'owner_test__for_portion_of_p'])
    UNION ALL
    SELECT p.pronamespace, p.proname, 'function', p.proowner
    FROM pg_proc AS p
    WHERE p.pronamespace = 'public'::regnamespace
      AND p.proname = ANY (ARRAY['owner_test__as_of', 'owner_test__between', 'owner_test__between_symmetric', 'owner_test__from_to']);

CREATE TABLE owner_test (col text PRIMARY KEY, s integer, e integer);
ALTER TABLE owner_test OWNER TO periods_acl_1;
SELECT periods.add_period('owner_test', 'p', 's', 'e');
SELECT periods.add_for_portion_view('owner_test', 'p');
SELECT periods.add_system_time_period('owner_test');
SELECT periods.add_system_versioning('owner_test');
TABLE show_owners ORDER BY object_name;

-- This should change everything
ALTER TABLE owner_test OWNER TO periods_acl_2;
TABLE show_owners ORDER BY object_name;

-- These should change nothing
ALTER TABLE owner_test_history OWNER TO periods_acl_3;
ALTER VIEW owner_test_with_history OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__as_of(timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__between(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__between_symmetric(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__from_to(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
TABLE show_owners ORDER BY object_name;

-- This should put the owner back to the base table's owner
SELECT periods.drop_system_versioning('owner_test');
ALTER TABLE owner_test_history OWNER TO periods_acl_3;
TABLE show_owners ORDER BY object_name;
SELECT periods.add_system_versioning('owner_test');
TABLE show_owners ORDER BY object_name;

SELECT periods.drop_system_versioning('owner_test', drop_behavior => 'CASCADE', purge => true);
SELECT periods.drop_for_portion_view('owner_test', NULL);
DROP TABLE owner_test CASCADE;
DROP VIEW show_owners;

/* FOR PORTION OF ACL */

-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_acls AS
    SELECT row_number() OVER (ORDER BY array_position(ARRAY['table', 'view', 'function'], object_type),
                                       schema_name, object_name, grantee, privilege_type) AS sort_order,
           *
    FROM (
        SELECT c.relnamespace::regnamespace AS schema_name,
               c.relname AS object_name,
               CASE c.relkind
                   WHEN 'r' THEN 'table'
                   WHEN 'v' THEN 'view'
               END AS object_type,
               acl.grantee::regrole::text AS grantee,
               acl.privilege_type
        FROM pg_class AS c
        CROSS JOIN LATERAL aclexplode(COALESCE(c.relacl, acldefault('r', c.relowner))) AS acl
        WHERE c.relname IN ('fpacl', 'fpacl__for_portion_of_p')
    ) AS _;

CREATE TABLE fpacl (col text PRIMARY KEY, s integer, e integer);
ALTER TABLE fpacl OWNER TO periods_acl_1;
SELECT periods.add_period('fpacl', 'p', 's', 'e');
SELECT periods.add_for_portion_view('fpacl', 'p');
TABLE show_acls ORDER BY sort_order;

GRANT SELECT, UPDATE ON TABLE fpacl__for_portion_of_p TO periods_acl_2; -- fail
GRANT SELECT, UPDATE ON TABLE fpacl TO periods_acl_2;
TABLE show_acls ORDER BY sort_order;

REVOKE UPDATE ON TABLE fpacl__for_portion_of_p FROM periods_acl_2; -- fail
REVOKE UPDATE ON TABLE fpacl FROM periods_acl_2;
TABLE show_acls ORDER BY sort_order;

SELECT periods.drop_for_portion_view('fpacl', 'p');
DROP TABLE fpacl CASCADE;
DROP VIEW show_acls;

/* History ACL */

-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_acls AS
    SELECT row_number() OVER (ORDER BY array_position(ARRAY['table', 'view', 'function'], object_type),
                                       schema_name, object_name, grantee, privilege_type) AS sort_order,
           *
    FROM (
        SELECT c.relnamespace::regnamespace AS schema_name,
               c.relname AS object_name,
               CASE c.relkind
                   WHEN 'r' THEN 'table'
                   WHEN 'v' THEN 'view'
               END AS object_type,
               acl.grantee::regrole::text AS grantee,
               acl.privilege_type
        FROM pg_class AS c
        CROSS JOIN LATERAL aclexplode(COALESCE(c.relacl, acldefault('r', c.relowner))) AS acl
        WHERE c.relname IN ('histacl', 'histacl_history', 'histacl_with_history')

        UNION ALL

        SELECT p.pronamespace::regnamespace,
               p.proname,
               'function',
               acl.grantee::regrole::text,
               acl.privilege_type
        FROM pg_proc AS p
        CROSS JOIN LATERAL aclexplode(COALESCE(p.proacl, acldefault('f', p.proowner))) AS acl
        WHERE p.proname IN ('histacl__as_of', 'histacl__between', 'histacl__between_symmetric', 'histacl__from_to')
    ) AS _;

CREATE TABLE histacl (col text);
ALTER TABLE histacl OWNER TO periods_acl_1;
SELECT periods.add_system_time_period('histacl');
SELECT periods.add_system_versioning('histacl');
TABLE show_acls ORDER BY sort_order;

-- Disconnect, add some privs to the history table, and reconnect
SELECT periods.drop_system_versioning('histacl');
GRANT ALL ON TABLE histacl_history TO periods_acl_3;
TABLE show_acls ORDER BY sort_order;
SELECT periods.add_system_versioning('histacl');
TABLE show_acls ORDER BY sort_order;

-- These next 6 blocks should fail
GRANT ALL ON TABLE histacl_history TO periods_acl_3; -- fail
GRANT SELECT ON TABLE histacl_history TO periods_acl_3; -- fail
REVOKE ALL ON TABLE histacl_history FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

GRANT ALL ON TABLE histacl_with_history TO periods_acl_3; -- fail
GRANT SELECT ON TABLE histacl_with_history TO periods_acl_3; -- fail
REVOKE ALL ON TABLE histacl_with_history FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

GRANT ALL ON FUNCTION histacl__as_of(timestamp with time zone) TO periods_acl_3; -- fail
GRANT EXECUTE ON FUNCTION histacl__as_of(timestamp with time zone) TO periods_acl_3; -- fail
REVOKE ALL ON FUNCTION histacl__as_of(timestamp with time zone) FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

GRANT ALL ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
GRANT EXECUTE ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
REVOKE ALL ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

GRANT ALL ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
GRANT EXECUTE ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
REVOKE ALL ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

GRANT ALL ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
GRANT EXECUTE ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
REVOKE ALL ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
TABLE show_acls ORDER BY sort_order;

-- This one should work and propagate
GRANT ALL ON TABLE histacl TO periods_acl_2;
TABLE show_acls ORDER BY sort_order;
REVOKE SELECT ON TABLE histacl FROM periods_acl_2;
TABLE show_acls ORDER BY sort_order;

SELECT periods.drop_system_versioning('histacl', drop_behavior => 'CASCADE', purge => true);
DROP TABLE histacl CASCADE;
DROP VIEW show_acls;

/* Who can modify the history table? */

CREATE TABLE retention (value integer);
ALTER TABLE retention OWNER TO periods_acl_1;
REVOKE ALL ON TABLE retention FROM PUBLIC;
GRANT ALL ON TABLE retention TO periods_acl_2;
GRANT SELECT ON TABLE retention TO periods_acl_3;
SELECT periods.add_system_time_period('retention');
SELECT periods.add_system_versioning('retention');

INSERT INTO retention (value) VALUES (1);
UPDATE retention SET value = 2;

SET ROLE TO periods_acl_3;
DELETE FROM retention_history; -- fail
SET ROLE TO periods_acl_2;
DELETE FROM retention_history; -- fail
SET ROLE TO periods_acl_1;
DELETE FROM retention_history; -- fail

-- test what the docs say to do
BEGIN;
SELECT periods.drop_system_versioning('retention');
GRANT DELETE ON TABLE retention_history TO CURRENT_USER;
DELETE FROM retention_history;
SELECT periods.add_system_versioning('retention');
COMMIT;

-- superuser can do anything
RESET ROLE;
DELETE FROM retention_history;

SELECT periods.drop_system_versioning('retention', drop_behavior => 'CASCADE', purge => true);
DROP TABLE retention CASCADE;

/* Clean up */

DROP ROLE periods_acl_1;
DROP ROLE periods_acl_2;
DROP ROLE periods_acl_3;