Skip to content

Commit 2d6a806

Browse files
HugoWenTDLinuxJedi
authored andcommitted
Add parameter of key file path for AWS KMS plugin
AWS KMS plugin saves all key files under the root folder of data directory. Increasing of the key IDs and key rotations will generate a lot of key files under the root folder, looks messy and hard to maintain the folder permission etc. Now introduce a new plugin parameter `aws_key_management_keyfile_dir` to define the directory for saving the key files for better maintenance. Detailed parameter information as following: ``` VARIABLE_NAME: AWS_KEY_MANAGEMENT_KEYFILE_DIR SESSION_VALUE: NULL GLOBAL_VALUE: <Directory path> GLOBAL_VALUE_ORIGIN: COMMAND-LINE DEFAULT_VALUE: VARIABLE_SCOPE: GLOBAL VARIABLE_TYPE: VARCHAR VARIABLE_COMMENT: Define the directory in which to save key files for the AWS key management plugin. If not set, the root datadir will be used READ_ONLY: YES COMMAND_LINE_ARGUMENT: REQUIRED GLOBAL_VALUE_PATH: NULL ``` All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.
1 parent 68542c6 commit 2d6a806

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

plugin/aws_key_management/aws_key_management_plugin.cc

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static unsigned long log_level;
8282
static int rotate_key;
8383
static int request_timeout;
8484
static char* endpoint_url;
85+
static char* keyfile_dir;
8586

8687
#ifndef DBUG_OFF
8788
#define WITH_AWS_MOCK 1
@@ -187,13 +188,23 @@ class MySQLLogSystem : public Aws::Utils::Logging::FormattedLogSystem
187188
}
188189
};
189190

190-
/* Get list of files in current directory */
191-
static vector<string> traverse_current_directory()
191+
/* Get keyfile directory */
192+
static const char * get_keyfile_dir()
193+
{
194+
if (keyfile_dir && keyfile_dir[0])
195+
return keyfile_dir;
196+
return ".";
197+
}
198+
199+
/* Get list of files in keyfile directory */
200+
static vector<string> traverse_keyfile_directory()
192201
{
193202
vector<string> v;
194203
#ifdef _WIN32
195204
WIN32_FIND_DATA find_data;
196-
HANDLE h= FindFirstFile("*.*", &find_data);
205+
char path[FN_REFLEN];
206+
snprintf(path, sizeof(path), "%s\\*.*", get_keyfile_dir());
207+
HANDLE h= FindFirstFile(path, &find_data);
197208
if (h == INVALID_HANDLE_VALUE)
198209
return v;
199210
do
@@ -203,7 +214,7 @@ static vector<string> traverse_current_directory()
203214
while (FindNextFile(h, &find_data));
204215
FindClose(h);
205216
#else
206-
DIR *dir = opendir(".");
217+
DIR *dir = opendir(get_keyfile_dir());
207218
if (!dir)
208219
return v;
209220
struct dirent *e;
@@ -272,7 +283,7 @@ static int plugin_init(void *p)
272283
if (init())
273284
return -1;
274285

275-
vector<string> files= traverse_current_directory();
286+
vector<string> files= traverse_keyfile_directory();
276287
for (size_t i=0; i < files.size(); i++)
277288
{
278289

@@ -316,7 +327,7 @@ static int plugin_deinit(void *p)
316327
/* Generate filename to store the ciphered key */
317328
static void format_keyfile_name(char *buf, size_t size, uint key_id, uint version)
318329
{
319-
snprintf(buf, size, "aws-kms-key.%u.%u", key_id, version);
330+
snprintf(buf, size, "%s%saws-kms-key.%u.%u", get_keyfile_dir(), IF_WIN("\\","/"), key_id, version);
320331
}
321332

322333
/* Extract key id and version from file name */
@@ -336,7 +347,7 @@ static int extract_id_and_version(const char *name, uint *id, uint *ver)
336347
static int load_key(KEY_INFO *info)
337348
{
338349
int ret;
339-
char path[256];
350+
char path[FN_REFLEN];
340351

341352
format_keyfile_name(path, sizeof(path), info->key_id, info->key_version);
342353
ret= read_and_decrypt_key(path, info);
@@ -531,7 +542,7 @@ static int generate_and_save_datakey(uint keyid, uint version)
531542
return -1;
532543

533544
string out;
534-
char filename[20];
545+
char filename[FN_REFLEN];
535546
format_keyfile_name(filename, sizeof(filename), keyid, version);
536547
int fd= open(filename, O_WRONLY |O_CREAT|O_BINARY, IF_WIN(_S_IREAD, S_IRUSR| S_IRGRP| S_IROTH));
537548
if (fd < 0)
@@ -652,7 +663,6 @@ static unsigned int get_key(
652663
return(0);
653664
}
654665

655-
656666
/* Plugin defs */
657667
struct st_mariadb_encryption aws_key_management_plugin= {
658668
MariaDB_ENCRYPTION_INTERFACE_VERSION,
@@ -725,6 +735,12 @@ static MYSQL_SYSVAR_STR(endpoint_url, endpoint_url,
725735
"Used to override the default AWS API endpoint. If not set, the default will be used",
726736
NULL, NULL, "");
727737

738+
static MYSQL_SYSVAR_STR(keyfile_dir, keyfile_dir,
739+
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
740+
"Define the directory in which to save key files for the AWS key"
741+
"management plugin. If not set, the root datadir will be used",
742+
NULL, NULL, "");
743+
728744
#if WITH_AWS_MOCK
729745
static MYSQL_SYSVAR_BOOL(mock, mock,
730746
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
@@ -740,6 +756,7 @@ static struct st_mysql_sys_var* settings[]= {
740756
MYSQL_SYSVAR(request_timeout),
741757
MYSQL_SYSVAR(region),
742758
MYSQL_SYSVAR(endpoint_url),
759+
MYSQL_SYSVAR(keyfile_dir),
743760
#if WITH_AWS_MOCK
744761
MYSQL_SYSVAR(mock),
745762
#endif

0 commit comments

Comments
 (0)