| 1 |
|
| 2 |
# |
| 3 |
# Tests the libpq builtin OAuth flow, as well as server-side HBA and validator |
| 4 |
# setup. |
| 5 |
# |
| 6 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
use warnings FATAL => 'all'; |
| 11 |
|
| 12 |
use JSON::PP qw(encode_json); |
| 13 |
use MIME::Base64 qw(encode_base64); |
| 14 |
use PostgreSQL::Test::Cluster; |
| 15 |
use PostgreSQL::Test::Utils; |
| 16 |
use Test::More; |
| 17 |
|
| 18 |
use FindBin; |
| 19 |
use lib $FindBin::RealBin; |
| 20 |
|
| 21 |
use OAuth::Server; |
| 22 |
|
| 23 |
if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\boauth\b/) |
| 24 |
{ |
| 25 |
plan skip_all => |
| 26 |
'Potentially unsafe test oauth not enabled in PG_TEST_EXTRA'; |
| 27 |
} |
| 28 |
|
| 29 |
unless (check_pg_config("#define HAVE_SYS_EVENT_H 1") |
| 30 |
or check_pg_config("#define HAVE_SYS_EPOLL_H 1")) |
| 31 |
{ |
| 32 |
plan skip_all => |
| 33 |
'OAuth server-side tests are not supported on this platform'; |
| 34 |
} |
| 35 |
|
| 36 |
if ($ENV{with_libcurl} ne 'yes') |
| 37 |
{ |
| 38 |
plan skip_all => 'client-side OAuth not supported by this build'; |
| 39 |
} |
| 40 |
|
| 41 |
if ($ENV{with_python} ne 'yes') |
| 42 |
{ |
| 43 |
plan skip_all => 'OAuth tests require --with-python to run'; |
| 44 |
} |
| 45 |
|
| 46 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 47 |
$node->init; |
| 48 |
$node->append_conf('postgresql.conf', "log_connections = all\n"); |
| 49 |
$node->append_conf('postgresql.conf', |
| 50 |
"oauth_validator_libraries = 'validator'\n"); |
| 51 |
# Needed to allow connect_fails to inspect postmaster log: |
| 52 |
$node->append_conf('postgresql.conf', "log_min_messages = debug2"); |
| 53 |
$node->start; |
| 54 |
|
| 55 |
$node->safe_psql('postgres', 'CREATE USER test;'); |
| 56 |
$node->safe_psql('postgres', 'CREATE USER testalt;'); |
| 57 |
$node->safe_psql('postgres', 'CREATE USER testparam;'); |
| 58 |
|
| 59 |
# Save a background connection for later configuration changes. |
| 60 |
my $bgconn = $node->background_psql('postgres'); |
| 61 |
|
| 62 |
my $webserver = OAuth::Server->new(); |
| 63 |
$webserver->run(); |
| 64 |
|
| 65 |
END |
| 66 |
{ |
| 67 |
my $exit_code = $?; |
| 68 |
|
| 69 |
$webserver->stop() if defined $webserver; # might have been SKIP'd |
| 70 |
|
| 71 |
$? = $exit_code; |
| 72 |
} |
| 73 |
|
| 74 |
my $port = $webserver->port(); |
| 75 |
my $issuer = "http://127.0.0.1:$port"; |
| 76 |
|
| 77 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 78 |
$node->append_conf( |
| 79 |
'pg_hba.conf', qq{ |
| 80 |
local all test oauth issuer="$issuer" scope="openid postgres" |
| 81 |
local all testalt oauth issuer="$issuer/.well-known/oauth-authorization-server/alternate" scope="openid postgres alt" |
| 82 |
local all testparam oauth issuer="$issuer/param" scope="openid postgres" |
| 83 |
}); |
| 84 |
$node->reload; |
| 85 |
|
| 86 |
my $log_start = $node->wait_for_log(qr/reloading configuration files/); |
| 87 |
|
| 88 |
# Check pg_hba_file_rules() support. |
| 89 |
my $contents = $bgconn->query_safe( |
| 90 |
qq(SELECT rule_number, auth_method, options |
| 91 |
FROM pg_hba_file_rules |
| 92 |
ORDER BY rule_number;)); |
| 93 |
is( $contents, |
| 94 |
qq{1|oauth|\{issuer=$issuer,"scope=openid postgres",validator=validator\} |
| 95 |
2|oauth|\{issuer=$issuer/.well-known/oauth-authorization-server/alternate,"scope=openid postgres alt",validator=validator\} |
| 96 |
3|oauth|\{issuer=$issuer/param,"scope=openid postgres",validator=validator\}}, |
| 97 |
"pg_hba_file_rules recreates OAuth HBA settings"); |
| 98 |
|
| 99 |
# To test against HTTP rather than HTTPS, we need to enable PGOAUTHDEBUG. But |
| 100 |
# first, check to make sure the client refuses such connections by default. |
| 101 |
$node->connect_fails( |
| 102 |
"user=test dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0635", |
| 103 |
"HTTPS is required without debug mode", |
| 104 |
expected_stderr => |
| 105 |
qr@OAuth discovery URI "\Q$issuer\E/.well-known/openid-configuration" must use HTTPS@ |
| 106 |
); |
| 107 |
|
| 108 |
$ENV{PGOAUTHDEBUG} = "UNSAFE"; |
| 109 |
|
| 110 |
my $user = "test"; |
| 111 |
$node->connect_ok( |
| 112 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0635", |
| 113 |
"connect as test", |
| 114 |
expected_stderr => |
| 115 |
qr@Visit https://example\.com/ and enter the code: postgresuser@, |
| 116 |
log_like => [ |
| 117 |
qr/oauth_validator: token="9243959234", role="$user"/, |
| 118 |
qr/oauth_validator: issuer="\Q$issuer\E", scope="openid postgres"/, |
| 119 |
qr/connection authenticated: identity="test" method=oauth/, |
| 120 |
qr/connection authorized/, |
| 121 |
]); |
| 122 |
|
| 123 |
# The /alternate issuer uses slightly different parameters, along with an |
| 124 |
# OAuth-style discovery document. |
| 125 |
$user = "testalt"; |
| 126 |
$node->connect_ok( |
| 127 |
"user=$user dbname=postgres oauth_issuer=$issuer/alternate oauth_client_id=f02c6361-0636", |
| 128 |
"connect as testalt", |
| 129 |
expected_stderr => |
| 130 |
qr@Visit https://example\.org/ and enter the code: postgresuser@, |
| 131 |
log_like => [ |
| 132 |
qr/oauth_validator: token="9243959234-alt", role="$user"/, |
| 133 |
qr|oauth_validator: issuer="\Q$issuer/.well-known/oauth-authorization-server/alternate\E", scope="openid postgres alt"|, |
| 134 |
qr/connection authenticated: identity="testalt" method=oauth/, |
| 135 |
qr/connection authorized/, |
| 136 |
]); |
| 137 |
|
| 138 |
# The issuer linked by the server must match the client's oauth_issuer setting. |
| 139 |
$node->connect_fails( |
| 140 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0636", |
| 141 |
"oauth_issuer must match discovery", |
| 142 |
expected_stderr => |
| 143 |
qr@server's discovery document at \Q$issuer/.well-known/oauth-authorization-server/alternate\E \(issuer "\Q$issuer/alternate\E"\) is incompatible with oauth_issuer \(\Q$issuer\E\)@ |
| 144 |
); |
| 145 |
|
| 146 |
# Test require_auth settings against OAUTHBEARER. |
| 147 |
my @cases = ( |
| 148 |
{ require_auth => "oauth" }, |
| 149 |
{ require_auth => "oauth,scram-sha-256" }, |
| 150 |
{ require_auth => "password,oauth" }, |
| 151 |
{ require_auth => "none,oauth" }, |
| 152 |
{ require_auth => "!scram-sha-256" }, |
| 153 |
{ require_auth => "!none" }, |
| 154 |
|
| 155 |
{ |
| 156 |
require_auth => "!oauth", |
| 157 |
failure => qr/server requested OAUTHBEARER authentication/ |
| 158 |
}, |
| 159 |
{ |
| 160 |
require_auth => "scram-sha-256", |
| 161 |
failure => qr/server requested OAUTHBEARER authentication/ |
| 162 |
}, |
| 163 |
{ |
| 164 |
require_auth => "!password,!oauth", |
| 165 |
failure => qr/server requested OAUTHBEARER authentication/ |
| 166 |
}, |
| 167 |
{ |
| 168 |
require_auth => "none", |
| 169 |
failure => qr/server requested SASL authentication/ |
| 170 |
}, |
| 171 |
{ |
| 172 |
require_auth => "!oauth,!scram-sha-256", |
| 173 |
failure => qr/server requested SASL authentication/ |
| 174 |
}); |
| 175 |
|
| 176 |
$user = "test"; |
| 177 |
foreach my $c (@cases) |
| 178 |
{ |
| 179 |
my $connstr = |
| 180 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0635 require_auth=$c->{'require_auth'}"; |
| 181 |
|
| 182 |
if (defined $c->{'failure'}) |
| 183 |
{ |
| 184 |
$node->connect_fails( |
| 185 |
$connstr, |
| 186 |
"require_auth=$c->{'require_auth'} fails", |
| 187 |
expected_stderr => $c->{'failure'}); |
| 188 |
} |
| 189 |
else |
| 190 |
{ |
| 191 |
$node->connect_ok( |
| 192 |
$connstr, |
| 193 |
"require_auth=$c->{'require_auth'} succeeds", |
| 194 |
expected_stderr => |
| 195 |
qr@Visit https://example\.com/ and enter the code: postgresuser@ |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
# Make sure the client_id and secret are correctly encoded. $vschars contains |
| 201 |
# every allowed character for a client_id/_secret (the "VSCHAR" class). |
| 202 |
# $vschars_esc is additionally backslash-escaped for inclusion in a |
| 203 |
# single-quoted connection string. |
| 204 |
my $vschars = |
| 205 |
" !\"#\$%&'()*+,-./0123456789:;<=>?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; |
| 206 |
my $vschars_esc = |
| 207 |
" !\"#\$%&\\'()*+,-./0123456789:;<=>?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; |
| 208 |
|
| 209 |
$node->connect_ok( |
| 210 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id='$vschars_esc'", |
| 211 |
"escapable characters: client_id", |
| 212 |
expected_stderr => |
| 213 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 214 |
$node->connect_ok( |
| 215 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id='$vschars_esc' oauth_client_secret='$vschars_esc'", |
| 216 |
"escapable characters: client_id and secret", |
| 217 |
expected_stderr => |
| 218 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 219 |
|
| 220 |
# |
| 221 |
# Further tests rely on support for specific behaviors in oauth_server.py. To |
| 222 |
# trigger these behaviors, we ask for the special issuer .../param (which is set |
| 223 |
# up in HBA for the testparam user) and encode magic instructions into the |
| 224 |
# oauth_client_id. |
| 225 |
# |
| 226 |
|
| 227 |
my $common_connstr = |
| 228 |
"user=testparam dbname=postgres oauth_issuer=$issuer/param "; |
| 229 |
my $base_connstr = $common_connstr; |
| 230 |
|
| 231 |
sub connstr |
| 232 |
{ |
| 233 |
my (%params) = @_; |
| 234 |
|
| 235 |
my $json = encode_json(\%params); |
| 236 |
my $encoded = encode_base64($json, ""); |
| 237 |
|
| 238 |
return "$base_connstr oauth_client_id=$encoded"; |
| 239 |
} |
| 240 |
|
| 241 |
# Make sure the param system works end-to-end first. |
| 242 |
$node->connect_ok( |
| 243 |
connstr(), |
| 244 |
"connect to /param", |
| 245 |
expected_stderr => |
| 246 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 247 |
|
| 248 |
$node->connect_ok( |
| 249 |
connstr(stage => 'token', retries => 1), |
| 250 |
"token retry", |
| 251 |
expected_stderr => |
| 252 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 253 |
$node->connect_ok( |
| 254 |
connstr(stage => 'token', retries => 2), |
| 255 |
"token retry (twice)", |
| 256 |
expected_stderr => |
| 257 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 258 |
$node->connect_ok( |
| 259 |
connstr(stage => 'all', retries => 1, interval => 2), |
| 260 |
"token retry (two second interval)", |
| 261 |
expected_stderr => |
| 262 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 263 |
$node->connect_ok( |
| 264 |
connstr(stage => 'all', retries => 1, interval => JSON::PP::null), |
| 265 |
"token retry (default interval)", |
| 266 |
expected_stderr => |
| 267 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 268 |
|
| 269 |
$node->connect_ok( |
| 270 |
connstr(stage => 'all', content_type => 'application/json;charset=utf-8'), |
| 271 |
"content type with charset", |
| 272 |
expected_stderr => |
| 273 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 274 |
$node->connect_ok( |
| 275 |
connstr( |
| 276 |
stage => 'all', |
| 277 |
content_type => "application/json \t;\t charset=utf-8"), |
| 278 |
"content type with charset (whitespace)", |
| 279 |
expected_stderr => |
| 280 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 281 |
$node->connect_ok( |
| 282 |
connstr(stage => 'device', uri_spelling => "verification_url"), |
| 283 |
"alternative spelling of verification_uri", |
| 284 |
expected_stderr => |
| 285 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 286 |
|
| 287 |
$node->connect_fails( |
| 288 |
connstr(stage => 'device', huge_response => JSON::PP::true), |
| 289 |
"bad device authz response: overlarge JSON", |
| 290 |
expected_stderr => |
| 291 |
qr/failed to obtain device authorization: response is too large/); |
| 292 |
$node->connect_fails( |
| 293 |
connstr(stage => 'token', huge_response => JSON::PP::true), |
| 294 |
"bad token response: overlarge JSON", |
| 295 |
expected_stderr => |
| 296 |
qr/failed to obtain access token: response is too large/); |
| 297 |
|
| 298 |
my $nesting_limit = 16; |
| 299 |
$node->connect_ok( |
| 300 |
connstr( |
| 301 |
stage => 'device', |
| 302 |
nested_array => $nesting_limit, |
| 303 |
nested_object => $nesting_limit), |
| 304 |
"nested arrays and objects, up to parse limit", |
| 305 |
expected_stderr => |
| 306 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 307 |
$node->connect_fails( |
| 308 |
connstr(stage => 'device', nested_array => $nesting_limit + 1), |
| 309 |
"bad discovery response: overly nested JSON array", |
| 310 |
expected_stderr => |
| 311 |
qr/failed to parse device authorization: JSON is too deeply nested/); |
| 312 |
$node->connect_fails( |
| 313 |
connstr(stage => 'device', nested_object => $nesting_limit + 1), |
| 314 |
"bad discovery response: overly nested JSON object", |
| 315 |
expected_stderr => |
| 316 |
qr/failed to parse device authorization: JSON is too deeply nested/); |
| 317 |
|
| 318 |
$node->connect_fails( |
| 319 |
connstr(stage => 'device', content_type => 'text/plain'), |
| 320 |
"bad device authz response: wrong content type", |
| 321 |
expected_stderr => |
| 322 |
qr/failed to parse device authorization: unexpected content type/); |
| 323 |
$node->connect_fails( |
| 324 |
connstr(stage => 'token', content_type => 'text/plain'), |
| 325 |
"bad token response: wrong content type", |
| 326 |
expected_stderr => |
| 327 |
qr/failed to parse access token response: unexpected content type/); |
| 328 |
$node->connect_fails( |
| 329 |
connstr(stage => 'token', content_type => 'application/jsonx'), |
| 330 |
"bad token response: wrong content type (correct prefix)", |
| 331 |
expected_stderr => |
| 332 |
qr/failed to parse access token response: unexpected content type/); |
| 333 |
|
| 334 |
$node->connect_fails( |
| 335 |
connstr( |
| 336 |
stage => 'all', |
| 337 |
interval => ~0, |
| 338 |
retries => 1, |
| 339 |
retry_code => "slow_down"), |
| 340 |
"bad token response: server overflows the device authz interval", |
| 341 |
expected_stderr => |
| 342 |
qr/failed to obtain access token: slow_down interval overflow/); |
| 343 |
|
| 344 |
$node->connect_fails( |
| 345 |
connstr(stage => 'token', error_code => "invalid_grant"), |
| 346 |
"bad token response: invalid_grant, no description", |
| 347 |
expected_stderr => qr/failed to obtain access token: \(invalid_grant\)/); |
| 348 |
$node->connect_fails( |
| 349 |
connstr( |
| 350 |
stage => 'token', |
| 351 |
error_code => "invalid_grant", |
| 352 |
error_desc => "grant expired"), |
| 353 |
"bad token response: expired grant", |
| 354 |
expected_stderr => |
| 355 |
qr/failed to obtain access token: grant expired \(invalid_grant\)/); |
| 356 |
$node->connect_fails( |
| 357 |
connstr( |
| 358 |
stage => 'token', |
| 359 |
error_code => "invalid_client", |
| 360 |
error_status => 401), |
| 361 |
"bad token response: client authentication failure, default description", |
| 362 |
expected_stderr => |
| 363 |
qr/failed to obtain access token: provider requires client authentication, and no oauth_client_secret is set \(invalid_client\)/ |
| 364 |
); |
| 365 |
$node->connect_fails( |
| 366 |
connstr( |
| 367 |
stage => 'token', |
| 368 |
error_code => "invalid_client", |
| 369 |
error_status => 401, |
| 370 |
error_desc => "authn failure"), |
| 371 |
"bad token response: client authentication failure, provided description", |
| 372 |
expected_stderr => |
| 373 |
qr/failed to obtain access token: authn failure \(invalid_client\)/); |
| 374 |
|
| 375 |
$node->connect_fails( |
| 376 |
connstr(stage => 'token', token => ""), |
| 377 |
"server rejects access: empty token", |
| 378 |
expected_stderr => qr/bearer authentication failed/); |
| 379 |
$node->connect_fails( |
| 380 |
connstr(stage => 'token', token => "****"), |
| 381 |
"server rejects access: invalid token contents", |
| 382 |
expected_stderr => qr/bearer authentication failed/); |
| 383 |
|
| 384 |
# Test behavior of the oauth_client_secret. |
| 385 |
$base_connstr = "$common_connstr oauth_client_secret=''"; |
| 386 |
|
| 387 |
$node->connect_ok( |
| 388 |
connstr(stage => 'all', expected_secret => ''), |
| 389 |
"empty oauth_client_secret", |
| 390 |
expected_stderr => |
| 391 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 392 |
|
| 393 |
$base_connstr = "$common_connstr oauth_client_secret='$vschars_esc'"; |
| 394 |
|
| 395 |
$node->connect_ok( |
| 396 |
connstr(stage => 'all', expected_secret => $vschars), |
| 397 |
"nonempty oauth_client_secret", |
| 398 |
expected_stderr => |
| 399 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 400 |
|
| 401 |
$node->connect_fails( |
| 402 |
connstr( |
| 403 |
stage => 'token', |
| 404 |
error_code => "invalid_client", |
| 405 |
error_status => 401), |
| 406 |
"bad token response: client authentication failure, default description with oauth_client_secret", |
| 407 |
expected_stderr => |
| 408 |
qr/failed to obtain access token: provider rejected the oauth_client_secret \(invalid_client\)/ |
| 409 |
); |
| 410 |
$node->connect_fails( |
| 411 |
connstr( |
| 412 |
stage => 'token', |
| 413 |
error_code => "invalid_client", |
| 414 |
error_status => 401, |
| 415 |
error_desc => "mutual TLS required for client"), |
| 416 |
"bad token response: client authentication failure, provided description with oauth_client_secret", |
| 417 |
expected_stderr => |
| 418 |
qr/failed to obtain access token: mutual TLS required for client \(invalid_client\)/ |
| 419 |
); |
| 420 |
|
| 421 |
# Count the number of calls to the internal flow when multiple retries are |
| 422 |
# triggered. The exact number depends on many things -- the TCP stack, the |
| 423 |
# version of Curl in use, random chance -- but a ridiculously high number |
| 424 |
# suggests something is wrong with our ability to clear multiplexer events after |
| 425 |
# they're no longer applicable. |
| 426 |
my ($ret, $stdout, $stderr) = $node->psql( |
| 427 |
'postgres', |
| 428 |
"SELECT 'connected for call count'", |
| 429 |
extra_params => ['-w'], |
| 430 |
connstr => connstr(stage => 'token', retries => 2), |
| 431 |
on_error_stop => 0); |
| 432 |
|
| 433 |
is($ret, 0, "call count connection succeeds"); |
| 434 |
like( |
| 435 |
$stderr, |
| 436 |
qr@Visit https://example\.com/ and enter the code: postgresuser@, |
| 437 |
"call count: stderr matches"); |
| 438 |
|
| 439 |
SKIP: |
| 440 |
{ |
| 441 |
# Curl 8.20.0 regressed this test case: |
| 442 |
# |
| 443 |
# https://github.com/curl/curl/issues/21547 |
| 444 |
# |
| 445 |
skip 'call-count test is known to fail with libcurl 8.20.0', 2 |
| 446 |
if $stderr =~ m/\Qinitialized libcurl 8.20.0\E/; |
| 447 |
|
| 448 |
my $count_pattern = qr/\[libpq\] total number of polls: (\d+)/; |
| 449 |
if (like($stderr, $count_pattern, "call count: count is printed")) |
| 450 |
{ |
| 451 |
# For reference, a typical flow with two retries might take between 5-15 |
| 452 |
# calls to the client implementation. And while this will probably |
| 453 |
# continue to change across OSes and Curl updates, we're likely in |
| 454 |
# trouble if we see hundreds or thousands of calls. |
| 455 |
$stderr =~ $count_pattern; |
| 456 |
unless (cmp_ok($1, '<', 100, "call count is reasonably small")) |
| 457 |
{ |
| 458 |
diag "full stderr:\n$stderr"; |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
# Stress test: make sure our builtin flow operates correctly even if the client |
| 464 |
# application isn't respecting PGRES_POLLING_READING/WRITING signals returned |
| 465 |
# from PQconnectPoll(). |
| 466 |
$base_connstr = |
| 467 |
"$common_connstr port=" . $node->port . " host=" . $node->host; |
| 468 |
my @cmd = ( |
| 469 |
"oauth_hook_client", "--no-hook", "--stress-async", |
| 470 |
connstr(stage => 'all', retries => 1, interval => 1)); |
| 471 |
|
| 472 |
note "running '" . join("' '", @cmd) . "'"; |
| 473 |
($stdout, $stderr) = run_command(\@cmd); |
| 474 |
|
| 475 |
like($stdout, qr/connection succeeded/, "stress-async: stdout matches"); |
| 476 |
unlike( |
| 477 |
$stderr, |
| 478 |
qr/connection to database failed/, |
| 479 |
"stress-async: stderr matches"); |
| 480 |
|
| 481 |
# |
| 482 |
# This section of tests reconfigures the validator module itself, rather than |
| 483 |
# the OAuth server. |
| 484 |
# |
| 485 |
|
| 486 |
# Searching the logs is easier if OAuth parameter discovery isn't cluttering |
| 487 |
# things up; hardcode the discovery URI. (Scope is hardcoded to empty to cover |
| 488 |
# that case as well.) |
| 489 |
$common_connstr = |
| 490 |
"dbname=postgres oauth_issuer=$issuer/.well-known/openid-configuration oauth_scope='' oauth_client_id=f02c6361-0635"; |
| 491 |
|
| 492 |
# Misbehaving validators must fail shut. |
| 493 |
$bgconn->query_safe("ALTER SYSTEM SET oauth_validator.authn_id TO ''"); |
| 494 |
$node->reload; |
| 495 |
$log_start = |
| 496 |
$node->wait_for_log(qr/reloading configuration files/, $log_start); |
| 497 |
|
| 498 |
$node->connect_fails( |
| 499 |
"$common_connstr user=test", |
| 500 |
"validator must set authn_id", |
| 501 |
expected_stderr => qr/OAuth bearer authentication failed/, |
| 502 |
log_like => [ |
| 503 |
qr/connection authenticated: identity=""/, |
| 504 |
qr/DETAIL:\s+Validator provided no identity/, |
| 505 |
qr/FATAL:\s+OAuth bearer authentication failed/, |
| 506 |
]); |
| 507 |
|
| 508 |
# Even if a validator authenticates the user, if the token isn't considered |
| 509 |
# valid, the connection fails. |
| 510 |
$bgconn->query_safe( |
| 511 |
"ALTER SYSTEM SET oauth_validator.authn_id TO 'test\@example.org'"); |
| 512 |
$bgconn->query_safe( |
| 513 |
"ALTER SYSTEM SET oauth_validator.authorize_tokens TO false"); |
| 514 |
$node->reload; |
| 515 |
$log_start = |
| 516 |
$node->wait_for_log(qr/reloading configuration files/, $log_start); |
| 517 |
|
| 518 |
$node->connect_fails( |
| 519 |
"$common_connstr user=test", |
| 520 |
"validator must authorize token explicitly", |
| 521 |
expected_stderr => qr/OAuth bearer authentication failed/, |
| 522 |
log_like => [ |
| 523 |
qr/connection authenticated: identity="test\@example\.org"/, |
| 524 |
qr/DETAIL:\s+Validator failed to authorize the provided token/, |
| 525 |
qr/FATAL:\s+OAuth bearer authentication failed/, |
| 526 |
]); |
| 527 |
|
| 528 |
# |
| 529 |
# Test user mapping. |
| 530 |
# |
| 531 |
|
| 532 |
# Allow "[email protected]" to log in under the test role. |
| 533 |
unlink($node->data_dir . '/pg_ident.conf'); |
| 534 |
$node->append_conf( |
| 535 |
'pg_ident.conf', qq{ |
| 536 |
oauthmap user\@example.com test |
| 537 |
}); |
| 538 |
|
| 539 |
# test and testalt use the map; testparam uses ident delegation. |
| 540 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 541 |
$node->append_conf( |
| 542 |
'pg_hba.conf', qq{ |
| 543 |
local all test oauth issuer="$issuer" scope="" map=oauthmap |
| 544 |
local all testalt oauth issuer="$issuer" scope="" map=oauthmap |
| 545 |
local all testparam oauth issuer="$issuer" scope="" delegate_ident_mapping=1 |
| 546 |
}); |
| 547 |
|
| 548 |
# To start, have the validator use the role names as authn IDs. |
| 549 |
$bgconn->query_safe("ALTER SYSTEM RESET oauth_validator.authn_id"); |
| 550 |
$bgconn->query_safe("ALTER SYSTEM RESET oauth_validator.authorize_tokens"); |
| 551 |
|
| 552 |
$node->reload; |
| 553 |
$log_start = |
| 554 |
$node->wait_for_log(qr/reloading configuration files/, $log_start); |
| 555 |
|
| 556 |
# The test and testalt roles should no longer map correctly. |
| 557 |
$node->connect_fails( |
| 558 |
"$common_connstr user=test", |
| 559 |
"mismatched username map (test)", |
| 560 |
expected_stderr => qr/OAuth bearer authentication failed/); |
| 561 |
$node->connect_fails( |
| 562 |
"$common_connstr user=testalt", |
| 563 |
"mismatched username map (testalt)", |
| 564 |
expected_stderr => qr/OAuth bearer authentication failed/); |
| 565 |
|
| 566 |
# Have the validator identify the end user as [email protected]. |
| 567 |
$bgconn->query_safe( |
| 568 |
"ALTER SYSTEM SET oauth_validator.authn_id TO 'user\@example.com'"); |
| 569 |
$node->reload; |
| 570 |
$log_start = |
| 571 |
$node->wait_for_log(qr/reloading configuration files/, $log_start); |
| 572 |
|
| 573 |
# Now the test role can be logged into. (testalt still can't be mapped.) |
| 574 |
$node->connect_ok( |
| 575 |
"$common_connstr user=test", |
| 576 |
"matched username map (test)", |
| 577 |
expected_stderr => |
| 578 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 579 |
$node->connect_fails( |
| 580 |
"$common_connstr user=testalt", |
| 581 |
"mismatched username map (testalt)", |
| 582 |
expected_stderr => qr/OAuth bearer authentication failed/); |
| 583 |
|
| 584 |
# testparam ignores the map entirely. |
| 585 |
$node->connect_ok( |
| 586 |
"$common_connstr user=testparam", |
| 587 |
"delegated ident (testparam)", |
| 588 |
expected_stderr => |
| 589 |
qr@Visit https://example\.com/ and enter the code: postgresuser@); |
| 590 |
|
| 591 |
$bgconn->query_safe("ALTER SYSTEM RESET oauth_validator.authn_id"); |
| 592 |
$node->reload; |
| 593 |
$log_start = |
| 594 |
$node->wait_for_log(qr/reloading configuration files/, $log_start); |
| 595 |
|
| 596 |
# |
| 597 |
# Test multiple validators. |
| 598 |
# |
| 599 |
|
| 600 |
$node->append_conf('postgresql.conf', |
| 601 |
"oauth_validator_libraries = 'validator, fail_validator'\n"); |
| 602 |
|
| 603 |
# With multiple validators, every HBA line must explicitly declare one. |
| 604 |
my $result = $node->restart(fail_ok => 1); |
| 605 |
is($result, 0, |
| 606 |
'restart fails without explicit validators in oauth HBA entries'); |
| 607 |
|
| 608 |
$log_start = $node->wait_for_log( |
| 609 |
qr/authentication method "oauth" requires argument "validator" to be set/, |
| 610 |
$log_start); |
| 611 |
|
| 612 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 613 |
$node->append_conf( |
| 614 |
'pg_hba.conf', qq{ |
| 615 |
local all test oauth validator=validator issuer="$issuer" scope="openid postgres" |
| 616 |
local all testalt oauth validator=fail_validator issuer="$issuer/.well-known/oauth-authorization-server/alternate" scope="openid postgres alt" |
| 617 |
}); |
| 618 |
$node->restart; |
| 619 |
|
| 620 |
$log_start = $node->wait_for_log(qr/ready to accept connections/, $log_start); |
| 621 |
|
| 622 |
# The test user should work as before. |
| 623 |
$user = "test"; |
| 624 |
$node->connect_ok( |
| 625 |
"user=$user dbname=postgres oauth_issuer=$issuer oauth_client_id=f02c6361-0635", |
| 626 |
"validator is used for $user", |
| 627 |
expected_stderr => |
| 628 |
qr@Visit https://example\.com/ and enter the code: postgresuser@, |
| 629 |
log_like => [qr/connection authorized/]); |
| 630 |
|
| 631 |
# testalt should be routed through the fail_validator. |
| 632 |
$user = "testalt"; |
| 633 |
$node->connect_fails( |
| 634 |
"user=$user dbname=postgres oauth_issuer=$issuer/.well-known/oauth-authorization-server/alternate oauth_client_id=f02c6361-0636", |
| 635 |
"fail_validator is used for $user", |
| 636 |
expected_stderr => qr/FATAL:\s+fail_validator: sentinel error/); |
| 637 |
|
| 638 |
# |
| 639 |
# Test ABI compatibility magic marker |
| 640 |
# |
| 641 |
$node->append_conf('postgresql.conf', |
| 642 |
"oauth_validator_libraries = 'magic_validator'\n"); |
| 643 |
unlink($node->data_dir . '/pg_hba.conf'); |
| 644 |
$node->append_conf( |
| 645 |
'pg_hba.conf', qq{ |
| 646 |
local all test oauth validator=magic_validator issuer="$issuer" scope="openid postgres" |
| 647 |
}); |
| 648 |
$node->restart; |
| 649 |
|
| 650 |
$log_start = $node->wait_for_log(qr/ready to accept connections/, $log_start); |
| 651 |
|
| 652 |
$node->connect_fails( |
| 653 |
"user=test dbname=postgres oauth_issuer=$issuer/.well-known/oauth-authorization-server/alternate oauth_client_id=f02c6361-0636", |
| 654 |
"magic_validator is used for $user", |
| 655 |
expected_stderr => |
| 656 |
qr/FATAL:\s+OAuth validator module "magic_validator": magic number mismatch/ |
| 657 |
); |
| 658 |
$node->stop; |
| 659 |
|
| 660 |
done_testing(); |