| 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 PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
my ($node_publisher, $node_subscriber, $publisher_connstr, $result, $offset); |
| 12 |
$offset = 0; |
| 13 |
|
| 14 |
sub publish_insert |
| 15 |
{ |
| 16 |
my ($tbl, $new_i) = @_; |
| 17 |
$node_publisher->safe_psql( |
| 18 |
'postgres', qq( |
| 19 |
SET SESSION AUTHORIZATION regress_alice; |
| 20 |
INSERT INTO $tbl (i) VALUES ($new_i); |
| 21 |
)); |
| 22 |
} |
| 23 |
|
| 24 |
sub publish_update |
| 25 |
{ |
| 26 |
my ($tbl, $old_i, $new_i) = @_; |
| 27 |
$node_publisher->safe_psql( |
| 28 |
'postgres', qq( |
| 29 |
SET SESSION AUTHORIZATION regress_alice; |
| 30 |
UPDATE $tbl SET i = $new_i WHERE i = $old_i; |
| 31 |
)); |
| 32 |
} |
| 33 |
|
| 34 |
sub publish_delete |
| 35 |
{ |
| 36 |
my ($tbl, $old_i) = @_; |
| 37 |
$node_publisher->safe_psql( |
| 38 |
'postgres', qq( |
| 39 |
SET SESSION AUTHORIZATION regress_alice; |
| 40 |
DELETE FROM $tbl WHERE i = $old_i; |
| 41 |
)); |
| 42 |
} |
| 43 |
|
| 44 |
sub expect_replication |
| 45 |
{ |
| 46 |
my ($tbl, $cnt, $min, $max, $testname) = @_; |
| 47 |
$node_publisher->wait_for_catchup('admin_sub'); |
| 48 |
$result = $node_subscriber->safe_psql( |
| 49 |
'postgres', qq( |
| 50 |
SELECT COUNT(i), MIN(i), MAX(i) FROM $tbl)); |
| 51 |
is($result, "$cnt|$min|$max", $testname); |
| 52 |
} |
| 53 |
|
| 54 |
sub expect_failure |
| 55 |
{ |
| 56 |
my ($tbl, $cnt, $min, $max, $re, $testname) = @_; |
| 57 |
$offset = $node_subscriber->wait_for_log($re, $offset); |
| 58 |
$result = $node_subscriber->safe_psql( |
| 59 |
'postgres', qq( |
| 60 |
SELECT COUNT(i), MIN(i), MAX(i) FROM $tbl)); |
| 61 |
is($result, "$cnt|$min|$max", $testname); |
| 62 |
} |
| 63 |
|
| 64 |
sub revoke_superuser |
| 65 |
{ |
| 66 |
my ($role) = @_; |
| 67 |
$node_subscriber->safe_psql( |
| 68 |
'postgres', qq( |
| 69 |
ALTER ROLE $role NOSUPERUSER)); |
| 70 |
} |
| 71 |
|
| 72 |
sub grant_superuser |
| 73 |
{ |
| 74 |
my ($role) = @_; |
| 75 |
$node_subscriber->safe_psql( |
| 76 |
'postgres', qq( |
| 77 |
ALTER ROLE $role SUPERUSER)); |
| 78 |
} |
| 79 |
|
| 80 |
# Create publisher and subscriber nodes with schemas owned and published by |
| 81 |
# "regress_alice" but subscribed and replicated by different role |
| 82 |
# "regress_admin". For partitioned tables, layout the partitions differently |
| 83 |
# on the publisher than on the subscriber. |
| 84 |
# |
| 85 |
$node_publisher = PostgreSQL::Test::Cluster->new('publisher'); |
| 86 |
$node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); |
| 87 |
$node_publisher->init(allows_streaming => 'logical'); |
| 88 |
$node_subscriber->init; |
| 89 |
$node_publisher->start; |
| 90 |
$node_subscriber->start; |
| 91 |
$publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; |
| 92 |
my %remainder_a = ( |
| 93 |
publisher => 0, |
| 94 |
subscriber => 1); |
| 95 |
my %remainder_b = ( |
| 96 |
publisher => 1, |
| 97 |
subscriber => 0); |
| 98 |
|
| 99 |
for my $node ($node_publisher, $node_subscriber) |
| 100 |
{ |
| 101 |
my $remainder_a = $remainder_a{ $node->name }; |
| 102 |
my $remainder_b = $remainder_b{ $node->name }; |
| 103 |
$node->safe_psql( |
| 104 |
'postgres', qq( |
| 105 |
CREATE ROLE regress_admin SUPERUSER LOGIN; |
| 106 |
CREATE ROLE regress_alice NOSUPERUSER LOGIN; |
| 107 |
GRANT CREATE ON DATABASE postgres TO regress_alice; |
| 108 |
GRANT PG_CREATE_SUBSCRIPTION TO regress_alice; |
| 109 |
SET SESSION AUTHORIZATION regress_alice; |
| 110 |
CREATE SCHEMA alice; |
| 111 |
GRANT USAGE ON SCHEMA alice TO regress_admin; |
| 112 |
|
| 113 |
CREATE TABLE alice.unpartitioned (i INTEGER); |
| 114 |
ALTER TABLE alice.unpartitioned REPLICA IDENTITY FULL; |
| 115 |
GRANT SELECT ON TABLE alice.unpartitioned TO regress_admin; |
| 116 |
|
| 117 |
CREATE TABLE alice.hashpart (i INTEGER) PARTITION BY HASH (i); |
| 118 |
ALTER TABLE alice.hashpart REPLICA IDENTITY FULL; |
| 119 |
GRANT SELECT ON TABLE alice.hashpart TO regress_admin; |
| 120 |
CREATE TABLE alice.hashpart_a PARTITION OF alice.hashpart |
| 121 |
FOR VALUES WITH (MODULUS 2, REMAINDER $remainder_a); |
| 122 |
ALTER TABLE alice.hashpart_a REPLICA IDENTITY FULL; |
| 123 |
CREATE TABLE alice.hashpart_b PARTITION OF alice.hashpart |
| 124 |
FOR VALUES WITH (MODULUS 2, REMAINDER $remainder_b); |
| 125 |
ALTER TABLE alice.hashpart_b REPLICA IDENTITY FULL; |
| 126 |
)); |
| 127 |
} |
| 128 |
$node_publisher->safe_psql( |
| 129 |
'postgres', qq( |
| 130 |
SET SESSION AUTHORIZATION regress_alice; |
| 131 |
|
| 132 |
CREATE PUBLICATION alice |
| 133 |
FOR TABLE alice.unpartitioned, alice.hashpart |
| 134 |
WITH (publish_via_partition_root = true); |
| 135 |
)); |
| 136 |
$node_subscriber->safe_psql( |
| 137 |
'postgres', qq( |
| 138 |
SET SESSION AUTHORIZATION regress_admin; |
| 139 |
CREATE SUBSCRIPTION admin_sub CONNECTION '$publisher_connstr' PUBLICATION alice WITH (password_required=false); |
| 140 |
)); |
| 141 |
|
| 142 |
# Wait for initial sync to finish |
| 143 |
$node_subscriber->wait_for_subscription_sync($node_publisher, 'admin_sub'); |
| 144 |
|
| 145 |
# Verify that "regress_admin" can replicate into the tables |
| 146 |
# |
| 147 |
publish_insert("alice.unpartitioned", 1); |
| 148 |
publish_insert("alice.unpartitioned", 3); |
| 149 |
publish_insert("alice.unpartitioned", 5); |
| 150 |
publish_update("alice.unpartitioned", 1 => 7); |
| 151 |
publish_delete("alice.unpartitioned", 3); |
| 152 |
expect_replication("alice.unpartitioned", 2, 5, 7, |
| 153 |
"superuser admin replicates into unpartitioned"); |
| 154 |
|
| 155 |
# Revoke and restore superuser privilege for "regress_admin", |
| 156 |
# verifying that replication fails while superuser privilege is |
| 157 |
# missing, but works again and catches up once superuser is restored. |
| 158 |
# |
| 159 |
revoke_superuser("regress_admin"); |
| 160 |
publish_update("alice.unpartitioned", 5 => 9); |
| 161 |
expect_failure( |
| 162 |
"alice.unpartitioned", |
| 163 |
2, |
| 164 |
5, |
| 165 |
7, |
| 166 |
qr/ERROR: ( [A-Z0-9]+:)? role "regress_admin" cannot SET ROLE to "regress_alice"/msi, |
| 167 |
"non-superuser admin fails to replicate update"); |
| 168 |
grant_superuser("regress_admin"); |
| 169 |
expect_replication("alice.unpartitioned", 2, 7, 9, |
| 170 |
"admin with restored superuser privilege replicates update"); |
| 171 |
|
| 172 |
# Privileges on the target role suffice for non-superuser replication. |
| 173 |
$node_subscriber->safe_psql( |
| 174 |
'postgres', qq( |
| 175 |
ALTER ROLE regress_admin NOSUPERUSER; |
| 176 |
GRANT regress_alice TO regress_admin; |
| 177 |
)); |
| 178 |
|
| 179 |
publish_insert("alice.unpartitioned", 11); |
| 180 |
expect_replication("alice.unpartitioned", 3, 7, 11, |
| 181 |
"nosuperuser admin with privileges on role can replicate INSERT into unpartitioned" |
| 182 |
); |
| 183 |
|
| 184 |
publish_update("alice.unpartitioned", 7 => 13); |
| 185 |
expect_replication("alice.unpartitioned", 3, 9, 13, |
| 186 |
"nosuperuser admin with privileges on role can replicate UPDATE into unpartitioned" |
| 187 |
); |
| 188 |
|
| 189 |
publish_delete("alice.unpartitioned", 9); |
| 190 |
expect_replication("alice.unpartitioned", 2, 11, 13, |
| 191 |
"nosuperuser admin with privileges on role can replicate DELETE into unpartitioned" |
| 192 |
); |
| 193 |
|
| 194 |
# Test partitioning |
| 195 |
# |
| 196 |
publish_insert("alice.hashpart", 101); |
| 197 |
publish_insert("alice.hashpart", 102); |
| 198 |
publish_insert("alice.hashpart", 103); |
| 199 |
publish_update("alice.hashpart", 102 => 120); |
| 200 |
publish_delete("alice.hashpart", 101); |
| 201 |
expect_replication("alice.hashpart", 2, 103, 120, |
| 202 |
"nosuperuser admin with privileges on role can replicate into hashpart"); |
| 203 |
|
| 204 |
# Force RLS on the target table and check that replication fails. |
| 205 |
$node_subscriber->safe_psql( |
| 206 |
'postgres', qq( |
| 207 |
SET SESSION AUTHORIZATION regress_alice; |
| 208 |
ALTER TABLE alice.unpartitioned ENABLE ROW LEVEL SECURITY; |
| 209 |
ALTER TABLE alice.unpartitioned FORCE ROW LEVEL SECURITY; |
| 210 |
)); |
| 211 |
|
| 212 |
publish_insert("alice.unpartitioned", 15); |
| 213 |
expect_failure( |
| 214 |
"alice.unpartitioned", |
| 215 |
2, |
| 216 |
11, |
| 217 |
13, |
| 218 |
qr/ERROR: ( [A-Z0-9]+:)? user "regress_alice" cannot replicate into relation with row-level security enabled: "unpartitioned\w*"/msi, |
| 219 |
"replication of insert into table with forced rls fails"); |
| 220 |
|
| 221 |
# Since replication acts as the table owner, replication will succeed if we don't force it. |
| 222 |
$node_subscriber->safe_psql( |
| 223 |
'postgres', qq( |
| 224 |
ALTER TABLE alice.unpartitioned NO FORCE ROW LEVEL SECURITY; |
| 225 |
)); |
| 226 |
expect_replication("alice.unpartitioned", 3, 11, 15, |
| 227 |
"non-superuser admin can replicate insert if rls is not forced"); |
| 228 |
|
| 229 |
$node_subscriber->safe_psql( |
| 230 |
'postgres', qq( |
| 231 |
ALTER TABLE alice.unpartitioned FORCE ROW LEVEL SECURITY; |
| 232 |
)); |
| 233 |
publish_update("alice.unpartitioned", 11 => 17); |
| 234 |
expect_failure( |
| 235 |
"alice.unpartitioned", |
| 236 |
3, |
| 237 |
11, |
| 238 |
15, |
| 239 |
qr/ERROR: ( [A-Z0-9]+:)? user "regress_alice" cannot replicate into relation with row-level security enabled: "unpartitioned\w*"/msi, |
| 240 |
"replication of update into table with forced rls fails"); |
| 241 |
$node_subscriber->safe_psql( |
| 242 |
'postgres', qq( |
| 243 |
ALTER TABLE alice.unpartitioned NO FORCE ROW LEVEL SECURITY; |
| 244 |
)); |
| 245 |
expect_replication("alice.unpartitioned", 3, 13, 17, |
| 246 |
"non-superuser admin can replicate update if rls is not forced"); |
| 247 |
|
| 248 |
# Remove some of alice's privileges on her own table. Then replication should fail. |
| 249 |
$node_subscriber->safe_psql( |
| 250 |
'postgres', qq( |
| 251 |
REVOKE SELECT, INSERT ON alice.unpartitioned FROM regress_alice; |
| 252 |
)); |
| 253 |
publish_insert("alice.unpartitioned", 19); |
| 254 |
expect_failure( |
| 255 |
"alice.unpartitioned", |
| 256 |
3, |
| 257 |
13, |
| 258 |
17, |
| 259 |
qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi, |
| 260 |
"replication of insert fails if table owner lacks insert permission"); |
| 261 |
|
| 262 |
# alice needs INSERT but not SELECT to replicate an INSERT. |
| 263 |
$node_subscriber->safe_psql( |
| 264 |
'postgres', qq( |
| 265 |
GRANT INSERT ON alice.unpartitioned TO regress_alice; |
| 266 |
)); |
| 267 |
expect_replication("alice.unpartitioned", 4, 13, 19, |
| 268 |
"restoring insert permission permits replication to continue"); |
| 269 |
|
| 270 |
# Now let's try an UPDATE and a DELETE. |
| 271 |
$node_subscriber->safe_psql( |
| 272 |
'postgres', qq( |
| 273 |
REVOKE UPDATE, DELETE ON alice.unpartitioned FROM regress_alice; |
| 274 |
)); |
| 275 |
publish_update("alice.unpartitioned", 13 => 21); |
| 276 |
publish_delete("alice.unpartitioned", 15); |
| 277 |
expect_failure( |
| 278 |
"alice.unpartitioned", |
| 279 |
4, |
| 280 |
13, |
| 281 |
19, |
| 282 |
qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi, |
| 283 |
"replication of update/delete fails if table owner lacks corresponding permission" |
| 284 |
); |
| 285 |
|
| 286 |
# Restoring UPDATE and DELETE is insufficient. |
| 287 |
$node_subscriber->safe_psql( |
| 288 |
'postgres', qq( |
| 289 |
GRANT UPDATE, DELETE ON alice.unpartitioned TO regress_alice; |
| 290 |
)); |
| 291 |
expect_failure( |
| 292 |
"alice.unpartitioned", |
| 293 |
4, |
| 294 |
13, |
| 295 |
19, |
| 296 |
qr/ERROR: ( [A-Z0-9]+:)? permission denied for table unpartitioned/msi, |
| 297 |
"replication of update/delete fails if table owner lacks SELECT permission" |
| 298 |
); |
| 299 |
|
| 300 |
# alice needs INSERT but not SELECT to replicate an INSERT. |
| 301 |
$node_subscriber->safe_psql( |
| 302 |
'postgres', qq( |
| 303 |
GRANT SELECT ON alice.unpartitioned TO regress_alice; |
| 304 |
)); |
| 305 |
expect_replication("alice.unpartitioned", 3, 17, 21, |
| 306 |
"restoring SELECT permission permits replication to continue"); |
| 307 |
|
| 308 |
# The apply worker should get restarted after the superuser privileges are |
| 309 |
# revoked for subscription owner alice. |
| 310 |
grant_superuser("regress_alice"); |
| 311 |
$node_subscriber->safe_psql( |
| 312 |
'postgres', qq( |
| 313 |
SET SESSION AUTHORIZATION regress_alice; |
| 314 |
CREATE SUBSCRIPTION regression_sub CONNECTION '$publisher_connstr' PUBLICATION alice; |
| 315 |
)); |
| 316 |
|
| 317 |
# Wait for initial sync to finish |
| 318 |
$node_subscriber->wait_for_subscription_sync($node_publisher, |
| 319 |
'regression_sub'); |
| 320 |
|
| 321 |
# Check the subscriber log from now on. |
| 322 |
$offset = -s $node_subscriber->logfile; |
| 323 |
|
| 324 |
revoke_superuser("regress_alice"); |
| 325 |
|
| 326 |
# After the user becomes non-superuser the apply worker should be restarted. |
| 327 |
$node_subscriber->wait_for_log( |
| 328 |
qr/LOG: ( [A-Z0-9]+:)? logical replication worker for subscription \"regression_sub\" will restart because the subscription owner's superuser privileges have been revoked/, |
| 329 |
$offset); |
| 330 |
|
| 331 |
# If the subscription connection requires a password ('password_required' |
| 332 |
# is true) then a non-superuser must specify that password in the connection |
| 333 |
# string. |
| 334 |
SKIP: |
| 335 |
{ |
| 336 |
skip |
| 337 |
"subscription password_required test cannot run without Unix-domain sockets", |
| 338 |
3 |
| 339 |
unless $use_unix_sockets; |
| 340 |
|
| 341 |
my $node_publisher1 = PostgreSQL::Test::Cluster->new('publisher1'); |
| 342 |
my $node_subscriber1 = PostgreSQL::Test::Cluster->new('subscriber1'); |
| 343 |
$node_publisher1->init(allows_streaming => 'logical'); |
| 344 |
$node_subscriber1->init; |
| 345 |
$node_publisher1->start; |
| 346 |
$node_subscriber1->start; |
| 347 |
my $publisher_connstr1 = |
| 348 |
$node_publisher1->connstr . ' user=regress_test_user dbname=postgres'; |
| 349 |
my $publisher_connstr2 = |
| 350 |
$node_publisher1->connstr |
| 351 |
. ' user=regress_test_user dbname=postgres password=secret'; |
| 352 |
|
| 353 |
for my $node ($node_publisher1, $node_subscriber1) |
| 354 |
{ |
| 355 |
$node->safe_psql( |
| 356 |
'postgres', qq( |
| 357 |
CREATE ROLE regress_test_user PASSWORD 'secret' LOGIN REPLICATION; |
| 358 |
GRANT CREATE ON DATABASE postgres TO regress_test_user; |
| 359 |
GRANT PG_CREATE_SUBSCRIPTION TO regress_test_user; |
| 360 |
)); |
| 361 |
} |
| 362 |
|
| 363 |
$node_publisher1->safe_psql( |
| 364 |
'postgres', qq( |
| 365 |
SET SESSION AUTHORIZATION regress_test_user; |
| 366 |
CREATE PUBLICATION regress_test_pub; |
| 367 |
)); |
| 368 |
$node_subscriber1->safe_psql( |
| 369 |
'postgres', qq( |
| 370 |
CREATE SUBSCRIPTION regress_test_sub CONNECTION '$publisher_connstr1' PUBLICATION regress_test_pub; |
| 371 |
)); |
| 372 |
|
| 373 |
# Wait for initial sync to finish |
| 374 |
$node_subscriber1->wait_for_subscription_sync($node_publisher1, |
| 375 |
'regress_test_sub'); |
| 376 |
|
| 377 |
my $save_pgpassword = $ENV{"PGPASSWORD"}; |
| 378 |
$ENV{"PGPASSWORD"} = 'secret'; |
| 379 |
|
| 380 |
# Setup pg_hba configuration so that logical replication connection without |
| 381 |
# password is not allowed. |
| 382 |
unlink($node_publisher1->data_dir . '/pg_hba.conf'); |
| 383 |
$node_publisher1->append_conf('pg_hba.conf', |
| 384 |
qq{local all regress_test_user md5}); |
| 385 |
$node_publisher1->reload; |
| 386 |
|
| 387 |
# Change the subscription owner to a non-superuser |
| 388 |
$node_subscriber1->safe_psql( |
| 389 |
'postgres', qq( |
| 390 |
ALTER SUBSCRIPTION regress_test_sub OWNER TO regress_test_user; |
| 391 |
)); |
| 392 |
|
| 393 |
# Non-superuser must specify password in the connection string |
| 394 |
my ($ret, $stdout, $stderr) = $node_subscriber1->psql( |
| 395 |
'postgres', qq( |
| 396 |
SET SESSION AUTHORIZATION regress_test_user; |
| 397 |
ALTER SUBSCRIPTION regress_test_sub REFRESH PUBLICATION; |
| 398 |
)); |
| 399 |
isnt($ret, 0, |
| 400 |
"non zero exit for subscription whose owner is a non-superuser must specify password parameter of the connection string" |
| 401 |
); |
| 402 |
ok( $stderr =~ |
| 403 |
m/DETAIL: Non-superusers must provide a password in the connection string./, |
| 404 |
'subscription whose owner is a non-superuser must specify password parameter of the connection string' |
| 405 |
); |
| 406 |
|
| 407 |
$ENV{"PGPASSWORD"} = $save_pgpassword; |
| 408 |
|
| 409 |
# It should succeed after including the password parameter of the connection |
| 410 |
# string. |
| 411 |
($ret, $stdout, $stderr) = $node_subscriber1->psql( |
| 412 |
'postgres', qq( |
| 413 |
SET SESSION AUTHORIZATION regress_test_user; |
| 414 |
ALTER SUBSCRIPTION regress_test_sub CONNECTION '$publisher_connstr2'; |
| 415 |
ALTER SUBSCRIPTION regress_test_sub REFRESH PUBLICATION; |
| 416 |
)); |
| 417 |
is($ret, 0, |
| 418 |
"Non-superuser will be able to refresh the publication after specifying the password parameter of the connection string" |
| 419 |
); |
| 420 |
} |
| 421 |
done_testing(); |