Debian Codegraph @

src/test/subscription/t/033_run_as_table_owner.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test that logical replication respects permissions
5 use strict;
6 use warnings FATAL => 'all';
7 use PostgreSQL::Test::Cluster;
8 use Test::More;
9
10 my ($node_publisher, $node_subscriber, $publisher_connstr, $result, $offset);
11 $offset = 0;
12
13 sub publish_insert
14 {
15 my ($tbl, $new_i) = @_;
16 $node_publisher->safe_psql(
17 'postgres', qq(
18 SET SESSION AUTHORIZATION regress_alice;
19 INSERT INTO $tbl (i) VALUES ($new_i);
20 ));
21 }
22
23 sub publish_update
24 {
25 my ($tbl, $old_i, $new_i) = @_;
26 $node_publisher->safe_psql(
27 'postgres', qq(
28 SET SESSION AUTHORIZATION regress_alice;
29 UPDATE $tbl SET i = $new_i WHERE i = $old_i;
30 ));
31 }
32
33 sub publish_delete
34 {
35 my ($tbl, $old_i) = @_;
36 $node_publisher->safe_psql(
37 'postgres', qq(
38 SET SESSION AUTHORIZATION regress_alice;
39 DELETE FROM $tbl WHERE i = $old_i;
40 ));
41 }
42
43 sub expect_replication
44 {
45 my ($tbl, $cnt, $min, $max, $testname) = @_;
46 $node_publisher->wait_for_catchup('admin_sub');
47 $result = $node_subscriber->safe_psql(
48 'postgres', qq(
49 SELECT COUNT(i), MIN(i), MAX(i) FROM $tbl));
50 is($result, "$cnt|$min|$max", $testname);
51 }
52
53 sub expect_failure
54 {
55 my ($tbl, $cnt, $min, $max, $re, $testname) = @_;
56 $offset = $node_subscriber->wait_for_log($re, $offset);
57 $result = $node_subscriber->safe_psql(
58 'postgres', qq(
59 SELECT COUNT(i), MIN(i), MAX(i) FROM $tbl));
60 is($result, "$cnt|$min|$max", $testname);
61 }
62
63 sub revoke_superuser
64 {
65 my ($role) = @_;
66 $node_subscriber->safe_psql(
67 'postgres', qq(
68 ALTER ROLE $role NOSUPERUSER));
69 }
70
71 # Create publisher and subscriber nodes with schemas owned and published by
72 # "regress_alice" but subscribed and replicated by different role
73 # "regress_admin" and "regress_admin2". For partitioned tables, layout the
74 # partitions differently on the publisher than on the subscriber.
75 #
76 $node_publisher = PostgreSQL::Test::Cluster->new('publisher');
77 $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber');
78 $node_publisher->init(allows_streaming => 'logical');
79 $node_subscriber->init;
80 $node_publisher->start;
81 $node_subscriber->start;
82 $publisher_connstr = $node_publisher->connstr . ' dbname=postgres';
83
84 for my $node ($node_publisher, $node_subscriber)
85 {
86 $node->safe_psql(
87 'postgres', qq(
88 CREATE ROLE regress_admin SUPERUSER LOGIN;
89 CREATE ROLE regress_admin2 SUPERUSER LOGIN;
90 CREATE ROLE regress_alice NOSUPERUSER LOGIN;
91 GRANT CREATE ON DATABASE postgres TO regress_alice;
92 SET SESSION AUTHORIZATION regress_alice;
93 CREATE SCHEMA alice;
94 GRANT USAGE ON SCHEMA alice TO regress_admin;
95
96 CREATE TABLE alice.unpartitioned (i INTEGER);
97 ALTER TABLE alice.unpartitioned REPLICA IDENTITY FULL;
98 GRANT SELECT ON TABLE alice.unpartitioned TO regress_admin;
99 ));
100 }
101 $node_publisher->safe_psql(
102 'postgres', qq(
103 SET SESSION AUTHORIZATION regress_alice;
104
105 CREATE PUBLICATION alice FOR TABLE alice.unpartitioned
106 WITH (publish_via_partition_root = true);
107 ));
108 $node_subscriber->safe_psql(
109 'postgres', qq(
110 SET SESSION AUTHORIZATION regress_admin;
111 CREATE SUBSCRIPTION admin_sub CONNECTION '$publisher_connstr' PUBLICATION alice
112 WITH (run_as_owner = true, password_required = false);
113 ));
114
115 # Wait for initial sync to finish
116 $node_subscriber->wait_for_subscription_sync($node_publisher, 'admin_sub');
117
118 # Verify that "regress_admin" can replicate into the tables
119 publish_insert("alice.unpartitioned", 1);
120 publish_insert("alice.unpartitioned", 3);
121 publish_insert("alice.unpartitioned", 5);
122 publish_update("alice.unpartitioned", 1 => 7);
123 publish_delete("alice.unpartitioned", 3);
124 expect_replication("alice.unpartitioned", 2, 5, 7, "superuser can replicate");
125
126 # Revoke superuser privilege for "regress_admin", and verify that we now
127 # fail to replicate an insert.
128 revoke_superuser("regress_admin");
129 publish_insert("alice.unpartitioned", 9);
130 expect_failure(
131 "alice.unpartitioned", 2, 5, 7,
132 qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi,
133 "with no privileges cannot replicate");
134
135 # Now grant DML privileges and verify that we can replicate an INSERT.
136 $node_subscriber->safe_psql(
137 'postgres', qq(
138 ALTER ROLE regress_admin NOSUPERUSER;
139 SET SESSION AUTHORIZATION regress_alice;
140 GRANT INSERT,UPDATE,DELETE ON alice.unpartitioned TO regress_admin;
141 REVOKE SELECT ON alice.unpartitioned FROM regress_admin;
142 ));
143 expect_replication("alice.unpartitioned", 3, 5, 9,
144 "with INSERT privilege can replicate INSERT");
145
146 # We can't yet replicate an UPDATE because we don't have SELECT.
147 publish_update("alice.unpartitioned", 5 => 11);
148 publish_delete("alice.unpartitioned", 9);
149 expect_failure(
150 "alice.unpartitioned",
151 3,
152 5,
153 9,
154 qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi,
155 "without SELECT privilege cannot replicate UPDATE or DELETE");
156
157 # After granting SELECT, replication resumes.
158 $node_subscriber->safe_psql(
159 'postgres', qq(
160 SET SESSION AUTHORIZATION regress_alice;
161 GRANT SELECT ON alice.unpartitioned TO regress_admin;
162 ));
163 expect_replication("alice.unpartitioned", 2, 7, 11,
164 "with all privileges can replicate");
165
166 # Remove all privileges again. Instead, give the ability to SET ROLE to
167 # regress_alice.
168 $node_subscriber->safe_psql(
169 'postgres', qq(
170 SET SESSION AUTHORIZATION regress_alice;
171 REVOKE ALL PRIVILEGES ON alice.unpartitioned FROM regress_admin;
172 RESET SESSION AUTHORIZATION;
173 GRANT regress_alice TO regress_admin WITH INHERIT FALSE, SET TRUE;
174 ));
175
176 # Because replication is running as the subscription owner in this test,
177 # the above grant doesn't help: it gives the ability to SET ROLE, but not
178 # privileges on the table.
179 publish_insert("alice.unpartitioned", 13);
180 expect_failure(
181 "alice.unpartitioned",
182 2,
183 7,
184 11,
185 qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi,
186 "with SET ROLE but not INHERIT cannot replicate");
187
188 # Now remove SET ROLE and add INHERIT and check that things start working.
189 $node_subscriber->safe_psql(
190 'postgres', qq(
191 GRANT regress_alice TO regress_admin WITH INHERIT TRUE, SET FALSE;
192 ));
193 expect_replication("alice.unpartitioned", 3, 7, 13,
194 "with INHERIT but not SET ROLE can replicate");
195
196 # Similar to the previous test, remove all privileges again and instead,
197 # give the ability to SET ROLE to regress_alice.
198 $node_subscriber->safe_psql(
199 'postgres', qq(
200 SET SESSION AUTHORIZATION regress_alice;
201 REVOKE ALL PRIVILEGES ON alice.unpartitioned FROM regress_admin;
202 RESET SESSION AUTHORIZATION;
203 GRANT regress_alice TO regress_admin WITH INHERIT FALSE, SET TRUE;
204 ));
205
206 # Because replication is running as the subscription owner in this test,
207 # the above grant doesn't help.
208 publish_insert("alice.unpartitioned", 14);
209 expect_failure(
210 "alice.unpartitioned", 3, 7, 13,
211 qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi,
212 "with no privileges cannot replicate");
213
214 # Allow the replication to run as table owner and check that things start
215 # working.
216 $node_subscriber->safe_psql(
217 'postgres', qq(
218 ALTER SUBSCRIPTION admin_sub SET (run_as_owner = false);
219 ));
220
221 expect_replication("alice.unpartitioned", 4, 7, 14,
222 "can replicate after setting run_as_owner to false");
223
224 # Remove the subscrition and truncate the table for the initial data sync
225 # tests.
226 $node_subscriber->safe_psql(
227 'postgres', qq(
228 DROP SUBSCRIPTION admin_sub;
229 TRUNCATE alice.unpartitioned;
230 ));
231
232 # Create a new subscription "admin_sub" owned by regress_admin2. It's
233 # disabled so that we revoke superuser privilege after creation.
234 $node_subscriber->safe_psql(
235 'postgres', qq(
236 SET SESSION AUTHORIZATION regress_admin2;
237 CREATE SUBSCRIPTION admin_sub CONNECTION '$publisher_connstr' PUBLICATION alice
238 WITH (run_as_owner = false, password_required = false, copy_data = true, enabled = false);
239 ));
240
241 # Revoke superuser privilege for "regress_admin2", and give it the
242 # ability to SET ROLE. Then enable the subscription "admin_sub".
243 revoke_superuser("regress_admin2");
244 $node_subscriber->safe_psql(
245 'postgres', qq(
246 GRANT regress_alice TO regress_admin2 WITH INHERIT FALSE, SET TRUE;
247 ALTER SUBSCRIPTION admin_sub ENABLE;
248 ));
249
250 # Because the initial data sync is working as the table owner, all
251 # data should be copied.
252 $node_subscriber->wait_for_subscription_sync($node_publisher, 'admin_sub');
253 expect_replication("alice.unpartitioned", 4, 7, 14,
254 "table owner can do the initial data copy");
255
256 done_testing();