| 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 |
my $hostaddr = '127.0.0.1'; |
| 16 |
my $user = "user01"; |
| 17 |
|
| 18 |
my $db0 = "db0"; # For node1 |
| 19 |
my $db1 = "db1"; # For node1 |
| 20 |
my $db2 = "db2"; # For node2 |
| 21 |
my $fdw_server = "db1_fdw"; |
| 22 |
my $fdw_server2 = "db2_fdw"; |
| 23 |
my $fdw_server3 = "db1_fdw_override"; |
| 24 |
|
| 25 |
my $node1 = PostgreSQL::Test::Cluster->new('node1'); |
| 26 |
my $node2 = PostgreSQL::Test::Cluster->new('node2'); |
| 27 |
|
| 28 |
$node1->init; |
| 29 |
$node2->init; |
| 30 |
|
| 31 |
$node1->start; |
| 32 |
$node2->start; |
| 33 |
|
| 34 |
# Test setup |
| 35 |
|
| 36 |
$node1->safe_psql('postgres', qq'CREATE USER $user WITH password \'pass\''); |
| 37 |
$node2->safe_psql('postgres', qq'CREATE USER $user WITH password \'pass\''); |
| 38 |
$ENV{PGPASSWORD} = "pass"; |
| 39 |
|
| 40 |
$node1->safe_psql('postgres', qq'CREATE DATABASE $db0'); |
| 41 |
$node1->safe_psql('postgres', qq'CREATE DATABASE $db1'); |
| 42 |
$node2->safe_psql('postgres', qq'CREATE DATABASE $db2'); |
| 43 |
|
| 44 |
setup_table($node1, $db1, "t"); |
| 45 |
setup_table($node2, $db2, "t2"); |
| 46 |
|
| 47 |
$node1->safe_psql($db0, 'CREATE EXTENSION IF NOT EXISTS postgres_fdw'); |
| 48 |
setup_fdw_server($node1, $db0, $fdw_server, $node1, $db1); |
| 49 |
setup_fdw_server($node1, $db0, $fdw_server2, $node2, $db2); |
| 50 |
setup_fdw_server($node1, $db0, $fdw_server3, $node1, $db1); |
| 51 |
|
| 52 |
setup_user_mapping($node1, $db0, $fdw_server); |
| 53 |
setup_user_mapping($node1, $db0, $fdw_server2); |
| 54 |
setup_user_mapping($node1, $db0, $fdw_server3); |
| 55 |
|
| 56 |
# Make the user have the same SCRAM key on both servers. Forcing to have the |
| 57 |
# same iteration and salt. |
| 58 |
my $rolpassword = $node1->safe_psql('postgres', |
| 59 |
qq"SELECT rolpassword FROM pg_authid WHERE rolname = '$user';"); |
| 60 |
$node2->safe_psql('postgres', qq"ALTER ROLE $user PASSWORD '$rolpassword'"); |
| 61 |
|
| 62 |
setup_pghba($node1); |
| 63 |
setup_pghba($node2); |
| 64 |
|
| 65 |
# End of test setup |
| 66 |
|
| 67 |
test_fdw_auth($node1, $db0, "t", $fdw_server, |
| 68 |
"SCRAM auth on the same database cluster must succeed"); |
| 69 |
test_fdw_auth($node1, $db0, "t2", $fdw_server2, |
| 70 |
"SCRAM auth on a different database cluster must succeed"); |
| 71 |
test_auth($node2, $db2, "t2", |
| 72 |
"SCRAM auth directly on foreign server should still succeed"); |
| 73 |
|
| 74 |
# Test that use_scram_passthrough=false on user mapping overrides server setting |
| 75 |
{ |
| 76 |
my $connstr = $node1->connstr($db0) . qq' user=$user'; |
| 77 |
|
| 78 |
$node1->safe_psql($db0, |
| 79 |
qq'ALTER USER MAPPING FOR $user SERVER $fdw_server3 OPTIONS(add use_scram_passthrough \'false\')', |
| 80 |
connstr => $connstr |
| 81 |
); |
| 82 |
|
| 83 |
$node1->safe_psql( |
| 84 |
$db0, |
| 85 |
qq'CREATE FOREIGN TABLE override_t (g int, col2 int) SERVER $fdw_server3 OPTIONS (table_name \'t\');', |
| 86 |
connstr => $connstr ); |
| 87 |
$node1->safe_psql($db0, qq'GRANT SELECT ON override_t TO $user;', connstr => $connstr); |
| 88 |
|
| 89 |
my ($ret, $stdout, $stderr) = $node1->psql( |
| 90 |
$db0, |
| 91 |
qq'SELECT count(1) FROM override_t', |
| 92 |
connstr => $connstr); |
| 93 |
|
| 94 |
is($ret, 3, 'SCRAM passthrough disabled on user mapping should fail'); |
| 95 |
like( |
| 96 |
$stderr, |
| 97 |
qr/password/i, |
| 98 |
'expected password-related error when scram passthrough disabled on user mapping'); |
| 99 |
} |
| 100 |
|
| 101 |
SKIP: |
| 102 |
{ |
| 103 |
skip "test requires Unix-domain sockets", 4 if !$use_unix_sockets; |
| 104 |
|
| 105 |
# Ensure that trust connections fail without superuser opt-in. |
| 106 |
unlink($node1->data_dir . '/pg_hba.conf'); |
| 107 |
unlink($node2->data_dir . '/pg_hba.conf'); |
| 108 |
|
| 109 |
$node1->append_conf( |
| 110 |
'pg_hba.conf', qq{ |
| 111 |
local db0 all scram-sha-256 |
| 112 |
local db1 all trust |
| 113 |
} |
| 114 |
); |
| 115 |
$node2->append_conf( |
| 116 |
'pg_hba.conf', qq{ |
| 117 |
local all all password |
| 118 |
} |
| 119 |
); |
| 120 |
|
| 121 |
$node1->restart; |
| 122 |
$node2->restart; |
| 123 |
|
| 124 |
my ($ret, $stdout, $stderr) = $node1->psql( |
| 125 |
$db0, |
| 126 |
qq'select count(1) from t', |
| 127 |
connstr => $node1->connstr($db0) . " user=$user"); |
| 128 |
|
| 129 |
is($ret, 3, 'loopback trust fails on the same cluster'); |
| 130 |
like( |
| 131 |
$stderr, |
| 132 |
qr/failed: authentication method requirement "scram-sha-256"/, |
| 133 |
'expected error from loopback trust (same cluster)'); |
| 134 |
|
| 135 |
($ret, $stdout, $stderr) = $node1->psql( |
| 136 |
$db0, |
| 137 |
qq'select count(1) from t2', |
| 138 |
connstr => $node1->connstr($db0) . " user=$user"); |
| 139 |
|
| 140 |
is($ret, 3, 'loopback password fails on a different cluster'); |
| 141 |
like( |
| 142 |
$stderr, |
| 143 |
qr/failed: authentication method requirement "scram-sha-256"/, |
| 144 |
'expected error from loopback password (different cluster)'); |
| 145 |
} |
| 146 |
|
| 147 |
# Helper functions |
| 148 |
|
| 149 |
sub test_auth |
| 150 |
{ |
| 151 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 152 |
|
| 153 |
my ($node, $db, $tbl, $testname) = @_; |
| 154 |
my $connstr = $node->connstr($db) . qq' user=$user'; |
| 155 |
|
| 156 |
my $ret = $node->safe_psql( |
| 157 |
$db, |
| 158 |
qq'SELECT count(1) FROM $tbl', |
| 159 |
connstr => $connstr); |
| 160 |
|
| 161 |
is($ret, '10', $testname); |
| 162 |
} |
| 163 |
|
| 164 |
sub test_fdw_auth |
| 165 |
{ |
| 166 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 167 |
|
| 168 |
my ($node, $db, $tbl, $fdw, $testname) = @_; |
| 169 |
my $connstr = $node->connstr($db) . qq' user=$user'; |
| 170 |
|
| 171 |
$node->safe_psql( |
| 172 |
$db, |
| 173 |
qq'IMPORT FOREIGN SCHEMA public LIMIT TO ($tbl) FROM SERVER $fdw INTO public;', |
| 174 |
connstr => $connstr); |
| 175 |
|
| 176 |
test_auth($node, $db, $tbl, $testname); |
| 177 |
} |
| 178 |
|
| 179 |
sub setup_pghba |
| 180 |
{ |
| 181 |
my ($node) = @_; |
| 182 |
|
| 183 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 184 |
$node->append_conf( |
| 185 |
'pg_hba.conf', qq{ |
| 186 |
local all all scram-sha-256 |
| 187 |
host all all $hostaddr/32 scram-sha-256 |
| 188 |
}); |
| 189 |
|
| 190 |
$node->restart; |
| 191 |
} |
| 192 |
|
| 193 |
sub setup_user_mapping |
| 194 |
{ |
| 195 |
my ($node, $db, $fdw) = @_; |
| 196 |
|
| 197 |
$node->safe_psql($db, |
| 198 |
qq'CREATE USER MAPPING FOR $user SERVER $fdw OPTIONS (user \'$user\');' |
| 199 |
); |
| 200 |
$node->safe_psql($db, qq'GRANT USAGE ON FOREIGN SERVER $fdw TO $user;'); |
| 201 |
$node->safe_psql($db, qq'GRANT ALL ON SCHEMA public TO $user'); |
| 202 |
} |
| 203 |
|
| 204 |
sub setup_fdw_server |
| 205 |
{ |
| 206 |
my ($node, $db, $fdw, $fdw_node, $dbname) = @_; |
| 207 |
my $host = $fdw_node->host; |
| 208 |
my $port = $fdw_node->port; |
| 209 |
|
| 210 |
$node->safe_psql( |
| 211 |
$db, qq'CREATE SERVER $fdw FOREIGN DATA WRAPPER postgres_fdw options ( |
| 212 |
host \'$host\', port \'$port\', dbname \'$dbname\', use_scram_passthrough \'true\') ' |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
sub setup_table |
| 217 |
{ |
| 218 |
my ($node, $db, $tbl) = @_; |
| 219 |
|
| 220 |
$node->safe_psql($db, |
| 221 |
qq'CREATE TABLE $tbl AS SELECT g, g + 1 FROM generate_series(1,10) g(g)' |
| 222 |
); |
| 223 |
$node->safe_psql($db, qq'GRANT USAGE ON SCHEMA public TO $user'); |
| 224 |
$node->safe_psql($db, qq'GRANT SELECT ON $tbl TO $user'); |
| 225 |
} |
| 226 |
|
| 227 |
done_testing(); |