Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
 
# Copyright (c) 2024-2026, PostgreSQL Global Development Group
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
use FindBin;
use lib $FindBin::RealBin;
 
use SSL::Server;
 
# This is the hostaddr used to connect to the server. This cannot be a
# hostname, because the server certificate is always for the domain
# postgresql-ssl-regression.test.
my $SERVERHOSTADDR = '127.0.0.1';
# This is the pattern to use in pg_hba.conf to match incoming connections.
my $SERVERHOSTCIDR = '127.0.0.1/32';
 
if ($ENV{with_ssl} ne 'openssl')
{
    plan skip_all => 'OpenSSL not supported by this build';
}
 
if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bssl\b/)
{
    plan skip_all =>
      'Potentially unsafe test SSL not enabled in PG_TEST_EXTRA';
}
 
my $ssl_server = SSL::Server->new();
 
if ($ssl_server->is_libressl)
{
    plan skip_all => 'SNI not supported when building with LibreSSL';
}
 
my $node = PostgreSQL::Test::Cluster->new('primary');
$node->init;
 
# PGHOST is enforced here to set up the node, subsequent connections
# will use a dedicated connection string.
$ENV{PGHOST} = $node->host;
$ENV{PGPORT} = $node->port;
$node->start;
 
my $exec_backend = $node->safe_psql('postgres', 'SHOW debug_exec_backend');
chomp($exec_backend);
 
$ssl_server->configure_test_server_for_ssl($node, $SERVERHOSTADDR,
    $SERVERHOSTCIDR, 'trust');
 
$ssl_server->switch_server_cert($node, certfile => 'server-cn-only');
 
my $connstr =
  "user=ssltestuser dbname=trustdb hostaddr=$SERVERHOSTADDR sslsni=1";
 
# Enable SNI with an encrypted host key and a per-host passphrase command.
{
    $ssl_server->switch_server_cert(
        $node,
        certfile => 'server-ip-cn-only',
        passphrase_cmd => '');
    my $verify_connstr =
      "$connstr sslmode=verify-full sslrootcert=ssl/root+server_ca.crt sslcert=invalid sslkey=invalid sslcrl=invalid sslcrldir=invalid";
    $node->connect_ok(
        "$verify_connstr host=192.0.2.1 sslsni=0",
        'global certificate before enabling SNI');
    $node->append_conf('pg_hosts.conf',
        'common-name.pg-ssltest.test server-cn-only.crt server-password.key root+client_ca.crt "echo secret1" on'
    );
    $node->append_conf('postgresql.conf', 'ssl_sni = on');
    my $log_offset = -s $node->logfile;
    $node->reload;
    $node->wait_for_log(qr/reloading configuration files/, $log_offset);
    $node->connect_ok(
        "$verify_connstr host=common-name.pg-ssltest.test",
        'reload enables SNI with the per-host passphrase command');
 
    ok(unlink($node->data_dir . '/pg_hosts.conf'));
    $node->append_conf('pg_hosts.conf', '');
    $node->append_conf('postgresql.conf', 'ssl_sni = off');
    $ssl_server->switch_server_cert($node, certfile => 'server-cn-only');
}
 
# Check certificate selection after a failed reload and a successful retry.
SKIP:
{
    skip 'SSL context retention requires forked backends', 11
      if $exec_backend =~ /on/;
 
    my $verify_connstr =
      "$connstr sslmode=verify-full sslrootcert=ssl/root+server_ca.crt sslcert=invalid sslkey=invalid sslcrl=invalid sslcrldir=invalid";
    $node->append_conf(
        'pg_hosts.conf', qq{
* server-ip-cn-only.crt server-ip-cn-only.key
common-name.pg-ssltest.test server-cn-only.crt server-cn-only.key
});
    $node->append_conf('postgresql.conf', 'ssl_sni = on');
    my $log_offset = -s $node->logfile;
    $node->reload;
    $node->wait_for_log(qr/reloading configuration files/, $log_offset);
    $node->connect_ok(
        "$verify_connstr host=common-name.pg-ssltest.test",
        'loaded SNI mode selects the named host certificate');
 
    $node->append_conf('postgresql.conf', 'ssl_sni = off');
    $node->append_conf('sslconfig.conf',
        "ssl_cert_file = 'missing-server.crt'");
    $log_offset = -s $node->logfile;
    $node->reload;
    $node->wait_for_log(qr/SSL configuration was not reloaded/, $log_offset);
    $node->connect_ok(
        "$verify_connstr host=common-name.pg-ssltest.test",
        'failed reload retains the named host certificate with a default host configured'
    );
 
    $ssl_server->switch_server_cert(
        $node,
        certfile => 'server-ip-cn-only',
        restart => 'no');
    $log_offset = -s $node->logfile;
    $node->reload;
    $node->wait_for_log(qr/reloading configuration files/, $log_offset);
    $node->connect_fails(
        "$verify_connstr host=common-name.pg-ssltest.test",
        'successful retry disables SNI host selection',
        expected_stderr =>
          qr/server certificate for "192\.0\.2\.1" does not match host name/);
    $node->connect_ok("$verify_connstr host=192.0.2.1 sslsni=0",
        'successful retry serves the configured certificate');
 
    ok(unlink($node->data_dir . '/pg_hosts.conf'));
    $node->append_conf('pg_hosts.conf', '');
    $ssl_server->switch_server_cert($node, certfile => 'server-cn-only');
}
 
##############################################################################
# postgresql.conf
##############################################################################
 
# Connect without any hosts configured in pg_hosts.conf, thus using the cert
# and key in postgresql.conf. pg_hosts.conf exists at this point but is empty
# apart from the comments stemming from the sample.
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require",
    "pg.conf: connect with correct server CA cert file sslmode=require");
 
$node->connect_fails(
    "$connstr sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg.conf: connect fails without intermediate for sslmode=verify-ca",
    expected_stderr => qr/certificate verify failed/);
 
# Add an entry in pg_hosts.conf with no default, and reload. Since ssl_sni is
# still 'off' we should still be able to connect using the certificates in
# postgresql.conf
$node->append_conf('pg_hosts.conf',
    "example.org server-cn-only.crt server-cn-only.key");
$node->reload;
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require",
    "pg.conf: connect with correct server CA cert file sslmode=require");
 
# Turn on SNI support and remove pg_hosts.conf and reload to make sure a
# missing file is treated like an empty file.
$node->append_conf('postgresql.conf', 'ssl_sni = on');
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->reload;
 
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require",
    "pg.conf: connect after deleting pg_hosts.conf");
 
##############################################################################
# pg_hosts.conf
##############################################################################
 
# Replicate the postgresql.conf configuration into pg_hosts.conf and retry the
# same tests as above.
$node->append_conf('pg_hosts.conf',
    "* server-cn-only.crt server-cn-only.key");
$node->reload;
 
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require",
    "pg_hosts.conf: connect to default, with correct server CA cert file sslmode=require"
);
 
$node->connect_fails(
    "$connstr sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to default, fail without intermediate for sslmode=verify-ca",
    expected_stderr => qr/certificate verify failed/);
 
# Add host entry for example.org which serves the server cert and its
# intermediate CA.  The previously existing default host still exists without
# a CA.
$node->append_conf('pg_hosts.conf',
    "example.org server-cn-only+server_ca.crt server-cn-only.key root_ca.crt"
);
$node->reload;
 
$node->connect_ok(
    "$connstr host=example.org sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to example.org and verify server CA");
 
$node->connect_ok(
    "$connstr host=Example.ORG sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to Example.ORG and verify server CA");
 
$node->connect_fails(
    "$connstr host=example.org sslrootcert=invalid sslmode=verify-ca",
    "pg_hosts.conf: connect to example.org but without server root cert, sslmode=verify-ca",
    expected_stderr => qr/root certificate file "invalid" does not exist/);
 
$node->connect_fails(
    "$connstr sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to default and fail to verify CA",
    expected_stderr => qr/certificate verify failed/);
 
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require",
    "pg_hosts.conf: connect to default with sslmode=require");
 
# Use multiple hostnames for a single configuration
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    "example.org,example.com,example.net server-cn-only+server_ca.crt server-cn-only.key root_ca.crt"
);
$node->reload;
 
$node->connect_ok(
    "$connstr host=example.org sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to example.org and verify server CA");
$node->connect_ok(
    "$connstr host=example.com sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to example.com and verify server CA");
$node->connect_ok(
    "$connstr host=example.net sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    "pg_hosts.conf: connect to example.net and verify server CA");
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.se",
    "pg_hosts.conf: connect to default with sslmode=require",
    expected_stderr => qr/unrecognized name/);
 
# Test @-inclusion of hostnames.
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    'example.org,@hostnames.txt server-cn-only+server_ca.crt server-cn-only.key root_ca.crt'
);
$node->append_conf(
    'hostnames.txt', qq{
example.com
example.net
});
$node->reload;
 
$node->connect_ok(
    "$connstr host=example.org sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    '@hostnames.txt: connect to example.org and verify server CA');
$node->connect_ok(
    "$connstr host=example.com sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    '@hostnames.txt: connect to example.com and verify server CA');
$node->connect_ok(
    "$connstr host=example.net sslrootcert=ssl/root_ca.crt sslmode=verify-ca",
    '@hostnames.txt: connect to example.net and verify server CA');
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.se",
    '@hostnames.txt: connect to default with sslmode=require',
    expected_stderr => qr/unrecognized name/);
 
# Add an incorrect entry specifying a default entry combined with hostnames
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    "example.org,*,example.net server-cn-only+server_ca.crt server-cn-only.key root_ca.crt"
);
my $result = $node->restart(fail_ok => 1);
is($result, 0,
    'pg_hosts.conf: restart fails with default entry combined with hostnames'
);
 
# Add incorrect duplicate entries.
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf(
    'pg_hosts.conf', qq{
* server-cn-only.crt server-cn-only.key
* server-cn-only.crt server-cn-only.key
});
$result = $node->restart(fail_ok => 1);
is($result, 0, 'pg_hosts.conf: restart fails with two default entries');
 
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf(
    'pg_hosts.conf', qq{
/no_sni/ server-cn-only.crt server-cn-only.key
/no_sni/ server-cn-only.crt server-cn-only.key
});
$result = $node->restart(fail_ok => 1);
is($result, 0, 'pg_hosts.conf: restart fails with two no_sni entries');
 
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf(
    'pg_hosts.conf', qq{
example.org server-cn-only.crt server-cn-only.key
example.net server-cn-only.crt server-cn-only.key
example.org server-cn-only.crt server-cn-only.key
});
$result = $node->restart(fail_ok => 1);
is($result, 0,
    'pg_hosts.conf: restart fails with two identical hostname entries');
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf(
    'pg_hosts.conf', qq{
example.org server-cn-only.crt server-cn-only.key
example.net,example.com,Example.org server-cn-only.crt server-cn-only.key
});
$result = $node->restart(fail_ok => 1);
is($result, 0,
    'pg_hosts.conf: restart fails with two identical hostname entries in lists'
);
 
# Modify pg_hosts.conf to no longer have the default host entry.
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    "example.org server-cn-only+server_ca.crt server-cn-only.key root_ca.crt"
);
$node->restart;
 
# Connecting without a hostname as well as with a hostname which isn't in the
# pg_hosts configuration should fail.
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslsni=0",
    "pg_hosts.conf: connect to default with sslmode=require",
    expected_stderr => qr/handshake failure/);
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.com",
    "pg_hosts.conf: connect to default with sslmode=require",
    expected_stderr => qr/unrecognized name/);
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example",
    "pg_hosts.conf: connect to 'example' with sslmode=require",
    expected_stderr => qr/unrecognized name/);
 
# Turn off SNI while the postgresql.conf configuration cannot be loaded, such
# that the reload fails to replace the SSL configuration.  The pg_hosts.conf
# configuration without a default host must remain in effect, ssl_sni will be
# set to 'off' but the previous config - including ssl_sni setting - is what
# will be used.
my $bsession = $node->background_psql('trustdb',
    connstr =>
      "$connstr host=example.org sslrootcert=ssl/root_ca.crt sslmode=verify-ca"
);
$result = $bsession->query_safe('SHOW ssl_sni');
is($result, 'on', 'SNI is enabled in active config for background session');
 
$node->append_conf(
    'postgresql.conf', qq{
ssl_sni = off
ssl_cert_file = 'nonexistent.crt'
});
my $node_loglocation = -s $node->logfile;
$node->reload;
 
$node->wait_for_log(qr/SSL configuration was not reloaded/,
    $node_loglocation);
$node->log_check('SSL reload triggered WARNING on ssl_sni state',
    $node_loglocation, log_like => [qr/SNI remains on/]);
 
$result = $bsession->query_safe('SHOW ssl_sni');
is($result, 'off', 'SNI is reported as off in background session');
$bsession->quit;
$node_loglocation = -s $node->logfile;
 
# EXEC_BACKEND backends load the SSL configuration on each connection instead
# of reusing the contexts in the postmaster
SKIP:
{
    skip 'SSL config reload is per connection in EXEC_BACKEND', 4
      if ($windows_os || $exec_backend =~ /on/);
 
    my ($rc, $stdout, $stderr) = $node->psql(
        'trustdb',
        qq[SHOW ssl_sni;],
        connstr =>
          "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.org"
    );
    is($rc, 0,
        "pg_hosts.conf: connect to example.org after failed reload with ssl_sni off"
    );
    is($stdout, 'off', 'ssl_sni is set to off in a new connection');
 
    $node->connect_fails(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslsni=0",
        "pg_hosts.conf: connect to default after failed reload with ssl_sni off",
        expected_stderr => qr/handshake failure/);
}
 
# Specifically test EXEC_BACKEND where a failed reload by definition cannot
# retain the previous configuration.  Here the server should not accept any
# connections.
SKIP:
{
    skip 'Only testing EXEC_BACKEND behavior', 2 if ($exec_backend !~ /on/);
    $node->connect_fails(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslsni=0",
        "pg_hosts.conf: connect to default after failed reload with ssl_sni off",
        expected_stderr => qr/server does not support SSL/);
}
 
# Reset configuration for the next test
$node->append_conf(
    'postgresql.conf', qq{
ssl_sni = on
ssl_cert_file = ''
});
$node_loglocation = -s $node->logfile;
$node->reload;
 
# Reconfigure with broken configuration for the key passphrase, the server
# should not start up
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    'localhost server-cn-only.crt server-password.key root+client_ca.crt "echo wrongpassword" on'
);
$result = $node->restart(fail_ok => 1);
is($result, 0,
    'pg_hosts.conf: restart fails with password-protected key when using the wrong passphrase command'
);
 
# Reconfigure again but with the correct passphrase set
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    'localhost server-cn-only.crt server-password.key root+client_ca.crt "echo secret1" on'
);
$result = $node->restart(fail_ok => 1);
is($result, 1,
    'pg_hosts.conf: restart succeeds with password-protected key when using the correct passphrase command'
);
 
# Make sure connecting works, and try to stress the reload logic by issuing
# subsequent reloads
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
    "pg_hosts.conf: connect with correct server CA cert file sslmode=require"
);
$node->reload;
$node->reload;
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
    "pg_hosts.conf: connect with correct server CA cert file after reloads");
$node->reload;
$node->reload;
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
    "pg_hosts.conf: connect with correct server CA cert file after more reloads"
);
 
# Test reloading a passphrase protected key without reloading support in the
# passphrase hook. Restarting should not give any errors in the log, but the
# subsequent reload should fail with an error regarding reloading.
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    'localhost server-cn-only.crt server-password.key root+client_ca.crt "echo secret1" off'
);
$node_loglocation = -s $node->logfile;
$result = $node->restart(fail_ok => 1);
is($result, 1,
    'pg_hosts.conf: restart succeeds with password-protected key when using the correct passphrase command'
);
my $log =
  PostgreSQL::Test::Utils::slurp_file($node->logfile, $node_loglocation);
unlike(
    $log,
    qr/cannot be reloaded because it requires a passphrase/,
    'log reload failure due to passphrase command reloading');
 
SKIP:
{
    # Passphrase reloads must be enabled on Windows (and EXEC_BACKEND) to
    # succeed even without a restart
    skip "Passphrase command reload required on Windows and EXEC_BACKEND", 1
      if ($windows_os || $exec_backend =~ /on/);
 
    $node->connect_ok(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
        "pg_hosts.conf: connect with correct server CA cert file sslmode=require"
    );
    # Reloading should fail since the passphrase cannot be reloaded, with an
    # error recorded in the log.  Since we keep existing contexts around it
    # should still work.
    $node_loglocation = -s $node->logfile;
    $node->reload;
    $node->connect_ok(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
        "pg_hosts.conf: connect with correct server CA cert file sslmode=require"
    );
    $log =
      PostgreSQL::Test::Utils::slurp_file($node->logfile, $node_loglocation);
    like(
        $log,
        qr/cannot be reloaded because it requires a passphrase/,
        'log reload failure due to passphrase command reloading');
}
 
# A host whose key cannot be loaded in an EXEC_BACKEND child must only affect
# connections to that host.
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    'localhost server-cn-only.crt server-cn-only.key');
$node->append_conf('pg_hosts.conf',
    'example.org server-cn-only.crt server-password.key root+client_ca.crt "echo secret1" off'
);
$node->restart;
$node_loglocation = -s $node->logfile;
 
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=localhost",
    "pg_hosts.conf: connect to host next to a passphrase host without reload"
);
 
if ($windows_os || $exec_backend =~ /on/)
{
    $node->connect_fails(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.org",
        "pg_hosts.conf: connect to passphrase host without reload fails",
        expected_stderr => qr/internal error/);
    $node->wait_for_log(qr/SSL configuration for host could not be loaded/,
        $node_loglocation);
}
else
{
    $node->connect_ok(
        "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.org",
        "pg_hosts.conf: connect to passphrase host without reload");
}
 
# Configure with only non-SNI connections allowed
ok(unlink($node->data_dir . '/pg_hosts.conf'));
$node->append_conf('pg_hosts.conf',
    "/no_sni/ server-cn-only.crt server-cn-only.key");
$node->restart;
 
$node->connect_ok(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslsni=0",
    "pg_hosts.conf: only non-SNI connections allowed");
 
$node->connect_fails(
    "$connstr sslrootcert=ssl/root+server_ca.crt sslmode=require host=example.org",
    "pg_hosts.conf: only non-SNI connections allowed, connecting with SNI",
    expected_stderr => qr/unrecognized name/);
 
# Test client CAs
 
# pg_hosts configuration
ok(unlink($node->data_dir . '/pg_hosts.conf'));
 
# Neither ssl_ca_file nor the default host should have any effect whatsoever on
# the following tests.
$node->append_conf('postgresql.conf', "ssl_ca_file = 'root+client_ca.crt'");
$node->append_conf('pg_hosts.conf',
    '* server-cn-only.crt server-cn-only.key root+client_ca.crt');
 
# example.org has an unconfigured CA.
$node->append_conf('pg_hosts.conf',
    'example.org server-cn-only.crt server-cn-only.key');
# example.com uses the client CA.
$node->append_conf('pg_hosts.conf',
    'example.com server-cn-only.crt server-cn-only.key root+client_ca.crt');
# example.net uses the server CA (which is wrong).
$node->append_conf('pg_hosts.conf',
    'example.net server-cn-only.crt server-cn-only.key root+server_ca.crt');
 
$node->restart;
 
$connstr =
  "user=ssltestuser dbname=certdb hostaddr=$SERVERHOSTADDR sslmode=require sslsni=1";
 
# example.org is unconfigured and should fail.
$node->connect_fails(
    "$connstr host=example.org sslcertmode=require sslcert=ssl/client.crt"
      . $ssl_server->sslkey('client.key'),
    "host: 'example.org', ca: '': connect with sslcert, no client CA configured",
    expected_stderr =>
      qr/client certificates can only be checked if a root certificate store is available/
);
 
# example.com is configured and should require a valid client cert.
$node->connect_fails(
    "$connstr host=example.com sslcertmode=disable",
    "host: 'example.com', ca: 'root+client_ca.crt': connect fails if no client certificate sent",
    expected_stderr => qr/connection requires a valid client certificate/);
 
$node->connect_ok(
    "$connstr host=example.com sslcertmode=require sslcert=ssl/client.crt "
      . $ssl_server->sslkey('client.key'),
    "host: 'example.com', ca: 'root+client_ca.crt': connect with sslcert, client certificate sent"
);
 
# example.net is configured and should require a client cert, but will
# always fail verification.
$node->connect_fails(
    "$connstr host=example.net sslcertmode=disable",
    "host: 'example.net', ca: 'root+server_ca.crt': connect fails if no client certificate sent",
    expected_stderr => qr/connection requires a valid client certificate/);
 
$node->connect_fails(
    "$connstr host=example.net sslcertmode=require sslcert=ssl/client.crt "
      . $ssl_server->sslkey('client.key'),
    "host: 'example.net', ca: 'root+server_ca.crt': connect with sslcert, client certificate sent",
    expected_stderr => qr/unknown ca/);
 
# Make sure the global CRL dir interacts properly with per-host trust.
$ssl_server->switch_server_cert(
    $node,
    certfile => 'server-cn-only',
    crldir => 'client-crldir');
 
$node->connect_fails(
    "$connstr host=example.com sslcertmode=require sslcert=ssl/client-revoked.crt "
      . $ssl_server->sslkey('client-revoked.key'),
    "host: 'example.com', ca: 'root+client_ca.crt': connect fails with revoked client cert",
    expected_stderr => qr/certificate revoked/);
 
# pg_hosts configuration with useless data at EOL
ok(unlink($node->data_dir . '/pg_hosts.conf'));
# example.org has an unconfigured CA.
$node->append_conf('pg_hosts.conf',
    'example.org server-cn-only.crt server-cn-only.key root+client_ca.crt "cmd" on TRAILING_TEXT MORE_TEXT'
);
$result = $node->restart(fail_ok => 1);
is($result, 0, 'pg_hosts.conf: restart fails with extra data at EOL');
# pg_hosts configuration with useless data at EOL
ok(unlink($node->data_dir . '/pg_hosts.conf'));
# example.org has an unconfigured CA.
$node->append_conf('pg_hosts.conf',
    'example.org server-cn-only.crt server-cn-only.key root+client_ca.crt "cmd" notabooleanvalue'
);
$result = $node->restart(fail_ok => 1);
is($result, 0,
    'pg_hosts.conf: restart fails with non-boolean value in boolean field');
 
done_testing();