Skip to content

Commit 398789b

Browse files
author
Thuan Duong
committed
Fix Bug: redis_auth don't work with redis_db
twitter#458
1 parent 34eb60f commit 398789b

6 files changed

Lines changed: 43 additions & 39 deletions

File tree

src/nc_message.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ _msg_get(void)
226226
msg->token = NULL;
227227

228228
msg->parser = NULL;
229-
msg->add_auth = NULL;
230229
msg->result = MSG_PARSE_OK;
231230

232231
msg->fragment = NULL;
@@ -293,7 +292,6 @@ msg_get(struct conn *conn, bool request, bool redis)
293292
} else {
294293
msg->parser = redis_parse_rsp;
295294
}
296-
msg->add_auth = redis_add_auth;
297295
msg->fragment = redis_fragment;
298296
msg->reply = redis_reply;
299297
msg->failure = redis_failure;
@@ -305,7 +303,6 @@ msg_get(struct conn *conn, bool request, bool redis)
305303
} else {
306304
msg->parser = memcache_parse_rsp;
307305
}
308-
msg->add_auth = memcache_add_auth;
309306
msg->fragment = memcache_fragment;
310307
msg->failure = memcache_failure;
311308
msg->pre_coalesce = memcache_pre_coalesce;

src/nc_message.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <nc_core.h>
2222

2323
typedef void (*msg_parse_t)(struct msg *);
24-
typedef rstatus_t (*msg_add_auth_t)(struct context *ctx, struct conn *c_conn, struct conn *s_conn);
2524
typedef rstatus_t (*msg_fragment_t)(struct msg *, uint32_t, struct msg_tqh *);
2625
typedef void (*msg_coalesce_t)(struct msg *r);
2726
typedef rstatus_t (*msg_reply_t)(struct msg *r);
@@ -223,7 +222,6 @@ struct msg {
223222

224223
msg_fragment_t fragment; /* message fragment */
225224
msg_reply_t reply; /* generate message reply (example: ping) */
226-
msg_add_auth_t add_auth; /* add auth message when we forward msg */
227225
msg_failure_t failure; /* transient failure response? */
228226

229227
msg_coalesce_t pre_coalesce; /* message pre-coalesce */

src/nc_request.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,6 @@ req_forward(struct context *ctx, struct conn *c_conn, struct msg *msg)
593593
}
594594
}
595595

596-
if (!conn_authenticated(s_conn)) {
597-
status = msg->add_auth(ctx, c_conn, s_conn);
598-
if (status != NC_OK) {
599-
req_forward_error(ctx, c_conn, msg);
600-
s_conn->err = errno;
601-
return;
602-
}
603-
}
604-
605596
s_conn->enqueue_inq(ctx, s_conn, msg);
606597

607598
req_forward_stats(ctx, s_conn->owner, msg);

src/proto/nc_memcache.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,13 +1545,6 @@ memcache_swallow_msg(struct conn *conn, struct msg *pmsg, struct msg *msg)
15451545
{
15461546
}
15471547

1548-
rstatus_t
1549-
memcache_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn)
1550-
{
1551-
NOT_REACHED();
1552-
return NC_OK;
1553-
}
1554-
15551548
rstatus_t
15561549
memcache_reply(struct msg *r)
15571550
{

src/proto/nc_proto.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ void memcache_parse_rsp(struct msg *r);
143143
bool memcache_failure(struct msg *r);
144144
void memcache_pre_coalesce(struct msg *r);
145145
void memcache_post_coalesce(struct msg *r);
146-
rstatus_t memcache_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn);
147146
rstatus_t memcache_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq);
148147
rstatus_t memcache_reply(struct msg *r);
149148
void memcache_post_connect(struct context *ctx, struct conn *conn, struct server *server);
@@ -154,7 +153,6 @@ void redis_parse_rsp(struct msg *r);
154153
bool redis_failure(struct msg *r);
155154
void redis_pre_coalesce(struct msg *r);
156155
void redis_post_coalesce(struct msg *r);
157-
rstatus_t redis_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn);
158156
rstatus_t redis_fragment(struct msg *r, uint32_t ncontinuum, struct msg_tqh *frag_msgq);
159157
rstatus_t redis_reply(struct msg *r);
160158
void redis_post_connect(struct context *ctx, struct conn *conn, struct server *server);

src/proto/nc_redis.c

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,20 +2839,23 @@ redis_handle_auth_req(struct msg *req, struct msg *rsp)
28392839
}
28402840

28412841
rstatus_t
2842-
redis_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn)
2842+
redis_add_auth(struct context *ctx, struct conn *conn, struct server *server, int *sended)
28432843
{
28442844
rstatus_t status;
28452845
struct msg *msg;
28462846
struct server_pool *pool;
28472847

2848-
ASSERT(!s_conn->client && !s_conn->proxy);
2849-
ASSERT(!conn_authenticated(s_conn));
2848+
ASSERT(!conn->client && !conn->proxy && conn->connected && conn->redis);
2849+
ASSERT(sended);
28502850

2851-
pool = c_conn->owner;
2851+
pool = server->owner;
2852+
if (!pool->require_auth) {
2853+
return NC_OK;
2854+
}
28522855

2853-
msg = msg_get(c_conn, true, c_conn->redis);
2856+
msg = msg_get(conn, true, conn->redis);
28542857
if (msg == NULL) {
2855-
c_conn->err = errno;
2858+
conn->err = errno;
28562859
return NC_ENOMEM;
28572860
}
28582861

@@ -2863,23 +2866,33 @@ redis_add_auth(struct context *ctx, struct conn *c_conn, struct conn *s_conn)
28632866
return status;
28642867
}
28652868

2869+
msg->type = MSG_REQ_REDIS_AUTH;
2870+
msg->result = MSG_PARSE_OK;
28662871
msg->swallow = 1;
2867-
s_conn->enqueue_inq(ctx, s_conn, msg);
2868-
s_conn->authenticated = 1;
2872+
msg->owner = NULL;
2873+
2874+
conn->authenticated = 1;
2875+
2876+
/* enqueue as head and send */
2877+
req_server_enqueue_imsgq_head(ctx, conn, msg);
2878+
*sended = 1;
2879+
2880+
log_debug(LOG_NOTICE, "sent 'AUTH %s' to %s | %s", pool->redis_auth.data,
2881+
pool->name.data, server->name.data);
28692882

28702883
return NC_OK;
28712884
}
28722885

2873-
void
2874-
redis_post_connect(struct context *ctx, struct conn *conn, struct server *server)
2886+
rstatus_t
2887+
redis_select_db(struct context *ctx, struct conn *conn, struct server *server, int *sended)
28752888
{
28762889
rstatus_t status;
28772890
struct server_pool *pool = server->owner;
28782891
struct msg *msg;
28792892
int digits;
28802893

2881-
ASSERT(!conn->client && conn->connected);
2882-
ASSERT(conn->redis);
2894+
ASSERT(!conn->client && conn->connected && conn->redis);
2895+
ASSERT(sended);
28832896

28842897
/*
28852898
* By default, every connection to redis uses the database DB 0. You
@@ -2888,7 +2901,7 @@ redis_post_connect(struct context *ctx, struct conn *conn, struct server *server
28882901
* on a per pool basis in the configuration
28892902
*/
28902903
if (pool->redis_db <= 0) {
2891-
return;
2904+
return NC_OK;
28922905
}
28932906

28942907
/*
@@ -2898,14 +2911,15 @@ redis_post_connect(struct context *ctx, struct conn *conn, struct server *server
28982911
*/
28992912
msg = msg_get(conn, true, conn->redis);
29002913
if (msg == NULL) {
2901-
return;
2914+
conn->err = errno;
2915+
return NC_ENOMEM;
29022916
}
29032917

29042918
digits = (pool->redis_db >= 10) ? (int)log10(pool->redis_db) + 1 : 1;
29052919
status = msg_prepend_format(msg, "*2\r\n$6\r\nSELECT\r\n$%d\r\n%d\r\n", digits, pool->redis_db);
29062920
if (status != NC_OK) {
29072921
msg_put(msg);
2908-
return;
2922+
return status;
29092923
}
29102924
msg->type = MSG_REQ_REDIS_SELECT;
29112925
msg->result = MSG_PARSE_OK;
@@ -2914,10 +2928,23 @@ redis_post_connect(struct context *ctx, struct conn *conn, struct server *server
29142928

29152929
/* enqueue as head and send */
29162930
req_server_enqueue_imsgq_head(ctx, conn, msg);
2917-
msg_send(ctx, conn);
2931+
*sended = 1;
29182932

29192933
log_debug(LOG_NOTICE, "sent 'SELECT %d' to %s | %s", pool->redis_db,
29202934
pool->name.data, server->name.data);
2935+
2936+
return NC_OK;
2937+
}
2938+
2939+
void
2940+
redis_post_connect(struct context *ctx, struct conn *conn, struct server *server)
2941+
{
2942+
int sended = 0;
2943+
redis_select_db(ctx, conn, server, &sended);
2944+
redis_add_auth(ctx, conn, server, &sended);
2945+
if (sended) {
2946+
msg_send(ctx, conn);
2947+
}
29212948
}
29222949

29232950
void

0 commit comments

Comments
 (0)