Skip to content

Commit 01f0056

Browse files
author
zhuchao-hit
committed
add/remove pwd
1 parent a9a3123 commit 01f0056

5 files changed

Lines changed: 109 additions & 63 deletions

File tree

lib/admin.lua

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818
1919
$%ENDLICENSE%$ --]]
2020

21-
--[[
22-
-- redefine next, used for iterate userdata
23-
rawnext = next
24-
function next(t,k)
25-
local m = getmetatable(t)
26-
local n = m and m.__next or rawnext
27-
return n(t,k)
28-
end
29-
]]
30-
3121
function set_error(errmsg)
3222
proxy.response = {
3323
type = proxy.MYSQLD_PACKET_ERR,
@@ -50,23 +40,12 @@ function read_query(packet)
5040
fields = {
5141
{ name = "backend_ndx",
5242
type = proxy.MYSQL_TYPE_LONG },
53-
--[[{ name = "username",
54-
type = proxy.MYSQL_TYPE_STRING },
55-
]]
5643
{ name = "address",
5744
type = proxy.MYSQL_TYPE_STRING },
5845
{ name = "state",
5946
type = proxy.MYSQL_TYPE_STRING },
6047
{ name = "type",
6148
type = proxy.MYSQL_TYPE_STRING },
62-
-- { name = "uuid",
63-
-- type = proxy.MYSQL_TYPE_STRING },
64-
-- { name = "connected_clients",
65-
-- type = proxy.MYSQL_TYPE_LONG },
66-
--[[
67-
{ name = "cur_idle_connections",
68-
type = proxy.MYSQL_TYPE_LONG },
69-
]]
7049
}
7150

7251
for i = 1, #proxy.global.backends do
@@ -83,30 +62,12 @@ function read_query(packet)
8362
}
8463
local b = proxy.global.backends[i]
8564

86-
rows[#rows + 1] = {
87-
i,
88-
b.dst.name, -- configured backend address
89-
states[b.state + 1], -- the C-id is pushed down starting at 0
90-
types[b.type + 1], -- the C-id is pushed down starting at 0
91-
--b.uuid, -- the MySQL Server's UUID if it is managed
92-
--b.connected_clients -- currently connected clients
93-
}
94-
95-
--[[
96-
for j, username in next, b.pool.users, 0 do
97-
98-
rows[#rows + 1] = {
99-
i,
100-
username,
101-
b.dst.name, -- configured backend address
102-
states[b.state + 1], -- the C-id is pushed down starting at 0
103-
types[b.type + 1], -- the C-id is pushed down starting at 0
104-
b.uuid, -- the MySQL Server's UUID if it is managed
105-
b.connected_clients, -- currently connected clients
106-
b.pool.users[username].cur_idle_connections
107-
}
108-
end
109-
]]
65+
rows[#rows + 1] = {
66+
i,
67+
b.dst.name, -- configured backend address
68+
states[b.state + 1], -- the C-id is pushed down starting at 0
69+
types[b.type + 1], -- the C-id is pushed down starting at 0
70+
}
11071
end
11172
elseif string.find(query:lower(), "^set%s+%a+%s+%d+$") then
11273
local state,id = string.match(query:lower(), "^set%s+(%a+)%s+(%d+)$")
@@ -133,10 +94,6 @@ function read_query(packet)
13394
type = proxy.MYSQL_TYPE_STRING },
13495
{ name = "type",
13596
type = proxy.MYSQL_TYPE_STRING },
136-
-- { name = "uuid",
137-
-- type = proxy.MYSQL_TYPE_STRING },
138-
-- { name = "connected_clients",
139-
-- type = proxy.MYSQL_TYPE_LONG },
14097
}
14198

14299
local states = {
@@ -157,8 +114,6 @@ function read_query(packet)
157114
b.dst.name, -- configured backend address
158115
states[b.state + 1], -- the C-id is pushed down starting at 0
159116
types[b.type + 1], -- the C-id is pushed down starting at 0
160-
-- b.uuid, -- the MySQL Server's UUID if it is managed
161-
-- b.connected_clients -- currently connected clients
162117
}
163118
elseif string.find(query:lower(), "^add%s+master%s+%d%d?%d?%.%d%d?%d?%.%d%d?%d?%.%d%d?%d?:%d%d?%d?%d?%d?$") then
164119
local newserver = string.match(query:lower(), "^add%s+master%s+(.+)$")
@@ -204,6 +159,22 @@ function read_query(packet)
204159
local client = string.match(query:lower(), "^remove%s+client%s+(.+)$")
205160
proxy.global.backends.removeclient = client
206161

162+
fields = {
163+
{ name = "status",
164+
type = proxy.MYSQL_TYPE_STRING },
165+
}
166+
elseif string.find(query:lower(), "^add%s+pwd%s+(.+):(.+)$") then
167+
local pwd = string.match(query:lower(), "^add%s+pwd%s+(.+)$")
168+
proxy.global.backends.addpwd = pwd
169+
170+
fields = {
171+
{ name = "status",
172+
type = proxy.MYSQL_TYPE_STRING },
173+
}
174+
elseif string.find(query:lower(), "^remove%s+pwd%s+(.+)$") then
175+
local pwd = string.match(query:lower(), "^remove%s+pwd%s+(.+)$")
176+
proxy.global.backends.removepwd = pwd
177+
207178
fields = {
208179
{ name = "status",
209180
type = proxy.MYSQL_TYPE_STRING },
@@ -222,14 +193,22 @@ function read_query(packet)
222193
type = proxy.MYSQL_TYPE_STRING },
223194
}
224195
rows[#rows + 1] = { "SELECT * FROM help", "shows this help" }
196+
225197
rows[#rows + 1] = { "SELECT * FROM backends", "lists the backends and their state" }
226198
rows[#rows + 1] = { "SET OFFLINE $backend_id", "offline backend server, $backend_id is backend_ndx's id" }
227199
rows[#rows + 1] = { "SET ONLINE $backend_id", "online backend server, ..." }
228200
rows[#rows + 1] = { "ADD MASTER $backend", "example: \"add master 127.0.0.1:3306\", ..." }
229201
rows[#rows + 1] = { "ADD SLAVE $backend", "example: \"add slave 127.0.0.1:3306\", ..." }
230202
rows[#rows + 1] = { "REMOVE BACKEND $backend_id", "example: \"remove backend 1\", ..." }
203+
204+
rows[#rows + 1] = { "SELECT * FROM clients", "lists the clients" }
231205
rows[#rows + 1] = { "ADD CLIENT $client", "example: \"add client 192.168.1.2\", ..." }
232206
rows[#rows + 1] = { "REMOVE CLIENT $client", "example: \"remove client 192.168.1.2\", ..." }
207+
208+
rows[#rows + 1] = { "SELECT * FROM pwds", "lists the pwds" }
209+
rows[#rows + 1] = { "ADD PWD $pwd", "example: \"add pwd user:password\", ..." }
210+
rows[#rows + 1] = { "REMOVE PWD $pwd", "example: \"remove pwd user\", ..." }
211+
233212
rows[#rows + 1] = { "SAVE CONFIG", "save the backends to config file" }
234213
else
235214
set_error("use 'SELECT * FROM help' to see the supported commands")

plugins/proxy/proxy-plugin.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ struct chassis_plugin_config {
223223
GHashTable *dt_table;
224224

225225
gchar **pwds;
226-
GHashTable *pwd_table;
226+
GHashTable *pwd_table[2];
227+
gint pwd_table_index;
227228

228229
network_mysqld_con *listen_con;
229230

@@ -954,7 +955,7 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_auth) {
954955

955956
con->state = CON_STATE_SEND_AUTH_RESULT;
956957

957-
GString *hashed_password = g_hash_table_lookup(config->pwd_table, auth->username->str);
958+
GString *hashed_password = g_hash_table_lookup(config->pwd_table[config->pwd_table_index], auth->username->str);
958959
if (hashed_password) {
959960
GString *expected_response = g_string_sized_new(20);
960961
network_mysqld_proto_password_scramble(expected_response, S(con->challenge), S(hashed_password));
@@ -1106,7 +1107,7 @@ void modify_user(network_mysqld_con* con) {
11061107
g_string_append_c(com_change_user, COM_CHANGE_USER);
11071108
g_string_append_len(com_change_user, client_user->str, client_user->len + 1);
11081109

1109-
GString* hashed_password = g_hash_table_lookup(config->pwd_table, client_user->str);
1110+
GString* hashed_password = g_hash_table_lookup(config->pwd_table[config->pwd_table_index], client_user->str);
11101111
if (!hashed_password) return;
11111112

11121113
GString* expected_response = g_string_sized_new(20);
@@ -1424,16 +1425,16 @@ NETWORK_MYSQLD_PLUGIN_PROTO(proxy_read_query) {
14241425
} else {
14251426
backend_ndx = rw_split(tokens, con);
14261427
}
1427-
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table);
1428+
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table[config->pwd_table_index]);
14281429
} else if (type == COM_INIT_DB || type == COM_SET_OPTION) {
14291430
backend_ndx = wrr_ro(con);
1430-
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table);
1431+
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table[config->pwd_table_index]);
14311432
}
14321433
}
14331434

14341435
if (send_sock == NULL) {
14351436
backend_ndx = idle_rw(con);
1436-
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table);
1437+
send_sock = network_connection_pool_lua_swap(con, backend_ndx, config->pwd_table[config->pwd_table_index]);
14371438
}
14381439
con->server = send_sock;
14391440
}
@@ -2214,6 +2215,10 @@ int network_mysqld_proxy_connection_init(network_mysqld_con *con) {
22142215
void network_mysqld_proxy_free(network_mysqld_con G_GNUC_UNUSED *con) {
22152216
}
22162217

2218+
void *string_free(GString *s) {
2219+
g_string_free(s, TRUE);
2220+
}
2221+
22172222
chassis_plugin_config * network_mysqld_proxy_plugin_new(void) {
22182223
config = g_new0(chassis_plugin_config, 1);
22192224

@@ -2227,7 +2232,9 @@ chassis_plugin_config * network_mysqld_proxy_plugin_new(void) {
22272232
config->ip_table_index = 0;
22282233
config->lvs_table = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, NULL);
22292234
config->dt_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
2230-
config->pwd_table = g_hash_table_new(g_str_hash, g_str_equal);
2235+
config->pwd_table[0] = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, string_free);
2236+
config->pwd_table[1] = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, string_free);
2237+
config->pwd_table_index = 0;
22312238
config->sql_log = NULL;
22322239
config->sql_log_type = NULL;
22332240
config->charset = NULL;
@@ -2293,8 +2300,10 @@ void network_mysqld_proxy_plugin_free(chassis_plugin_config *oldconfig) {
22932300
g_hash_table_remove_all(config->dt_table);
22942301
g_hash_table_destroy(config->dt_table);
22952302

2296-
g_hash_table_remove_all(config->pwd_table);
2297-
g_hash_table_destroy(config->pwd_table);
2303+
g_hash_table_remove_all(config->pwd_table[0]);
2304+
g_hash_table_destroy(config->pwd_table[0]);
2305+
g_hash_table_remove_all(config->pwd_table[1]);
2306+
g_hash_table_destroy(config->pwd_table[1]);
22982307

22992308
if (config->sql_log) fclose(config->sql_log);
23002309
if (config->sql_log_type) g_free(config->sql_log_type);
@@ -2586,8 +2595,7 @@ int network_mysqld_proxy_plugin_apply_config(chassis *chas, chassis_plugin_confi
25862595
if (raw_pwd) {
25872596
GString* hashed_password = g_string_new(NULL);
25882597
network_mysqld_proto_password_hash(hashed_password, raw_pwd, strlen(raw_pwd));
2589-
2590-
g_hash_table_insert(config->pwd_table, user, hashed_password);
2598+
g_hash_table_insert(config->pwd_table[config->pwd_table_index], g_strdup(user), hashed_password);
25912599
} else {
25922600
g_critical("password decrypt failed");
25932601
return -1;
@@ -2597,6 +2605,8 @@ int network_mysqld_proxy_plugin_apply_config(chassis *chas, chassis_plugin_confi
25972605
return -1;
25982606
}
25992607
}
2608+
chas->backends->pwd_table = config->pwd_table;
2609+
chas->backends->pwd_table_index = &(config->pwd_table_index);
26002610

26012611
/* load the script and setup the global tables */
26022612
network_mysqld_lua_setup_global(chas->sc->L, chas);

src/network-backend-lua.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,22 @@ static int proxy_backends_set(lua_State *L) {
167167
gchar *address = g_strdup(lua_tostring(L, -1));
168168
network_backends_removeclient(bs, address);
169169
g_free(address);
170+
} else if (strleq(key, keysize, C("addpwd"))) {
171+
gchar *address = g_strdup(lua_tostring(L, -1));
172+
network_backends_addpwd(bs, address);
173+
g_free(address);
174+
} else if (strleq(key, keysize, C("removepwd"))) {
175+
gchar *address = g_strdup(lua_tostring(L, -1));
176+
network_backends_removepwd(bs, address);
177+
g_free(address);
170178
} else if (strleq(key, keysize, C("saveconfig"))) {
171179
network_backends_save(bs);
172180
} else {
173181
return luaL_error(L, "proxy.global.backends.%s is not writable", key);
174182
}
175183
return 1;
176184
}
185+
177186
static int proxy_backends_len(lua_State *L) {
178187
network_backends_t *bs = *(network_backends_t **)luaL_checkself(L);
179188

@@ -192,4 +201,3 @@ int network_backends_lua_getmetatable(lua_State *L) {
192201

193202
return proxy_getmetatable(L, methods);
194203
}
195-

src/network-backend.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ void copy_key(guint *key, guint *value, GHashTable *table) {
133133
g_hash_table_add(table, new_key);
134134
}
135135

136+
void copy_pwd(gchar *key, GString *value, GHashTable *table) {
137+
g_hash_table_insert(table, g_strdup(key), g_string_new_len(S(value)));
138+
}
139+
136140
int network_backends_addclient(network_backends_t *bs, gchar *address) {
137141
guint* sum = g_new0(guint, 1);
138142
char* token;
@@ -152,6 +156,35 @@ int network_backends_addclient(network_backends_t *bs, gchar *address) {
152156
return 0;
153157
}
154158

159+
int network_backends_addpwd(network_backends_t *bs, gchar *address) {
160+
char *user = NULL, *pwd = NULL;
161+
gboolean is_complete = FALSE;
162+
163+
if ((user = strsep(&address, ":")) != NULL) {
164+
if ((pwd = strsep(&address, ":")) != NULL) {
165+
is_complete = TRUE;
166+
}
167+
}
168+
169+
if (is_complete == FALSE) {
170+
g_warning("incorrect password settings");
171+
return -1;
172+
}
173+
174+
GString* hashed_password = g_string_new(NULL);
175+
network_mysqld_proto_password_hash(hashed_password, pwd, strlen(pwd));
176+
177+
gint index = *(bs->pwd_table_index);
178+
GHashTable *old_table = bs->pwd_table[index];
179+
GHashTable *new_table = bs->pwd_table[1-index];
180+
g_hash_table_remove_all(new_table);
181+
g_hash_table_foreach(old_table, copy_pwd, new_table);
182+
g_hash_table_insert(new_table, g_strdup(user), hashed_password);
183+
g_atomic_int_set(bs->pwd_table_index, 1-index);
184+
185+
return 0;
186+
}
187+
155188
int network_backends_removeclient(network_backends_t *bs, gchar *address) {
156189
guint sum;
157190
char* token;
@@ -171,6 +204,18 @@ int network_backends_removeclient(network_backends_t *bs, gchar *address) {
171204
return 0;
172205
}
173206

207+
int network_backends_removepwd(network_backends_t *bs, gchar *address) {
208+
gint index = *(bs->pwd_table_index);
209+
GHashTable *old_table = bs->pwd_table[index];
210+
GHashTable *new_table = bs->pwd_table[1-index];
211+
g_hash_table_remove_all(new_table);
212+
g_hash_table_foreach(old_table, copy_pwd, new_table);
213+
g_hash_table_remove(new_table, address);
214+
g_atomic_int_set(bs->pwd_table_index, 1-index);
215+
216+
return 0;
217+
}
218+
174219
void append_key(guint *key, guint *value, GString *str) {
175220
g_string_append_c(str, ',');
176221
guint sum = *key;

src/network-backend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ typedef struct {
7777
gchar *default_file;
7878
GHashTable **ip_table;
7979
gint *ip_table_index;
80+
GHashTable **pwd_table;
81+
gint *pwd_table_index;
8082
} network_backends_t;
8183

8284
NETWORK_API network_backends_t *network_backends_new(guint event_thread_count, gchar *default_file);
@@ -85,6 +87,8 @@ NETWORK_API int network_backends_add(network_backends_t *backends, gchar *addres
8587
NETWORK_API int network_backends_remove(network_backends_t *backends, guint index);
8688
NETWORK_API int network_backends_addclient(network_backends_t *backends, gchar *address);
8789
NETWORK_API int network_backends_removeclient(network_backends_t *backends, gchar *address);
90+
NETWORK_API int network_backends_addpwd(network_backends_t *backends, gchar *address);
91+
NETWORK_API int network_backends_removepwd(network_backends_t *backends, gchar *address);
8892
NETWORK_API int network_backends_check(network_backends_t *backends);
8993
NETWORK_API network_backend_t * network_backends_get(network_backends_t *backends, guint ndx);
9094
NETWORK_API guint network_backends_count(network_backends_t *backends);

0 commit comments

Comments
 (0)