| 1 |
# Copyright (c) 2024-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Test SCRAM authentication when opening a new connection with a foreign |
| 4 |
# server. |
| 5 |
# |
| 6 |
# The test is executed by testing the SCRAM authentifcation on a loopback |
| 7 |
# connection on the same server and with different servers. |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings FATAL => 'all'; |
| 11 |
use PostgreSQL::Test::Utils; |
| 12 |
use PostgreSQL::Test::Cluster; |
| 13 |
use Test::More; |
| 14 |
|
| 15 |
if (!$use_unix_sockets) |
| 16 |
{ |
| 17 |
plan skip_all => "test requires Unix-domain sockets"; |
| 18 |
} |
| 19 |
|
| 20 |
my $user = "user01"; |
| 21 |
|
| 22 |
my $db0 = "db0"; # For node1 |
| 23 |
my $db1 = "db1"; # For node1 |
| 24 |
my $db2 = "db2"; # For node2 |
| 25 |
my $fdw_server = "db1_fdw"; |
| 26 |
my $fdw_server2 = "db2_fdw"; |
| 27 |
my $fdw_server3 = "db1_fdw_override"; |
| 28 |
my $fdw_invalid_server = "db2_fdw_invalid"; # For invalid fdw options |
| 29 |
my $fdw_invalid_server2 = |
| 30 |
"db2_fdw_invalid2"; # For invalid scram keys fdw options |
| 31 |
|
| 32 |
my $node1 = PostgreSQL::Test::Cluster->new('node1'); |
| 33 |
my $node2 = PostgreSQL::Test::Cluster->new('node2'); |
| 34 |
|
| 35 |
$node1->init; |
| 36 |
$node2->init; |
| 37 |
|
| 38 |
$node1->start; |
| 39 |
$node2->start; |
| 40 |
|
| 41 |
# Test setup |
| 42 |
|
| 43 |
$node1->safe_psql('postgres', qq'CREATE USER $user WITH password \'pass\''); |
| 44 |
$node2->safe_psql('postgres', qq'CREATE USER $user WITH password \'pass\''); |
| 45 |
$ENV{PGPASSWORD} = "pass"; |
| 46 |
|
| 47 |
$node1->safe_psql('postgres', qq'CREATE DATABASE $db0'); |
| 48 |
$node1->safe_psql('postgres', qq'CREATE DATABASE $db1'); |
| 49 |
$node2->safe_psql('postgres', qq'CREATE DATABASE $db2'); |
| 50 |
|
| 51 |
setup_table($node1, $db1, "t"); |
| 52 |
setup_table($node2, $db2, "t2"); |
| 53 |
|
| 54 |
$node1->safe_psql($db0, 'CREATE EXTENSION IF NOT EXISTS dblink'); |
| 55 |
setup_fdw_server($node1, $db0, $fdw_server, $node1, $db1); |
| 56 |
setup_fdw_server($node1, $db0, $fdw_server2, $node2, $db2); |
| 57 |
setup_invalid_fdw_server($node1, $db0, $fdw_invalid_server, $node2, $db2); |
| 58 |
setup_fdw_server($node1, $db0, $fdw_invalid_server2, $node2, $db2); |
| 59 |
setup_fdw_server($node1, $db0, $fdw_server3, $node1, $db1); |
| 60 |
|
| 61 |
setup_user_mapping($node1, $db0, $fdw_server); |
| 62 |
setup_user_mapping($node1, $db0, $fdw_server2); |
| 63 |
setup_user_mapping($node1, $db0, $fdw_invalid_server); |
| 64 |
setup_user_mapping($node1, $db0, $fdw_server3); |
| 65 |
|
| 66 |
# Make the user have the same SCRAM key on both servers. Forcing to have the |
| 67 |
# same iteration and salt. |
| 68 |
my $rolpassword = $node1->safe_psql('postgres', |
| 69 |
qq"SELECT rolpassword FROM pg_authid WHERE rolname = '$user';"); |
| 70 |
$node2->safe_psql('postgres', qq"ALTER ROLE $user PASSWORD '$rolpassword'"); |
| 71 |
|
| 72 |
unlink($node1->data_dir . '/pg_hba.conf'); |
| 73 |
unlink($node2->data_dir . '/pg_hba.conf'); |
| 74 |
|
| 75 |
$node1->append_conf( |
| 76 |
'pg_hba.conf', qq{ |
| 77 |
local db0 all scram-sha-256 |
| 78 |
local db1 all scram-sha-256 |
| 79 |
} |
| 80 |
); |
| 81 |
$node2->append_conf( |
| 82 |
'pg_hba.conf', qq{ |
| 83 |
local db2 all scram-sha-256 |
| 84 |
} |
| 85 |
); |
| 86 |
|
| 87 |
$node1->restart; |
| 88 |
$node2->restart; |
| 89 |
|
| 90 |
# End of test setup |
| 91 |
|
| 92 |
test_scram_keys_is_not_overwritten($node1, $db0, $fdw_invalid_server2); |
| 93 |
|
| 94 |
test_fdw_auth($node1, $db0, "t", $fdw_server, |
| 95 |
"SCRAM auth on the same database cluster must succeed"); |
| 96 |
|
| 97 |
test_fdw_auth($node1, $db0, "t2", $fdw_server2, |
| 98 |
"SCRAM auth on a different database cluster must succeed"); |
| 99 |
|
| 100 |
test_fdw_auth_with_invalid_overwritten_require_auth($fdw_invalid_server); |
| 101 |
|
| 102 |
# Test that use_scram_passthrough=false on user mapping overrides server setting |
| 103 |
{ |
| 104 |
my $connstr = $node1->connstr($db0) . qq' user=$user'; |
| 105 |
|
| 106 |
$node1->safe_psql($db0, |
| 107 |
qq'ALTER USER MAPPING FOR $user SERVER $fdw_server3 OPTIONS(add use_scram_passthrough \'false\')', |
| 108 |
connstr => $connstr |
| 109 |
); |
| 110 |
|
| 111 |
my ($ret, $stdout, $stderr) = $node1->psql( |
| 112 |
$db0, |
| 113 |
"select * from dblink('$fdw_server3', 'select * from t') as t(a int, b int)", |
| 114 |
connstr => $connstr); |
| 115 |
|
| 116 |
is($ret, 3, 'SCRAM passthrough disabled on user mapping should fail'); |
| 117 |
like( |
| 118 |
$stderr, |
| 119 |
qr/password/i, |
| 120 |
'expected password-related error when scram passthrough disabled on user mapping'); |
| 121 |
} |
| 122 |
|
| 123 |
# Ensure that trust connections fail without superuser opt-in. |
| 124 |
unlink($node1->data_dir . '/pg_hba.conf'); |
| 125 |
unlink($node2->data_dir . '/pg_hba.conf'); |
| 126 |
|
| 127 |
$node1->append_conf( |
| 128 |
'pg_hba.conf', qq{ |
| 129 |
local db0 all scram-sha-256 |
| 130 |
local db1 all trust |
| 131 |
} |
| 132 |
); |
| 133 |
$node2->append_conf( |
| 134 |
'pg_hba.conf', qq{ |
| 135 |
local all all password |
| 136 |
} |
| 137 |
); |
| 138 |
|
| 139 |
$node1->restart; |
| 140 |
$node2->restart; |
| 141 |
|
| 142 |
my ($ret, $stdout, $stderr) = $node1->psql( |
| 143 |
$db0, |
| 144 |
"SELECT * FROM dblink('$fdw_server', 'SELECT * FROM t') AS t(a int, b int)", |
| 145 |
connstr => $node1->connstr($db0) . " user=$user"); |
| 146 |
|
| 147 |
is($ret, 3, 'loopback trust fails on the same cluster'); |
| 148 |
like( |
| 149 |
$stderr, |
| 150 |
qr/failed: authentication method requirement "scram-sha-256" failed: server did not complete authentication/, |
| 151 |
'expected error from loopback trust (same cluster)'); |
| 152 |
|
| 153 |
($ret, $stdout, $stderr) = $node1->psql( |
| 154 |
$db0, |
| 155 |
"SELECT * FROM dblink('$fdw_server2', 'SELECT * FROM t2') AS t2(a int, b int)", |
| 156 |
connstr => $node1->connstr($db0) . " user=$user"); |
| 157 |
|
| 158 |
is($ret, 3, 'loopback password fails on a different cluster'); |
| 159 |
like( |
| 160 |
$stderr, |
| 161 |
qr/authentication method requirement "scram-sha-256" failed: server requested a cleartext password/, |
| 162 |
'expected error from loopback password (different cluster)'); |
| 163 |
|
| 164 |
# Helper functions |
| 165 |
|
| 166 |
sub test_fdw_auth |
| 167 |
{ |
| 168 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 169 |
|
| 170 |
my ($node, $db, $tbl, $fdw, $testname) = @_; |
| 171 |
my $connstr = $node->connstr($db) . qq' user=$user'; |
| 172 |
|
| 173 |
my $ret = $node->safe_psql( |
| 174 |
$db, |
| 175 |
qq"SELECT count(1) FROM dblink('$fdw', 'SELECT * FROM $tbl') AS $tbl(a int, b int)", |
| 176 |
connstr => $connstr); |
| 177 |
|
| 178 |
is($ret, '10', $testname); |
| 179 |
} |
| 180 |
|
| 181 |
sub test_fdw_auth_with_invalid_overwritten_require_auth |
| 182 |
{ |
| 183 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 184 |
|
| 185 |
my ($fdw) = @_; |
| 186 |
|
| 187 |
my ($ret, $stdout, $stderr) = $node1->psql( |
| 188 |
$db0, |
| 189 |
"select * from dblink('$fdw', 'select * from t') as t(a int, b int)", |
| 190 |
connstr => $node1->connstr($db0) . " user=$user"); |
| 191 |
|
| 192 |
is($ret, 3, 'loopback trust fails when overwriting require_auth'); |
| 193 |
like( |
| 194 |
$stderr, |
| 195 |
qr/password or GSSAPI delegated credentials required/, |
| 196 |
'expected error when connecting to a fdw overwriting the require_auth' |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
sub test_scram_keys_is_not_overwritten |
| 201 |
{ |
| 202 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 203 |
|
| 204 |
my ($node, $db, $fdw) = @_; |
| 205 |
|
| 206 |
my ($ret, $stdout, $stderr) = $node->psql( |
| 207 |
$db, |
| 208 |
qq'CREATE USER MAPPING FOR $user SERVER $fdw OPTIONS (user \'$user\', scram_client_key \'key\');', |
| 209 |
connstr => $node->connstr($db) . " user=$user"); |
| 210 |
|
| 211 |
is($ret, 3, 'user mapping creation fails when using scram_client_key'); |
| 212 |
like( |
| 213 |
$stderr, |
| 214 |
qr/ERROR: invalid option "scram_client_key"/, |
| 215 |
'user mapping creation fails when using scram_client_key'); |
| 216 |
|
| 217 |
($ret, $stdout, $stderr) = $node->psql( |
| 218 |
$db, |
| 219 |
qq'CREATE USER MAPPING FOR $user SERVER $fdw OPTIONS (user \'$user\', scram_server_key \'key\');', |
| 220 |
connstr => $node->connstr($db) . " user=$user"); |
| 221 |
|
| 222 |
is($ret, 3, 'user mapping creation fails when using scram_server_key'); |
| 223 |
like( |
| 224 |
$stderr, |
| 225 |
qr/ERROR: invalid option "scram_server_key"/, |
| 226 |
'user mapping creation fails when using scram_server_key'); |
| 227 |
} |
| 228 |
|
| 229 |
sub setup_user_mapping |
| 230 |
{ |
| 231 |
my ($node, $db, $fdw) = @_; |
| 232 |
|
| 233 |
$node->safe_psql($db, |
| 234 |
qq'CREATE USER MAPPING FOR $user SERVER $fdw OPTIONS (user \'$user\');' |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
sub setup_fdw_server |
| 239 |
{ |
| 240 |
my ($node, $db, $fdw, $fdw_node, $dbname) = @_; |
| 241 |
my $host = $fdw_node->host; |
| 242 |
my $port = $fdw_node->port; |
| 243 |
|
| 244 |
$node->safe_psql( |
| 245 |
$db, qq'CREATE SERVER $fdw FOREIGN DATA WRAPPER dblink_fdw options ( |
| 246 |
host \'$host\', port \'$port\', dbname \'$dbname\', use_scram_passthrough \'true\') ' |
| 247 |
); |
| 248 |
$node->safe_psql($db, qq'GRANT USAGE ON FOREIGN SERVER $fdw TO $user;'); |
| 249 |
$node->safe_psql($db, qq'GRANT ALL ON SCHEMA public TO $user'); |
| 250 |
} |
| 251 |
|
| 252 |
sub setup_invalid_fdw_server |
| 253 |
{ |
| 254 |
my ($node, $db, $fdw, $fdw_node, $dbname) = @_; |
| 255 |
my $host = $fdw_node->host; |
| 256 |
my $port = $fdw_node->port; |
| 257 |
|
| 258 |
$node->safe_psql( |
| 259 |
$db, qq'CREATE SERVER $fdw FOREIGN DATA WRAPPER dblink_fdw options ( |
| 260 |
host \'$host\', port \'$port\', dbname \'$dbname\', use_scram_passthrough \'true\', require_auth \'none\') ' |
| 261 |
); |
| 262 |
$node->safe_psql($db, qq'GRANT USAGE ON FOREIGN SERVER $fdw TO $user;'); |
| 263 |
$node->safe_psql($db, qq'GRANT ALL ON SCHEMA public TO $user'); |
| 264 |
} |
| 265 |
|
| 266 |
sub setup_table |
| 267 |
{ |
| 268 |
my ($node, $db, $tbl) = @_; |
| 269 |
|
| 270 |
$node->safe_psql($db, |
| 271 |
qq'CREATE TABLE $tbl AS SELECT g as a, g + 1 as b FROM generate_series(1,10) g(g)' |
| 272 |
); |
| 273 |
$node->safe_psql($db, qq'GRANT USAGE ON SCHEMA public TO $user'); |
| 274 |
$node->safe_psql($db, qq'GRANT SELECT ON $tbl TO $user'); |
| 275 |
} |
| 276 |
|
| 277 |
done_testing(); |