valkey/tests/modules
KarthikSubbarao f8a5a4f70c
Custom authentication for Modules (#11659)
This change adds new module callbacks that can override the default password based authentication associated with ACLs. With this, Modules can register auth callbacks through which they can implement their own Authentication logic. When `AUTH` and `HELLO AUTH ...` commands are used, Module based authentication is attempted and then normal password based authentication is attempted if needed.
The new Module APIs added in this PR are - `RM_RegisterCustomAuthCallback` and `RM_BlockClientOnAuth` and `RedisModule_ACLAddLogEntryByUserName `.

Module based authentication will be attempted for all Redis users (created through the ACL SETUSER cmd or through Module APIs) even if the Redis user does not exist at the time of the command. This gives a chance for the Module to create the RedisModule user and then authenticate via the RedisModule API - from the custom auth callback.

For the AUTH command, we will support both variations - `AUTH <username> <password>` and `AUTH <password>`. In case of the `AUTH <password>` variation, the custom auth callbacks are triggered with “default” as the username and password as what is provided.


### RedisModule_RegisterCustomAuthCallback
```
void RM_RegisterCustomAuthCallback(RedisModuleCtx *ctx, RedisModuleCustomAuthCallback cb) {
```
This API registers a callback to execute to prior to normal password based authentication. Multiple callbacks can be registered across different modules. These callbacks are responsible for either handling the authentication, each authenticating the user or explicitly denying, or deferring it to other authentication mechanisms. Callbacks are triggered in the order they were registered. When a Module is unloaded, all the auth callbacks registered by it are unregistered. The callbacks are attempted, in the order of most recently registered callbacks, when the AUTH/HELLO (with AUTH field is provided) commands are called. The callbacks will be called with a module context along with a username and a password, and are expected to take one of the following actions:

 (1) Authenticate - Use the RM_Authenticate* API successfully and return `REDISMODULE_AUTH_HANDLED`. This will immediately end the auth chain as successful and add the OK reply.
(2) Block a client on authentication - Use the `RM_BlockClientOnAuth` API and return `REDISMODULE_AUTH_HANDLED`. Here, the client will be blocked until the `RM_UnblockClient `API is used which will trigger the auth reply callback (provided earlier through the `RM_BlockClientOnAuth`). In this reply callback, the Module should authenticate, deny or skip handling authentication.
(3) Deny Authentication - Return `REDISMODULE_AUTH_HANDLED` without authenticating or blocking the client. Optionally, `err` can be set to a custom error message. This will immediately end the auth chain as unsuccessful and add the ERR reply.
(4) Skip handling Authentication - Return `REDISMODULE_AUTH_NOT_HANDLED` without blocking the client. This will allow the engine to attempt the next custom auth callback.

If none of the callbacks authenticate or deny auth, then password based auth is attempted and will authenticate or add failure logs and reply to the clients accordingly.

### RedisModule_BlockClientOnAuth
```
RedisModuleBlockedClient *RM_BlockClientOnAuth(RedisModuleCtx *ctx, RedisModuleCustomAuthCallback reply_callback,
                                               void (*free_privdata)(RedisModuleCtx*,void*))
```
This API can only be used from a Module from the custom auth callback. If a client is not in the middle of custom module based authentication, ERROR is returned. Otherwise, the client is blocked and the `RedisModule_BlockedClient` is returned similar to the `RedisModule_BlockClient` API.

### RedisModule_ACLAddLogEntryByUserName
```
int RM_ACLAddLogEntryByUserName(RedisModuleCtx *ctx, RedisModuleString *username, RedisModuleString *object, RedisModuleACLLogEntryReason reason)
```
Adds a new entry in the ACL log with the `username` RedisModuleString provided. This simplifies the Module usage because now, developers do not need to create a Module User just to add an error ACL Log entry. Aside from accepting username (RedisModuleString) instead of a RedisModuleUser, it is the same as the existing `RedisModule_ACLAddLogEntry` API.


### Breaking changes
- HELLO command - Clients can now only set the client name and RESP protocol from the `HELLO` command if they are authenticated. Also, we now finish command arg validation first and return early with a ERR reply if any arg is invalid. This is to avoid mutating the client name / RESP from a command that would have failed on invalid arguments.

### Notable behaviors
- Module unblocking - Now, we will not allow Modules to block the client from inside the context of a reply callback (triggered from the Module unblock flow `moduleHandleBlockedClients`).

---------

Co-authored-by: Madelyn Olson <34459052+madolson@users.noreply.github.com>
2023-03-15 15:18:42 -07:00
..
aclcheck.c Allow specifying ACL reason for module log entry (#10559) 2022-04-11 22:16:17 -07:00
auth.c Custom authentication for Modules (#11659) 2023-03-15 15:18:42 -07:00
basics.c Skip test for sdsRemoveFreeSpace when mem_allocator is not jemalloc (#11878) 2023-03-07 09:06:58 +02:00
blockedclient.c Fix crash on RM_Call with script mode. (#10886) 2022-06-21 10:01:13 +03:00
blockonbackground.c Modules: Mark all APIs non-experimental (#9983) 2021-12-30 12:17:22 +02:00
blockonkeys.c Add reply_schema to command json files (internal for now) (#10273) 2023-03-11 10:14:16 +02:00
cmdintrospection.c Repurpose redisCommandArg's name as the unique ID (#11051) 2022-08-18 15:09:36 +03:00
commandfilter.c Modules: Mark all APIs non-experimental (#9983) 2021-12-30 12:17:22 +02:00
datatype2.c Use const char pointer in redismodule.h as far as possible (#10064) 2022-01-18 15:55:20 +02:00
datatype.c Adding module api for processing commands during busy jobs and allow flagging the commands that should be handled at this status (#9963) 2022-01-20 09:05:53 +02:00
defragtest.c Build TLS as a loadable module 2022-08-23 12:37:56 +03:00
eventloop.c delete obsolete REDISMODULE_EXPERIMENTAL_API define in module demos (#10527) 2022-04-05 08:21:41 +03:00
fork.c Fix race in module fork kill test (#10717) 2022-05-12 20:10:38 +03:00
getchannels.c Implemented module getchannels api and renamed channel keyspec (#10299) 2022-02-22 11:00:03 +02:00
getkeys.c Handle key-spec flags with modules (#10237) 2022-02-08 10:01:35 +02:00
hash.c Sort out the mess around writable replicas and lookupKeyRead/Write (#9572) 2021-11-28 11:26:28 +02:00
hooks.c Add a special notification unlink available only for modules (#9406) 2022-11-30 11:56:36 +02:00
infotest.c Escape unsafe field name characters in INFO. (#8492) 2021-02-15 17:08:53 +02:00
keyspace_events.c Module API to allow writes after key space notification hooks (#11199) 2022-11-24 19:00:04 +02:00
keyspecs.c RM_CreateCommand should not set CMD_KEY_VARIABLE_FLAGS automatically (#11320) 2022-09-28 14:15:07 +03:00
list.c Fix crash due to to reuse iterator entry after list deletion in module (#11383) 2022-10-22 20:36:50 +03:00
Makefile Custom authentication for Modules (#11659) 2023-03-15 15:18:42 -07:00
mallocsize.c Add RM_MallocSizeString, RM_MallocSizeDict (#10542) 2022-04-17 08:31:57 +03:00
misc.c Match REDISMODULE_OPEN_KEY_* flags to LOOKUP_* flags (#11772) 2023-02-09 14:59:05 +02:00
moduleauthtwo.c Custom authentication for Modules (#11659) 2023-03-15 15:18:42 -07:00
moduleconfigs.c Bug fixes for enum configs with overlapping bit flags (module API) (#10661) 2022-05-09 13:36:53 +03:00
moduleconfigstwo.c Module Configurations (#10285) 2022-03-30 15:47:06 +03:00
postnotifications.c Module API to allow writes after key space notification hooks (#11199) 2022-11-24 19:00:04 +02:00
propagate.c sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
publish.c Add RM_PublishMessageShard (#10543) 2022-04-17 15:43:22 +03:00
reply.c Add test to cover NAN reply using a module (#11482) 2022-11-13 13:12:22 +02:00
scan.c Replace deprecated REDISMODULE_POSTPONED_ARRAY_LEN in module tests and examples (#9677) 2021-10-25 12:00:43 +03:00
stream.c Sort out the mess around writable replicas and lookupKeyRead/Write (#9572) 2021-11-28 11:26:28 +02:00
subcommands.c Block some specific characters in module command names (#11434) 2022-11-03 13:19:49 +02:00
test_lazyfree.c Sort out the mess around writable replicas and lookupKeyRead/Write (#9572) 2021-11-28 11:26:28 +02:00
testrdb.c Avoid saving module aux on RDB if no aux data was saved by the module. (#11374) 2022-10-18 19:45:46 +03:00
timer.c Modules: Mark all APIs non-experimental (#9983) 2021-12-30 12:17:22 +02:00
usercall.c Cleanup around script_caller, fix tracking of scripts and ACL logging for RM_Call (#11770) 2023-02-16 08:07:35 +02:00
zset.c Sort out the mess around writable replicas and lookupKeyRead/Write (#9572) 2021-11-28 11:26:28 +02:00