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 #5

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
19 changes: 13 additions & 6 deletions rct_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#define RCT_AE_CONTINUE 0
#define RCT_AE_STOP 1

unsigned int dictSdsHash(const void *key) {
static unsigned int dictSdsHash(const void *key) {
return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
}

int dictSdsKeyCompare(void *privdata, const void *key1,
static int dictSdsKeyCompare(void *privdata, const void *key1,
const void *key2)
{
int l1,l2;
Expand All @@ -24,7 +24,7 @@ int dictSdsKeyCompare(void *privdata, const void *key1,
return memcmp(key1, key2, l1) == 0;
}

void dictSdsDestructor(void *privdata, void *val)
static void dictSdsDestructor(void *privdata, void *val)
{
DICT_NOTUSED(privdata);

Expand Down Expand Up @@ -64,6 +64,7 @@ create_context(struct instance *nci)

rct_ctx->cc = NULL;
rct_ctx->address = NULL;
rct_ctx->auth = NULL;

commands = dictCreate(&commandTableDictType,NULL);
if(commands == NULL)
Expand All @@ -78,6 +79,9 @@ create_context(struct instance *nci)
if (nci->addr)
rct_ctx->address = sdsnew(nci->addr);

if (nci->auth)
rct_ctx->auth = sdsnew(nci->auth);

cmd_parts = sdssplitargs(nci->command, &cmd_parts_count);
if(cmd_parts == NULL || cmd_parts_count <= 0)
{
Expand Down Expand Up @@ -1957,7 +1961,7 @@ int async_command_init(async_command *acmd, rctContext *ctx, char *addrs, int fl

acmd->ctx = ctx;

acmd->acc = redisClusterAsyncConnect(addrs, HIRCLUSTER_FLAG_ADD_SLAVE);
acmd->acc = redisClusterAsyncConnect(addrs, ctx->auth, HIRCLUSTER_FLAG_ADD_SLAVE);
if(acmd->acc == NULL){
log_error("Connect to %s failed.", addrs);
goto error;
Expand Down Expand Up @@ -2169,7 +2173,7 @@ static int do_command_one_node_async(async_command *acmd, struct cluster_node *n
char **argv;
size_t *argvlen;
sds *str;

if(acmd == NULL || node == NULL){
return RCT_ERROR;
}
Expand Down Expand Up @@ -5945,7 +5949,10 @@ int core_core(rctContext *ctx)
flags = HIRCLUSTER_FLAG_ADD_SLAVE;
}

cc = redisClusterConnect(ctx->address, flags);
if(ctx->auth)
cc = redisClusterConnectWithAuth(ctx->address, ctx->auth, flags);
else
cc = redisClusterConnect(ctx->address, flags);
//cc = redisClusterConnectAllWithTimeout(addr, timeout, flags);
if(cc == NULL || cc->err)
{
Expand Down
2 changes: 2 additions & 0 deletions rct_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct instance {
char *conf_filename; /* configuration filename */
int interval; /* stats aggregation interval */
char *addr; /* stats monitoring addr */
char *auth; /* auth */
char hostname[RCT_MAXHOSTNAMELEN]; /* hostname */
pid_t pid; /* process id */
char *pid_filename; /* pid filename */
Expand All @@ -111,6 +112,7 @@ typedef struct rctContext {
redisClusterContext *cc;
dict *commands; /* Command table */
sds address;
sds auth;
char *cmd;
uint8_t redis_role;
uint8_t simple;
Expand Down
17 changes: 13 additions & 4 deletions rct_option.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#define RCT_OPTION_BUFFER_DEFAULT 1024*1024

#define RCT_AUTH NULL

static struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
Expand All @@ -36,10 +37,11 @@ static struct option long_options[] = {
{ "thread", required_argument, NULL, 't' },
{ "buffer", required_argument, NULL, 'b' },
{ "step", required_argument, NULL, 'S' },
{ "auth", required_argument, NULL, 'A' },
{ NULL, 0, NULL, 0 }
};

static char short_options[] = "hVdo:v:c:a:i:p:C:r:st:b:S:";
static char short_options[] = "hVdo:v:c:a:i:p:C:r:st:b:S:A:";

void
rct_show_usage(void)
Expand All @@ -48,7 +50,7 @@ rct_show_usage(void)
"Usage: redis-cluster-tool [-?hVds] [-v verbosity level] [-o output file]" CRLF
" [-c conf file] [-a addr] [-i interval]" CRLF
" [-p pid file] [-C command] [-r redis role]" CRLF
" [-t thread number] [-b buffer size]" CRLF
" [-t thread number] [-b buffer size] [-A auth]" CRLF
"");
log_stderr(
"Options:" CRLF
Expand All @@ -67,6 +69,7 @@ rct_show_usage(void)
" -r, --role=S : set the role of the nodes that command to execute on (default: %s, you can input: %s, %s or %s)" CRLF
" -t, --thread=N : set how many threads to run the job(default: %d)" CRLF
" -b, --buffer=S : set buffer size to run the job (default: %lld byte, unit:G/M/K)" CRLF
" -A, --auth=S : set redis cluster auth (default: %s)" CRLF
"",
RCT_LOG_DEFAULT, RCT_LOG_MIN, RCT_LOG_MAX,
RCT_LOG_PATH != NULL ? RCT_LOG_PATH : "stderr",
Expand All @@ -80,7 +83,8 @@ rct_show_usage(void)
RCT_OPTION_REDIS_ROLE_MASTER,
RCT_OPTION_REDIS_ROLE_SLAVE,
RCT_OPTION_THREAD_COUNT_DEFAULT,
RCT_OPTION_BUFFER_DEFAULT);
RCT_OPTION_BUFFER_DEFAULT,
RCT_AUTH);

rct_show_command_usage();
}
Expand Down Expand Up @@ -195,7 +199,11 @@ rct_get_options(int argc, char **argv, struct instance *nci)
case 'C':
nci->command = optarg;
break;


case 'A':
nci->auth = optarg;
break;

case 'r':
nci->role = optarg;
if(strcmp(nci->role, RCT_OPTION_REDIS_ROLE_ALL) != 0
Expand Down Expand Up @@ -254,6 +262,7 @@ rct_get_options(int argc, char **argv, struct instance *nci)
case 'C':
case 'r':
case 'b':
case 'A':
log_stderr("redis-cluster-tool: option -%c requires a string", optopt);
break;

Expand Down
4 changes: 2 additions & 2 deletions rct_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ rct_assert(const char *cond, const char *file, int line, int panic)
}
}

int
static int
_vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
{
int n;
Expand Down Expand Up @@ -501,7 +501,7 @@ _vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
return (int)(size - 1);
}

int
static int
_scnprintf(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
Expand Down
4 changes: 2 additions & 2 deletions rct_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ void rct_assert(const char *cond, const char *file, int line, int panic);
void rct_stacktrace(int skip_count);
void rct_stacktrace_fd(int fd);

int _scnprintf(char *buf, size_t size, const char *fmt, ...);
int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
static int _scnprintf(char *buf, size_t size, const char *fmt, ...);
static int _vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
int64_t rct_usec_now(void);
int64_t rct_msec_now(void);

Expand Down