Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
 
# Copyright (c) 2021-2026, PostgreSQL Global Development Group
 
use strict;
use warnings FATAL => 'all';
 
use File::Copy;
 
use PostgreSQL::Test::Utils;
use Test::More;
use PostgreSQL::Test::Cluster;
 
unless (($ENV{with_ssl} || "") eq 'openssl')
{
    plan skip_all => 'OpenSSL not supported by this build';
}
 
my $libressl = not check_pg_config("#define HAVE_SSL_CTX_SET_CERT_CB 1");
 
my $rot13pass = "SbbOnE1";
 
# see the Makefile for how the certificate and key have been generated
 
my $node = PostgreSQL::Test::Cluster->new('main');
$node->init;
$node->append_conf('postgresql.conf',
    "ssl_passphrase.passphrase = '$rot13pass'");
$node->append_conf('postgresql.conf',
    "shared_preload_libraries = 'ssl_passphrase_func'");
$node->append_conf('postgresql.conf', "ssl = 'on'");
 
my $ddir = $node->data_dir;
 
# install certificate and protected key
copy("server.crt", $ddir);
copy("server.key", $ddir);
chmod 0600, "$ddir/server.key" or die $!;
 
$node->start;
 
# if the server is running we must have successfully transformed the passphrase
ok(-e "$ddir/postmaster.pid", "postgres started");
 
$node->stop('fast');
 
# should get a warning if ssl_passphrase_command is set
my $log = $node->rotate_logfile();
 
$node->append_conf('postgresql.conf',
    "ssl_passphrase_command = 'echo spl0tz'");
 
$node->start;
 
$node->stop('fast');
 
my $log_contents = slurp_file($log);
 
like(
    $log_contents,
    qr/WARNING.*"ssl_passphrase_command" setting ignored by ssl_passphrase_func module/,
    "ssl_passphrase_command set warning");
 
# set the wrong passphrase
$node->append_conf('postgresql.conf', "ssl_passphrase.passphrase = 'blurfl'");
 
# try to start the server again
my $ret = PostgreSQL::Test::Utils::system_log(
    'pg_ctl',
    '--pgdata' => $node->data_dir,
    '--log' => $node->logfile,
    'start');
 
 
# with a bad passphrase the server should not start
ok($ret, "pg_ctl fails with bad passphrase");
ok(!-e "$ddir/postmaster.pid", "postgres not started with bad passphrase");
 
# just in case
$node->stop('fast');
 
# Make sure the hook is bypassed when SNI is enabled.
SKIP:
{
    skip 'SNI not supported with LibreSSL', 2 if ($libressl);
 
    $node->append_conf(
        'postgresql.conf', qq{
ssl_passphrase_command = 'echo FooBaR1'
ssl_sni = on
});
    $node->append_conf(
        'pg_hosts.conf', qq{
example.org "$ddir/server.crt" "$ddir/server.key" "" "echo FooBaR1" on
example.com "$ddir/server.crt" "$ddir/server.key" "" "echo FooBaR1" on
});
 
    # If the servers starts and runs, the bad ssl_passphrase.passphrase was
    # correctly ignored.
    $node->start;
    ok(-e "$ddir/postmaster.pid", "postgres started after SNI");
 
    $node->stop('fast');
    $log_contents = slurp_file($log);
    like(
        $log_contents,
        qr/WARNING.*SNI is enabled; installed TLS init hook will be ignored/,
        "server warns that init hook and SNI are incompatible");
    # Ensure that the warning was printed once and not once per host line
    my $count = () =
      $log_contents =~ m/installed TLS init hook will be ignored/;
    is($count, 1, 'Only one WARNING');
}
 
done_testing();