Skip to content

Commit ffc18c1

Browse files
author
zhuchao-hit
committed
save pwds to config file
1 parent 01f0056 commit ffc18c1

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

plugins/proxy/proxy-plugin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,7 @@ int network_mysqld_proxy_plugin_apply_config(chassis *chas, chassis_plugin_confi
25962596
GString* hashed_password = g_string_new(NULL);
25972597
network_mysqld_proto_password_hash(hashed_password, raw_pwd, strlen(raw_pwd));
25982598
g_hash_table_insert(config->pwd_table[config->pwd_table_index], g_strdup(user), hashed_password);
2599+
g_hash_table_insert(chas->backends->raw_pwds, g_strdup(user), raw_pwd);
25992600
} else {
26002601
g_critical("password decrypt failed");
26012602
return -1;

src/network-backend.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ network_backends_t *network_backends_new(guint event_thread_count, gchar *defaul
7373
bs->global_wrr = g_wrr_poll_new();
7474
bs->event_thread_count = event_thread_count;
7575
bs->default_file = g_strdup(default_file);
76+
bs->raw_pwds = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
7677

7778
return bs;
7879
}
@@ -112,6 +113,9 @@ void network_backends_free(network_backends_t *bs) {
112113
g_wrr_poll_free(bs->global_wrr);
113114
g_free(bs->default_file);
114115

116+
g_hash_table_remove_all(bs->raw_pwds);
117+
g_hash_table_destroy(bs->raw_pwds);
118+
115119
g_free(bs);
116120
}
117121

@@ -182,6 +186,8 @@ int network_backends_addpwd(network_backends_t *bs, gchar *address) {
182186
g_hash_table_insert(new_table, g_strdup(user), hashed_password);
183187
g_atomic_int_set(bs->pwd_table_index, 1-index);
184188

189+
g_hash_table_insert(bs->raw_pwds, g_strdup(user), g_strdup(pwd));
190+
185191
return 0;
186192
}
187193

@@ -212,6 +218,7 @@ int network_backends_removepwd(network_backends_t *bs, gchar *address) {
212218
g_hash_table_foreach(old_table, copy_pwd, new_table);
213219
g_hash_table_remove(new_table, address);
214220
g_atomic_int_set(bs->pwd_table_index, 1-index);
221+
g_hash_table_remove(bs->raw_pwds, address);
215222

216223
return 0;
217224
}
@@ -286,6 +293,23 @@ int network_backends_save(network_backends_t *bs) {
286293

287294
g_string_free(client_ips, TRUE);
288295

296+
GHashTableIter iter;
297+
g_hash_table_iter_init(&iter, bs->raw_pwds);
298+
gchar *user = NULL, *pwd = NULL;
299+
300+
GString *pwds = g_string_new(NULL);
301+
while (g_hash_table_iter_next(&iter, &user, &pwd)) {
302+
g_string_append_printf(pwds, ",%s:%s", user, pwd);
303+
}
304+
305+
if (pwds->len != 0) {
306+
g_key_file_set_value(keyfile, "mysql-proxy", "pwds", pwds->str+1);
307+
} else {
308+
g_key_file_set_value(keyfile, "mysql-proxy", "pwds", "");
309+
}
310+
311+
g_string_free(pwds, TRUE);
312+
289313
gsize file_size = 0;
290314
gchar *file_buf = g_key_file_to_data(keyfile, &file_size, NULL);
291315
if (FALSE == g_file_set_contents(bs->default_file, file_buf, file_size, &gerr)) {

src/network-backend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ typedef struct {
7979
gint *ip_table_index;
8080
GHashTable **pwd_table;
8181
gint *pwd_table_index;
82+
GHashTable *raw_pwds;
8283
} network_backends_t;
8384

8485
NETWORK_API network_backends_t *network_backends_new(guint event_thread_count, gchar *default_file);

0 commit comments

Comments
 (0)