Skip to content

Commit

Permalink
Code re-arrangement, Makefile fixes and implementing '.tde' relation …
Browse files Browse the repository at this point in the history
…fork (#5)

- Fixing the Makefile to link with ssl and crypto libs required by the extension.

- An entry file pg_tde.c for all extension-related initialization functions

- A new ".tde" relation fork to store the encryption key and related data. Currently, the relation fork gets created with new relations and a dummy encryption key gets stored in the fork.

- Re-arranging the directory structure to separate source and header files
  • Loading branch information
codeforall authored Aug 24, 2023
1 parent 19ee9a6 commit 9fa73a6
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 51 deletions.
21 changes: 18 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
# contrib/pg_tde/Makefile

PGFILEDESC = "pg_tde access method"

MODULE_big = pg_tde
EXTENSION = pg_tde
DATA = pg_tde--1.0.sql
REGRESS = pg_tde
TAP_TESTS = 0

SUBDIRS = src
OBJS = src/encryption/enc_tuple.o \
src/encryption/enc_aes.o \
src/access/pg_tde_io.o \
src/access/pg_tdeam_visibility.o \
src/access/pg_tde_tdemap.o \
src/access/pg_tdeam.o \
src/access/pg_tdetoast.o \
src/access/pg_tde_prune.o \
src/access/pg_tde_vacuumlazy.o \
src/access/pg_tde_visibilitymap.o \
src/access/pg_tde_rewrite.o \
src/access/pg_tdeam_handler.o \
src/pg_tde.o


ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
override PG_CPPFLAGS += -I$(CURDIR)/src/include
include $(PGXS)
else
subdir = contrib/postgres-tde-ext
top_builddir = ../..
override PG_CPPFLAGS += -I$(top_srcdir)/$(subdir)/src/include
include $(top_builddir)/src/Makefile.global
include $(top_srcdir)/contrib/contrib-global.mk
endif

$(recurse)
SHLIB_LINK += $(filter -lcrypto -lssl, $(LIBS))
4 changes: 3 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pg_tde_sources = files(
'src/pg_tde.c',
'src/access/pg_tde_tdemap.c',
'src/access/pg_tdeam.c',
'src/access/pg_tdeam_handler.c',
'src/access/pg_tdeam_visibility.c',
Expand All @@ -12,7 +14,7 @@ pg_tde_sources = files(
'src/encryption/enc_aes.c',
)

incdir = include_directories('src')
incdir = include_directories('src/include')

pg_tde = shared_module('pg_tde',
pg_tde_sources,
Expand Down
19 changes: 0 additions & 19 deletions src/Makefile

This file was deleted.

6 changes: 3 additions & 3 deletions src/access/pg_tde_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "pg_tde_io.h"
#include "pg_tde_visibilitymap.h"
#include "access/pg_tdeam.h"
#include "access/pg_tde_io.h"
#include "access/pg_tde_visibilitymap.h"
#include "encryption/enc_tuple.h"

#include "access/htup_details.h"
Expand Down
4 changes: 2 additions & 2 deletions src/access/pg_tde_prune.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "pg_tdeam_xlog.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdeam_xlog.h"

#include "access/htup_details.h"
#include "access/transam.h"
Expand Down
8 changes: 4 additions & 4 deletions src/access/pg_tde_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@

#include <unistd.h>

#include "pg_tdeam.h"
#include "pg_tdeam_xlog.h"
#include "pg_tdetoast.h"
#include "pg_tde_rewrite.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdeam_xlog.h"
#include "access/pg_tdetoast.h"
#include "access/pg_tde_rewrite.h"
#include "encryption/enc_tuple.h"

#include "access/transam.h"
Expand Down
58 changes: 58 additions & 0 deletions src/access/pg_tde_tdemap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*-------------------------------------------------------------------------
*
* pg_tde_tdemap.c
* tde relation fork manager code
*
*
* IDENTIFICATION
* src/access/pg_tde_tdemap.c
*
*-------------------------------------------------------------------------
*/

#include "postgres.h"
#include "access/pg_tde_tdemap.h"
#include "storage/fd.h"
#include "utils/wait_event.h"

/*
* Creates a relation fork file relfilenode.tde that contains the
* encryption key for the relation.
*/
void
pg_tde_create_key_fork(const RelFileLocator *newrlocator, Relation rel)
{
char *rel_file_path;
char *key_file_path;
File file = -1;
char enc_key[256]; /* Dummy key */

/* We get a relation name for MAIN fork and manually append the
* .tde postfix to the file name
*/
rel_file_path = relpathperm(*newrlocator, MAIN_FORKNUM);
key_file_path = psprintf("%s.tde", rel_file_path);
pfree(rel_file_path);

file = PathNameOpenFile(key_file_path, O_RDWR | O_CREAT | PG_BINARY);
if (file < 0)
{
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not open tde key file %s", key_file_path)));
}
/* TODO:
* For now just write a dummy data to the file. We will write the actual
* key later.
*/
snprintf(enc_key, sizeof(enc_key), "Percona TDE Dummy key for relation:%s", RelationGetRelationName(rel));
if (FileWrite(file, enc_key, sizeof(enc_key),
0, WAIT_EVENT_DATA_FILE_WRITE) != sizeof(enc_key))
ereport(FATAL, (errcode_for_file_access(),
errmsg("Could not write key data to file: %s",
key_file_path)));

/* For now just clode the key file.*/
pfree(key_file_path);
FileClose(file);
}
6 changes: 3 additions & 3 deletions src/access/pg_tde_vacuumlazy.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

#include <math.h>

#include "pg_tdeam.h"
#include "pg_tdeam_xlog.h"
#include "pg_tde_visibilitymap.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdeam_xlog.h"
#include "access/pg_tde_visibilitymap.h"
#include "encryption/enc_tuple.h"

#include "access/amapi.h"
Expand Down
4 changes: 2 additions & 2 deletions src/access/pg_tde_visibilitymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@

#include "postgres.h"

#include "pg_tdeam_xlog.h"
#include "pg_tde_visibilitymap.h"
#include "access/pg_tdeam_xlog.h"
#include "access/pg_tde_visibilitymap.h"

#include "access/xloginsert.h"
#include "access/xlogutils.h"
Expand Down
10 changes: 5 additions & 5 deletions src/access/pg_tdeam.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "pg_tdeam_xlog.h"
#include "pg_tdetoast.h"
#include "pg_tde_io.h"
#include "pg_tde_visibilitymap.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdeam_xlog.h"
#include "access/pg_tdetoast.h"
#include "access/pg_tde_io.h"
#include "access/pg_tde_visibilitymap.h"
#include "encryption/enc_tuple.h"

#include "access/bufmask.h"
Expand Down
16 changes: 11 additions & 5 deletions src/access/pg_tdeam_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "pg_tdetoast.h"
#include "pg_tde_rewrite.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdetoast.h"
#include "access/pg_tde_rewrite.h"
#include "access/pg_tde_tdemap.h"

#include "encryption/enc_tuple.h"

Expand All @@ -51,8 +52,6 @@
#include "utils/builtins.h"
#include "utils/rel.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(pg_tdeam_handler);


Expand Down Expand Up @@ -635,6 +634,13 @@ pg_tdeam_relation_set_new_filelocator(Relation rel,
}

smgrclose(srel);
if (rel->rd_rel->relkind == RELKIND_RELATION ||
rel->rd_rel->relkind == RELKIND_MATVIEW )
{
ereport(DEBUG2,
(errmsg("creating key file for relation %s", RelationGetRelationName(rel))));
pg_tde_create_key_fork(newrlocator, rel);
}
}

static void
Expand Down
2 changes: 1 addition & 1 deletion src/access/pg_tdeam_visibility.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "access/pg_tdeam.h"

#include "access/htup_details.h"
#include "access/multixact.h"
Expand Down
4 changes: 2 additions & 2 deletions src/access/pg_tdetoast.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#include "postgres.h"

#include "pg_tdeam.h"
#include "pg_tdetoast.h"
#include "access/pg_tdeam.h"
#include "access/pg_tdetoast.h"

#include "access/detoast.h"
#include "access/genam.h"
Expand Down
2 changes: 1 addition & 1 deletion src/encryption/enc_tuple.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "access/pg_tde_defines.h"
#include "pg_tde_defines.h"
#define ENCRYPTION_DEBUG 1

#include "postgres.h"
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions src/include/access/pg_tde_tdemap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*-------------------------------------------------------------------------
*
* pg_tde_tdemap.h
* TDE relation fork manapulation.
*
*-------------------------------------------------------------------------
*/
#ifndef PG_TDE_MAP_H
#define PG_TDE_MAP_H

#include "utils/rel.h"
#include "storage/relfilelocator.h"

extern void pg_tde_create_key_fork(const RelFileLocator *newrlocator, Relation rel);

#endif /* PG_TDE_MAP_H */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions src/pg_tde.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*-------------------------------------------------------------------------
*
* pg_tde.c
* Main file: setup GUCs, shared memory, hooks and other general-purpose
* routines.
*
* IDENTIFICATION
* contrib/pg_tde/src/pg_tde.c
*
*-------------------------------------------------------------------------
*/

#include "postgres.h"
#include "funcapi.h"

PG_MODULE_MAGIC;
void _PG_init(void);

void
_PG_init(void)
{

}

0 comments on commit 9fa73a6

Please sign in to comment.