Skip to content

Commit

Permalink
added functions to get_client_id, name, username, info and certificate
Browse files Browse the repository at this point in the history
added example and tests
  • Loading branch information
Dmitry Polyakovsky committed Feb 24, 2025
1 parent 35dcd00 commit c80b770
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ crate-type = ["cdylib"]
name = "expire"
crate-type = ["cdylib"]

[[example]]
name = "client"
crate-type = ["cdylib"]

[dependencies]
bitflags = "2.8.0"
libc = "0.2"
Expand Down
33 changes: 33 additions & 0 deletions examples/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use valkey_module::alloc::ValkeyAlloc;
use valkey_module::{valkey_module, Context, ValkeyResult, ValkeyString};

valkey_module! {
name: "client",
version: 1,
allocator: (ValkeyAlloc, ValkeyAlloc),
data_types: [],
commands: [
["client.id", get_client_id, "readonly", 1, 1, 1],
]
}

fn get_client_id(ctx: &Context, _args: Vec<ValkeyString>) -> ValkeyResult {
let client_id = ctx.get_client_id();
ctx.log_notice(&format!(
"client_username: {:?}",
ctx.get_client_username().to_string()
));
ctx.log_notice(&format!(
"client_name: {:?}",
ctx.get_client_name().to_string()
));
ctx.log_notice(&format!(
"client_cert: {:?}",
ctx.get_client_cert().to_string()
));
ctx.log_notice(&format!(
"client_info: {:?}",
ctx.get_client_info()
));
Ok((client_id as i64).into())
}
33 changes: 32 additions & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::redisvalue::ValkeyValueKey;
use crate::{
add_info_begin_dict_field, add_info_end_dict_field, add_info_field_double,
add_info_field_long_long, add_info_field_str, add_info_field_unsigned_long_long, raw, utils,
Status,
RedisModule_GetClientCertificate, RedisModule_GetClientId, RedisModule_GetClientInfoById,
RedisModule_GetClientNameById, RedisModule_GetClientUserNameById, Status,
};
use crate::{add_info_section, ValkeyResult};
use crate::{ValkeyError, ValkeyString, ValkeyValue};
Expand Down Expand Up @@ -829,6 +830,36 @@ impl Context {
.map_err(|_e| ValkeyError::Str("User does not have permissions on key"))
}

pub fn get_client_id(&self) -> u64 {
unsafe { RedisModule_GetClientId.unwrap()(self.ctx) }
}

pub fn get_client_name(&self) -> ValkeyString {
let client_id = self.get_client_id();
let client_name = unsafe { RedisModule_GetClientNameById.unwrap()(self.ctx, client_id) };
ValkeyString::from_redis_module_string(self.ctx, client_name)
}

pub fn get_client_username(&self) -> ValkeyString {
let client_id = self.get_client_id();
let client_username =
unsafe { RedisModule_GetClientUserNameById.unwrap()(self.ctx, client_id) };
ValkeyString::from_redis_module_string(self.ctx, client_username)
}

pub fn get_client_cert(&self) -> ValkeyString {
let client_id = self.get_client_id();
let client_cert = unsafe { RedisModule_GetClientCertificate.unwrap()(self.ctx, client_id) };
ValkeyString::from_redis_module_string(self.ctx, client_cert)
}

pub fn get_client_info(&self) -> ValkeyValue {
let client_id = self.get_client_id();
let client_info =
unsafe { RedisModule_GetClientInfoById.unwrap()(self.ctx as *mut c_void, client_id) };
(client_info as i64).into()
}

api!(
[RedisModule_AddPostNotificationJob],
/// When running inside a key space notification callback, it is dangerous and highly discouraged to perform any write
Expand Down
13 changes: 13 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,16 @@ fn test_acl_categories() -> Result<()> {
assert!(response_data.contains(&search_str));
Ok(())
}

#[test]
fn test_client() -> Result<()> {
let port = 6507;
let _guards =
vec![start_valkey_server_with_module("client", port)
.with_context(|| FAILED_TO_START_SERVER)?];
let mut con = get_valkey_connection(port).with_context(|| FAILED_TO_CONNECT_TO_SERVER)?;
redis::cmd("client.id")
.exec(&mut con)
.with_context(|| "failed execute client.id")?;
Ok(())
}

0 comments on commit c80b770

Please sign in to comment.