add authmodule #3

Merged
zeekling merged 1 commits from drone into master 2022-05-10 15:06:32 +00:00
3 changed files with 81 additions and 6 deletions
Showing only changes of commit e6fe5045cb - Show all commits

View File

@ -1,12 +1,13 @@
uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not') uname_S := $(shell sh -c 'uname -s 2>/dev/null || echo not')
WARN=-Wall -W -Wno-missing-field-initializers
# Compile flags for linux / osx # Compile flags for linux / osx
ifeq ($(uname_S),Linux) 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 SHOBJ_LDFLAGS ?= -shared
else 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 SHOBJ_LDFLAGS ?= -bundle -undefined dynamic_lookup
endif endif
@ -25,4 +26,4 @@ redis-acl.so: redis-acl.xo
$(LD) -o $@ $^ $(SHOBJ_LDFLAGS) $(LIBS) -lc $(LD) -o $@ $^ $(SHOBJ_LDFLAGS) $(LIBS) -lc
clean: clean:
rm -rf *.xo *.so rm -rf *.xo *.so *.o

View File

@ -1,7 +1,61 @@
#include <stdio.h> #include <stdio.h>
#include "redis-acl.h"
#include "redismodule.h" #include "redismodule.h"
int main(){ static RedisModuleCommandFilter *filter;
printf("Hello world\n");
return 0; 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;
}

20
src/redis-acl.h Normal file
View File

@ -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