Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auth in redis cluster #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc, const char
int redisClusterGetReply(redisClusterContext *cc, void **reply);
void redisCLusterReset(redisClusterContext *cc);

redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags);
redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, const char *auth, int flags);
int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);
int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);
int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, char *cmd, int len);
Expand Down Expand Up @@ -185,7 +185,7 @@ should be checked after creation to see if there were errors creating the connec
Because the connection that will be created is non-blocking, the kernel is not able to
instantly return if the specified host and port is able to accept a connection.
```c
redisClusterAsyncContext *acc = redisClusterAsyncConnect("127.0.0.1:6379", HIRCLUSTER_FLAG_NULL);
redisClusterAsyncContext *acc = redisClusterAsyncConnect("127.0.0.1:6379", "auth", HIRCLUSTER_FLAG_NULL);
if (acc->err) {
printf("Error: %s\n", acc->errstr);
// handle error
Expand Down
79 changes: 72 additions & 7 deletions hircluster.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "adlist.h"
#include "hiarray.h"
#include "command.h"
#include "net.h"
#include "dict.c"

#define REDIS_COMMAND_CLUSTER_NODES "CLUSTER NODES"
Expand Down Expand Up @@ -1291,6 +1292,20 @@ cluster_update_route_by_addr(redisClusterContext *cc,
goto error;
}

#if 1
// Add auth
if(cc->auth){
reply = redisCommand(c, "auth %s", cc->auth);
if(reply == NULL){
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"Command(auth XXXXX) reply error(NULL).");
goto error;
}

freeReplyObject(reply);
}
#endif

if(cc->flags & HIRCLUSTER_FLAG_ROUTE_USE_SLOTS){
reply = redisCommand(c, REDIS_COMMAND_CLUSTER_SLOTS);
if(reply == NULL){
Expand Down Expand Up @@ -1998,6 +2013,7 @@ static redisClusterContext *redisClusterContextInit(void) {

cc->err = 0;
cc->errstr[0] = '\0';
cc->auth[0] = '\0';
cc->ip = NULL;
cc->port = 0;
cc->flags = 0;
Expand Down Expand Up @@ -2151,7 +2167,6 @@ static redisClusterContext *_redisClusterConnect(redisClusterContext *cc, const
{
return NULL;
}


address = sdssplitlen(addrs, strlen(addrs), CLUSTER_ADDRESS_SEPARATOR,
strlen(CLUSTER_ADDRESS_SEPARATOR), &address_count);
Expand Down Expand Up @@ -2194,10 +2209,34 @@ redisClusterContext *redisClusterConnect(const char *addrs, int flags)
{
cc->flags |= flags;
}

return _redisClusterConnect(cc, addrs);
}

redisClusterContext *redisClusterConnectWithAuth(const char *addrs, const char *auth, int flags)
{
redisClusterContext *cc;

cc = redisClusterContextInit();

if(cc == NULL)
{
return NULL;
}

cc->flags |= REDIS_BLOCK;
if(flags)
{
cc->flags |= flags;
}

if(auth)
{
memcpy(cc->auth, auth, strlen(auth));
}

return _redisClusterConnect(cc, addrs);
}
redisClusterContext *redisClusterConnectWithTimeout(
const char *addrs, const struct timeval tv, int flags)
{
Expand Down Expand Up @@ -2226,7 +2265,8 @@ redisClusterContext *redisClusterConnectWithTimeout(
return _redisClusterConnect(cc, addrs);
}

redisClusterContext *redisClusterConnectNonBlock(const char *addrs, int flags) {
redisClusterContext *redisClusterConnectNonBlock(const char *addrs,
const char *auth, int flags) {

redisClusterContext *cc;

Expand All @@ -2242,7 +2282,12 @@ redisClusterContext *redisClusterConnectNonBlock(const char *addrs, int flags) {
{
cc->flags |= flags;
}


if(auth)
{
memcpy(cc->auth, auth, strlen(auth));
}

return _redisClusterConnect(cc, addrs);
}

Expand Down Expand Up @@ -4044,7 +4089,7 @@ redisAsyncContext * actx_get_by_node(redisClusterAsyncContext *acc,
cluster_node *node)
{
redisAsyncContext *ac;

if(node == NULL)
{
return NULL;
Expand Down Expand Up @@ -4089,6 +4134,25 @@ redisAsyncContext * actx_get_by_node(redisClusterAsyncContext *acc,

node->acon = ac;

#if 1
//Add auth
redisReply *reply = NULL;
ac->c.flags |= REDIS_BLOCK;
redisSetBlocking(&ac->c, 1);
if (acc->cc->auth)
{
reply = redisCommand(&ac->c, "auth %s", acc->cc->auth);
if(reply == NULL){
__redisClusterSetError(acc->cc, REDIS_ERR_OTHER,
"Command(auth XXXXX) reply error(NULL).");
}

freeReplyObject(reply);
}
redisSetBlocking(&ac->c, 0);
ac->c.flags &= ~REDIS_BLOCK;

#endif
return ac;
}

Expand Down Expand Up @@ -4143,12 +4207,13 @@ static redisAsyncContext *actx_get_after_update_route_by_slot(
return ac;
}

redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags) {
redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs,
const char *auth, int flags) {

redisClusterContext *cc;
redisClusterAsyncContext *acc;

cc = redisClusterConnectNonBlock(addrs, flags);
cc = redisClusterConnectNonBlock(addrs, auth, flags);
if(cc == NULL)
{
return NULL;
Expand Down
7 changes: 4 additions & 3 deletions hircluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ extern "C" {
typedef struct redisClusterContext {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
char auth[128];
sds ip;
int port;

int flags;

enum redisConnectionType connection_type;
Expand All @@ -98,9 +98,10 @@ typedef struct redisClusterContext {
} redisClusterContext;

redisClusterContext *redisClusterConnect(const char *addrs, int flags);
redisClusterContext *redisClusterConnectWithAuth(const char *addrs, const char *auth, int flags);
redisClusterContext *redisClusterConnectWithTimeout(const char *addrs,
const struct timeval tv, int flags);
redisClusterContext *redisClusterConnectNonBlock(const char *addrs, int flags);
redisClusterContext *redisClusterConnectNonBlock(const char *addrs, const char *auth, int flags);

void redisClusterFree(redisClusterContext *cc);

Expand Down Expand Up @@ -158,7 +159,7 @@ typedef struct redisClusterAsyncContext {

} redisClusterAsyncContext;

redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags);
redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, const char *auth, int flags);
int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);
int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);
int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, char *cmd, int len);
Expand Down
2 changes: 1 addition & 1 deletion net.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ static int redisCreateSocket(redisContext *c, int type) {
return REDIS_OK;
}

static int redisSetBlocking(redisContext *c, int blocking) {
int redisSetBlocking(redisContext *c, int blocking) {
int flags;

/* Set the socket nonblocking.
Expand Down
2 changes: 1 addition & 1 deletion net.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ int redisContextConnectBindTcp(redisContext *c, const char *addr, int port,
const char *source_addr);
int redisContextConnectUnix(redisContext *c, const char *path, const struct timeval *timeout);
int redisKeepAlive(redisContext *c, int interval);

int redisSetBlocking(redisContext *c, int blocking);
#endif