From e6fe5045cb1ceb1e65f2d3eab98dd1f15e626ffd Mon Sep 17 00:00:00 2001 From: zeekling Date: Tue, 10 May 2022 23:05:43 +0800 Subject: [PATCH] add authmodule --- src/Makefile | 7 +++--- src/redis-acl.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++--- src/redis-acl.h | 20 +++++++++++++++++ 3 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/redis-acl.h diff --git a/src/Makefile b/src/Makefile index 4331a8c..a3fcb7a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,12 +1,13 @@ uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') +WARN=-Wall -W -Wno-missing-field-initializers # Compile flags for linux / osx ifeq ($(uname_S),Linux) - SHOBJ_CFLAGS ?= -W -Wall -fno-common -g -ggdb -std=c99 -O2 + SHOBJ_CFLAGS ?= -W -Wall -fno-common -g -ggdb -std=c11 -O2 $(WARN) $(OPTIMIZATION) SHOBJ_LDFLAGS ?= -shared else - SHOBJ_CFLAGS ?= -W -Wall -dynamic -fno-common -g -ggdb -std=c99 -O2 + SHOBJ_CFLAGS ?= -W -Wall -dynamic -fno-common -g -ggdb -std=c11 -O2 $(WARN) $(OPTIMIZATION) SHOBJ_LDFLAGS ?= -bundle -undefined dynamic_lookup endif @@ -25,4 +26,4 @@ redis-acl.so: redis-acl.xo $(LD) -o $@ $^ $(SHOBJ_LDFLAGS) $(LIBS) -lc clean: - rm -rf *.xo *.so + rm -rf *.xo *.so *.o diff --git a/src/redis-acl.c b/src/redis-acl.c index 006b68d..a240117 100644 --- a/src/redis-acl.c +++ b/src/redis-acl.c @@ -1,7 +1,61 @@ + #include +#include "redis-acl.h" #include "redismodule.h" -int main(){ - printf("Hello world\n"); - return 0; +static RedisModuleCommandFilter *filter; + +void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter) { + int log = 0; + int pos = 0; + RedisModule_Log(NULL, LOG_LEVL_NOTICE, "command filter"); + while (pos < RedisModule_CommandFilterArgsCount(filter)) { + const RedisModuleString *arg = RedisModule_CommandFilterArgGet(filter, pos); + size_t arg_len; + const char *arg_str = RedisModule_StringPtrLen(arg, &arg_len); + RedisModule_Log(NULL, LOG_LEVL_NOTICE, "str=%s,len=%d", arg_str, arg_len); + // 解密 + pos++; + } + RedisModuleUser *user = RedisModule_CreateModuleUser("default"); + if (user == NULL) { + RedisModule_Log(NULL, LOG_LEVL_NOTICE, "user is null"); + + } + } + +int AuthCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + + return REDISMODULE_OK; +} + +void create_users(RedisModuleCtx *ctx) { + RedisModuleUser *user = RedisModule_CreateModuleUser("default"); + RedisModule_SetModuleUserACL(user, "allcommands"); + RedisModule_SetModuleUserACL(user, "allkeys"); + RedisModule_SetModuleUserACL(user, "on"); + RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init module user success!"); + RedisModule_FreeModuleUser(user); +} + +int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { + REDISMODULE_NOT_USED(argv); + REDISMODULE_NOT_USED(argc); + if (RedisModule_Init(ctx, "redis-auth", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) { + RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init redis-auth failed"); + return REDISMODULE_ERR; + } + + create_users(ctx); + + + filter = RedisModule_RegisterCommandFilter(ctx, AuthFilter_CommandFilter, 0); + if (filter == NULL) { + return REDISMODULE_ERR; + } + + RedisModule_Log(ctx, LOG_LEVL_NOTICE, "init redis-auth success!"); + return REDISMODULE_OK; +} + diff --git a/src/redis-acl.h b/src/redis-acl.h new file mode 100644 index 0000000..641518a --- /dev/null +++ b/src/redis-acl.h @@ -0,0 +1,20 @@ + +#ifndef REDISAUTH_H +#include "redismodule.h" + +/* Error status return values. */ +#define REDISMODULE_OK 0 +#define REDISMODULE_ERR 1 +#define LOG_LEVL_NOTICE "notice" + +/* * + * Redis Auth command + * */ +int AuthCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc); + +void AuthFilter_CommandFilter(RedisModuleCommandFilter *filter); + +#endif // REDISAUTH_H + + +