| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# OVERVIEW |
| 5 |
# -------- |
| 6 |
# |
| 7 |
# Test negotiation of SSL and GSSAPI encryption |
| 8 |
# |
| 9 |
# We test all combinations of: |
| 10 |
# |
| 11 |
# - all the libpq client options that affect the protocol negotiations |
| 12 |
# (gssencmode, sslmode, sslnegotiation) |
| 13 |
# - server accepting or rejecting the authentication due to |
| 14 |
# pg_hba.conf entries |
| 15 |
# - SSL and GSS enabled/disabled in the server |
| 16 |
# |
| 17 |
# That's a lot of combinations, so we use a table-driven approach. |
| 18 |
# Each combination is represented by a line in a table. The line lists |
| 19 |
# the options specifying the test case, and an expected outcome. The |
| 20 |
# expected outcome includes whether the connection succeeds or fails, |
| 21 |
# and whether it uses SSL, GSS or no encryption. It also includes a |
| 22 |
# condensed trace of what steps were taken during the negotiation. |
| 23 |
# That can catch cases like useless retries, or if the encryption |
| 24 |
# methods are attempted in wrong order, even when it doesn't affect |
| 25 |
# the final outcome. |
| 26 |
# |
| 27 |
# TEST TABLE FORMAT |
| 28 |
# ----------------- |
| 29 |
# |
| 30 |
# Example of the test table format: |
| 31 |
# |
| 32 |
# # USER GSSENCMODE SSLMODE EVENTS -> OUTCOME |
| 33 |
# testuser disable allow connect, authok -> plain |
| 34 |
# . . prefer connect, sslaccept, authok -> ssl |
| 35 |
# testuser require * connect, gssreject -> fail |
| 36 |
# |
| 37 |
# USER, GSSENCMODE and SSLMODE fields are the libpq 'user', |
| 38 |
# 'gssencmode' and 'sslmode' options used in the test. As a shorthand, |
| 39 |
# a single dot ('.') can be used in the USER, GSSENCMODE, and SSLMODE |
| 40 |
# fields, to indicate "same as on previous line". A '*' can be used |
| 41 |
# as a wildcard; it is expanded to mean all possible values of that |
| 42 |
# field. |
| 43 |
# |
| 44 |
# The EVENTS field is a condensed trace of expected steps during the |
| 45 |
# negotiation: |
| 46 |
# |
| 47 |
# connect: a TCP connection was established |
| 48 |
# reconnect: TCP connection was disconnected, and a new one was established |
| 49 |
# sslaccept: client requested SSL encryption and server accepted it |
| 50 |
# sslreject: client requested SSL encryption but server rejected it |
| 51 |
# gssaccept: client requested GSSAPI encryption and server accepted it |
| 52 |
# gssreject: client requested GSSAPI encryption but server rejected it |
| 53 |
# authok: client sent startup packet and authentication was performed successfully |
| 54 |
# authfail: client sent startup packet but server rejected the authentication |
| 55 |
# |
| 56 |
# The event trace can be used to verify that the client negotiated the |
| 57 |
# connection properly in more detail than just by looking at the |
| 58 |
# outcome. For example, if the client opens spurious extra TCP |
| 59 |
# connections, that would show up in the EVENTS. |
| 60 |
# |
| 61 |
# The OUTCOME field indicates the expected result of the test: |
| 62 |
# |
| 63 |
# plain: an unencrypted connection was established |
| 64 |
# ssl: SSL connection was established |
| 65 |
# gss: GSSAPI encrypted connection was established |
| 66 |
# fail: the connection attempt failed |
| 67 |
# |
| 68 |
# Empty lines are ignored. '#' can be used to mark the rest of the |
| 69 |
# line as a comment. |
| 70 |
|
| 71 |
use strict; |
| 72 |
use warnings FATAL => 'all'; |
| 73 |
use PostgreSQL::Test::Utils; |
| 74 |
use PostgreSQL::Test::Cluster; |
| 75 |
use PostgreSQL::Test::Kerberos; |
| 76 |
use File::Basename; |
| 77 |
use File::Copy; |
| 78 |
use Test::More; |
| 79 |
|
| 80 |
if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\blibpq_encryption\b/) |
| 81 |
{ |
| 82 |
plan skip_all => |
| 83 |
'Potentially unsafe test libpq_encryption not enabled in PG_TEST_EXTRA'; |
| 84 |
} |
| 85 |
|
| 86 |
# Only run the GSSAPI tests when compiled with GSSAPI support and |
| 87 |
# PG_TEST_EXTRA includes 'kerberos' |
| 88 |
my $gss_supported = $ENV{with_gssapi} eq 'yes'; |
| 89 |
my $kerberos_enabled = |
| 90 |
$ENV{PG_TEST_EXTRA} && $ENV{PG_TEST_EXTRA} =~ /\bkerberos\b/; |
| 91 |
my $ssl_supported = $ENV{with_ssl} eq 'openssl'; |
| 92 |
|
| 93 |
### |
| 94 |
### Prepare test server for GSSAPI and SSL authentication, with a few |
| 95 |
### different test users and helper functions. We don't actually |
| 96 |
### enable SSL and kerberos in the server yet, we will do that later. |
| 97 |
### |
| 98 |
|
| 99 |
my $host = 'enc-test-localhost.postgresql.example.com'; |
| 100 |
my $hostaddr = '127.0.0.1'; |
| 101 |
my $servercidr = '127.0.0.1/32'; |
| 102 |
|
| 103 |
my $node = PostgreSQL::Test::Cluster->new('node'); |
| 104 |
$node->init; |
| 105 |
$node->append_conf( |
| 106 |
'postgresql.conf', qq{ |
| 107 |
listen_addresses = '$hostaddr' |
| 108 |
|
| 109 |
# Capturing the EVENTS that occur during tests requires these settings |
| 110 |
log_connections = 'receipt,authentication,authorization' |
| 111 |
log_disconnections = on |
| 112 |
trace_connection_negotiation = on |
| 113 |
lc_messages = 'C' |
| 114 |
}); |
| 115 |
my $pgdata = $node->data_dir; |
| 116 |
|
| 117 |
my $dbname = 'postgres'; |
| 118 |
my $username = 'enctest'; |
| 119 |
my $application = '001_negotiate_encryption.pl'; |
| 120 |
|
| 121 |
my $gssuser_password = 'secret1'; |
| 122 |
|
| 123 |
my $krb; |
| 124 |
|
| 125 |
if ($gss_supported != 0 && $kerberos_enabled != 0) |
| 126 |
{ |
| 127 |
note "setting up Kerberos"; |
| 128 |
|
| 129 |
my $realm = 'EXAMPLE.COM'; |
| 130 |
$krb = PostgreSQL::Test::Kerberos->new($host, $hostaddr, $realm); |
| 131 |
$node->append_conf('postgresql.conf', |
| 132 |
"krb_server_keyfile = '$krb->{keytab}'\n"); |
| 133 |
} |
| 134 |
|
| 135 |
if ($ssl_supported != 0) |
| 136 |
{ |
| 137 |
my $certdir = dirname(__FILE__) . "/../../../test/ssl/ssl"; |
| 138 |
|
| 139 |
copy "$certdir/server-cn-only.crt", "$pgdata/server.crt" |
| 140 |
|| die "copying server.crt: $!"; |
| 141 |
copy "$certdir/server-cn-only.key", "$pgdata/server.key" |
| 142 |
|| die "copying server.key: $!"; |
| 143 |
chmod(0600, "$pgdata/server.key") |
| 144 |
or die "failed to change permissions on server keys: $!"; |
| 145 |
|
| 146 |
# Start with SSL disabled. |
| 147 |
$node->append_conf('postgresql.conf', "ssl = off\n"); |
| 148 |
} |
| 149 |
|
| 150 |
$node->start; |
| 151 |
|
| 152 |
# Check if the extension injection_points is available, as it may be |
| 153 |
# possible that this script is run with installcheck, where the module |
| 154 |
# would not be installed by default. |
| 155 |
my $injection_points_supported = $node->check_extension('injection_points'); |
| 156 |
|
| 157 |
$node->safe_psql('postgres', 'CREATE USER localuser;'); |
| 158 |
$node->safe_psql('postgres', 'CREATE USER testuser;'); |
| 159 |
$node->safe_psql('postgres', 'CREATE USER ssluser;'); |
| 160 |
$node->safe_psql('postgres', 'CREATE USER nossluser;'); |
| 161 |
$node->safe_psql('postgres', 'CREATE USER gssuser;'); |
| 162 |
$node->safe_psql('postgres', 'CREATE USER nogssuser;'); |
| 163 |
if ($injection_points_supported != 0) |
| 164 |
{ |
| 165 |
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;'); |
| 166 |
} |
| 167 |
|
| 168 |
my $unixdir = $node->safe_psql('postgres', 'SHOW unix_socket_directories;'); |
| 169 |
chomp($unixdir); |
| 170 |
|
| 171 |
# Helper function that returns the encryption method in use in the |
| 172 |
# connection. |
| 173 |
$node->safe_psql( |
| 174 |
'postgres', q{ |
| 175 |
CREATE FUNCTION current_enc() RETURNS text LANGUAGE plpgsql AS $$ |
| 176 |
DECLARE |
| 177 |
ssl_in_use bool; |
| 178 |
gss_in_use bool; |
| 179 |
BEGIN |
| 180 |
ssl_in_use = (SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()); |
| 181 |
gss_in_use = (SELECT encrypted FROM pg_stat_gssapi WHERE pid = pg_backend_pid()); |
| 182 |
|
| 183 |
raise log 'ssl % gss %', ssl_in_use, gss_in_use; |
| 184 |
|
| 185 |
IF ssl_in_use AND gss_in_use THEN |
| 186 |
RETURN 'ssl+gss'; -- shouldn't happen |
| 187 |
ELSIF ssl_in_use THEN |
| 188 |
RETURN 'ssl'; |
| 189 |
ELSIF gss_in_use THEN |
| 190 |
RETURN 'gss'; |
| 191 |
ELSE |
| 192 |
RETURN 'plain'; |
| 193 |
END IF; |
| 194 |
END; |
| 195 |
$$; |
| 196 |
}); |
| 197 |
|
| 198 |
# Only accept SSL connections from $servercidr. Our tests don't depend on this |
| 199 |
# but seems best to keep it as narrow as possible for security reasons. |
| 200 |
open my $hba, '>', "$pgdata/pg_hba.conf" or die $!; |
| 201 |
print $hba qq{ |
| 202 |
# TYPE DATABASE USER ADDRESS METHOD OPTIONS |
| 203 |
local postgres localuser trust |
| 204 |
host postgres testuser $servercidr trust |
| 205 |
hostnossl postgres nossluser $servercidr trust |
| 206 |
hostnogssenc postgres nogssuser $servercidr trust |
| 207 |
}; |
| 208 |
|
| 209 |
print $hba qq{ |
| 210 |
hostssl postgres ssluser $servercidr trust |
| 211 |
} if ($ssl_supported != 0); |
| 212 |
|
| 213 |
print $hba qq{ |
| 214 |
hostgssenc postgres gssuser $servercidr trust |
| 215 |
} if ($gss_supported != 0 && $kerberos_enabled != 0); |
| 216 |
close $hba; |
| 217 |
$node->reload; |
| 218 |
|
| 219 |
# Ok, all prepared. Run the tests. |
| 220 |
|
| 221 |
my @all_test_users = |
| 222 |
('testuser', 'ssluser', 'nossluser', 'gssuser', 'nogssuser'); |
| 223 |
my @all_gssencmodes = ('disable', 'prefer', 'require'); |
| 224 |
my @all_sslmodes = ('disable', 'allow', 'prefer', 'require'); |
| 225 |
my @all_sslnegotiations = ('postgres', 'direct'); |
| 226 |
|
| 227 |
### |
| 228 |
### Run tests with GSS and SSL disabled in the server |
| 229 |
### |
| 230 |
my $test_table; |
| 231 |
if ($ssl_supported) |
| 232 |
{ |
| 233 |
$test_table = q{ |
| 234 |
# USER GSSENCMODE SSLMODE SSLNEGOTIATION EVENTS -> OUTCOME |
| 235 |
testuser disable disable postgres connect, authok -> plain |
| 236 |
. . allow postgres connect, authok -> plain |
| 237 |
. . prefer postgres connect, sslreject, authok -> plain |
| 238 |
. . require postgres connect, sslreject -> fail |
| 239 |
. . . direct connect, directsslreject -> fail |
| 240 |
. prefer disable postgres connect, authok -> plain |
| 241 |
. . allow postgres connect, authok -> plain |
| 242 |
. . prefer postgres connect, sslreject, authok -> plain |
| 243 |
. . require postgres connect, sslreject -> fail |
| 244 |
. . . direct connect, directsslreject -> fail |
| 245 |
|
| 246 |
# sslnegotiation=direct is not accepted unless sslmode=require or stronger |
| 247 |
* * disable direct - -> fail |
| 248 |
* * allow direct - -> fail |
| 249 |
* * prefer direct - -> fail |
| 250 |
}; |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
# Compiled without SSL support |
| 255 |
$test_table = q{ |
| 256 |
# USER GSSENCMODE SSLMODE SSLNEGOTIATION EVENTS -> OUTCOME |
| 257 |
testuser disable disable postgres connect, authok -> plain |
| 258 |
. . allow postgres connect, authok -> plain |
| 259 |
. . prefer postgres connect, authok -> plain |
| 260 |
. prefer disable postgres connect, authok -> plain |
| 261 |
. . allow postgres connect, authok -> plain |
| 262 |
. . prefer postgres connect, authok -> plain |
| 263 |
|
| 264 |
# Without SSL support, sslmode=require and sslnegotiation=direct are |
| 265 |
# not accepted at all |
| 266 |
* * require * - -> fail |
| 267 |
* * * direct - -> fail |
| 268 |
}; |
| 269 |
} |
| 270 |
|
| 271 |
# All attempts with gssencmode=require fail without connecting because |
| 272 |
# no credential cache has been configured in the client. (Or if GSS |
| 273 |
# support is not compiled in, they will fail because of that.) |
| 274 |
$test_table .= q{ |
| 275 |
testuser require * * - -> fail |
| 276 |
}; |
| 277 |
|
| 278 |
note("Running tests with SSL and GSS disabled in the server"); |
| 279 |
test_matrix($node, |
| 280 |
['testuser'], \@all_gssencmodes, \@all_sslmodes, \@all_sslnegotiations, |
| 281 |
parse_table($test_table)); |
| 282 |
|
| 283 |
|
| 284 |
### |
| 285 |
### Run tests with GSS disabled and SSL enabled in the server |
| 286 |
### |
| 287 |
SKIP: |
| 288 |
{ |
| 289 |
skip "SSL not supported by this build" if $ssl_supported == 0; |
| 290 |
|
| 291 |
$test_table = q{ |
| 292 |
# USER GSSENCMODE SSLMODE SSLNEGOTIATION EVENTS -> OUTCOME |
| 293 |
testuser disable disable postgres connect, authok -> plain |
| 294 |
. . allow postgres connect, authok -> plain |
| 295 |
. . prefer postgres connect, sslaccept, authok -> ssl |
| 296 |
. . require postgres connect, sslaccept, authok -> ssl |
| 297 |
. . . direct connect, directsslaccept, authok -> ssl |
| 298 |
ssluser . disable postgres connect, authfail -> fail |
| 299 |
. . allow postgres connect, authfail, reconnect, sslaccept, authok -> ssl |
| 300 |
. . prefer postgres connect, sslaccept, authok -> ssl |
| 301 |
. . require postgres connect, sslaccept, authok -> ssl |
| 302 |
. . . direct connect, directsslaccept, authok -> ssl |
| 303 |
nossluser . disable postgres connect, authok -> plain |
| 304 |
. . allow postgres connect, authok -> plain |
| 305 |
. . prefer postgres connect, sslaccept, authfail, reconnect, authok -> plain |
| 306 |
. . require postgres connect, sslaccept, authfail -> fail |
| 307 |
. . require direct connect, directsslaccept, authfail -> fail |
| 308 |
|
| 309 |
# sslnegotiation=direct is not accepted unless sslmode=require or stronger |
| 310 |
* * disable direct - -> fail |
| 311 |
* * allow direct - -> fail |
| 312 |
* * prefer direct - -> fail |
| 313 |
}; |
| 314 |
|
| 315 |
# Enable SSL in the server |
| 316 |
$node->adjust_conf('postgresql.conf', 'ssl', 'on'); |
| 317 |
$node->reload; |
| 318 |
|
| 319 |
note("Running tests with SSL enabled in server"); |
| 320 |
test_matrix($node, [ 'testuser', 'ssluser', 'nossluser' ], |
| 321 |
['disable'], \@all_sslmodes, \@all_sslnegotiations, |
| 322 |
parse_table($test_table)); |
| 323 |
|
| 324 |
if ($injection_points_supported != 0) |
| 325 |
{ |
| 326 |
$node->safe_psql( |
| 327 |
'postgres', |
| 328 |
"SELECT injection_points_attach('backend-initialize', 'error');", |
| 329 |
connstr => "user=localuser host=$unixdir"); |
| 330 |
connect_test( |
| 331 |
$node, |
| 332 |
"user=testuser sslmode=prefer", |
| 333 |
'connect, backenderror -> fail'); |
| 334 |
$node->restart; |
| 335 |
|
| 336 |
$node->safe_psql( |
| 337 |
'postgres', |
| 338 |
"SELECT injection_points_attach('backend-initialize-v2-error', 'error');", |
| 339 |
connstr => "user=localuser host=$unixdir"); |
| 340 |
connect_test( |
| 341 |
$node, |
| 342 |
"user=testuser sslmode=prefer", |
| 343 |
'connect, v2error -> fail'); |
| 344 |
$node->restart; |
| 345 |
|
| 346 |
$node->safe_psql( |
| 347 |
'postgres', |
| 348 |
"SELECT injection_points_attach('backend-ssl-startup', 'error');", |
| 349 |
connstr => "user=localuser host=$unixdir"); |
| 350 |
connect_test( |
| 351 |
$node, |
| 352 |
"user=testuser sslmode=prefer", |
| 353 |
'connect, sslaccept, backenderror, reconnect, authok -> plain'); |
| 354 |
$node->restart; |
| 355 |
} |
| 356 |
|
| 357 |
# Disable SSL again |
| 358 |
$node->adjust_conf('postgresql.conf', 'ssl', 'off'); |
| 359 |
$node->reload; |
| 360 |
} |
| 361 |
|
| 362 |
### |
| 363 |
### Run tests with GSS enabled, SSL disabled in the server |
| 364 |
### |
| 365 |
SKIP: |
| 366 |
{ |
| 367 |
skip "GSSAPI/Kerberos not supported by this build" if $gss_supported == 0; |
| 368 |
skip "kerberos not enabled in PG_TEST_EXTRA" if $kerberos_enabled == 0; |
| 369 |
|
| 370 |
$krb->create_principal('gssuser', $gssuser_password); |
| 371 |
$krb->create_ticket('gssuser', $gssuser_password); |
| 372 |
|
| 373 |
$test_table = q{ |
| 374 |
# USER GSSENCMODE SSLMODE SSLNEGOTIATION EVENTS -> OUTCOME |
| 375 |
testuser disable disable postgres connect, authok -> plain |
| 376 |
. . allow postgres connect, authok -> plain |
| 377 |
. . prefer postgres connect, sslreject, authok -> plain |
| 378 |
. . require postgres connect, sslreject -> fail |
| 379 |
. . . direct connect, directsslreject -> fail |
| 380 |
. prefer * postgres connect, gssaccept, authok -> gss |
| 381 |
. prefer require direct connect, gssaccept, authok -> gss |
| 382 |
. require * postgres connect, gssaccept, authok -> gss |
| 383 |
. . require direct connect, gssaccept, authok -> gss |
| 384 |
|
| 385 |
gssuser disable disable postgres connect, authfail -> fail |
| 386 |
. . allow postgres connect, authfail, reconnect, sslreject -> fail |
| 387 |
. . prefer postgres connect, sslreject, authfail -> fail |
| 388 |
. . require postgres connect, sslreject -> fail |
| 389 |
. . . direct connect, directsslreject -> fail |
| 390 |
. prefer * postgres connect, gssaccept, authok -> gss |
| 391 |
. prefer require direct connect, gssaccept, authok -> gss |
| 392 |
. require * postgres connect, gssaccept, authok -> gss |
| 393 |
. . require direct connect, gssaccept, authok -> gss |
| 394 |
|
| 395 |
nogssuser disable disable postgres connect, authok -> plain |
| 396 |
. . allow postgres connect, authok -> plain |
| 397 |
. . prefer postgres connect, sslreject, authok -> plain |
| 398 |
. . require postgres connect, sslreject -> fail |
| 399 |
. . . direct connect, directsslreject -> fail |
| 400 |
. prefer disable postgres connect, gssaccept, authfail, reconnect, authok -> plain |
| 401 |
. . allow postgres connect, gssaccept, authfail, reconnect, authok -> plain |
| 402 |
. . prefer postgres connect, gssaccept, authfail, reconnect, sslreject, authok -> plain |
| 403 |
. . require postgres connect, gssaccept, authfail, reconnect, sslreject -> fail |
| 404 |
. . . direct connect, gssaccept, authfail, reconnect, directsslreject -> fail |
| 405 |
. require disable postgres connect, gssaccept, authfail -> fail |
| 406 |
. . allow postgres connect, gssaccept, authfail -> fail |
| 407 |
. . prefer postgres connect, gssaccept, authfail -> fail |
| 408 |
. . require postgres connect, gssaccept, authfail -> fail # If both GSSAPI and sslmode are required, and GSS is not available -> fail |
| 409 |
. . . direct connect, gssaccept, authfail -> fail # If both GSSAPI and sslmode are required, and GSS is not available -> fail |
| 410 |
|
| 411 |
# sslnegotiation=direct is not accepted unless sslmode=require or stronger |
| 412 |
* * disable direct - -> fail |
| 413 |
* * allow direct - -> fail |
| 414 |
* * prefer direct - -> fail |
| 415 |
}; |
| 416 |
|
| 417 |
# The expected events and outcomes above assume that SSL support |
| 418 |
# is enabled. When libpq is compiled without SSL support, all |
| 419 |
# attempts to connect with sslmode=require or |
| 420 |
# sslnegotiation=direct would fail immediately without even |
| 421 |
# connecting to the server. Skip those, because we tested them |
| 422 |
# earlier already. |
| 423 |
my ($sslmodes, $sslnegotiations); |
| 424 |
if ($ssl_supported != 0) |
| 425 |
{ |
| 426 |
($sslmodes, $sslnegotiations) = |
| 427 |
(\@all_sslmodes, \@all_sslnegotiations); |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
($sslmodes, $sslnegotiations) = (['disable'], ['postgres']); |
| 432 |
} |
| 433 |
|
| 434 |
note("Running tests with GSS enabled in server"); |
| 435 |
test_matrix($node, [ 'testuser', 'gssuser', 'nogssuser' ], |
| 436 |
\@all_gssencmodes, $sslmodes, $sslnegotiations, |
| 437 |
parse_table($test_table)); |
| 438 |
|
| 439 |
if ($injection_points_supported != 0) |
| 440 |
{ |
| 441 |
$node->safe_psql( |
| 442 |
'postgres', |
| 443 |
"SELECT injection_points_attach('backend-initialize', 'error');", |
| 444 |
connstr => "user=localuser host=$unixdir"); |
| 445 |
connect_test( |
| 446 |
$node, |
| 447 |
"user=testuser gssencmode=prefer sslmode=disable", |
| 448 |
'connect, backenderror, reconnect, backenderror -> fail'); |
| 449 |
$node->restart; |
| 450 |
|
| 451 |
$node->safe_psql( |
| 452 |
'postgres', |
| 453 |
"SELECT injection_points_attach('backend-initialize-v2-error', 'error');", |
| 454 |
connstr => "user=localuser host=$unixdir"); |
| 455 |
connect_test( |
| 456 |
$node, |
| 457 |
"user=testuser gssencmode=prefer sslmode=disable", |
| 458 |
'connect, v2error, reconnect, v2error -> fail'); |
| 459 |
$node->restart; |
| 460 |
|
| 461 |
$node->safe_psql( |
| 462 |
'postgres', |
| 463 |
"SELECT injection_points_attach('backend-gssapi-startup', 'error');", |
| 464 |
connstr => "user=localuser host=$unixdir"); |
| 465 |
connect_test( |
| 466 |
$node, |
| 467 |
"user=testuser gssencmode=prefer sslmode=disable", |
| 468 |
'connect, gssaccept, backenderror, reconnect, authok -> plain'); |
| 469 |
$node->restart; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
### |
| 474 |
### Tests with both GSS and SSL enabled in the server |
| 475 |
### |
| 476 |
SKIP: |
| 477 |
{ |
| 478 |
skip "SSL not supported by this build" if $ssl_supported == 0; |
| 479 |
skip "GSSAPI/Kerberos not supported by this build" if $gss_supported == 0; |
| 480 |
skip "kerberos not enabled in PG_TEST_EXTRA" if $kerberos_enabled == 0; |
| 481 |
|
| 482 |
# Sanity check that GSSAPI is still enabled from previous test. |
| 483 |
connect_test( |
| 484 |
$node, |
| 485 |
'user=testuser gssencmode=prefer sslmode=prefer', |
| 486 |
'connect, gssaccept, authok -> gss'); |
| 487 |
|
| 488 |
# Enable SSL |
| 489 |
$node->adjust_conf('postgresql.conf', 'ssl', 'on'); |
| 490 |
$node->reload; |
| 491 |
|
| 492 |
$test_table = q{ |
| 493 |
# USER GSSENCMODE SSLMODE SSLNEGOTIATION EVENTS -> OUTCOME |
| 494 |
testuser disable disable postgres connect, authok -> plain |
| 495 |
. . allow postgres connect, authok -> plain |
| 496 |
. . prefer postgres connect, sslaccept, authok -> ssl |
| 497 |
. . require postgres connect, sslaccept, authok -> ssl |
| 498 |
. . . direct connect, directsslaccept, authok -> ssl |
| 499 |
. prefer disable postgres connect, gssaccept, authok -> gss |
| 500 |
. . allow postgres connect, gssaccept, authok -> gss |
| 501 |
. . prefer postgres connect, gssaccept, authok -> gss |
| 502 |
. . require postgres connect, gssaccept, authok -> gss # If both GSS and SSL is possible, GSS is chosen over SSL, even if sslmode=require |
| 503 |
. . . direct connect, gssaccept, authok -> gss |
| 504 |
. require disable postgres connect, gssaccept, authok -> gss |
| 505 |
. . allow postgres connect, gssaccept, authok -> gss |
| 506 |
. . prefer postgres connect, gssaccept, authok -> gss |
| 507 |
. . require postgres connect, gssaccept, authok -> gss # If both GSS and SSL is possible, GSS is chosen over SSL, even if sslmode=require |
| 508 |
. . . direct connect, gssaccept, authok -> gss |
| 509 |
|
| 510 |
gssuser disable disable postgres connect, authfail -> fail |
| 511 |
. . allow postgres connect, authfail, reconnect, sslaccept, authfail -> fail |
| 512 |
. . prefer postgres connect, sslaccept, authfail, reconnect, authfail -> fail |
| 513 |
. . require postgres connect, sslaccept, authfail -> fail |
| 514 |
. . . direct connect, directsslaccept, authfail -> fail |
| 515 |
. prefer disable postgres connect, gssaccept, authok -> gss |
| 516 |
. . allow postgres connect, gssaccept, authok -> gss |
| 517 |
. . prefer postgres connect, gssaccept, authok -> gss |
| 518 |
. . require postgres connect, gssaccept, authok -> gss # GSS is chosen over SSL, even though sslmode=require |
| 519 |
. . . direct connect, gssaccept, authok -> gss |
| 520 |
. require disable postgres connect, gssaccept, authok -> gss |
| 521 |
. . allow postgres connect, gssaccept, authok -> gss |
| 522 |
. . prefer postgres connect, gssaccept, authok -> gss |
| 523 |
. . require postgres connect, gssaccept, authok -> gss # If both GSS and SSL is possible, GSS is chosen over SSL, even if sslmode=require |
| 524 |
. . . direct connect, gssaccept, authok -> gss |
| 525 |
|
| 526 |
ssluser disable disable postgres connect, authfail -> fail |
| 527 |
. . allow postgres connect, authfail, reconnect, sslaccept, authok -> ssl |
| 528 |
. . prefer postgres connect, sslaccept, authok -> ssl |
| 529 |
. . require postgres connect, sslaccept, authok -> ssl |
| 530 |
. . . direct connect, directsslaccept, authok -> ssl |
| 531 |
. prefer disable postgres connect, gssaccept, authfail, reconnect, authfail -> fail |
| 532 |
. . allow postgres connect, gssaccept, authfail, reconnect, authfail, reconnect, sslaccept, authok -> ssl |
| 533 |
. . prefer postgres connect, gssaccept, authfail, reconnect, sslaccept, authok -> ssl |
| 534 |
. . require postgres connect, gssaccept, authfail, reconnect, sslaccept, authok -> ssl |
| 535 |
. . . direct connect, gssaccept, authfail, reconnect, directsslaccept, authok -> ssl |
| 536 |
. require disable postgres connect, gssaccept, authfail -> fail |
| 537 |
. . allow postgres connect, gssaccept, authfail -> fail |
| 538 |
. . prefer postgres connect, gssaccept, authfail -> fail |
| 539 |
. . require postgres connect, gssaccept, authfail -> fail # If both GSS and SSL are required, the sslmode=require is effectively ignored and GSS is required |
| 540 |
. . . direct connect, gssaccept, authfail -> fail |
| 541 |
|
| 542 |
nogssuser disable disable postgres connect, authok -> plain |
| 543 |
. . allow postgres connect, authok -> plain |
| 544 |
. . prefer postgres connect, sslaccept, authok -> ssl |
| 545 |
. . require postgres connect, sslaccept, authok -> ssl |
| 546 |
. . . direct connect, directsslaccept, authok -> ssl |
| 547 |
. prefer disable postgres connect, gssaccept, authfail, reconnect, authok -> plain |
| 548 |
. . allow postgres connect, gssaccept, authfail, reconnect, authok -> plain |
| 549 |
. . prefer postgres connect, gssaccept, authfail, reconnect, sslaccept, authok -> ssl |
| 550 |
. . require postgres connect, gssaccept, authfail, reconnect, sslaccept, authok -> ssl |
| 551 |
. . . direct connect, gssaccept, authfail, reconnect, directsslaccept, authok -> ssl |
| 552 |
. require disable postgres connect, gssaccept, authfail -> fail |
| 553 |
. . allow postgres connect, gssaccept, authfail -> fail |
| 554 |
. . prefer postgres connect, gssaccept, authfail -> fail |
| 555 |
. . require postgres connect, gssaccept, authfail -> fail # If both GSS and SSL are required, the sslmode=require is effectively ignored and GSS is required |
| 556 |
. . . direct connect, gssaccept, authfail -> fail |
| 557 |
|
| 558 |
nossluser disable disable postgres connect, authok -> plain |
| 559 |
. . allow postgres connect, authok -> plain |
| 560 |
. . prefer postgres connect, sslaccept, authfail, reconnect, authok -> plain |
| 561 |
. . require postgres connect, sslaccept, authfail -> fail |
| 562 |
. . . direct connect, directsslaccept, authfail -> fail |
| 563 |
. prefer * postgres connect, gssaccept, authok -> gss |
| 564 |
. . require direct connect, gssaccept, authok -> gss |
| 565 |
. require * postgres connect, gssaccept, authok -> gss |
| 566 |
. . require direct connect, gssaccept, authok -> gss |
| 567 |
|
| 568 |
# sslnegotiation=direct is not accepted unless sslmode=require or stronger |
| 569 |
* * disable direct - -> fail |
| 570 |
* * allow direct - -> fail |
| 571 |
* * prefer direct - -> fail |
| 572 |
}; |
| 573 |
|
| 574 |
note("Running tests with both GSS and SSL enabled in server"); |
| 575 |
test_matrix( |
| 576 |
$node, |
| 577 |
[ 'testuser', 'gssuser', 'ssluser', 'nogssuser', 'nossluser' ], |
| 578 |
\@all_gssencmodes, |
| 579 |
\@all_sslmodes, |
| 580 |
\@all_sslnegotiations, |
| 581 |
parse_table($test_table)); |
| 582 |
} |
| 583 |
|
| 584 |
### |
| 585 |
### Test negotiation over unix domain sockets. |
| 586 |
### |
| 587 |
SKIP: |
| 588 |
{ |
| 589 |
skip "Unix domain sockets not supported" unless ($unixdir ne ""); |
| 590 |
|
| 591 |
# libpq doesn't attempt SSL or GSSAPI over Unix domain |
| 592 |
# sockets. The server would reject them too. |
| 593 |
connect_test( |
| 594 |
$node, |
| 595 |
"user=localuser gssencmode=prefer sslmode=prefer host=$unixdir", |
| 596 |
'connect, authok -> plain'); |
| 597 |
connect_test($node, |
| 598 |
"user=localuser gssencmode=require sslmode=prefer host=$unixdir", |
| 599 |
'- -> fail'); |
| 600 |
} |
| 601 |
|
| 602 |
done_testing(); |
| 603 |
|
| 604 |
|
| 605 |
### Helper functions |
| 606 |
|
| 607 |
# Test the cube of parameters: user, gssencmode, sslmode, and sslnegotiation |
| 608 |
sub test_matrix |
| 609 |
{ |
| 610 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 611 |
|
| 612 |
my ($pg_node, $test_users, $gssencmodes, $sslmodes, $sslnegotiations, |
| 613 |
%expected) |
| 614 |
= @_; |
| 615 |
|
| 616 |
foreach my $test_user (@{$test_users}) |
| 617 |
{ |
| 618 |
foreach my $gssencmode (@{$gssencmodes}) |
| 619 |
{ |
| 620 |
foreach my $client_mode (@{$sslmodes}) |
| 621 |
{ |
| 622 |
# sslnegotiation only makes a difference if SSL is enabled. This saves a few combinations. |
| 623 |
my ($key, $expected_events); |
| 624 |
foreach my $negotiation (@{$sslnegotiations}) |
| 625 |
{ |
| 626 |
$key = "$test_user $gssencmode $client_mode $negotiation"; |
| 627 |
$expected_events = $expected{$key}; |
| 628 |
if (!defined($expected_events)) |
| 629 |
{ |
| 630 |
$expected_events = |
| 631 |
"<line missing from expected output table>"; |
| 632 |
} |
| 633 |
connect_test( |
| 634 |
$pg_node, |
| 635 |
"user=$test_user gssencmode=$gssencmode sslmode=$client_mode sslnegotiation=$negotiation", |
| 636 |
$expected_events); |
| 637 |
} |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
# Try to establish a connection to the server using libpq. Verify the |
| 644 |
# negotiation events and outcome. |
| 645 |
sub connect_test |
| 646 |
{ |
| 647 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 648 |
|
| 649 |
my ($node, $connstr, $expected_events_and_outcome) = @_; |
| 650 |
|
| 651 |
my $test_name = " '$connstr' -> $expected_events_and_outcome"; |
| 652 |
|
| 653 |
my $connstr_full = ""; |
| 654 |
$connstr_full .= "dbname=postgres " unless $connstr =~ m/dbname=/; |
| 655 |
$connstr_full .= "host=$host hostaddr=$hostaddr " |
| 656 |
unless $connstr =~ m/host=/; |
| 657 |
$connstr_full .= $connstr; |
| 658 |
|
| 659 |
# Get the current size of the logfile before running the test. |
| 660 |
# After the test, we can then check just the new lines that have |
| 661 |
# appeared. (This is the same approach that the $node->log_contains |
| 662 |
# function uses). |
| 663 |
my $log_location = -s $node->logfile; |
| 664 |
|
| 665 |
# XXX: Pass command with -c, because I saw intermittent test |
| 666 |
# failures like this: |
| 667 |
# |
| 668 |
# ack Broken pipe: write( 13, 'SELECT current_enc()' ) at /usr/local/lib/perl5/site_perl/IPC/Run/IO.pm line 550. |
| 669 |
# |
| 670 |
# I think that happens if the connection fails before we write the |
| 671 |
# query to its stdin. This test gets a lot of connection failures |
| 672 |
# on purpose. |
| 673 |
my ($ret, $stdout, $stderr) = $node->psql( |
| 674 |
'postgres', |
| 675 |
'', |
| 676 |
extra_params => [ |
| 677 |
'--no-password', '--command' => 'SELECT current_enc()', |
| 678 |
], |
| 679 |
connstr => "$connstr_full", |
| 680 |
on_error_stop => 0); |
| 681 |
|
| 682 |
my $outcome = $ret == 0 ? $stdout : 'fail'; |
| 683 |
|
| 684 |
# Parse the EVENTS from the log file. |
| 685 |
my $log_contents = |
| 686 |
PostgreSQL::Test::Utils::slurp_file($node->logfile, $log_location); |
| 687 |
my @events = parse_log_events($log_contents); |
| 688 |
|
| 689 |
# Check that the events and outcome match the expected events and |
| 690 |
# outcome |
| 691 |
my $events_and_outcome = join(', ', @events) . " -> $outcome"; |
| 692 |
is($events_and_outcome, $expected_events_and_outcome, $test_name) |
| 693 |
or diag("$stderr"); |
| 694 |
} |
| 695 |
|
| 696 |
# Parse a test table. See comment at top of the file for the format. |
| 697 |
sub parse_table |
| 698 |
{ |
| 699 |
my ($table) = @_; |
| 700 |
my @lines = split /\n/, $table; |
| 701 |
|
| 702 |
my %expected; |
| 703 |
|
| 704 |
my ($user, $gssencmode, $sslmode, $sslnegotiation); |
| 705 |
foreach my $line (@lines) |
| 706 |
{ |
| 707 |
|
| 708 |
# Trim comments |
| 709 |
$line =~ s/#.*$//; |
| 710 |
|
| 711 |
# Trim whitespace at beginning and end |
| 712 |
$line =~ s/^\s+//; |
| 713 |
$line =~ s/\s+$//; |
| 714 |
|
| 715 |
# Ignore empty lines (includes comment-only lines) |
| 716 |
next if $line eq ''; |
| 717 |
|
| 718 |
$line =~ m/^(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S.*)\s*->\s*(\S+)\s*$/ |
| 719 |
or die "could not parse line \"$line\""; |
| 720 |
$user = $1 unless $1 eq "."; |
| 721 |
$gssencmode = $2 unless $2 eq "."; |
| 722 |
$sslmode = $3 unless $3 eq "."; |
| 723 |
$sslnegotiation = $4 unless $4 eq "."; |
| 724 |
|
| 725 |
# Normalize the whitespace in the "EVENTS -> OUTCOME" part |
| 726 |
my @events = split /,\s*/, $5; |
| 727 |
my $outcome = $6; |
| 728 |
my $events_str = join(', ', @events); |
| 729 |
$events_str =~ s/\s+$//; # trim whitespace |
| 730 |
my $events_and_outcome = "$events_str -> $outcome"; |
| 731 |
|
| 732 |
my %expanded = |
| 733 |
expand_expected_line($user, $gssencmode, $sslmode, $sslnegotiation, |
| 734 |
$events_and_outcome); |
| 735 |
%expected = (%expected, %expanded); |
| 736 |
} |
| 737 |
return %expected; |
| 738 |
} |
| 739 |
|
| 740 |
# Expand wildcards on a test table line |
| 741 |
sub expand_expected_line |
| 742 |
{ |
| 743 |
my ($user, $gssencmode, $sslmode, $sslnegotiation, $expected) = @_; |
| 744 |
|
| 745 |
my %result; |
| 746 |
if ($user eq '*') |
| 747 |
{ |
| 748 |
foreach my $x (@all_test_users) |
| 749 |
{ |
| 750 |
%result = ( |
| 751 |
%result, |
| 752 |
expand_expected_line( |
| 753 |
$x, $gssencmode, $sslmode, $sslnegotiation, $expected)); |
| 754 |
} |
| 755 |
} |
| 756 |
elsif ($gssencmode eq '*') |
| 757 |
{ |
| 758 |
foreach my $x (@all_gssencmodes) |
| 759 |
{ |
| 760 |
%result = ( |
| 761 |
%result, |
| 762 |
expand_expected_line( |
| 763 |
$user, $x, $sslmode, $sslnegotiation, $expected)); |
| 764 |
} |
| 765 |
} |
| 766 |
elsif ($sslmode eq '*') |
| 767 |
{ |
| 768 |
foreach my $x (@all_sslmodes) |
| 769 |
{ |
| 770 |
%result = ( |
| 771 |
%result, |
| 772 |
expand_expected_line( |
| 773 |
$user, $gssencmode, $x, $sslnegotiation, $expected)); |
| 774 |
} |
| 775 |
} |
| 776 |
elsif ($sslnegotiation eq '*') |
| 777 |
{ |
| 778 |
foreach my $x (@all_sslnegotiations) |
| 779 |
{ |
| 780 |
%result = ( |
| 781 |
%result, |
| 782 |
expand_expected_line( |
| 783 |
$user, $gssencmode, $sslmode, $x, $expected)); |
| 784 |
} |
| 785 |
} |
| 786 |
else |
| 787 |
{ |
| 788 |
$result{"$user $gssencmode $sslmode $sslnegotiation"} = $expected; |
| 789 |
} |
| 790 |
return %result; |
| 791 |
} |
| 792 |
|
| 793 |
# Scrape the server log for the negotiation events that match the |
| 794 |
# EVENTS field of the test tables. |
| 795 |
sub parse_log_events |
| 796 |
{ |
| 797 |
my ($log_contents) = (@_); |
| 798 |
|
| 799 |
my @events = (); |
| 800 |
|
| 801 |
my @lines = split /\n/, $log_contents; |
| 802 |
foreach my $line (@lines) |
| 803 |
{ |
| 804 |
push @events, "reconnect" |
| 805 |
if $line =~ /connection received/ && scalar(@events) > 0; |
| 806 |
push @events, "connect" |
| 807 |
if $line =~ /connection received/ && scalar(@events) == 0; |
| 808 |
push @events, "sslaccept" if $line =~ /SSLRequest accepted/; |
| 809 |
push @events, "sslreject" if $line =~ /SSLRequest rejected/; |
| 810 |
push @events, "directsslaccept" |
| 811 |
if $line =~ /direct SSL connection accepted/; |
| 812 |
push @events, "directsslreject" |
| 813 |
if $line =~ /direct SSL connection rejected/; |
| 814 |
push @events, "gssaccept" if $line =~ /GSSENCRequest accepted/; |
| 815 |
push @events, "gssreject" if $line =~ /GSSENCRequest rejected/; |
| 816 |
push @events, "authfail" if $line =~ /no pg_hba.conf entry/; |
| 817 |
push @events, "authok" if $line =~ /connection authenticated/; |
| 818 |
push @events, "backenderror" |
| 819 |
if $line =~ /error triggered for injection point backend-/; |
| 820 |
push @events, "v2error" |
| 821 |
if $line =~ /protocol version 2 error triggered/; |
| 822 |
} |
| 823 |
|
| 824 |
# No events at all is represented by "-" |
| 825 |
if (scalar @events == 0) |
| 826 |
{ |
| 827 |
push @events, "-"; |
| 828 |
} |
| 829 |
|
| 830 |
return @events; |
| 831 |
} |