From 8ce641f99709669133c6cbb12aa3d516af7897aa Mon Sep 17 00:00:00 2001 From: Andrew Dunstan Date: Thu, 7 Feb 2019 10:22:49 -0500 Subject: Fix searchpath and module location for pg_rewind and ssl TAP tests The modules RewindTest.pm and ServerSetup.pm are really only useful for TAP tests, so they really belong in the TAP test directories. In addition, ServerSetup.pm is renamed to SSLServer.pm. The test scripts have their own directories added to the search path so that the relocated modules will be found, regardless of where the tests are run from, even on modern perl where "." is no longer in the searchpath. Discussion: https://postgr.es/m/e4b0f366-269c-73c3-9c90-d9cb0f4db1f9@2ndQuadrant.com Backpatch as appropriate to 9.5 --- src/bin/pg_rewind/RewindTest.pm | 295 ----------------------------- src/bin/pg_rewind/t/001_basic.pl | 3 + src/bin/pg_rewind/t/002_databases.pl | 3 + src/bin/pg_rewind/t/003_extrafiles.pl | 3 + src/bin/pg_rewind/t/004_pg_xlog_symlink.pl | 3 + src/bin/pg_rewind/t/005_same_timeline.pl | 3 + src/bin/pg_rewind/t/RewindTest.pm | 295 +++++++++++++++++++++++++++++ src/test/ssl/ServerSetup.pm | 199 ------------------- src/test/ssl/t/001_ssltests.pl | 7 +- src/test/ssl/t/002_scram.pl | 7 +- src/test/ssl/t/SSLServer.pm | 199 +++++++++++++++++++ 11 files changed, 521 insertions(+), 496 deletions(-) delete mode 100644 src/bin/pg_rewind/RewindTest.pm create mode 100644 src/bin/pg_rewind/t/RewindTest.pm delete mode 100644 src/test/ssl/ServerSetup.pm create mode 100644 src/test/ssl/t/SSLServer.pm (limited to 'src') diff --git a/src/bin/pg_rewind/RewindTest.pm b/src/bin/pg_rewind/RewindTest.pm deleted file mode 100644 index 85cae7e47b1..00000000000 --- a/src/bin/pg_rewind/RewindTest.pm +++ /dev/null @@ -1,295 +0,0 @@ -package RewindTest; - -# Test driver for pg_rewind. Each test consists of a cycle where a new cluster -# is first created with initdb, and a streaming replication standby is set up -# to follow the master. Then the master is shut down and the standby is -# promoted, and finally pg_rewind is used to rewind the old master, using the -# standby as the source. -# -# To run a test, the test script (in t/ subdirectory) calls the functions -# in this module. These functions should be called in this sequence: -# -# 1. setup_cluster - creates a PostgreSQL cluster that runs as the master -# -# 2. start_master - starts the master server -# -# 3. create_standby - runs pg_basebackup to initialize a standby server, and -# sets it up to follow the master. -# -# 4. promote_standby - runs "pg_ctl promote" to promote the standby server. -# The old master keeps running. -# -# 5. run_pg_rewind - stops the old master (if it's still running) and runs -# pg_rewind to synchronize it with the now-promoted standby server. -# -# 6. clean_rewind_test - stops both servers used in the test, if they're -# still running. -# -# The test script can use the helper functions master_psql and standby_psql -# to run psql against the master and standby servers, respectively. The -# test script can also use the $connstr_master and $connstr_standby global -# variables, which contain libpq connection strings for connecting to the -# master and standby servers. The data directories are also available -# in paths $test_master_datadir and $test_standby_datadir - -use strict; -use warnings; - -use Carp; -use Config; -use Exporter 'import'; -use File::Copy; -use File::Path qw(rmtree); -use IPC::Run qw(run); -use PostgresNode; -use TestLib; -use Test::More; - -our @EXPORT = qw( - $node_master - $node_standby - - master_psql - standby_psql - check_query - - setup_cluster - start_master - create_standby - promote_standby - run_pg_rewind - clean_rewind_test -); - -# Our nodes. -our $node_master; -our $node_standby; - -sub master_psql -{ - my $cmd = shift; - - system_or_bail 'psql', '-q', '--no-psqlrc', '-d', - $node_master->connstr('postgres'), '-c', "$cmd"; - return; -} - -sub standby_psql -{ - my $cmd = shift; - - system_or_bail 'psql', '-q', '--no-psqlrc', '-d', - $node_standby->connstr('postgres'), '-c', "$cmd"; - return; -} - -# Run a query against the master, and check that the output matches what's -# expected -sub check_query -{ - local $Test::Builder::Level = $Test::Builder::Level + 1; - - my ($query, $expected_stdout, $test_name) = @_; - my ($stdout, $stderr); - - # we want just the output, no formatting - my $result = run [ - 'psql', '-q', '-A', '-t', '--no-psqlrc', '-d', - $node_master->connstr('postgres'), - '-c', $query - ], - '>', \$stdout, '2>', \$stderr; - - # We don't use ok() for the exit code and stderr, because we want this - # check to be just a single test. - if (!$result) - { - fail("$test_name: psql exit code"); - } - elsif ($stderr ne '') - { - diag $stderr; - fail("$test_name: psql no stderr"); - } - else - { - $stdout =~ s/\r//g if $Config{osname} eq 'msys'; - is($stdout, $expected_stdout, "$test_name: query result matches"); - } - return; -} - -sub setup_cluster -{ - my $extra_name = shift; # Used to differentiate clusters - my $extra = shift; # Extra params for initdb - - # Initialize master, data checksums are mandatory - $node_master = - get_new_node('master' . ($extra_name ? "_${extra_name}" : '')); - $node_master->init(allows_streaming => 1, extra => $extra); - - # Set wal_keep_segments to prevent WAL segment recycling after enforced - # checkpoints in the tests. - $node_master->append_conf( - 'postgresql.conf', qq( -wal_keep_segments = 20 -)); - return; -} - -sub start_master -{ - $node_master->start; - - #### Now run the test-specific parts to initialize the master before setting - # up standby - - return; -} - -sub create_standby -{ - my $extra_name = shift; - - $node_standby = - get_new_node('standby' . ($extra_name ? "_${extra_name}" : '')); - $node_master->backup('my_backup'); - $node_standby->init_from_backup($node_master, 'my_backup'); - my $connstr_master = $node_master->connstr(); - - $node_standby->append_conf( - "postgresql.conf", qq( -primary_conninfo='$connstr_master application_name=rewind_standby' -)); - - $node_standby->set_standby_mode(); - - # Start standby - $node_standby->start; - - # The standby may have WAL to apply before it matches the primary. That - # is fine, because no test examines the standby before promotion. - - return; -} - -sub promote_standby -{ - #### Now run the test-specific parts to run after standby has been started - # up standby - - # Wait for the standby to receive and write all WAL. - $node_master->wait_for_catchup('rewind_standby', 'write'); - - # Now promote standby and insert some new data on master, this will put - # the master out-of-sync with the standby. - $node_standby->promote; - - # Force a checkpoint after the promotion. pg_rewind looks at the control - # file to determine what timeline the server is on, and that isn't updated - # immediately at promotion, but only at the next checkpoint. When running - # pg_rewind in remote mode, it's possible that we complete the test steps - # after promotion so quickly that when pg_rewind runs, the standby has not - # performed a checkpoint after promotion yet. - standby_psql("checkpoint"); - - return; -} - -sub run_pg_rewind -{ - my $test_mode = shift; - my $master_pgdata = $node_master->data_dir; - my $standby_pgdata = $node_standby->data_dir; - my $standby_connstr = $node_standby->connstr('postgres'); - my $tmp_folder = TestLib::tempdir; - - # Stop the master and be ready to perform the rewind - $node_master->stop; - - # At this point, the rewind processing is ready to run. - # We now have a very simple scenario with a few diverged WAL record. - # The real testing begins really now with a bifurcation of the possible - # scenarios that pg_rewind supports. - - # Keep a temporary postgresql.conf for master node or it would be - # overwritten during the rewind. - copy( - "$master_pgdata/postgresql.conf", - "$tmp_folder/master-postgresql.conf.tmp"); - - # Now run pg_rewind - if ($test_mode eq "local") - { - - # Do rewind using a local pgdata as source - # Stop the master and be ready to perform the rewind - $node_standby->stop; - command_ok( - [ - 'pg_rewind', - "--debug", - "--source-pgdata=$standby_pgdata", - "--target-pgdata=$master_pgdata", - "--no-sync" - ], - 'pg_rewind local'); - } - elsif ($test_mode eq "remote") - { - - # Do rewind using a remote connection as source - command_ok( - [ - 'pg_rewind', "--debug", - "--source-server", $standby_connstr, - "--target-pgdata=$master_pgdata", - "--no-sync" - ], - 'pg_rewind remote'); - } - else - { - - # Cannot come here normally - croak("Incorrect test mode specified"); - } - - # Now move back postgresql.conf with old settings - move( - "$tmp_folder/master-postgresql.conf.tmp", - "$master_pgdata/postgresql.conf"); - - chmod( - $node_master->group_access() ? 0640 : 0600, - "$master_pgdata/postgresql.conf") - or BAIL_OUT( - "unable to set permissions for $master_pgdata/postgresql.conf"); - - # Plug-in rewound node to the now-promoted standby node - my $port_standby = $node_standby->port; - $node_master->append_conf( - 'postgresql.conf', qq( -primary_conninfo='port=$port_standby' -)); - - $node_master->set_standby_mode(); - - # Restart the master to check that rewind went correctly - $node_master->start; - - #### Now run the test-specific parts to check the result - - return; -} - -# Clean up after the test. Stop both servers, if they're still running. -sub clean_rewind_test -{ - $node_master->teardown_node if defined $node_master; - $node_standby->teardown_node if defined $node_standby; - return; -} - -1; diff --git a/src/bin/pg_rewind/t/001_basic.pl b/src/bin/pg_rewind/t/001_basic.pl index 53dbf45be29..115192170e5 100644 --- a/src/bin/pg_rewind/t/001_basic.pl +++ b/src/bin/pg_rewind/t/001_basic.pl @@ -3,6 +3,9 @@ use warnings; use TestLib; use Test::More tests => 10; +use FindBin; +use lib $FindBin::RealBin; + use RewindTest; sub run_test diff --git a/src/bin/pg_rewind/t/002_databases.pl b/src/bin/pg_rewind/t/002_databases.pl index 2c9e4278311..6dc05720a1e 100644 --- a/src/bin/pg_rewind/t/002_databases.pl +++ b/src/bin/pg_rewind/t/002_databases.pl @@ -3,6 +3,9 @@ use warnings; use TestLib; use Test::More tests => 6; +use FindBin; +use lib $FindBin::RealBin; + use RewindTest; sub run_test diff --git a/src/bin/pg_rewind/t/003_extrafiles.pl b/src/bin/pg_rewind/t/003_extrafiles.pl index 496f38c4570..c4040bd5620 100644 --- a/src/bin/pg_rewind/t/003_extrafiles.pl +++ b/src/bin/pg_rewind/t/003_extrafiles.pl @@ -7,6 +7,9 @@ use Test::More tests => 4; use File::Find; +use FindBin; +use lib $FindBin::RealBin; + use RewindTest; diff --git a/src/bin/pg_rewind/t/004_pg_xlog_symlink.pl b/src/bin/pg_rewind/t/004_pg_xlog_symlink.pl index 280eceb9929..ed1ddb6b60c 100644 --- a/src/bin/pg_rewind/t/004_pg_xlog_symlink.pl +++ b/src/bin/pg_rewind/t/004_pg_xlog_symlink.pl @@ -17,6 +17,9 @@ else plan tests => 4; } +use FindBin; +use lib $FindBin::RealBin; + use RewindTest; sub run_test diff --git a/src/bin/pg_rewind/t/005_same_timeline.pl b/src/bin/pg_rewind/t/005_same_timeline.pl index 0e334ee191a..40dbc44caa3 100644 --- a/src/bin/pg_rewind/t/005_same_timeline.pl +++ b/src/bin/pg_rewind/t/005_same_timeline.pl @@ -3,6 +3,9 @@ use warnings; use TestLib; use Test::More tests => 1; +use FindBin; +use lib $FindBin::RealBin; + use RewindTest; # Test that running pg_rewind if the two clusters are on the same diff --git a/src/bin/pg_rewind/t/RewindTest.pm b/src/bin/pg_rewind/t/RewindTest.pm new file mode 100644 index 00000000000..85cae7e47b1 --- /dev/null +++ b/src/bin/pg_rewind/t/RewindTest.pm @@ -0,0 +1,295 @@ +package RewindTest; + +# Test driver for pg_rewind. Each test consists of a cycle where a new cluster +# is first created with initdb, and a streaming replication standby is set up +# to follow the master. Then the master is shut down and the standby is +# promoted, and finally pg_rewind is used to rewind the old master, using the +# standby as the source. +# +# To run a test, the test script (in t/ subdirectory) calls the functions +# in this module. These functions should be called in this sequence: +# +# 1. setup_cluster - creates a PostgreSQL cluster that runs as the master +# +# 2. start_master - starts the master server +# +# 3. create_standby - runs pg_basebackup to initialize a standby server, and +# sets it up to follow the master. +# +# 4. promote_standby - runs "pg_ctl promote" to promote the standby server. +# The old master keeps running. +# +# 5. run_pg_rewind - stops the old master (if it's still running) and runs +# pg_rewind to synchronize it with the now-promoted standby server. +# +# 6. clean_rewind_test - stops both servers used in the test, if they're +# still running. +# +# The test script can use the helper functions master_psql and standby_psql +# to run psql against the master and standby servers, respectively. The +# test script can also use the $connstr_master and $connstr_standby global +# variables, which contain libpq connection strings for connecting to the +# master and standby servers. The data directories are also available +# in paths $test_master_datadir and $test_standby_datadir + +use strict; +use warnings; + +use Carp; +use Config; +use Exporter 'import'; +use File::Copy; +use File::Path qw(rmtree); +use IPC::Run qw(run); +use PostgresNode; +use TestLib; +use Test::More; + +our @EXPORT = qw( + $node_master + $node_standby + + master_psql + standby_psql + check_query + + setup_cluster + start_master + create_standby + promote_standby + run_pg_rewind + clean_rewind_test +); + +# Our nodes. +our $node_master; +our $node_standby; + +sub master_psql +{ + my $cmd = shift; + + system_or_bail 'psql', '-q', '--no-psqlrc', '-d', + $node_master->connstr('postgres'), '-c', "$cmd"; + return; +} + +sub standby_psql +{ + my $cmd = shift; + + system_or_bail 'psql', '-q', '--no-psqlrc', '-d', + $node_standby->connstr('postgres'), '-c', "$cmd"; + return; +} + +# Run a query against the master, and check that the output matches what's +# expected +sub check_query +{ + local $Test::Builder::Level = $Test::Builder::Level + 1; + + my ($query, $expected_stdout, $test_name) = @_; + my ($stdout, $stderr); + + # we want just the output, no formatting + my $result = run [ + 'psql', '-q', '-A', '-t', '--no-psqlrc', '-d', + $node_master->connstr('postgres'), + '-c', $query + ], + '>', \$stdout, '2>', \$stderr; + + # We don't use ok() for the exit code and stderr, because we want this + # check to be just a single test. + if (!$result) + { + fail("$test_name: psql exit code"); + } + elsif ($stderr ne '') + { + diag $stderr; + fail("$test_name: psql no stderr"); + } + else + { + $stdout =~ s/\r//g if $Config{osname} eq 'msys'; + is($stdout, $expected_stdout, "$test_name: query result matches"); + } + return; +} + +sub setup_cluster +{ + my $extra_name = shift; # Used to differentiate clusters + my $extra = shift; # Extra params for initdb + + # Initialize master, data checksums are mandatory + $node_master = + get_new_node('master' . ($extra_name ? "_${extra_name}" : '')); + $node_master->init(allows_streaming => 1, extra => $extra); + + # Set wal_keep_segments to prevent WAL segment recycling after enforced + # checkpoints in the tests. + $node_master->append_conf( + 'postgresql.conf', qq( +wal_keep_segments = 20 +)); + return; +} + +sub start_master +{ + $node_master->start; + + #### Now run the test-specific parts to initialize the master before setting + # up standby + + return; +} + +sub create_standby +{ + my $extra_name = shift; + + $node_standby = + get_new_node('standby' . ($extra_name ? "_${extra_name}" : '')); + $node_master->backup('my_backup'); + $node_standby->init_from_backup($node_master, 'my_backup'); + my $connstr_master = $node_master->connstr(); + + $node_standby->append_conf( + "postgresql.conf", qq( +primary_conninfo='$connstr_master application_name=rewind_standby' +)); + + $node_standby->set_standby_mode(); + + # Start standby + $node_standby->start; + + # The standby may have WAL to apply before it matches the primary. That + # is fine, because no test examines the standby before promotion. + + return; +} + +sub promote_standby +{ + #### Now run the test-specific parts to run after standby has been started + # up standby + + # Wait for the standby to receive and write all WAL. + $node_master->wait_for_catchup('rewind_standby', 'write'); + + # Now promote standby and insert some new data on master, this will put + # the master out-of-sync with the standby. + $node_standby->promote; + + # Force a checkpoint after the promotion. pg_rewind looks at the control + # file to determine what timeline the server is on, and that isn't updated + # immediately at promotion, but only at the next checkpoint. When running + # pg_rewind in remote mode, it's possible that we complete the test steps + # after promotion so quickly that when pg_rewind runs, the standby has not + # performed a checkpoint after promotion yet. + standby_psql("checkpoint"); + + return; +} + +sub run_pg_rewind +{ + my $test_mode = shift; + my $master_pgdata = $node_master->data_dir; + my $standby_pgdata = $node_standby->data_dir; + my $standby_connstr = $node_standby->connstr('postgres'); + my $tmp_folder = TestLib::tempdir; + + # Stop the master and be ready to perform the rewind + $node_master->stop; + + # At this point, the rewind processing is ready to run. + # We now have a very simple scenario with a few diverged WAL record. + # The real testing begins really now with a bifurcation of the possible + # scenarios that pg_rewind supports. + + # Keep a temporary postgresql.conf for master node or it would be + # overwritten during the rewind. + copy( + "$master_pgdata/postgresql.conf", + "$tmp_folder/master-postgresql.conf.tmp"); + + # Now run pg_rewind + if ($test_mode eq "local") + { + + # Do rewind using a local pgdata as source + # Stop the master and be ready to perform the rewind + $node_standby->stop; + command_ok( + [ + 'pg_rewind', + "--debug", + "--source-pgdata=$standby_pgdata", + "--target-pgdata=$master_pgdata", + "--no-sync" + ], + 'pg_rewind local'); + } + elsif ($test_mode eq "remote") + { + + # Do rewind using a remote connection as source + command_ok( + [ + 'pg_rewind', "--debug", + "--source-server", $standby_connstr, + "--target-pgdata=$master_pgdata", + "--no-sync" + ], + 'pg_rewind remote'); + } + else + { + + # Cannot come here normally + croak("Incorrect test mode specified"); + } + + # Now move back postgresql.conf with old settings + move( + "$tmp_folder/master-postgresql.conf.tmp", + "$master_pgdata/postgresql.conf"); + + chmod( + $node_master->group_access() ? 0640 : 0600, + "$master_pgdata/postgresql.conf") + or BAIL_OUT( + "unable to set permissions for $master_pgdata/postgresql.conf"); + + # Plug-in rewound node to the now-promoted standby node + my $port_standby = $node_standby->port; + $node_master->append_conf( + 'postgresql.conf', qq( +primary_conninfo='port=$port_standby' +)); + + $node_master->set_standby_mode(); + + # Restart the master to check that rewind went correctly + $node_master->start; + + #### Now run the test-specific parts to check the result + + return; +} + +# Clean up after the test. Stop both servers, if they're still running. +sub clean_rewind_test +{ + $node_master->teardown_node if defined $node_master; + $node_standby->teardown_node if defined $node_standby; + return; +} + +1; diff --git a/src/test/ssl/ServerSetup.pm b/src/test/ssl/ServerSetup.pm deleted file mode 100644 index 5acba52310f..00000000000 --- a/src/test/ssl/ServerSetup.pm +++ /dev/null @@ -1,199 +0,0 @@ -# This module sets up a test server, for the SSL regression tests. -# -# The server is configured as follows: -# -# - SSL enabled, with the server certificate specified by argument to -# switch_server_cert function. -# - ssl/root+client_ca.crt as the CA root for validating client certs. -# - reject non-SSL connections -# - a database called trustdb that lets anyone in -# - another database called certdb that uses certificate authentication, ie. -# the client must present a valid certificate signed by the client CA -# - two users, called ssltestuser and anotheruser. -# -# The server is configured to only accept connections from localhost. If you -# want to run the client from another host, you'll have to configure that -# manually. -# -# Note: Someone running these test could have key or certificate files -# in their ~/.postgresql/, which would interfere with the tests. The -# way to override that is to specify sslcert=invalid and/or -# sslrootcert=invalid if no actual certificate is used for a -# particular test. libpq will ignore specifications that name -# nonexisting files. (sslkey and sslcrl do not need to specified -# explicitly because an invalid sslcert or sslrootcert, respectively, -# causes those to be ignored.) - -package ServerSetup; - -use strict; -use warnings; -use PostgresNode; -use TestLib; -use File::Basename; -use File::Copy; -use Test::More; - -use Exporter 'import'; -our @EXPORT = qw( - configure_test_server_for_ssl - switch_server_cert - test_connect_fails - test_connect_ok -); - -# Define a couple of helper functions to test connecting to the server. - -# The first argument is a base connection string to use for connection. -# The second argument is a complementary connection string. -sub test_connect_ok -{ - local $Test::Builder::Level = $Test::Builder::Level + 1; - - my ($common_connstr, $connstr, $test_name) = @_; - - my $cmd = [ - 'psql', '-X', '-A', '-t', '-c', - "SELECT \$\$connected with $connstr\$\$", - '-d', "$common_connstr $connstr" - ]; - - command_ok($cmd, $test_name); - return; -} - -sub test_connect_fails -{ - local $Test::Builder::Level = $Test::Builder::Level + 1; - - my ($common_connstr, $connstr, $expected_stderr, $test_name) = @_; - - my $cmd = [ - 'psql', '-X', '-A', '-t', '-c', - "SELECT \$\$connected with $connstr\$\$", - '-d', "$common_connstr $connstr" - ]; - - command_fails_like($cmd, $expected_stderr, $test_name); - return; -} - -# Copy a set of files, taking into account wildcards -sub copy_files -{ - my $orig = shift; - my $dest = shift; - - my @orig_files = glob $orig; - foreach my $orig_file (@orig_files) - { - my $base_file = basename($orig_file); - copy($orig_file, "$dest/$base_file") - or die "Could not copy $orig_file to $dest"; - } - return; -} - -sub configure_test_server_for_ssl -{ - my ($node, $serverhost, $authmethod, $password, $password_enc) = @_; - - my $pgdata = $node->data_dir; - - # Create test users and databases - $node->psql('postgres', "CREATE USER ssltestuser"); - $node->psql('postgres', "CREATE USER anotheruser"); - $node->psql('postgres', "CREATE DATABASE trustdb"); - $node->psql('postgres', "CREATE DATABASE certdb"); - - # Update password of each user as needed. - if (defined($password)) - { - $node->psql('postgres', - "SET password_encryption='$password_enc'; ALTER USER ssltestuser PASSWORD '$password';" - ); - $node->psql('postgres', - "SET password_encryption='$password_enc'; ALTER USER anotheruser PASSWORD '$password';" - ); - } - - # enable logging etc. - open my $conf, '>>', "$pgdata/postgresql.conf"; - print $conf "fsync=off\n"; - print $conf "log_connections=on\n"; - print $conf "log_hostname=on\n"; - print $conf "listen_addresses='$serverhost'\n"; - print $conf "log_statement=all\n"; - - # enable SSL and set up server key - print $conf "include 'sslconfig.conf'"; - - close $conf; - - # ssl configuration will be placed here - open my $sslconf, '>', "$pgdata/sslconfig.conf"; - close $sslconf; - - # Copy all server certificates and keys, and client root cert, to the data dir - copy_files("ssl/server-*.crt", $pgdata); - copy_files("ssl/server-*.key", $pgdata); - chmod(0600, glob "$pgdata/server-*.key") or die $!; - copy_files("ssl/root+client_ca.crt", $pgdata); - copy_files("ssl/root_ca.crt", $pgdata); - copy_files("ssl/root+client.crl", $pgdata); - - # Stop and restart server to load new listen_addresses. - $node->restart; - - # Change pg_hba after restart because hostssl requires ssl=on - configure_hba_for_ssl($node, $serverhost, $authmethod); - - return; -} - -# Change the configuration to use given server cert file, and reload -# the server so that the configuration takes effect. -sub switch_server_cert -{ - my $node = $_[0]; - my $certfile = $_[1]; - my $cafile = $_[2] || "root+client_ca"; - my $pgdata = $node->data_dir; - - open my $sslconf, '>', "$pgdata/sslconfig.conf"; - print $sslconf "ssl=on\n"; - print $sslconf "ssl_ca_file='$cafile.crt'\n"; - print $sslconf "ssl_cert_file='$certfile.crt'\n"; - print $sslconf "ssl_key_file='$certfile.key'\n"; - print $sslconf "ssl_crl_file='root+client.crl'\n"; - close $sslconf; - - $node->restart; - return; -} - -sub configure_hba_for_ssl -{ - my ($node, $serverhost, $authmethod) = @_; - my $pgdata = $node->data_dir; - - # Only accept SSL connections from localhost. Our tests don't depend on this - # but seems best to keep it as narrow as possible for security reasons. - # - # When connecting to certdb, also check the client certificate. - open my $hba, '>', "$pgdata/pg_hba.conf"; - print $hba - "# TYPE DATABASE USER ADDRESS METHOD\n"; - print $hba - "hostssl trustdb all $serverhost/32 $authmethod\n"; - print $hba - "hostssl trustdb all ::1/128 $authmethod\n"; - print $hba - "hostssl certdb all $serverhost/32 cert\n"; - print $hba - "hostssl certdb all ::1/128 cert\n"; - close $hba; - return; -} - -1; diff --git a/src/test/ssl/t/001_ssltests.pl b/src/test/ssl/t/001_ssltests.pl index 915007805e5..2f6dfad23c4 100644 --- a/src/test/ssl/t/001_ssltests.pl +++ b/src/test/ssl/t/001_ssltests.pl @@ -3,9 +3,14 @@ use warnings; use PostgresNode; use TestLib; use Test::More; -use ServerSetup; + use File::Copy; +use FindBin; +use lib $FindBin::RealBin; + +use SSLServer; + if ($ENV{with_openssl} eq 'yes') { plan tests => 71; diff --git a/src/test/ssl/t/002_scram.pl b/src/test/ssl/t/002_scram.pl index d45b9c5db3a..e636efa54dc 100644 --- a/src/test/ssl/t/002_scram.pl +++ b/src/test/ssl/t/002_scram.pl @@ -5,9 +5,14 @@ use warnings; use PostgresNode; use TestLib; use Test::More; -use ServerSetup; + use File::Copy; +use FindBin; +use lib $FindBin::RealBin; + +use SSLServer; + if ($ENV{with_openssl} ne 'yes') { plan skip_all => 'SSL not supported by this build'; diff --git a/src/test/ssl/t/SSLServer.pm b/src/test/ssl/t/SSLServer.pm new file mode 100644 index 00000000000..b1b5b7f0b34 --- /dev/null +++ b/src/test/ssl/t/SSLServer.pm @@ -0,0 +1,199 @@ +# This module sets up a test server, for the SSL regression tests. +# +# The server is configured as follows: +# +# - SSL enabled, with the server certificate specified by argument to +# switch_server_cert function. +# - ssl/root+client_ca.crt as the CA root for validating client certs. +# - reject non-SSL connections +# - a database called trustdb that lets anyone in +# - another database called certdb that uses certificate authentication, ie. +# the client must present a valid certificate signed by the client CA +# - two users, called ssltestuser and anotheruser. +# +# The server is configured to only accept connections from localhost. If you +# want to run the client from another host, you'll have to configure that +# manually. +# +# Note: Someone running these test could have key or certificate files +# in their ~/.postgresql/, which would interfere with the tests. The +# way to override that is to specify sslcert=invalid and/or +# sslrootcert=invalid if no actual certificate is used for a +# particular test. libpq will ignore specifications that name +# nonexisting files. (sslkey and sslcrl do not need to specified +# explicitly because an invalid sslcert or sslrootcert, respectively, +# causes those to be ignored.) + +package SSLServer; + +use strict; +use warnings; +use PostgresNode; +use TestLib; +use File::Basename; +use File::Copy; +use Test::More; + +use Exporter 'import'; +our @EXPORT = qw( + configure_test_server_for_ssl + switch_server_cert + test_connect_fails + test_connect_ok +); + +# Define a couple of helper functions to test connecting to the server. + +# The first argument is a base connection string to use for connection. +# The second argument is a complementary connection string. +sub test_connect_ok +{ + local $Test::Builder::Level = $Test::Builder::Level + 1; + + my ($common_connstr, $connstr, $test_name) = @_; + + my $cmd = [ + 'psql', '-X', '-A', '-t', '-c', + "SELECT \$\$connected with $connstr\$\$", + '-d', "$common_connstr $connstr" + ]; + + command_ok($cmd, $test_name); + return; +} + +sub test_connect_fails +{ + local $Test::Builder::Level = $Test::Builder::Level + 1; + + my ($common_connstr, $connstr, $expected_stderr, $test_name) = @_; + + my $cmd = [ + 'psql', '-X', '-A', '-t', '-c', + "SELECT \$\$connected with $connstr\$\$", + '-d', "$common_connstr $connstr" + ]; + + command_fails_like($cmd, $expected_stderr, $test_name); + return; +} + +# Copy a set of files, taking into account wildcards +sub copy_files +{ + my $orig = shift; + my $dest = shift; + + my @orig_files = glob $orig; + foreach my $orig_file (@orig_files) + { + my $base_file = basename($orig_file); + copy($orig_file, "$dest/$base_file") + or die "Could not copy $orig_file to $dest"; + } + return; +} + +sub configure_test_server_for_ssl +{ + my ($node, $serverhost, $authmethod, $password, $password_enc) = @_; + + my $pgdata = $node->data_dir; + + # Create test users and databases + $node->psql('postgres', "CREATE USER ssltestuser"); + $node->psql('postgres', "CREATE USER anotheruser"); + $node->psql('postgres', "CREATE DATABASE trustdb"); + $node->psql('postgres', "CREATE DATABASE certdb"); + + # Update password of each user as needed. + if (defined($password)) + { + $node->psql('postgres', + "SET password_encryption='$password_enc'; ALTER USER ssltestuser PASSWORD '$password';" + ); + $node->psql('postgres', + "SET password_encryption='$password_enc'; ALTER USER anotheruser PASSWORD '$password';" + ); + } + + # enable logging etc. + open my $conf, '>>', "$pgdata/postgresql.conf"; + print $conf "fsync=off\n"; + print $conf "log_connections=on\n"; + print $conf "log_hostname=on\n"; + print $conf "listen_addresses='$serverhost'\n"; + print $conf "log_statement=all\n"; + + # enable SSL and set up server key + print $conf "include 'sslconfig.conf'"; + + close $conf; + + # ssl configuration will be placed here + open my $sslconf, '>', "$pgdata/sslconfig.conf"; + close $sslconf; + + # Copy all server certificates and keys, and client root cert, to the data dir + copy_files("ssl/server-*.crt", $pgdata); + copy_files("ssl/server-*.key", $pgdata); + chmod(0600, glob "$pgdata/server-*.key") or die $!; + copy_files("ssl/root+client_ca.crt", $pgdata); + copy_files("ssl/root_ca.crt", $pgdata); + copy_files("ssl/root+client.crl", $pgdata); + + # Stop and restart server to load new listen_addresses. + $node->restart; + + # Change pg_hba after restart because hostssl requires ssl=on + configure_hba_for_ssl($node, $serverhost, $authmethod); + + return; +} + +# Change the configuration to use given server cert file, and reload +# the server so that the configuration takes effect. +sub switch_server_cert +{ + my $node = $_[0]; + my $certfile = $_[1]; + my $cafile = $_[2] || "root+client_ca"; + my $pgdata = $node->data_dir; + + open my $sslconf, '>', "$pgdata/sslconfig.conf"; + print $sslconf "ssl=on\n"; + print $sslconf "ssl_ca_file='$cafile.crt'\n"; + print $sslconf "ssl_cert_file='$certfile.crt'\n"; + print $sslconf "ssl_key_file='$certfile.key'\n"; + print $sslconf "ssl_crl_file='root+client.crl'\n"; + close $sslconf; + + $node->restart; + return; +} + +sub configure_hba_for_ssl +{ + my ($node, $serverhost, $authmethod) = @_; + my $pgdata = $node->data_dir; + + # Only accept SSL connections from localhost. Our tests don't depend on this + # but seems best to keep it as narrow as possible for security reasons. + # + # When connecting to certdb, also check the client certificate. + open my $hba, '>', "$pgdata/pg_hba.conf"; + print $hba + "# TYPE DATABASE USER ADDRESS METHOD\n"; + print $hba + "hostssl trustdb all $serverhost/32 $authmethod\n"; + print $hba + "hostssl trustdb all ::1/128 $authmethod\n"; + print $hba + "hostssl certdb all $serverhost/32 cert\n"; + print $hba + "hostssl certdb all ::1/128 cert\n"; + close $hba; + return; +} + +1; -- cgit v1.2.3