| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
|
| 7 |
use File::Copy; |
| 8 |
|
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
|
| 13 |
unless (($ENV{with_ssl} || "") eq 'openssl') |
| 14 |
{ |
| 15 |
plan skip_all => 'OpenSSL not supported by this build'; |
| 16 |
} |
| 17 |
|
| 18 |
my $rot13pass = "SbbOnE1"; |
| 19 |
|
| 20 |
# see the Makefile for how the certificate and key have been generated |
| 21 |
|
| 22 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 23 |
$node->init; |
| 24 |
$node->append_conf('postgresql.conf', |
| 25 |
"ssl_passphrase.passphrase = '$rot13pass'"); |
| 26 |
$node->append_conf('postgresql.conf', |
| 27 |
"shared_preload_libraries = 'ssl_passphrase_func'"); |
| 28 |
$node->append_conf('postgresql.conf', "ssl = 'on'"); |
| 29 |
|
| 30 |
my $ddir = $node->data_dir; |
| 31 |
|
| 32 |
# install certificate and protected key |
| 33 |
copy("server.crt", $ddir); |
| 34 |
copy("server.key", $ddir); |
| 35 |
chmod 0600, "$ddir/server.key" or die $!; |
| 36 |
|
| 37 |
$node->start; |
| 38 |
|
| 39 |
# if the server is running we must have successfully transformed the passphrase |
| 40 |
ok(-e "$ddir/postmaster.pid", "postgres started"); |
| 41 |
|
| 42 |
$node->stop('fast'); |
| 43 |
|
| 44 |
# should get a warning if ssl_passphrase_command is set |
| 45 |
my $log = $node->rotate_logfile(); |
| 46 |
|
| 47 |
$node->append_conf('postgresql.conf', |
| 48 |
"ssl_passphrase_command = 'echo spl0tz'"); |
| 49 |
|
| 50 |
$node->start; |
| 51 |
|
| 52 |
$node->stop('fast'); |
| 53 |
|
| 54 |
my $log_contents = slurp_file($log); |
| 55 |
|
| 56 |
like( |
| 57 |
$log_contents, |
| 58 |
qr/WARNING.*"ssl_passphrase_command" setting ignored by ssl_passphrase_func module/, |
| 59 |
"ssl_passphrase_command set warning"); |
| 60 |
|
| 61 |
# set the wrong passphrase |
| 62 |
$node->append_conf('postgresql.conf', "ssl_passphrase.passphrase = 'blurfl'"); |
| 63 |
|
| 64 |
# try to start the server again |
| 65 |
my $ret = PostgreSQL::Test::Utils::system_log( |
| 66 |
'pg_ctl', |
| 67 |
'--pgdata' => $node->data_dir, |
| 68 |
'--log' => $node->logfile, |
| 69 |
'start'); |
| 70 |
|
| 71 |
|
| 72 |
# with a bad passphrase the server should not start |
| 73 |
ok($ret, "pg_ctl fails with bad passphrase"); |
| 74 |
ok(!-e "$ddir/postmaster.pid", "postgres not started with bad passphrase"); |
| 75 |
|
| 76 |
# just in case |
| 77 |
$node->stop('fast'); |
| 78 |
|
| 79 |
done_testing(); |