2014-12-09 10:21:18 -05:00
|
|
|
# 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 authentiction, 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.
|
|
|
|
|
package ServerSetup;
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use warnings;
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
use PostgresNode;
|
2014-12-09 10:21:18 -05:00
|
|
|
use TestLib;
|
2015-04-09 15:07:18 -04:00
|
|
|
use File::Basename;
|
|
|
|
|
use File::Copy;
|
2014-12-09 10:21:18 -05:00
|
|
|
use Test::More;
|
|
|
|
|
|
|
|
|
|
use Exporter 'import';
|
|
|
|
|
our @EXPORT = qw(
|
|
|
|
|
configure_test_server_for_ssl switch_server_cert
|
|
|
|
|
);
|
|
|
|
|
|
2015-04-09 15:07:18 -04:00
|
|
|
# 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);
|
2015-05-23 21:35:49 -04:00
|
|
|
copy($orig_file, "$dest/$base_file")
|
|
|
|
|
or die "Could not copy $orig_file to $dest";
|
2015-04-09 15:07:18 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 10:21:18 -05:00
|
|
|
sub configure_test_server_for_ssl
|
|
|
|
|
{
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
my $node = $_[0];
|
2015-09-02 16:21:38 -04:00
|
|
|
my $serverhost = $_[1];
|
2014-12-09 10:21:18 -05:00
|
|
|
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
my $pgdata = $node->data_dir;
|
|
|
|
|
|
2015-05-23 21:35:49 -04:00
|
|
|
# Create test users and databases
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
$node->psql('postgres', "CREATE USER ssltestuser");
|
|
|
|
|
$node->psql('postgres', "CREATE USER anotheruser");
|
|
|
|
|
$node->psql('postgres', "CREATE DATABASE trustdb");
|
|
|
|
|
$node->psql('postgres', "CREATE DATABASE certdb");
|
2014-12-09 10:21:18 -05:00
|
|
|
|
2015-05-23 21:35:49 -04:00
|
|
|
# enable logging etc.
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
open CONF, ">>$pgdata/postgresql.conf";
|
2015-05-23 21:35:49 -04:00
|
|
|
print CONF "fsync=off\n";
|
|
|
|
|
print CONF "log_connections=on\n";
|
|
|
|
|
print CONF "log_hostname=on\n";
|
2015-09-02 16:21:38 -04:00
|
|
|
print CONF "listen_addresses='$serverhost'\n";
|
2015-05-23 21:35:49 -04:00
|
|
|
print CONF "log_statement=all\n";
|
2014-12-09 10:21:18 -05:00
|
|
|
|
2015-05-23 21:35:49 -04:00
|
|
|
# enable SSL and set up server key
|
|
|
|
|
print CONF "include 'sslconfig.conf'";
|
2014-12-09 10:21:18 -05:00
|
|
|
|
2015-05-23 21:35:49 -04:00
|
|
|
close CONF;
|
2014-12-09 10:21:18 -05:00
|
|
|
|
2015-05-23 21:35:49 -04:00
|
|
|
# Copy all server certificates and keys, and client root cert, to the data dir
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
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);
|
Don't share SSL_CTX between libpq connections.
There were several issues with the old coding:
1. There was a race condition, if two threads opened a connection at the
same time. We used a mutex around SSL_CTX_* calls, but that was not
enough, e.g. if one thread SSL_CTX_load_verify_locations() with one
path, and another thread set it with a different path, before the first
thread got to establish the connection.
2. Opening two different connections, with different sslrootcert settings,
seemed to fail outright with "SSL error: block type is not 01". Not sure
why.
3. We created the SSL object, before calling SSL_CTX_load_verify_locations
and SSL_CTX_use_certificate_chain_file on the SSL context. That was
wrong, because the options set on the SSL context are propagated to the
SSL object, when the SSL object is created. If they are set after the
SSL object has already been created, they won't take effect until the
next connection. (This is bug #14329)
At least some of these could've been fixed while still using a shared
context, but it would've been more complicated and error-prone. To keep
things simple, let's just use a separate SSL context for each connection,
and accept the overhead.
Backpatch to all supported versions.
Report, analysis and test case by Kacper Zuk.
Discussion: <20160920101051.1355.79453@wrigleys.postgresql.org>
2016-10-07 05:20:39 -04:00
|
|
|
copy_files("ssl/root_ca.crt", $pgdata);
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
copy_files("ssl/root+client.crl", $pgdata);
|
2014-12-09 10:21:18 -05:00
|
|
|
|
|
|
|
|
# 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.
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
open HBA, ">$pgdata/pg_hba.conf";
|
2015-05-23 21:35:49 -04:00
|
|
|
print HBA
|
|
|
|
|
"# TYPE DATABASE USER ADDRESS METHOD\n";
|
|
|
|
|
print HBA
|
2015-09-02 16:21:38 -04:00
|
|
|
"hostssl trustdb ssltestuser $serverhost/32 trust\n";
|
2015-05-23 21:35:49 -04:00
|
|
|
print HBA
|
|
|
|
|
"hostssl trustdb ssltestuser ::1/128 trust\n";
|
|
|
|
|
print HBA
|
2015-09-02 16:21:38 -04:00
|
|
|
"hostssl certdb ssltestuser $serverhost/32 cert\n";
|
2015-05-23 21:35:49 -04:00
|
|
|
print HBA
|
|
|
|
|
"hostssl certdb ssltestuser ::1/128 cert\n";
|
|
|
|
|
close HBA;
|
2014-12-09 10:21:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Change the configuration to use given server cert file, and restart
|
|
|
|
|
# the server so that the configuration takes effect.
|
|
|
|
|
sub switch_server_cert
|
|
|
|
|
{
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
my $node = $_[0];
|
2015-05-23 21:35:49 -04:00
|
|
|
my $certfile = $_[1];
|
Don't share SSL_CTX between libpq connections.
There were several issues with the old coding:
1. There was a race condition, if two threads opened a connection at the
same time. We used a mutex around SSL_CTX_* calls, but that was not
enough, e.g. if one thread SSL_CTX_load_verify_locations() with one
path, and another thread set it with a different path, before the first
thread got to establish the connection.
2. Opening two different connections, with different sslrootcert settings,
seemed to fail outright with "SSL error: block type is not 01". Not sure
why.
3. We created the SSL object, before calling SSL_CTX_load_verify_locations
and SSL_CTX_use_certificate_chain_file on the SSL context. That was
wrong, because the options set on the SSL context are propagated to the
SSL object, when the SSL object is created. If they are set after the
SSL object has already been created, they won't take effect until the
next connection. (This is bug #14329)
At least some of these could've been fixed while still using a shared
context, but it would've been more complicated and error-prone. To keep
things simple, let's just use a separate SSL context for each connection,
and accept the overhead.
Backpatch to all supported versions.
Report, analysis and test case by Kacper Zuk.
Discussion: <20160920101051.1355.79453@wrigleys.postgresql.org>
2016-10-07 05:20:39 -04:00
|
|
|
my $cafile = $_[2] || "root+client_ca";
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
my $pgdata = $node->data_dir;
|
2015-05-23 21:35:49 -04:00
|
|
|
|
Don't share SSL_CTX between libpq connections.
There were several issues with the old coding:
1. There was a race condition, if two threads opened a connection at the
same time. We used a mutex around SSL_CTX_* calls, but that was not
enough, e.g. if one thread SSL_CTX_load_verify_locations() with one
path, and another thread set it with a different path, before the first
thread got to establish the connection.
2. Opening two different connections, with different sslrootcert settings,
seemed to fail outright with "SSL error: block type is not 01". Not sure
why.
3. We created the SSL object, before calling SSL_CTX_load_verify_locations
and SSL_CTX_use_certificate_chain_file on the SSL context. That was
wrong, because the options set on the SSL context are propagated to the
SSL object, when the SSL object is created. If they are set after the
SSL object has already been created, they won't take effect until the
next connection. (This is bug #14329)
At least some of these could've been fixed while still using a shared
context, but it would've been more complicated and error-prone. To keep
things simple, let's just use a separate SSL context for each connection,
and accept the overhead.
Backpatch to all supported versions.
Report, analysis and test case by Kacper Zuk.
Discussion: <20160920101051.1355.79453@wrigleys.postgresql.org>
2016-10-07 05:20:39 -04:00
|
|
|
diag "Restarting server with certfile \"$certfile\" and cafile \"$cafile\"...";
|
2015-05-23 21:35:49 -04:00
|
|
|
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
open SSLCONF, ">$pgdata/sslconfig.conf";
|
2015-05-23 21:35:49 -04:00
|
|
|
print SSLCONF "ssl=on\n";
|
Don't share SSL_CTX between libpq connections.
There were several issues with the old coding:
1. There was a race condition, if two threads opened a connection at the
same time. We used a mutex around SSL_CTX_* calls, but that was not
enough, e.g. if one thread SSL_CTX_load_verify_locations() with one
path, and another thread set it with a different path, before the first
thread got to establish the connection.
2. Opening two different connections, with different sslrootcert settings,
seemed to fail outright with "SSL error: block type is not 01". Not sure
why.
3. We created the SSL object, before calling SSL_CTX_load_verify_locations
and SSL_CTX_use_certificate_chain_file on the SSL context. That was
wrong, because the options set on the SSL context are propagated to the
SSL object, when the SSL object is created. If they are set after the
SSL object has already been created, they won't take effect until the
next connection. (This is bug #14329)
At least some of these could've been fixed while still using a shared
context, but it would've been more complicated and error-prone. To keep
things simple, let's just use a separate SSL context for each connection,
and accept the overhead.
Backpatch to all supported versions.
Report, analysis and test case by Kacper Zuk.
Discussion: <20160920101051.1355.79453@wrigleys.postgresql.org>
2016-10-07 05:20:39 -04:00
|
|
|
print SSLCONF "ssl_ca_file='$cafile.crt'\n";
|
2015-05-23 21:35:49 -04:00
|
|
|
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;
|
|
|
|
|
|
2015-09-02 16:21:38 -04:00
|
|
|
# Stop and restart server to reload the new config.
|
Refactor Perl test code
The original code was a bit clunky; make it more amenable for further
reuse by creating a new Perl package PostgresNode, which is an
object-oriented representation of a single server, with some support
routines such as init, start, stop, psql. This serves as a better basis
on which to build further test code, and enables writing tests that use
more than one server without too much complication.
This commit modifies a lot of the existing test files, mostly to remove
explicit calls to system commands (pg_ctl) replacing them with method
calls of a PostgresNode object. The result is quite a bit more
straightforward.
Also move some initialization code to BEGIN and INIT blocks instead of
having it straight in as top-level code.
This commit also introduces package RecursiveCopy so that we can copy
whole directories without having to depend on packages that may not be
present on vanilla Perl 5.8 installations.
I also ran perltidy on the modified files, which changes some code sites
that are not otherwise touched by this patch. I tried to avoid this,
but it ended up being more trouble than it's worth.
Authors: Michael Paquier, Álvaro Herrera
Review: Noah Misch
2015-12-02 16:46:16 -05:00
|
|
|
$node->restart;
|
2014-12-09 10:21:18 -05:00
|
|
|
}
|