Skip to content

Commit 36e548b

Browse files
committed
PG-1870 Load pg_tde and setup key in TAP tests
This makes sure pg_tde is loaded and keys are setup when running postgresql TAP suite. No TDE features are enabled at this point. When running with meson the setup is sped up by creating a template for the key files before the first test is run. In make this setup will currently be done once per test file instead of once for the whole suite.
1 parent 415cb8d commit 36e548b

File tree

6 files changed

+180
-10
lines changed

6 files changed

+180
-10
lines changed

meson.build

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,48 @@ sys.exit(sp.returncode)
34453445
env: test_env,
34463446
suite: ['setup'])
34473447

3448+
test_tde_template_dir = test_install_destdir / 'pg_tde_template'
3449+
test_env.set('TDE_TEMPLATE_DIR', test_tde_template_dir)
3450+
3451+
test('init_tde_files',
3452+
find_program('sh', required: true, native: true),
3453+
args: [
3454+
'-c', '''
3455+
set -e
3456+
3457+
if [ -z "$TDE_MODE" ]; then
3458+
exit;
3459+
fi
3460+
3461+
set -e
3462+
3463+
PATH="$1":$PATH
3464+
TMP_DATA_DIR=$(mktemp -d)
3465+
3466+
rm -rf "$2"
3467+
mkdir "$2"
3468+
3469+
pg_ctl -D "$TMP_DATA_DIR" init -o '--set shared_preload_libraries=pg_tde'
3470+
3471+
postgres --single -F -j -D "$TMP_DATA_DIR" postgres << SQL
3472+
CREATE EXTENSION pg_tde;
3473+
SELECT pg_tde_add_global_key_provider_file('global_test_provider', '$2/pg_tde_test_keys');
3474+
SELECT pg_tde_create_key_using_global_key_provider('test_default_key', 'global_test_provider');
3475+
SELECT pg_tde_set_default_key_using_global_key_provider('test_default_key', 'global_test_provider');
3476+
SQL
3477+
3478+
cp -RPp "$TMP_DATA_DIR/pg_tde" "$2/pg_tde"
3479+
rm -rf "$TMP_DATA_DIR"
3480+
''',
3481+
'init_tde_files',
3482+
temp_install_bindir,
3483+
test_tde_template_dir
3484+
],
3485+
priority: setup_tests_priority - 2,
3486+
timeout: 300,
3487+
is_parallel: false,
3488+
env: test_env,
3489+
suite: ['setup'])
34483490

34493491

34503492
###############################################################

src/bin/pg_rewind/t/002_databases.pl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
use RewindTest;
1313

14+
if ($ENV{TDE_MODE} and not $ENV{TDE_MODE_NOSKIP})
15+
{
16+
plan skip_all =>
17+
"pg_combinebackup doesn't set filemodes of pg_tde/ correctly?";
18+
}
19+
1420
sub run_test
1521
{
1622
my $test_mode = shift;

src/bin/pg_upgrade/t/002_pg_upgrade.pl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
use PostgreSQL::Test::AdjustUpgrade;
1616
use Test::More;
1717

18-
if (defined($ENV{TDE_MODE}))
19-
{
20-
plan skip_all => "Running with TDE doesn't support special server starts yet";
21-
}
22-
2318
# Can be changed to test the other modes.
2419
my $mode = $ENV{PG_TEST_PG_UPGRADE_MODE} || '--copy';
2520

src/test/perl/PostgreSQL/Test/Cluster.pm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ use File::Temp ();
107107
use IPC::Run;
108108
use PostgreSQL::Version;
109109
use PostgreSQL::Test::RecursiveCopy;
110+
use PostgreSQL::Test::TdeCluster;
110111
use Socket;
111112
use Test::More;
112113
use PostgreSQL::Test::Utils ();
@@ -1527,6 +1528,11 @@ sub new
15271528
}
15281529
}
15291530

1531+
if ($ENV{TDE_MODE})
1532+
{
1533+
bless $node, 'PostgreSQL::Test::TdeCluster';
1534+
}
1535+
15301536
# Add node to list of nodes
15311537
push(@all_nodes, $node);
15321538

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package PostgreSQL::Test::TdeCluster;
2+
3+
use parent 'PostgreSQL::Test::Cluster';
4+
5+
use strict;
6+
use warnings FATAL => 'all';
7+
8+
use List::Util ();
9+
use PostgreSQL::Test::RecursiveCopy ();
10+
use PostgreSQL::Test::Utils ();
11+
12+
our ($tde_template_dir);
13+
14+
BEGIN
15+
{
16+
$ENV{TDE_MODE_NOSKIP} = 0 unless defined($ENV{TDE_MODE_NOSKIP});
17+
}
18+
19+
sub init
20+
{
21+
my ($self, %params) = @_;
22+
23+
$self->SUPER::init(%params);
24+
25+
$self->SUPER::append_conf('postgresql.conf',
26+
'shared_preload_libraries = pg_tde');
27+
28+
$self->_tde_init_principal_key;
29+
30+
return;
31+
}
32+
33+
sub append_conf
34+
{
35+
my ($self, $filename, $str) = @_;
36+
37+
if ($filename eq 'postgresql.conf' or $filename eq 'postgresql.auto.conf')
38+
{
39+
# TODO: Will not work with shared_preload_libraries= without any
40+
# libraries, but no TAP test currently do that.
41+
$str =~
42+
s/shared_preload_libraries *= *'?([^'\n]+)'?/shared_preload_libraries = 'pg_tde,$1'/;
43+
}
44+
45+
$self->SUPER::append_conf($filename, $str);
46+
}
47+
48+
sub pg_tde_dir
49+
{
50+
my ($self) = @_;
51+
return $self->data_dir . '/pg_tde';
52+
}
53+
54+
sub _tde_init_principal_key
55+
{
56+
my ($self) = @_;
57+
58+
my $tde_template_dir = $ENV{TDE_TEMPLATE_DIR}
59+
if defined($ENV{TDE_TEMPLATE_DIR});
60+
61+
unless (defined($tde_template_dir))
62+
{
63+
$tde_template_dir =
64+
$PostgreSQL::Test::Utils::tmp_check . '/pg_tde_template';
65+
66+
unless (-e $tde_template_dir)
67+
{
68+
my $temp_dir = PostgreSQL::Test::Utils::tempdir();
69+
mkdir $tde_template_dir;
70+
71+
PostgreSQL::Test::Utils::system_log(
72+
'initdb',
73+
'-D' => $temp_dir,
74+
'--set' => 'shared_preload_libraries=pg_tde');
75+
76+
_tde_init_sql_command(
77+
$temp_dir, 'postgres', qq(
78+
CREATE EXTENSION pg_tde;
79+
SELECT pg_tde_add_global_key_provider_file('global_test_provider', '$tde_template_dir/pg_tde_test_keys');
80+
SELECT pg_tde_create_key_using_global_key_provider('default_test_key', 'global_test_provider');
81+
SELECT pg_tde_set_default_key_using_global_key_provider('default_test_key', 'global_test_provider');
82+
));
83+
84+
PostgreSQL::Test::Utils::system_log('cp', '-R', '-P', '-p',
85+
$temp_dir . '/pg_tde',
86+
$tde_template_dir);
87+
}
88+
}
89+
90+
PostgreSQL::Test::Utils::system_log('cp', '-R', '-P', '-p',
91+
$tde_template_dir . '/pg_tde',
92+
$self->pg_tde_dir);
93+
94+
# We don't want clusters sharing the KMS file as any concurrent writes will
95+
# mess it up.
96+
PostgreSQL::Test::Utils::system_log(
97+
'cp', '-R', '-P', '-p',
98+
$tde_template_dir . '/pg_tde_test_keys',
99+
$self->basedir . '/pg_tde_test_keys');
100+
101+
PostgreSQL::Test::Utils::system_log(
102+
'pg_tde_change_key_provider',
103+
'-D' => $self->data_dir,
104+
'1664',
105+
'global_test_provider',
106+
'file',
107+
$self->basedir . '/pg_tde_test_keys');
108+
}
109+
110+
sub _tde_init_sql_command
111+
{
112+
my ($datadir, $database, $sql) = @_;
113+
PostgreSQL::Test::Utils::run_log(
114+
[
115+
'postgres',
116+
'--single', '-j', '-F',
117+
'-D' => $datadir,
118+
'-c' => 'exit_on_error=true',
119+
'-c' => 'log_checkpoints=false',
120+
$database,
121+
],
122+
'<',
123+
\$sql);
124+
}
125+
126+
1;

src/test/recovery/t/027_stream_regress.pl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
use Test::More;
1010
use File::Basename;
1111

12-
if (defined($ENV{TDE_MODE}))
13-
{
14-
plan skip_all => "Running with TDE doesn't support special server starts yet";
15-
}
16-
1712
# Initialize primary node
1813
my $node_primary = PostgreSQL::Test::Cluster->new('primary');
1914
$node_primary->init(allows_streaming => 1);

0 commit comments

Comments
 (0)