|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Tests that pg_tde_basebackup -E works after setting just a default |
| 4 | +# principal key, without first restarting the primary. Before the fix, |
| 5 | +# the server (WAL) principal key was only materialized lazily on the |
| 6 | +# next server start, so taking an encrypted-WAL base backup of a freshly |
| 7 | +# configured cluster would fail with "could not find server principal key". |
| 8 | + |
| 9 | +use strict; |
| 10 | +use warnings; |
| 11 | +use File::Basename; |
| 12 | +use Test::More; |
| 13 | +use PostgreSQL::Test::Cluster; |
| 14 | +use PostgreSQL::Test::Utils; |
| 15 | +use PostgreSQL::Test::RecursiveCopy; |
| 16 | + |
| 17 | +my $keyfile = '/tmp/basebackup_default_key.per'; |
| 18 | +unlink($keyfile); |
| 19 | + |
| 20 | +my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 21 | +$primary->init(allows_streaming => 1); |
| 22 | +$primary->append_conf('postgresql.conf', |
| 23 | + "shared_preload_libraries = 'pg_tde'"); |
| 24 | +$primary->start; |
| 25 | + |
| 26 | +$primary->safe_psql('postgres', 'CREATE EXTENSION pg_tde;'); |
| 27 | +$primary->safe_psql('postgres', |
| 28 | + "SELECT pg_tde_add_global_key_provider_file('file-provider','$keyfile');" |
| 29 | +); |
| 30 | +$primary->safe_psql('postgres', |
| 31 | + "SELECT pg_tde_create_key_using_global_key_provider('key1','file-provider');" |
| 32 | +); |
| 33 | +$primary->safe_psql('postgres', |
| 34 | + "SELECT pg_tde_set_default_key_using_global_key_provider('key1','file-provider');" |
| 35 | +); |
| 36 | + |
| 37 | +my $server_key = $primary->safe_psql('postgres', |
| 38 | + 'SELECT key_name FROM pg_tde_server_key_info();'); |
| 39 | +is($server_key, 'key1', |
| 40 | + 'server principal key auto-configured when default key is set'); |
| 41 | + |
| 42 | +my $tempdir = PostgreSQL::Test::Utils::tempdir; |
| 43 | +my $backup_dir = "$tempdir/backup"; |
| 44 | + |
| 45 | +mkdir $backup_dir or die "mkdir $backup_dir failed: $!"; |
| 46 | +PostgreSQL::Test::RecursiveCopy::copypath($primary->data_dir . '/pg_tde', |
| 47 | + $backup_dir . '/pg_tde'); |
| 48 | + |
| 49 | +$primary->command_ok( |
| 50 | + [ |
| 51 | + 'pg_tde_basebackup', '-D', |
| 52 | + $backup_dir, '-h', |
| 53 | + $primary->host, '-p', |
| 54 | + $primary->port, '--checkpoint', |
| 55 | + 'fast', '--no-sync', |
| 56 | + '-E', '-X', |
| 57 | + 'stream', |
| 58 | + ], |
| 59 | + 'pg_tde_basebackup -E succeeds after only setting default key'); |
| 60 | + |
| 61 | +$primary->stop; |
| 62 | + |
| 63 | +done_testing(); |
0 commit comments