kividb 1.2.2

A fast Redis®-compatible in-memory store written in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// src/commands/acl.rs
//
// ACL command handler. The users, their rules and how they are enforced are
// in `crate::acl`; this is the command surface over them.

use crate::acl::{AclLogEntry, AclUser};
use crate::commands::ctx::Ctx;
use crate::resp::write::*;

pub fn handle(ctx: &mut Ctx, name: &str) -> Option<Vec<u8>> {
    if name != "ACL" {
        return None;
    }

    let cmd = &ctx.cmd;
    if cmd.len() < 2 {
        return Some(resp_error(
            "ERR wrong number of arguments for 'acl' command",
        ));
    }

    let sub = String::from_utf8_lossy(cmd[1]).to_ascii_uppercase();

    match sub.as_str() {
        // ── ACL WHOAMI ────────────────────────────────────────────────────────
        // Returns the username of the current connection.
        // We always return "default" — we only have one user.
        "WHOAMI" => Some(resp_bulk_str(ctx.authenticated_user)),

        // ── ACL LIST ──────────────────────────────────────────────────────────
        // Returns all users in ACL file format.
        "LIST" => {
            let store = ctx.acl_store.read();
            let mut users: Vec<String> = store.values().map(|u| u.to_acl_string()).collect();
            users.sort(); // deterministic output
            Some(resp_array(users.iter().map(|s| resp_bulk_str(s)).collect()))
        }

        // ── ACL USERS ─────────────────────────────────────────────────────────
        // Returns a list of all usernames.
        "USERS" => {
            let store = ctx.acl_store.read();
            let mut names: Vec<String> = store.keys().cloned().collect();
            names.sort();
            Some(resp_array(names.iter().map(|s| resp_bulk_str(s)).collect()))
        }

        // ── ACL CAT ───────────────────────────────────────────────────────────
        // ACL CAT           → list of all category names
        // ACL CAT <cat>     → list of commands in that category
        "CAT" => {
            if cmd.len() >= 3 {
                // Return commands in a specific category — delegate to server.rs helper
                let cat = String::from_utf8_lossy(cmd[2]).to_ascii_lowercase();
                match crate::commands::server::acl_cat_commands_opt(&cat) {
                    Some(cmds) => Some(resp_array(cmds.iter().map(|c| resp_bulk_str(c)).collect())),
                    None => Some(resp_error(&format!(
                        "ERR Unknown category '{}', try ACL CAT",
                        cat
                    ))),
                }
            } else {
                // Return all category names
                let cats = crate::commands::server::ACL_CATEGORIES;
                Some(resp_array(cats.iter().map(|c| resp_bulk_str(c)).collect()))
            }
        }

        // ── ACL GETUSER ───────────────────────────────────────────────────────
        // Returns the rules for a specific user as a map.
        "GETUSER" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'acl|getuser' command",
                ));
            }
            let username = match std::str::from_utf8(cmd[2]) {
                Ok(s) => s,
                Err(_) => return Some(resp_error("ERR invalid username")),
            };
            let store = ctx.acl_store.read();
            match store.get(username) {
                None => Some(resp_null_bulk()),
                Some(user) => {
                    // A flat array of key-value pairs (a map, in RESP2 terms).
                    // The rules are strings, as Redis 7 returns them — one
                    // string each for commands, keys and channels — which is
                    // what client libraries parse.
                    let mut flags = vec![if user.enabled { "on" } else { "off" }];
                    if user.nopass {
                        flags.push("nopass");
                    }
                    let passwords: Vec<Vec<u8>> =
                        user.passwords.iter().map(|h| resp_bulk_str(h)).collect();
                    let selectors: Vec<Vec<u8>> = user
                        .selectors
                        .iter()
                        .map(|s| {
                            let keys = s
                                .keys
                                .iter()
                                .map(|k| k.to_string())
                                .collect::<Vec<_>>()
                                .join(" ");
                            let channels = s
                                .channels
                                .iter()
                                .map(|c| format!("&{c}"))
                                .collect::<Vec<_>>()
                                .join(" ");
                            resp_array(vec![
                                resp_bulk_str("commands"),
                                resp_bulk_str(&s.commands),
                                resp_bulk_str("keys"),
                                resp_bulk_str(&keys),
                                resp_bulk_str("channels"),
                                resp_bulk_str(&channels),
                            ])
                        })
                        .collect();

                    Some(resp_array(vec![
                        resp_bulk_str("flags"),
                        resp_array(flags.iter().map(|f| resp_bulk_str(f)).collect()),
                        resp_bulk_str("passwords"),
                        resp_array(passwords),
                        resp_bulk_str("commands"),
                        resp_bulk_str(&user.commands),
                        resp_bulk_str("keys"),
                        resp_bulk_str(&user.keys_string()),
                        resp_bulk_str("channels"),
                        resp_bulk_str(&user.channels_string()),
                        resp_bulk_str("selectors"),
                        resp_array(selectors),
                    ]))
                }
            }
        }

        // ── ACL LOG ───────────────────────────────────────────────────────────
        // ACL LOG [count | RESET]
        "LOG" => {
            // As Redis: `LOG`, `LOG <count>` or `LOG RESET`, and nothing more.
            if cmd.len() > 3 {
                let sub = &cmd[1][..cmd[1].len().min(128)];
                return Some(resp_error(&format!(
                    "ERR unknown subcommand or wrong number of arguments for '{}'. Try ACL HELP.",
                    String::from_utf8_lossy(sub).replace(['\r', '\n'], " ")
                )));
            }
            // Ten entries unless a count is given; a negative count is none.
            let mut count: i64 = 10;
            if let Some(arg) = cmd.get(2) {
                if arg.eq_ignore_ascii_case(b"RESET") {
                    ctx.acl_log.reset();
                    return Some(resp_simple("OK"));
                }
                count = match crate::commands::login::strict_integer(arg) {
                    Some(n) => n.max(0),
                    None => return Some(resp_error("ERR value is not an integer or out of range")),
                };
            }
            Some(format_acl_log(ctx.acl_log.get(count as usize), ctx.resp))
        }

        // ── ACL SETUSER ───────────────────────────────────────────────────────
        // ACL SETUSER username [rule ...]
        // Creates or modifies a user. Rules: on/off, nopass, >password, ~pattern, +cmd, -cmd
        "SETUSER" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'acl|setuser' command",
                ));
            }
            // What tracking clients are still to be told of (the writes before
            // this in a transaction) is told under the permissions they had,
            // before this changes them, as Redis does: a BCAST client is told
            // only of keys its user may read.
            crate::tracking::flush_pending(
                ctx.tracking_table,
                ctx.client_registry,
                Some(ctx.client_id),
            );
            let username = match std::str::from_utf8(cmd[2]) {
                Ok(s) => s.to_string(),
                Err(_) => return Some(resp_error("ERR invalid username")),
            };
            if username.is_empty() || username.contains([' ', '\0', '\t', '\n', '\r']) {
                return Some(resp_error(
                    "ERR Usernames can't contain spaces or null characters",
                ));
            }
            let mut args: Vec<String> = Vec::with_capacity(cmd.len().saturating_sub(3));
            for rule_bytes in cmd.iter().skip(3) {
                match std::str::from_utf8(rule_bytes) {
                    Ok(s) => args.push(s.to_string()),
                    // A password is any bytes, as in Redis: one that is not
                    // UTF-8 is added, or removed, by its hash.
                    Err(_) => match rule_bytes.split_first() {
                        Some((b'>', password)) => {
                            args.push(format!("#{}", crate::acl::sha256_hex(password)))
                        }
                        Some((b'<', password)) => {
                            args.push(format!("!{}", crate::acl::sha256_hex(password)))
                        }
                        _ => return Some(resp_error("ERR invalid rule")),
                    },
                }
            }
            let rules = match crate::acl::merge_selector_args(args.iter().map(String::as_str)) {
                Ok(rules) => rules,
                Err(e) => return Some(resp_error(&format!("ERR {e}"))),
            };

            // The rules are applied to a copy, and the copy replaces the user
            // only if every one of them was accepted: a SETUSER that returns
            // an error has changed nothing.
            let mut store = ctx.acl_store.write();
            let mut user = store
                .get(&username)
                .cloned()
                .unwrap_or_else(|| AclUser::new(&username));
            for rule in &rules {
                if let Err(e) = crate::acl::apply_rule(&mut user, rule) {
                    return Some(resp_error(&format!(
                        "ERR Error in ACL SETUSER modifier '{rule}': {e}"
                    )));
                }
            }
            store.insert(username, user);
            drop(store);
            // Connections already open as this user pick the change up.
            crate::acl::acl_changed();

            // The ACL file is written by `ACL SAVE`, and only by it. Writing
            // it here made the command fail — after it had taken effect —
            // wherever the file is not writable (a read-only mount), and
            // overwrote a file that something else manages.
            Some(resp_simple("OK"))
        }

        // ── ACL DELUSER ───────────────────────────────────────────────────────
        // ACL DELUSER username [username ...]
        "DELUSER" => {
            if cmd.len() < 3 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'acl|deluser' command",
                ));
            }
            // Checked before anything is deleted, so that an error means
            // nothing happened.
            if cmd.iter().skip(2).any(|name| *name == b"default") {
                return Some(resp_error("ERR The 'default' user cannot be removed"));
            }
            let mut deleted = 0i64;
            let mut store = ctx.acl_store.write();
            for name_bytes in cmd.iter().skip(2) {
                let name = match std::str::from_utf8(name_bytes) {
                    Ok(s) => s,
                    Err(_) => continue,
                };
                if store.remove(name).is_some() {
                    deleted += 1;
                }
            }
            drop(store);
            if deleted > 0 {
                // Connections logged in as a deleted user are closed.
                crate::acl::acl_changed();
            }
            Some(resp_integer(deleted))
        }

        // ── ACL GENPASS ───────────────────────────────────────────────────────
        // ACL GENPASS [bits]
        // Generates a cryptographically random hex string.
        "GENPASS" => {
            let bits: usize = if cmd.len() >= 3 {
                match std::str::from_utf8(cmd[2])
                    .ok()
                    .and_then(|s| s.parse::<usize>().ok())
                {
                    Some(n) if n > 0 && n <= 4096 => n.div_ceil(4) * 4,
                    _ => {
                        return Some(resp_error(
                            "ERR The optional argument must be between 1 and 4096, and divisible by 4",
                        ));
                    }
                }
            } else {
                256
            };
            // A hex digit per 4 bits, as Redis gives them: 1 to 4 bits are one
            // digit. Whole bytes were taken, none for under 8 bits, and the
            // password was empty (#140).
            let digits = bits.div_ceil(4);
            let random_bytes: Vec<u8> = (0..digits.div_ceil(2))
                .map(|_| rand::random::<u8>())
                .collect();
            let mut hex: String = random_bytes.iter().map(|b| format!("{:02x}", b)).collect();
            hex.truncate(digits);
            Some(resp_bulk_str(&hex))
        }

        // ── ACL DRYRUN ────────────────────────────────────────────────────────
        // ACL DRYRUN username command [arg ...]
        // Says whether the user could run the command — the command, its
        // keys and its channels — without running it.
        "DRYRUN" => {
            if cmd.len() < 4 {
                return Some(resp_error(
                    "ERR wrong number of arguments for 'acl|dryrun' command",
                ));
            }
            let username = match std::str::from_utf8(cmd[2]) {
                Ok(s) => s,
                Err(_) => return Some(resp_error("ERR invalid username")),
            };
            let target_cmd = String::from_utf8_lossy(cmd[3]).to_ascii_uppercase();
            let store = ctx.acl_store.read();
            let Some(user) = store.get(username) else {
                return Some(resp_error(&format!("ERR User '{username}' not found")));
            };
            if !crate::commands::server::is_registered_command(&target_cmd) {
                return Some(resp_error(&format!(
                    "ERR Command '{}' not found",
                    String::from_utf8_lossy(cmd[3])
                )));
            }
            match crate::acl::check_permission(user, &target_cmd, &cmd[3..]) {
                Ok(()) => Some(resp_simple("OK")),
                Err(denied) => {
                    let message = denied.message(username);
                    Some(resp_bulk_str(
                        message.strip_prefix("NOPERM ").unwrap_or(&message),
                    ))
                }
            }
        }

        // ── ACL SAVE / LOAD ───────────────────────────────────────────────────
        // SAVE writes the users in memory to the ACL file; LOAD makes the
        // file's users the users in memory — all of them, or, if the file
        // has an error in it, none.
        "SAVE" => {
            if ctx.config.aclfile.is_empty() {
                return Some(resp_error(
                    "ERR This KiviDB instance is not configured to use an ACL file. \
                    You may want to specify users via the ACL SETUSER command and \
                    make sure to also set an aclfile configuration directive.",
                ));
            }
            match crate::acl::save_acl_file(&ctx.config.aclfile, ctx.acl_store) {
                Ok(_) => Some(resp_simple("OK")),
                Err(e) => Some(resp_error(&format!("ERR saving ACL file: {e}"))),
            }
        }

        "LOAD" => {
            if ctx.config.aclfile.is_empty() {
                return Some(resp_error(
                    "ERR This KiviDB instance is not configured to use an ACL file",
                ));
            }
            match crate::acl::load_acl_file(
                &ctx.config.aclfile,
                ctx.acl_store,
                &ctx.config.requirepass,
            ) {
                Ok(_) => Some(resp_simple("OK")),
                Err(e) => Some(resp_error(&format!(
                    "ERR {e}. WARNING: ACL errors detected, no change to the previously \
                     active ACL rules was performed"
                ))),
            }
        }

        // Its error said to try it, and it was unknown (#134).
        "HELP" => Some(crate::commands::server::help(
            "ACL",
            &[
                (
                    "CAT [<category>]",
                    "List all commands that belong to <category>, or all command categories",
                ),
                (
                    "DELUSER <username> [<username> ...]",
                    "Delete a list of users.",
                ),
                (
                    "DRYRUN <username> <command> [<arg> ...]",
                    "Returns whether the user can execute the given command without executing the command.",
                ),
                ("GETUSER <username>", "Get the user's details."),
                (
                    "GENPASS [<bits>]",
                    "Generate a secure 256-bit user password. The optional `bits` argument can be used to specify a different size.",
                ),
                ("LIST", "Show users details in config file format."),
                ("LOAD", "Reload users from the ACL file."),
                ("LOG [<count> | RESET]", "Show the ACL log entries."),
                ("SAVE", "Save the current config to the ACL file."),
                (
                    "SETUSER <username> <attribute> [<attribute> ...]",
                    "Create or modify a user with the specified attributes.",
                ),
                ("USERS", "List all the registered usernames."),
                ("WHOAMI", "Return the current connection username."),
            ],
        )),

        _ => Some(resp_error(&format!(
            "ERR unknown subcommand '{}'. Try ACL HELP.",
            sub
        ))),
    }
}

// ── Format ACL LOG entries as RESP ────────────────────────────────────────────

/// As Redis's `ACL LOG`: a map of ten fields an entry, in its order.
fn format_acl_log(entries: Vec<AclLogEntry>, resp: u8) -> Vec<u8> {
    let items: Vec<Vec<u8>> = entries
        .into_iter()
        .map(|e| {
            let fields = vec![
                resp_bulk_str("count"),
                resp_integer(e.count as i64),
                resp_bulk_str("reason"),
                resp_bulk_str(&e.reason),
                resp_bulk_str("context"),
                resp_bulk_str(&e.context),
                resp_bulk_str("object"),
                resp_bulk_str(&e.object),
                resp_bulk_str("username"),
                resp_bulk_str(&e.username),
                resp_bulk_str("age-seconds"),
                resp_double_for(resp, e.age_seconds),
                resp_bulk_str("client-info"),
                resp_bulk_str(&e.client_info),
                resp_bulk_str("entry-id"),
                resp_integer(e.id as i64),
                resp_bulk_str("timestamp-created"),
                resp_integer(e.timestamp_created as i64),
                resp_bulk_str("timestamp-last-updated"),
                resp_integer(e.timestamp_last_updated as i64),
            ];
            if resp >= 3 {
                resp_map(fields)
            } else {
                resp_array(fields)
            }
        })
        .collect();
    resp_array(items)
}