2021-09-23 05:52:56 +00:00
|
|
|
|
|
|
|
#include "redismodule.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2022-01-20 21:05:27 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
2021-09-23 05:52:56 +00:00
|
|
|
|
|
|
|
/* A wrap for SET command with ACL check on the key. */
|
|
|
|
int set_aclcheck_key(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2022-01-20 21:05:27 +00:00
|
|
|
if (argc < 4) {
|
2021-09-23 05:52:56 +00:00
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
|
|
|
|
2022-01-20 21:05:27 +00:00
|
|
|
int permissions;
|
|
|
|
const char *flags = RedisModule_StringPtrLen(argv[1], NULL);
|
|
|
|
|
|
|
|
if (!strcasecmp(flags, "W")) {
|
2022-02-22 09:00:03 +00:00
|
|
|
permissions = REDISMODULE_CMD_KEY_UPDATE;
|
2022-01-20 21:05:27 +00:00
|
|
|
} else if (!strcasecmp(flags, "R")) {
|
2022-02-22 09:00:03 +00:00
|
|
|
permissions = REDISMODULE_CMD_KEY_ACCESS;
|
2022-01-20 21:05:27 +00:00
|
|
|
} else if (!strcasecmp(flags, "*")) {
|
2022-02-22 09:00:03 +00:00
|
|
|
permissions = REDISMODULE_CMD_KEY_UPDATE | REDISMODULE_CMD_KEY_ACCESS;
|
|
|
|
} else if (!strcasecmp(flags, "~")) {
|
|
|
|
permissions = 0; /* Requires either read or write */
|
2022-01-20 21:05:27 +00:00
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithError(ctx, "INVALID FLAGS");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2021-09-23 05:52:56 +00:00
|
|
|
/* Check that the key can be accessed */
|
|
|
|
RedisModuleString *user_name = RedisModule_GetCurrentUserName(ctx);
|
|
|
|
RedisModuleUser *user = RedisModule_GetModuleUserFromUserName(user_name);
|
2022-01-20 21:05:27 +00:00
|
|
|
int ret = RedisModule_ACLCheckKeyPermissions(user, argv[2], permissions);
|
2021-09-23 05:52:56 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
RedisModule_ReplyWithError(ctx, "DENIED KEY");
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
RedisModule_FreeString(ctx, user_name);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2022-01-20 21:05:27 +00:00
|
|
|
RedisModuleCallReply *rep = RedisModule_Call(ctx, "SET", "v", argv + 2, argc - 2);
|
2021-09-23 05:52:56 +00:00
|
|
|
if (!rep) {
|
|
|
|
RedisModule_ReplyWithError(ctx, "NULL reply returned");
|
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
RedisModule_FreeCallReply(rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
RedisModule_FreeString(ctx, user_name);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A wrap for PUBLISH command with ACL check on the channel. */
|
|
|
|
int publish_aclcheck_channel(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
if (argc != 3) {
|
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the pubsub channel can be accessed */
|
|
|
|
RedisModuleString *user_name = RedisModule_GetCurrentUserName(ctx);
|
|
|
|
RedisModuleUser *user = RedisModule_GetModuleUserFromUserName(user_name);
|
2022-02-22 09:00:03 +00:00
|
|
|
int ret = RedisModule_ACLCheckChannelPermissions(user, argv[1], REDISMODULE_CMD_CHANNEL_SUBSCRIBE);
|
2021-09-23 05:52:56 +00:00
|
|
|
if (ret != 0) {
|
|
|
|
RedisModule_ReplyWithError(ctx, "DENIED CHANNEL");
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
RedisModule_FreeString(ctx, user_name);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisModuleCallReply *rep = RedisModule_Call(ctx, "PUBLISH", "v", argv + 1, argc - 1);
|
|
|
|
if (!rep) {
|
|
|
|
RedisModule_ReplyWithError(ctx, "NULL reply returned");
|
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
RedisModule_FreeCallReply(rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
RedisModule_FreeString(ctx, user_name);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A wrap for RM_Call that check first that the command can be executed */
|
|
|
|
int rm_call_aclcheck_cmd(RedisModuleCtx *ctx, RedisModuleUser *user, RedisModuleString **argv, int argc) {
|
|
|
|
if (argc < 2) {
|
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the command can be executed */
|
|
|
|
int ret = RedisModule_ACLCheckCommandPermissions(user, argv + 1, argc - 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
RedisModule_ReplyWithError(ctx, "DENIED CMD");
|
|
|
|
/* Add entry to ACL log */
|
2022-04-12 05:16:17 +00:00
|
|
|
RedisModule_ACLAddLogEntry(ctx, user, argv[1], REDISMODULE_ACL_LOG_CMD);
|
2021-09-23 05:52:56 +00:00
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmd = RedisModule_StringPtrLen(argv[1], NULL);
|
|
|
|
|
|
|
|
RedisModuleCallReply* rep = RedisModule_Call(ctx, cmd, "v", argv + 2, argc - 2);
|
|
|
|
if(!rep){
|
|
|
|
RedisModule_ReplyWithError(ctx, "NULL reply returned");
|
|
|
|
}else{
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
RedisModule_FreeCallReply(rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rm_call_aclcheck_cmd_default_user(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
RedisModuleString *user_name = RedisModule_GetCurrentUserName(ctx);
|
|
|
|
RedisModuleUser *user = RedisModule_GetModuleUserFromUserName(user_name);
|
|
|
|
|
|
|
|
int res = rm_call_aclcheck_cmd(ctx, user, argv, argc);
|
|
|
|
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
RedisModule_FreeString(ctx, user_name);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rm_call_aclcheck_cmd_module_user(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
/* Create a user and authenticate */
|
|
|
|
RedisModuleUser *user = RedisModule_CreateModuleUser("testuser1");
|
|
|
|
RedisModule_SetModuleUserACL(user, "allcommands");
|
|
|
|
RedisModule_SetModuleUserACL(user, "allkeys");
|
|
|
|
RedisModule_SetModuleUserACL(user, "on");
|
|
|
|
RedisModule_AuthenticateClientWithUser(ctx, user, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
int res = rm_call_aclcheck_cmd(ctx, user, argv, argc);
|
|
|
|
|
|
|
|
/* authenticated back to "default" user (so once we free testuser1 we will not disconnected */
|
|
|
|
RedisModule_AuthenticateClientWithACLUser(ctx, "default", 7, NULL, NULL, NULL);
|
|
|
|
RedisModule_FreeModuleUser(user);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 12:13:28 +00:00
|
|
|
int rm_call_aclcheck_with_errors(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
if(argc < 2){
|
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmd = RedisModule_StringPtrLen(argv[1], NULL);
|
|
|
|
|
|
|
|
RedisModuleCallReply* rep = RedisModule_Call(ctx, cmd, "vEC", argv + 2, argc - 2);
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
RedisModule_FreeCallReply(rep);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2021-09-23 05:52:56 +00:00
|
|
|
/* A wrap for RM_Call that pass the 'C' flag to do ACL check on the command. */
|
|
|
|
int rm_call_aclcheck(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
if(argc < 2){
|
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmd = RedisModule_StringPtrLen(argv[1], NULL);
|
|
|
|
|
|
|
|
RedisModuleCallReply* rep = RedisModule_Call(ctx, cmd, "vC", argv + 2, argc - 2);
|
|
|
|
if(!rep) {
|
|
|
|
char err[100];
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
|
|
|
RedisModule_ReplyWithError(ctx, "ERR NOPERM");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snprintf(err, sizeof(err) - 1, "ERR errno=%d", errno);
|
|
|
|
RedisModule_ReplyWithError(ctx, err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
RedisModule_FreeCallReply(rep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
if (RedisModule_Init(ctx,"aclcheck",1,REDISMODULE_APIVER_1)== REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2021-11-28 09:26:28 +00:00
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.set.check.key", set_aclcheck_key,"write",0,0,0) == REDISMODULE_ERR)
|
2021-09-23 05:52:56 +00:00
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.publish.check.channel", publish_aclcheck_channel,"",0,0,0) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.rm_call.check.cmd", rm_call_aclcheck_cmd_default_user,"",0,0,0) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.rm_call.check.cmd.module.user", rm_call_aclcheck_cmd_module_user,"",0,0,0) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2021-11-28 09:26:28 +00:00
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.rm_call", rm_call_aclcheck,
|
|
|
|
"write",0,0,0) == REDISMODULE_ERR)
|
2021-09-23 05:52:56 +00:00
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 12:13:28 +00:00
|
|
|
if (RedisModule_CreateCommand(ctx,"aclcheck.rm_call_with_errors", rm_call_aclcheck_with_errors,
|
|
|
|
"write",0,0,0) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2021-09-23 05:52:56 +00:00
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|