Skip to content

PG-1447 Test and fix support for template databases #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: TDE_REL_17_STABLE
Choose a base branch
from

Conversation

jeltz
Copy link
Collaborator

@jeltz jeltz commented Apr 26, 2025

Using a database with encrypted relations as template works with the WAL_LOG strategy as long as you have configured a default principal key. It is a bit hackish but works and since encrypted objects in a template database is likely to be an uncommon use case this is fine for now.

The test file is put last in the test run order since there is still no good way to clean up default principal keys.

Additionally we assert there are no encrypted relations when using FILE_COPY.

The FILE_COPY strategy of CREATE DATABASE does not use the SMGR so it cannot properly copy and re-encrypt encrypted relations. So we check for such relations and error out if any are found.

We look for encrypted relations simply by counting the number of keys in the key map file of the database. This simple approach is fine even with the risk of a left-over key from a crash or a bug due to the FILE_COPY strategy being rare and that we therefore want to keep this code minimal.

@jeltz jeltz requested review from dutow and dAdAbird as code owners April 26, 2025 00:33
@jeltz jeltz force-pushed the tde/template-dbs branch from 72fc9a3 to 115832a Compare April 26, 2025 00:34
@codecov-commenter
Copy link

codecov-commenter commented Apr 26, 2025

Codecov Report

Attention: Patch coverage is 84.00000% with 12 lines in your changes missing coverage. Please review.

Project coverage is 79.03%. Comparing base (fd43ead) to head (9a8e257).

❌ Your project status has failed because the head coverage (79.03%) is below the target coverage (90.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@                  Coverage Diff                  @@
##           TDE_REL_17_STABLE     #270      +/-   ##
=====================================================
+ Coverage              78.93%   79.03%   +0.10%     
=====================================================
  Files                     22       22              
  Lines                   2464     2533      +69     
  Branches                 385      397      +12     
=====================================================
+ Hits                    1945     2002      +57     
- Misses                   444      452       +8     
- Partials                  75       79       +4     
Components Coverage Δ
access 81.57% <86.95%> (+0.02%) ⬆️
catalog 86.32% <ø> (ø)
common 92.50% <ø> (ø)
encryption 71.90% <ø> (ø)
keyring 72.07% <ø> (ø)
src 59.56% <82.69%> (+3.83%) ⬆️
smgr 98.01% <ø> (ø)
transam ∅ <ø> (∅)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jeltz jeltz force-pushed the tde/template-dbs branch 7 times, most recently from 7ea8a82 to c3c8917 Compare April 26, 2025 02:26
(1 row)

CREATE DATABASE new_db TEMPLATE template_db;
ERROR: database "new_db" already exists
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something seems to be wrong in this testcase - this database already exists.

The description also said it only works if we have a default key, but we didn't have one during the previous create db.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems possibly like a bug unrelated to the PR. And since it only happens when when TDE is enabled for the whole test suite it will not directly be an issue now that that has been changed but I think we should take a look and try to figure out if I accidentally found a bug here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the bug was in my test script. Doh. The issue is that we cannot delete the current default principal key. These tests used to run before the default key provider tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by adding a comment and commenting out the code.

@jeltz jeltz force-pushed the tde/template-dbs branch 4 times, most recently from d2b6a47 to 33509c4 Compare April 28, 2025 13:03
@jeltz jeltz requested review from dutow and AndersAstrand April 28, 2025 15:55
Copy link
Collaborator

@AndersAstrand AndersAstrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment in Makefile and meson.build should probably be clearer.

@@ -8,7 +8,7 @@ DATA = pg_tde--1.0-rc.sql
# Since meson supports skipping test suites this is a make only feature
ifndef TDE_MODE
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_tde/pg_tde.conf
# default_principal_key needs to run after key_provider.
# default_principal_key needs and create_database to run after key_provider.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence makes no sense to me, but looking at the code it seems like you want to say that default_principal_key needs to run after key_provider, and create_database needs to run after default_principal_key?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was lazy when writing it. Should fix it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that there is currently no way to delete default principal keys so these tests becom order dependent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

jeltz added 2 commits April 28, 2025 19:05
Using a database with encrypted relations as template works works with
the WAL_LOG strateg as long as you have configured a default principal
key. It is a bit hackish but works and since encrypted objects in a
template database is likely to be an uncommon use case this is fine for
now.

The test file is put last in the test run order since there is still no
good way to clean up default principal keys.
The FILE_COPY strategy of CREATE DATABASE does not use the SMGR so it
cannot properly copy and re-encrypt encrypted relations. So we check for
such relations and error out if any are found.

We look for encrypted relations simply by counting the number of keys in
the key map file of the database. This simple approach is fine even with
the risk of a left-over key from a crash or a bug due to the FILE_COPY
method being rare and that we therefore want to keep this code minimal.
@jeltz jeltz force-pushed the tde/template-dbs branch from 33509c4 to 9a8e257 Compare April 28, 2025 17:05
@jeltz jeltz requested a review from AndersAstrand April 28, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants