2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
#include "valkeymodule.h"
|
2021-09-23 08:52:56 +03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2022-01-20 13:05:27 -08:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
2021-09-23 08:52:56 +03:00
|
|
|
|
|
|
|
/* A wrap for SET command with ACL check on the key. */
|
2024-04-23 08:55:44 -07:00
|
|
|
int set_aclcheck_key(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2022-01-20 13:05:27 -08:00
|
|
|
if (argc < 4) {
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 13:05:27 -08:00
|
|
|
int permissions;
|
2024-04-23 08:55:44 -07:00
|
|
|
const char *flags = ValkeyModule_StringPtrLen(argv[1], NULL);
|
2022-01-20 13:05:27 -08:00
|
|
|
|
|
|
|
if (!strcasecmp(flags, "W")) {
|
2024-04-23 08:55:44 -07:00
|
|
|
permissions = VALKEYMODULE_CMD_KEY_UPDATE;
|
2022-01-20 13:05:27 -08:00
|
|
|
} else if (!strcasecmp(flags, "R")) {
|
2024-04-23 08:55:44 -07:00
|
|
|
permissions = VALKEYMODULE_CMD_KEY_ACCESS;
|
2022-01-20 13:05:27 -08:00
|
|
|
} else if (!strcasecmp(flags, "*")) {
|
2024-04-23 08:55:44 -07:00
|
|
|
permissions = VALKEYMODULE_CMD_KEY_UPDATE | VALKEYMODULE_CMD_KEY_ACCESS;
|
2022-02-22 01:00:03 -08:00
|
|
|
} else if (!strcasecmp(flags, "~")) {
|
|
|
|
permissions = 0; /* Requires either read or write */
|
2022-01-20 13:05:27 -08:00
|
|
|
} else {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "INVALID FLAGS");
|
|
|
|
return VALKEYMODULE_OK;
|
2022-01-20 13:05:27 -08:00
|
|
|
}
|
|
|
|
|
2021-09-23 08:52:56 +03:00
|
|
|
/* Check that the key can be accessed */
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleString *user_name = ValkeyModule_GetCurrentUserName(ctx);
|
|
|
|
ValkeyModuleUser *user = ValkeyModule_GetModuleUserFromUserName(user_name);
|
|
|
|
int ret = ValkeyModule_ACLCheckKeyPermissions(user, argv[2], permissions);
|
2021-09-23 08:52:56 +03:00
|
|
|
if (ret != 0) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "DENIED KEY");
|
|
|
|
ValkeyModule_FreeModuleUser(user);
|
|
|
|
ValkeyModule_FreeString(ctx, user_name);
|
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCallReply *rep = ValkeyModule_Call(ctx, "SET", "v", argv + 2, argc - 2);
|
2021-09-23 08:52:56 +03:00
|
|
|
if (!rep) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "NULL reply returned");
|
2021-09-23 08:52:56 +03:00
|
|
|
} else {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
ValkeyModule_FreeCallReply(rep);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_FreeModuleUser(user);
|
|
|
|
ValkeyModule_FreeString(ctx, user_name);
|
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A wrap for PUBLISH command with ACL check on the channel. */
|
2024-04-23 08:55:44 -07:00
|
|
|
int publish_aclcheck_channel(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2021-09-23 08:52:56 +03:00
|
|
|
if (argc != 3) {
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the pubsub channel can be accessed */
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleString *user_name = ValkeyModule_GetCurrentUserName(ctx);
|
|
|
|
ValkeyModuleUser *user = ValkeyModule_GetModuleUserFromUserName(user_name);
|
|
|
|
int ret = ValkeyModule_ACLCheckChannelPermissions(user, argv[1], VALKEYMODULE_CMD_CHANNEL_SUBSCRIBE);
|
2021-09-23 08:52:56 +03:00
|
|
|
if (ret != 0) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "DENIED CHANNEL");
|
|
|
|
ValkeyModule_FreeModuleUser(user);
|
|
|
|
ValkeyModule_FreeString(ctx, user_name);
|
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCallReply *rep = ValkeyModule_Call(ctx, "PUBLISH", "v", argv + 1, argc - 1);
|
2021-09-23 08:52:56 +03:00
|
|
|
if (!rep) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "NULL reply returned");
|
2021-09-23 08:52:56 +03:00
|
|
|
} else {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
ValkeyModule_FreeCallReply(rep);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_FreeModuleUser(user);
|
|
|
|
ValkeyModule_FreeString(ctx, user_name);
|
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* A wrap for RM_Call that check first that the command can be executed */
|
2024-04-23 08:55:44 -07:00
|
|
|
int rm_call_aclcheck_cmd(ValkeyModuleCtx *ctx, ValkeyModuleUser *user, ValkeyModuleString **argv, int argc) {
|
2021-09-23 08:52:56 +03:00
|
|
|
if (argc < 2) {
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that the command can be executed */
|
2024-04-23 08:55:44 -07:00
|
|
|
int ret = ValkeyModule_ACLCheckCommandPermissions(user, argv + 1, argc - 1);
|
2021-09-23 08:52:56 +03:00
|
|
|
if (ret != 0) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "DENIED CMD");
|
2021-09-23 08:52:56 +03:00
|
|
|
/* Add entry to ACL log */
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ACLAddLogEntry(ctx, user, argv[1], VALKEYMODULE_ACL_LOG_CMD);
|
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
const char* cmd = ValkeyModule_StringPtrLen(argv[1], NULL);
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCallReply* rep = ValkeyModule_Call(ctx, cmd, "v", argv + 2, argc - 2);
|
2021-09-23 08:52:56 +03:00
|
|
|
if(!rep){
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "NULL reply returned");
|
2021-09-23 08:52:56 +03:00
|
|
|
}else{
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
ValkeyModule_FreeCallReply(rep);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int rm_call_aclcheck_cmd_default_user(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
ValkeyModuleString *user_name = ValkeyModule_GetCurrentUserName(ctx);
|
|
|
|
ValkeyModuleUser *user = ValkeyModule_GetModuleUserFromUserName(user_name);
|
2021-09-23 08:52:56 +03:00
|
|
|
|
|
|
|
int res = rm_call_aclcheck_cmd(ctx, user, argv, argc);
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_FreeModuleUser(user);
|
|
|
|
ValkeyModule_FreeString(ctx, user_name);
|
2021-09-23 08:52:56 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int rm_call_aclcheck_cmd_module_user(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2021-09-23 08:52:56 +03:00
|
|
|
/* Create a user and authenticate */
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleUser *user = ValkeyModule_CreateModuleUser("testuser1");
|
|
|
|
ValkeyModule_SetModuleUserACL(user, "allcommands");
|
|
|
|
ValkeyModule_SetModuleUserACL(user, "allkeys");
|
|
|
|
ValkeyModule_SetModuleUserACL(user, "on");
|
|
|
|
ValkeyModule_AuthenticateClientWithUser(ctx, user, NULL, NULL, NULL);
|
2021-09-23 08:52:56 +03:00
|
|
|
|
|
|
|
int res = rm_call_aclcheck_cmd(ctx, user, argv, argc);
|
|
|
|
|
|
|
|
/* authenticated back to "default" user (so once we free testuser1 we will not disconnected */
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_AuthenticateClientWithACLUser(ctx, "default", 7, NULL, NULL, NULL);
|
|
|
|
ValkeyModule_FreeModuleUser(user);
|
2021-09-23 08:52:56 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int rm_call_aclcheck_with_errors(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc){
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
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 14:13:28 +02:00
|
|
|
|
|
|
|
if(argc < 2){
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
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 14:13:28 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
const char* cmd = ValkeyModule_StringPtrLen(argv[1], NULL);
|
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 14:13:28 +02:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCallReply* rep = ValkeyModule_Call(ctx, cmd, "vEC", argv + 2, argc - 2);
|
|
|
|
ValkeyModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
ValkeyModule_FreeCallReply(rep);
|
|
|
|
return VALKEYMODULE_OK;
|
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 14:13:28 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 08:52:56 +03:00
|
|
|
/* A wrap for RM_Call that pass the 'C' flag to do ACL check on the command. */
|
2024-04-23 08:55:44 -07:00
|
|
|
int rm_call_aclcheck(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc){
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
2021-09-23 08:52:56 +03:00
|
|
|
|
|
|
|
if(argc < 2){
|
2024-04-23 08:55:44 -07:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
const char* cmd = ValkeyModule_StringPtrLen(argv[1], NULL);
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCallReply* rep = ValkeyModule_Call(ctx, cmd, "vC", argv + 2, argc - 2);
|
2021-09-23 08:52:56 +03:00
|
|
|
if(!rep) {
|
|
|
|
char err[100];
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "ERR NOPERM");
|
2021-09-23 08:52:56 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
snprintf(err, sizeof(err) - 1, "ERR errno=%d", errno);
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, err);
|
2021-09-23 08:52:56 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithCallReply(ctx, rep);
|
|
|
|
ValkeyModule_FreeCallReply(rep);
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int module_test_acl_category(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
|
|
|
ValkeyModule_ReplyWithSimpleString(ctx, "OK");
|
|
|
|
return VALKEYMODULE_OK;
|
2023-03-21 10:07:11 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int commandBlockCheck(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
2023-03-21 10:07:11 -07:00
|
|
|
int response_ok = 0;
|
2024-04-23 08:55:44 -07:00
|
|
|
int result = ValkeyModule_CreateCommand(ctx,"command.that.should.fail", module_test_acl_category, "", 0, 0, 0);
|
|
|
|
response_ok |= (result == VALKEYMODULE_OK);
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
result = ValkeyModule_AddACLCategory(ctx,"blockedcategory");
|
|
|
|
response_ok |= (result == VALKEYMODULE_OK);
|
2023-08-30 13:01:24 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleCommand *parent = ValkeyModule_GetCommand(ctx,"block.commands.outside.onload");
|
|
|
|
result = ValkeyModule_SetCommandACLCategories(parent, "write");
|
|
|
|
response_ok |= (result == VALKEYMODULE_OK);
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
result = ValkeyModule_CreateSubcommand(parent,"subcommand.that.should.fail",module_test_acl_category,"",0,0,0);
|
|
|
|
response_ok |= (result == VALKEYMODULE_OK);
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2023-08-30 13:01:24 -07:00
|
|
|
/* This validates that it's not possible to create commands or add
|
|
|
|
* a new ACL Category outside OnLoad function.
|
2023-03-21 10:07:11 -07:00
|
|
|
* thus returns an error if they succeed. */
|
|
|
|
if (response_ok) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithError(ctx, "UNEXPECTEDOK");
|
2023-03-21 10:07:11 -07:00
|
|
|
} else {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_ReplyWithSimpleString(ctx, "OK");
|
2023-03-21 10:07:11 -07:00
|
|
|
}
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
2023-03-21 10:07:11 -07:00
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_Init(ctx,"aclcheck",1,VALKEYMODULE_APIVER_1)== VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (argc > 1) return ValkeyModule_WrongArity(ctx);
|
2023-08-30 13:01:24 -07:00
|
|
|
|
|
|
|
/* When that flag is passed, we try to create too many categories,
|
2024-04-23 08:55:44 -07:00
|
|
|
* and the test expects this to fail. In this case the server returns VALKEYMODULE_ERR
|
2023-08-30 13:01:24 -07:00
|
|
|
* and set errno to ENOMEM*/
|
|
|
|
if (argc == 1) {
|
|
|
|
long long fail_flag = 0;
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_StringToLongLong(argv[0], &fail_flag);
|
2023-08-30 13:01:24 -07:00
|
|
|
if (fail_flag) {
|
|
|
|
for (size_t j = 0; j < 45; j++) {
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModuleString* name = ValkeyModule_CreateStringPrintf(ctx, "customcategory%zu", j);
|
|
|
|
if (ValkeyModule_AddACLCategory(ctx, ValkeyModule_StringPtrLen(name, NULL)) == VALKEYMODULE_ERR) {
|
|
|
|
ValkeyModule_Assert(errno == ENOMEM);
|
|
|
|
ValkeyModule_FreeString(ctx, name);
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-08-30 13:01:24 -07:00
|
|
|
}
|
2024-04-23 08:55:44 -07:00
|
|
|
ValkeyModule_FreeString(ctx, name);
|
2023-08-30 13:01:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.set.check.key", set_aclcheck_key,"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"block.commands.outside.onload", commandBlockCheck,"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.module.command.aclcategories.write", module_test_acl_category,"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
ValkeyModuleCommand *aclcategories_write = ValkeyModule_GetCommand(ctx,"aclcheck.module.command.aclcategories.write");
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_SetCommandACLCategories(aclcategories_write, "write") == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.module.command.aclcategories.write.function.read.category", module_test_acl_category,"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
ValkeyModuleCommand *read_category = ValkeyModule_GetCommand(ctx,"aclcheck.module.command.aclcategories.write.function.read.category");
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_SetCommandACLCategories(read_category, "read") == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.module.command.aclcategories.read.only.category", module_test_acl_category,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
ValkeyModuleCommand *read_only_category = ValkeyModule_GetCommand(ctx,"aclcheck.module.command.aclcategories.read.only.category");
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_SetCommandACLCategories(read_only_category, "read") == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-03-21 10:07:11 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.publish.check.channel", publish_aclcheck_channel,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.rm_call.check.cmd", rm_call_aclcheck_cmd_default_user,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.rm_call.check.cmd.module.user", rm_call_aclcheck_cmd_module_user,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.rm_call", rm_call_aclcheck,
|
|
|
|
"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2021-09-23 08:52:56 +03:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.rm_call_with_errors", rm_call_aclcheck_with_errors,
|
|
|
|
"write",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_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 14:13:28 +02:00
|
|
|
|
2023-08-30 13:01:24 -07:00
|
|
|
/* This validates that, when module tries to add a category with invalid characters,
|
2024-04-23 08:55:44 -07:00
|
|
|
* the server returns VALKEYMODULE_ERR and set errno to `EINVAL` */
|
|
|
|
if (ValkeyModule_AddACLCategory(ctx,"!nval!dch@r@cter$") == VALKEYMODULE_ERR)
|
|
|
|
ValkeyModule_Assert(errno == EINVAL);
|
2023-08-30 13:01:24 -07:00
|
|
|
else
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_ERR;
|
2023-08-30 13:01:24 -07:00
|
|
|
|
|
|
|
/* This validates that, when module tries to add a category that already exists,
|
2024-04-23 08:55:44 -07:00
|
|
|
* the server returns VALKEYMODULE_ERR and set errno to `EBUSY` */
|
|
|
|
if (ValkeyModule_AddACLCategory(ctx,"write") == VALKEYMODULE_ERR)
|
|
|
|
ValkeyModule_Assert(errno == EBUSY);
|
2023-08-30 13:01:24 -07:00
|
|
|
else
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_ERR;
|
2023-08-30 13:01:24 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_AddACLCategory(ctx,"foocategory") == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-08-30 13:01:24 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_CreateCommand(ctx,"aclcheck.module.command.test.add.new.aclcategories", module_test_acl_category,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
ValkeyModuleCommand *test_add_new_aclcategories = ValkeyModule_GetCommand(ctx,"aclcheck.module.command.test.add.new.aclcategories");
|
2023-08-30 13:01:24 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
if (ValkeyModule_SetCommandACLCategories(test_add_new_aclcategories, "foocategory") == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-08-30 13:01:24 -07:00
|
|
|
|
2024-04-23 08:55:44 -07:00
|
|
|
return VALKEYMODULE_OK;
|
2021-09-23 08:52:56 +03:00
|
|
|
}
|