PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/ssl/ServerSetup.pm

https://gitlab.com/kajan-rezg/postgres
Perl | 118 lines | 74 code | 16 blank | 28 comment | 0 complexity | d0203cc54e952a6dcd1baf384787b210 MD5 | raw file
  1. # This module sets up a test server, for the SSL regression tests.
  2. #
  3. # The server is configured as follows:
  4. #
  5. # - SSL enabled, with the server certificate specified by argument to
  6. # switch_server_cert function.
  7. # - ssl/root+client_ca.crt as the CA root for validating client certs.
  8. # - reject non-SSL connections
  9. # - a database called trustdb that lets anyone in
  10. # - another database called certdb that uses certificate authentiction, ie.
  11. # the client must present a valid certificate signed by the client CA
  12. # - two users, called ssltestuser and anotheruser.
  13. #
  14. # The server is configured to only accept connections from localhost. If you
  15. # want to run the client from another host, you'll have to configure that
  16. # manually.
  17. package ServerSetup;
  18. use strict;
  19. use warnings;
  20. use PostgresNode;
  21. use TestLib;
  22. use File::Basename;
  23. use File::Copy;
  24. use Test::More;
  25. use Exporter 'import';
  26. our @EXPORT = qw(
  27. configure_test_server_for_ssl switch_server_cert
  28. );
  29. # Copy a set of files, taking into account wildcards
  30. sub copy_files
  31. {
  32. my $orig = shift;
  33. my $dest = shift;
  34. my @orig_files = glob $orig;
  35. foreach my $orig_file (@orig_files)
  36. {
  37. my $base_file = basename($orig_file);
  38. copy($orig_file, "$dest/$base_file")
  39. or die "Could not copy $orig_file to $dest";
  40. }
  41. }
  42. sub configure_test_server_for_ssl
  43. {
  44. my $node = $_[0];
  45. my $serverhost = $_[1];
  46. my $pgdata = $node->data_dir;
  47. # Create test users and databases
  48. $node->psql('postgres', "CREATE USER ssltestuser");
  49. $node->psql('postgres', "CREATE USER anotheruser");
  50. $node->psql('postgres', "CREATE DATABASE trustdb");
  51. $node->psql('postgres', "CREATE DATABASE certdb");
  52. # enable logging etc.
  53. open CONF, ">>$pgdata/postgresql.conf";
  54. print CONF "fsync=off\n";
  55. print CONF "log_connections=on\n";
  56. print CONF "log_hostname=on\n";
  57. print CONF "listen_addresses='$serverhost'\n";
  58. print CONF "log_statement=all\n";
  59. # enable SSL and set up server key
  60. print CONF "include 'sslconfig.conf'";
  61. close CONF;
  62. # Copy all server certificates and keys, and client root cert, to the data dir
  63. copy_files("ssl/server-*.crt", $pgdata);
  64. copy_files("ssl/server-*.key", $pgdata);
  65. chmod(0600, glob "$pgdata/server-*.key") or die $!;
  66. copy_files("ssl/root+client_ca.crt", $pgdata);
  67. copy_files("ssl/root+client.crl", $pgdata);
  68. # Only accept SSL connections from localhost. Our tests don't depend on this
  69. # but seems best to keep it as narrow as possible for security reasons.
  70. #
  71. # When connecting to certdb, also check the client certificate.
  72. open HBA, ">$pgdata/pg_hba.conf";
  73. print HBA
  74. "# TYPE DATABASE USER ADDRESS METHOD\n";
  75. print HBA
  76. "hostssl trustdb ssltestuser $serverhost/32 trust\n";
  77. print HBA
  78. "hostssl trustdb ssltestuser ::1/128 trust\n";
  79. print HBA
  80. "hostssl certdb ssltestuser $serverhost/32 cert\n";
  81. print HBA
  82. "hostssl certdb ssltestuser ::1/128 cert\n";
  83. close HBA;
  84. }
  85. # Change the configuration to use given server cert file, and restart
  86. # the server so that the configuration takes effect.
  87. sub switch_server_cert
  88. {
  89. my $node = $_[0];
  90. my $certfile = $_[1];
  91. my $pgdata = $node->data_dir;
  92. diag "Restarting server with certfile \"$certfile\"...";
  93. open SSLCONF, ">$pgdata/sslconfig.conf";
  94. print SSLCONF "ssl=on\n";
  95. print SSLCONF "ssl_ca_file='root+client_ca.crt'\n";
  96. print SSLCONF "ssl_cert_file='$certfile.crt'\n";
  97. print SSLCONF "ssl_key_file='$certfile.key'\n";
  98. print SSLCONF "ssl_crl_file='root+client.crl'\n";
  99. close SSLCONF;
  100. # Stop and restart server to reload the new config.
  101. $node->restart;
  102. }