Skip to content

Commit

Permalink
Use a custom prefix instead of a hardcoded serverid for keys
Browse files Browse the repository at this point in the history
With this change, key names no longer contain the hardcoded serverid,
but instead use a user specifiable and optional prefix.

With default settings the prefix is empty, and we generate key names
in the format of "<name>-<version>". With a non empty prefix set,
the format changes to "<prefix>-<name>-<version>".

The test suite is also modified, so that the existing TAP test uses
a custom prefix, and the SQL tests use an empty prefix, to test both
cases.
  • Loading branch information
dutow committed Nov 30, 2023
1 parent 07fe8e4 commit 4b9636b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/include/keyring/keyring_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <json.h>

extern char* keyringConfigFile;
extern char* keyringKeyPrefix;

void keyringRegisterVariables(void);

Expand Down
9 changes: 8 additions & 1 deletion src/keyring/keyring_api.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "keyring/keyring_api.h"
#include "keyring/keyring_file.h"
#include "keyring/keyring_config.h"

#include "postgres.h"
#include "access/xlog.h"
Expand Down Expand Up @@ -82,7 +83,13 @@ const keyInfo* keyringStoreKey(keyName name, keyData data)
keyName keyringConstructKeyName(const char* internalName, unsigned version)
{
keyName name;
snprintf(name.name, sizeof(name.name), "%s-%u-%lu", internalName, version, GetSystemIdentifier());
if(keyringKeyPrefix != NULL && strlen(keyringKeyPrefix) > 0)
{
snprintf(name.name, sizeof(name.name), "%s-%s-%u", keyringKeyPrefix, internalName, version);
} else
{
snprintf(name.name, sizeof(name.name), "%s-%u", internalName, version);
}
return name;
}

Expand Down
30 changes: 30 additions & 0 deletions src/keyring/keyring_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@
#include "utils/guc.h"

char* keyringConfigFile = "";
char* keyringKeyPrefix = "";

static bool keyringCheckKeyPrefix(char **newval, void **extra, GucSource source)
{
if(*newval == NULL || strlen(*newval) == 0)
{
return 1; // empty
}

if(strlen(*newval) > 32)
{
elog(ERROR, "The maximum length of pg_tde.keyringKeyPrefix is 32 characters.");
return 0;
}

return 1;
}

static bool keyringCheckConfigFile(char **newval, void **extra, GucSource source)
{
Expand Down Expand Up @@ -45,6 +62,7 @@ static void keyringAssignConfigFile(const char *newval, void *extra)

void keyringRegisterVariables(void)
{

DefineCustomStringVariable("pg_tde.keyringConfigFile", /* name */
"Location of the configuration file for the keyring", /* short_desc */
NULL, /* long_desc */
Expand All @@ -56,6 +74,18 @@ void keyringRegisterVariables(void)
&keyringAssignConfigFile, /* assign_hook */
NULL /* show_hook */
);

DefineCustomStringVariable("pg_tde.keyringKeyPrefix", /* name */
"Location of the configuration file for the keyring", /* short_desc */
NULL, /* long_desc */
&keyringKeyPrefix, /* value address */
"", /* boot value */
PGC_POSTMASTER, /* context */
0, /* flags */
&keyringCheckKeyPrefix, /* check_hook */
NULL, /* assign_hook */
NULL /* show_hook */
);
}

bool keyringLoadConfiguration(const char* configFileName)
Expand Down
1 change: 1 addition & 0 deletions t/001_basic.pl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
# UPDATE postgresql.conf to include/load pg_tde library
open $conf, '>>', "$pgdata/postgresql.conf";
print $conf "pg_tde.keyringConfigFile = '/tmp/keyring.json'\n";
print $conf "pg_tde.keyringKeyPrefix = 'this-is-a-prefix'\n";
close $conf;

$rt_value = $node->start();
Expand Down

0 comments on commit 4b9636b

Please sign in to comment.