Skip to content
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

Refactoring #309

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 18 additions & 40 deletions src/access/pg_tde_tdemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,7 @@ pg_tde_delete_tde_files(Oid dbOid, Oid spcOid)
char db_keydata_path[MAXPGPATH] = {0};

/* Set the file paths */
pg_tde_set_db_file_paths(&(RelFileLocator) {
spcOid,
dbOid,
0},
db_map_path, db_keydata_path);
pg_tde_set_db_file_paths(dbOid, spcOid, db_map_path, db_keydata_path);

/* Remove these files without emitting any error */
PathNameDeleteTemporaryFile(db_map_path, false);
Expand Down Expand Up @@ -277,10 +273,8 @@ pg_tde_save_principal_key(TDEPrincipalKeyInfo *principal_key_info)
char db_keydata_path[MAXPGPATH] = {0};

/* Set the file paths */
pg_tde_set_db_file_paths(&(RelFileLocator) {
principal_key_info->tablespaceId,
principal_key_info->databaseId,
0},
pg_tde_set_db_file_paths(principal_key_info->databaseId,
principal_key_info->tablespaceId,
db_map_path, db_keydata_path);

ereport(LOG, (errmsg("pg_tde_save_principal_key")));
Expand Down Expand Up @@ -413,7 +407,7 @@ pg_tde_write_one_map_entry(int fd, const RelFileLocator *rlocator, int flags, in
if (bytes_written != MAP_ENTRY_SIZE)
{
char db_map_path[MAXPGPATH] = {0};
pg_tde_set_db_file_paths(rlocator, db_map_path, NULL);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL);
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not write tde map file \"%s\": %m",
Expand All @@ -422,7 +416,7 @@ pg_tde_write_one_map_entry(int fd, const RelFileLocator *rlocator, int flags, in
if (pg_fsync(fd) != 0)
{
char db_map_path[MAXPGPATH] = {0};
pg_tde_set_db_file_paths(rlocator, db_map_path, NULL);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, NULL);
ereport(data_sync_elevel(ERROR),
(errcode_for_file_access(),
errmsg("could not fsync file \"%s\": %m", db_map_path)));
Expand Down Expand Up @@ -503,7 +497,7 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, RelKeyData *enc_rel_k
Assert(rlocator);

/* Set the file paths */
pg_tde_set_db_file_paths(rlocator, db_map_path, db_keydata_path);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, db_keydata_path);

/* Create the map entry and then add the encrypted key to the data file */
key_index = pg_tde_write_map_entry(rlocator, db_map_path, principal_key_info);
Expand All @@ -528,7 +522,7 @@ pg_tde_delete_key_map_entry(const RelFileLocator *rlocator)
Assert(rlocator);

/* Get the file paths */
pg_tde_set_db_file_paths(rlocator, db_map_path, db_keydata_path);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, db_keydata_path);

errno = 0;
/* Remove the map entry if found */
Expand Down Expand Up @@ -573,7 +567,7 @@ pg_tde_free_key_map_entry(const RelFileLocator *rlocator, off_t offset)
Assert(rlocator);

/* Get the file paths */
pg_tde_set_db_file_paths(rlocator, db_map_path, db_keydata_path);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, db_keydata_path);

/* Remove the map entry if found */
LWLockAcquire(lock_files, LW_EXCLUSIVE);
Expand Down Expand Up @@ -656,10 +650,8 @@ pg_tde_perform_rotate_key(TDEPrincipalKey *principal_key, TDEPrincipalKey *new_p
char db_keydata_path[MAXPGPATH] = {0};

/* Set the file paths */
pg_tde_set_db_file_paths(&(RelFileLocator) {
principal_key->keyInfo.tablespaceId,
principal_key->keyInfo.databaseId,
0},
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId,
principal_key->keyInfo.tablespaceId,
db_map_path, db_keydata_path);

/* Let's update the pathnames in the local variable for ease of use/readability */
Expand Down Expand Up @@ -774,10 +766,8 @@ pg_tde_write_map_keydata_files(off_t map_size, char *m_file_data, off_t keydata_
fheader = (TDEFileHeader *) m_file_data;

/* Set the file paths */
pg_tde_set_db_file_paths(&(RelFileLocator) {
fheader->principal_key_info.tablespaceId,
fheader->principal_key_info.databaseId,
0},
pg_tde_set_db_file_paths(fheader->principal_key_info.databaseId,
fheader->principal_key_info.tablespaceId,
db_map_path, db_keydata_path);

/* Initialize the new files and set the names */
Expand Down Expand Up @@ -875,7 +865,7 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator)
}

/* Get the file paths */
pg_tde_set_db_file_paths(rlocator, db_map_path, db_keydata_path);
pg_tde_set_db_file_paths(rlocator->dbOid, rlocator->spcOid, db_map_path, db_keydata_path);

/* Read the map entry and get the index of the relation key */
key_index = pg_tde_process_map_entry(rlocator, db_map_path, &offset, false);
Expand All @@ -895,9 +885,9 @@ pg_tde_get_key_from_file(const RelFileLocator *rlocator)
}

inline void
pg_tde_set_db_file_paths(const RelFileLocator *rlocator, char *map_path, char *keydata_path)
pg_tde_set_db_file_paths(Oid dbOid, Oid spcOid, char *map_path, char *keydata_path)
{
char *db_path = pg_tde_get_tde_file_dir(rlocator->dbOid, rlocator->spcOid);
char *db_path = pg_tde_get_tde_file_dir(dbOid, spcOid);

if (map_path)
join_path_components(map_path, db_path, PG_TDE_MAP_FILENAME);
Expand Down Expand Up @@ -1199,11 +1189,7 @@ pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *princi
if ((read_pos + INTERNAL_KEY_LEN) > lseek(keydata_fd, 0, SEEK_END))
{
char db_keydata_path[MAXPGPATH] = {0};
pg_tde_set_db_file_paths(&(RelFileLocator) {
principal_key->keyInfo.tablespaceId,
principal_key->keyInfo.databaseId,
0},
NULL, db_keydata_path);
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path);
ereport(FATAL,
(errcode(ERRCODE_NO_DATA_FOUND),
errmsg("could not find the required key at index %d in tde data file \"%s\": %m",
Expand All @@ -1216,11 +1202,7 @@ pg_tde_read_one_keydata(int keydata_fd, int32 key_index, TDEPrincipalKey *princi
if (pg_pread(keydata_fd, &(enc_rel_key_data->internal_key), INTERNAL_KEY_LEN, read_pos) != INTERNAL_KEY_LEN)
{
char db_keydata_path[MAXPGPATH] = {0};
pg_tde_set_db_file_paths(&(RelFileLocator) {
principal_key->keyInfo.tablespaceId,
principal_key->keyInfo.databaseId,
0},
NULL, db_keydata_path);
pg_tde_set_db_file_paths(principal_key->keyInfo.databaseId, principal_key->keyInfo.tablespaceId, NULL, db_keydata_path);
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not read key at index %d in tde key data file \"%s\": %m",
Expand All @@ -1247,11 +1229,7 @@ pg_tde_get_principal_key_info(Oid dbOid, Oid spcOid)
char db_map_path[MAXPGPATH] = {0};

/* Set the file paths */
pg_tde_set_db_file_paths(&(RelFileLocator) {
spcOid,
dbOid,
0},
db_map_path, NULL);
pg_tde_set_db_file_paths(dbOid, spcOid, db_map_path, NULL);

/*
* Ensuring that we always open the file in binary mode. The caller must
Expand Down
3 changes: 1 addition & 2 deletions src/catalog/tde_global_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ TDEInitGlobalKeys(const char *dir)
#ifndef FRONTEND
char db_map_path[MAXPGPATH] = {0};

pg_tde_set_db_file_paths(&GLOBAL_SPACE_RLOCATOR(XLOG_TDE_OID),
db_map_path, NULL);
pg_tde_set_db_file_paths(GLOBAL_DATA_TDE_OID, GLOBALTABLESPACE_OID, db_map_path, NULL);
if (access(db_map_path, F_OK) == -1)
{
init_default_keyring();
Expand Down
2 changes: 1 addition & 1 deletion src/include/access/pg_tde_tdemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extern RelKeyData *tde_encrypt_rel_key(TDEPrincipalKey *principal_key, RelKeyDat
extern RelKeyData *tde_decrypt_rel_key(TDEPrincipalKey *principal_key, RelKeyData *enc_rel_key_data, const RelFileLocator *rlocator);
extern RelKeyData *pg_tde_get_key_from_file(const RelFileLocator *rlocator);

extern void pg_tde_set_db_file_paths(const RelFileLocator *rlocator, char *map_path, char *keydata_path);
extern void pg_tde_set_db_file_paths(Oid dbOid, Oid spcOid, char *map_path, char *keydata_path);

const char * tde_sprint_key(InternalKey *k);

Expand Down
Loading