5080 Commits

Author SHA1 Message Date
Andreas Karlsson
5f279ad13b Fix broken reuse of deleted entries in key map file
Since we tried to check if flags & MAP_ENTRY_EMPTY was true when
searching for empty entries the code was broken since x & 0 always is
false. We fix this by refactoring pg_tde_read_one_map_entry() so the
filtering of the entries is done outside the function. This make
implementing search for empty entries much easier.
2025-04-22 16:04:37 +02:00
Andreas Karlsson
a6f774e57e PG-1444 Move relation key deleteion to smgr_unlink()
Replaces the old way we deleted keys which was built for tde_heap_basic
with deleting the the relation key when smgr_unlink() is called on the
main fork. This function is always called after commit/abort when a
relation deletion has been registered, even if no main fork would exist.

This approach means we do not need to WAL log any event for deleting
relation keys, the normal SMGR unlink also handles that which fits well
into the current approach of doing most of the encryption at the SMGR
layer.

We also remove the subtransaction test which is no longer useful since
it tested things very specific to the old key deleteion.
2025-04-22 11:59:49 +02:00
Andreas Karlsson
ec51d0895a PG-1444 Remove dead code for relation key deletion redo
This code is dead and there is no plan to re-use it any time soon.
2025-04-22 11:59:49 +02:00
Anders Åstrand
71da1f03f2 Run pgperltidy
Just as we use pgindent we should probably use pgperltidy. This is an
initial run of it using the following command:
src/tools/pgindent/pgperltidy contrib/pg_tde
2025-04-22 11:40:24 +02:00
Mohit Joshi
54cd79c81e
PG-1517 - Automate testcase for (#243)
PG-1473 - Executing pg_tde_verify_principal_key() must require key
viewer permission.
2025-04-22 14:49:45 +05:30
Anders Åstrand
607cf9397d Remove unused tests
These were not present in meson.build or Makefile, and as such are just
extra maintenance cost for no value.
2025-04-22 10:33:23 +02:00
Andreas Karlsson
eed26578cc Remove pgtde_init_pg() TAP helper
This helper mostly added confusion by making it seem like it did more
work than is actually did. And especially since we will want to call
init in the future with different parameters for some tests or
initialize from a backup.
2025-04-22 09:02:40 +02:00
Andreas Karlsson
ca37d73e9d Remove dead code for scanning key providers by type
This code has never been used.
2025-04-19 03:07:04 +02:00
Mohit Joshi
eb8b307b4c
PG-1491 Add SQL test cases for pg_tde_is_encrypted() for partitioned tables (#207)
PG-1491 pg_tde_is_encrypted() is broken for partitioned tables

Following new tescases added:
partition_table.sql
2025-04-18 15:15:57 +03:00
Andreas Karlsson
cf2806d9f3 Simplify setup_files_dir() in pgtde.pm 2025-04-18 09:59:59 +02:00
Andreas Karlsson
5fb92917f7 Drop support for PostgreSQL <15 in pgtde.pm
Since until we actually have CI for older versions the code is likely
broken anyway we might as well not try to support versions we do not
actually support. It is easy to re-add this once we want to add support
for PostgreSQL 14.
2025-04-18 09:59:59 +02:00
Andreas Karlsson
1df4ff5110 Remove unnecesary comments and code in pgtde.pm
None of this is useful or used.
2025-04-18 09:59:59 +02:00
Andreas Karlsson
3b6d3d46dd Use append_conf() helper in TAP tests
This is what PostgreSQL has been using since 9.6 so we can safely use it
too in our tests.
2025-04-18 01:51:40 +02:00
Anders Åstrand
1390dd0cef PG-1535 Do not delete global key provider in use
The code wrongly assumed that the databaseId set in the keyInfo returned
from GetPrincipalKeyNoDefault() would be the Oid of the key provider
owner, while in reality it is the Oid of the database using it as a
principal key.
2025-04-17 20:25:47 +02:00
Shahid Ullah
40c32b2de2
[PG-1545] - Remove pg_tde_revoke_global_key_management_from_role refe… (#228)
…rence
2025-04-17 18:43:55 +05:00
Andreas Karlsson
ee3279b9b4 PG-1441 Sort resource manager record types
Since we are breaking backwards compatbility anyway we might as well
have them in a bit nicer order.
2025-04-17 11:02:17 +02:00
Andreas Karlsson
ac53512847 PG-1441 Further improve resource manager type names
The type names were a a mess with diffeent naming convetions, e.g. KEY
vs PRINCIPAL_KEY, so try to standardize on something sane.
2025-04-17 11:02:17 +02:00
Andreas Karlsson
33d78260c5 PG-1441 Add record type names and descriptions for all types
Some resource manager record types were missing the name or the
description.
2025-04-17 11:02:17 +02:00
Andreas Karlsson
35ff2fdca8 PG-1441 Improve pg_tde resource manager record types and decriptions
While there is a quite big variation already among PostgreSQL's own
record types and decriptions at least try not to invent something
totally different.
2025-04-17 11:02:17 +02:00
Andreas Karlsson
ee8c285ce6 PG-1441 Change the name of the WAL resource manager
Seems like we used a plaholder name so instead picked pg_tde since that
is more than clear enough.
2025-04-17 11:02:17 +02:00
Andreas Karlsson
8a7fc7acb5 PG-1441 Clean up code formatting of pg_tde WAL resource manager 2025-04-17 11:02:17 +02:00
Andrew Pogrebnoy
cb80b20a43 pfree tmp key when set principal key
Pushing a principal key to the cache, we copy it to the shared mem.
Hence, the palloced tmp version can be freed.
2025-04-17 09:38:10 +03:00
Andreas Karlsson
832cbb4f4d Convert output of TAP tests to be more like pg_regress
This improves readbility of diffs a lot.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
908d0776ba Rename test helper append_to_file() to append_to_result_file()
This way we can also import PostgreSQL's test utilities into the
current namespace.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
b190cd0985 Add test helper to TAP tests which runs psql and appends
This simplifies working with tests a lot since now we will also always
get the errors from failed queries directly in the test output instead
of it being truncated when the TAP tests aborts due to the query
failing.

There is still a good case for why we should instead write idiomatic TAP
tests but this at least does a lot to improve the expereince of people
who have to work with these tests without changing the way the tests
work. Plus that the code is cleaner now so it should be easier to move
away from this way of testing in the future.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
891adb150c Always restart the server in the same way in our TAP tests
It is hard to keep track when every place does it differently.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
3bb5edf7b9 Remove pointless white space and comments from TAP tests
It removed readability rather than improved it.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
233305bbc4 Remove unused includes in TAP tests
Additionally we remove unnecessary copies if the generated config file.
2025-04-17 00:32:50 +02:00
Andreas Karlsson
945cbe436b Make global variable local in TAP tests 2025-04-17 00:32:50 +02:00
Andreas Karlsson
a4818a845f Clean up TAP test helpers
Checking for PostgreSQL versions can be done in a cleaner way plus we do
not need to run chmod.
2025-04-17 00:32:50 +02:00
Andrew Pogrebnoy
cc459df896 Always pfree tmp WAL key when allocated
We didn't pfree the key in case of `EncryptXLog == true && key == true`
2025-04-16 18:17:41 +03:00
Andreas Karlsson
033f7cae77 Make sure we have tests which fetch KMIP and Vault keys
Before this we had no coverage of the code path for successfully
fetching a KMIP key from the key provider.
2025-04-16 14:03:54 +02:00
Andreas Karlsson
44c3619f5e Stop pretending that we use autotools to build config.h
We have not been using autotools since commit
e0978a8be6c70b2fccc86ca1cb8fc5499dd83a88 so stop pretending that we do
related to config.h and instead directly have the necessary defines with
the right names in pg_tde.h.
2025-04-16 14:02:48 +02:00
Andreas Karlsson
c10665840e Remove dead code in pg_tde_defs.c 2025-04-16 14:02:48 +02:00
Andreas Karlsson
6aa4203eca Actually run the test of pg_tde_version()
The test was added in fb5658242eed71b645768ec35ec13d3e4c8cc1e4 but never
actually added to the lists of tests.
2025-04-16 14:02:31 +02:00
Anders Åstrand
a08ed99570 Modernize the way we call ereport() part 2
These were not updated in 725c34da520cca4597f6751d1d671c6191466200 and
we still want to use this way of calling ereport() everywhere.
2025-04-16 11:05:52 +02:00
Andreas Karlsson
20ab981941 PG-1440 Remove unnecesary query for test case
This query gave no extra coverage, either in qualitity or quality.
2025-04-16 08:40:49 +02:00
Andreas Karlsson
99ef6b20f9 PG-1440 Only let superusers modify the global key proviers
Since as soon as we have installed pg_tde the database owner can call
any function created by the extension so any database owner can meddle
with any global key provider. The only way to prevent the database owner
to do whatever they want add permissions checks to the C code and here
we keep that check simple by limiting modifying the global key provider
to only the super user.

Additionally we also protect the function for settting the WAL key, for
setting the default key and to be paranoid also the function for using a
global key provider to set the database key. The third is not obvious if
it is necessary or not but I chose to be paranoid and relax that
restirction later once we have demed it to be secure.
2025-04-16 08:40:49 +02:00
Artem Gavrilov
c8dd16849f PG-1458 Add default key info/verify funcions 2025-04-15 14:57:27 +02:00
Andrew Pogrebnoi
92e40cdc38
XLog signed info when creating a Principal Key
Before this commit, we XLogged an unsigned PrincipalKey info when
creating the key. Which leads to:

1. In case of crash recovery, the redo would rewrite a map_ file with
an empty sign info. And the server would later fail to start with
"Failed to verify principal key header..."

2. Replicas would create a _map file with an empty sign info. Which in
turn leads to a fail on restart.


For PG-1539
2025-04-15 13:03:43 +03:00
Andreas Karlsson
06b671fc04 PG-1460 Always generate a new WAL key on server start
Since you can take a copy of a PostgreSQL data directory and start both
the old and the new version you could get two versions where the same
encrypted counter is used for CTR which would mean we could comapre
them and potentially decrypt the data. For this reason we need to
generate a new WAL key every time we start the server.
2025-04-15 11:24:24 +02:00
Andreas Karlsson
253c8a4a72 PG-1504 Open the relation directly with the RangeVar
Instead of first looking up the oid and then using that to open the
relation we can use relation_openrv() with the RangeVar directly.
2025-04-15 11:23:20 +02:00
Andreas Karlsson
7587e44a09 PG-1504 Return null pointer from event triggers
According to to the manual we should return a null pointer, but it is
contradicted by an example on the same page but let's follow what
plpgsql does.

    An event trigger function must return a NULL pointer
    (not an SQL null value, that is, do not
    set isNull true).
2025-04-15 11:23:20 +02:00
Andreas Karlsson
36b17e8579 PG-1504 Use castNode() when casting nodes
This gives us some extra protection against typos.
2025-04-15 11:23:20 +02:00
Andreas Karlsson
090e356f4b PG-1504 Remove unused field in tdeCurrentCreateEvent 2025-04-15 11:23:20 +02:00
Andreas Karlsson
71ce4c9157 PG-1504 Avoid early return where it does not improve reasbaility 2025-04-15 11:23:20 +02:00
Andreas Karlsson
9d22ae3f81 PG-1504 Remove side effects from checkEncryptionStatus()
A function with that name should not have side effects so let's add
a function which only checks if a table should be encrypted based on
the AM.
2025-04-15 11:23:20 +02:00
Andreas Karlsson
5afe678b60 PG-1504 Do not abuse checkEncryptionClause() for just one feature
The only part of the code which makes sense to call when we know the
table is ecrypted is the check for if a principal key is configured.
2025-04-15 11:23:20 +02:00
Andreas Karlsson
19bef897e6 PG-1510 Use a unique IV per relation fork
The security of the encryption is reduced if we reuse the same
initiation vector more than necessary so we make sure to use a unique IV
per relation fork, with the exception of the initialization fork which
is used by unlogged indexes when restarting the server after a crash. It
is copied with low-level file system functions to the main fork on crash
recovery so it needs to use the same IV as the main fork.

The init fork issue is in no way more a security issue than to the
extent that ideally we should pick a new IV when truncating unlogged
tables on crash recovery but to fix this we would need to change the
SMGR API and moving the copying of the intialization fork into that. And
in the long term this might be what we want to do.
2025-04-15 11:23:02 +02:00
Andreas Karlsson
93ef4510b4 Fix typo in comment of Makefile/meson.build 2025-04-14 11:14:46 +02:00