Skip to content

Commit

Permalink
A simple way to override default AUTH
Browse files Browse the repository at this point in the history
  • Loading branch information
madolson committed Jun 22, 2021
1 parent a049f62 commit e3a1bc5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/modules/helloacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "../redismodule.h"
#include <pthread.h>
#include <unistd.h>
#include <strings.h>

// A simple global user
static RedisModuleUser *global;
Expand Down Expand Up @@ -141,7 +142,6 @@ int AuthAsyncCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,

pthread_t tid;
RedisModuleBlockedClient *bc = RedisModule_BlockClient(ctx, HelloACL_Reply, HelloACL_Timeout, HelloACL_FreeData, TIMEOUT_TIME);


void **targs = RedisModule_Alloc(sizeof(void*)*2);
targs[0] = bc;
Expand All @@ -155,6 +155,20 @@ int AuthAsyncCommand_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv,
return REDISMODULE_OK;
}

/* Example of overriding the default auth command with a new custom auth
* command .*/
void AuthAsyncCommand_AuthOverride(RedisModuleCommandFilterCtx *fctx) {
size_t cmd_len;
const RedisModuleString *arg0 = RedisModule_CommandFilterArgGet(fctx, 0);
const char *cmd_string = RedisModule_StringPtrLen(arg0, &cmd_len);

if (!strncasecmp("auth", cmd_string, cmd_len)) {
RedisModuleString *str = RedisModule_CreateString(NULL,
"HELLOACL.AUTHASYNC", 18);
RedisModule_CommandFilterArgReplace(fctx, 0, str);
}
}

/* This function must be present on each Redis module. It is used in order to
* register the commands into the Redis server. */
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
Expand All @@ -180,6 +194,8 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
AuthAsyncCommand_RedisCommand,"no-auth",0,0,0) == REDISMODULE_ERR)
return REDISMODULE_ERR;

RedisModule_RegisterCommandFilter(ctx, AuthAsyncCommand_AuthOverride, 0);

global = RedisModule_CreateModuleUser("global");
RedisModule_SetModuleUserACL(global, "allcommands");
RedisModule_SetModuleUserACL(global, "allkeys");
Expand Down

0 comments on commit e3a1bc5

Please sign in to comment.