Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃毀 Implement new server commands from Redis 6 馃毀 #90

Merged
merged 9 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type ConditionalArray = Raw[];
export type RedisCommands = {
// Connection
auth(password: string): Promise<Status>;
auth(username: string, password: string): Promise<Status>;
echo(message: string): Promise<BulkString>;
ping(): Promise<Status>;
ping(message: string): Promise<BulkString>;
Expand Down Expand Up @@ -396,6 +397,18 @@ export type RedisCommands = {
// Cluster
// cluster //
// Server
acl_cat(parameter?: string): Promise<BulkString[]>;
acl_deluser(parameter: string): Promise<Integer>;
acl_genpass(parameter?: number): Promise<Status>;
acl_getuser(parameter: string): Promise<BulkString[]>;
acl_help(): Promise<BulkString[]>;
acl_list(): Promise<BulkString[]>;
acl_load(): Promise<Status>;
acl_log(parameter: string | number): Promise<Status | BulkString[]>;
acl_save(): Promise<Status>;
acl_setuser(username: string, rule: string): Promise<Status>;
acl_users(): Promise<BulkString[]>;
acl_whoami(): Promise<Status>;
bgrewriteaof(): Promise<Status>;
bgsave(): Promise<Status>;
// client //
Expand Down Expand Up @@ -437,6 +450,9 @@ export type RedisCommands = {
samples?: number;
},
): Promise<Integer>;
module_list(): Promise<BulkString[]>;
module_load(path: string, args: string): Promise<Status>;
module_unload(name: string): Promise<Status>;
monitor(): void;
role(): Promise<
| ["master", Integer, BulkString[][]]
Expand Down
78 changes: 76 additions & 2 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,74 @@ class RedisImpl implements RedisCommands {
return reply as Status | BulkNil;
}

acl_cat(categoryname?: string) {
if (categoryname) {
return this.execArrayReply<BulkString>("ACL", "CAT", categoryname);
} else {
return this.execArrayReply<BulkString>("ACL", "CAT");
}
}

acl_deluser(username: string) {
return this.execIntegerReply("ACL", "DELUSER", username);
}

acl_genpass(bits?: Integer) {
if (bits) {
return this.execStatusReply("ACL", "GENPASS", bits);
} else {
return this.execStatusReply("ACL", "GENPASS");
}
}

acl_getuser(username: string) {
return this.execArrayReply<BulkString>("ACL", "GETUSER", username);
}

acl_help() {
return this.execArrayReply<BulkString>("ACL", "HELP");
}

acl_list() {
return this.execArrayReply<BulkString>("ACL", "LIST");
}

acl_load() {
return this.execStatusReply("ACL", "LOAD");
}

acl_log(param: string|number) {
if (param === "RESET" || param === "reset") {
return this.execStatusReply("ACL", "LOG", "RESET");
}
return this.execArrayReply<BulkString>("ACL", "LOG", param);
}

acl_save() {
return this.execStatusReply("ACL", "SAVE");
}

acl_setuser(username: string, rule: string) {
return this.execStatusReply("ACL", "SETUSER", username, rule);
}

acl_users() {
return this.execArrayReply<BulkString>("ACL", "USERS");
}

acl_whoami() {
return this.execStatusReply("ACL", "WHOAMI");
}

append(key: string, value: string | number) {
return this.execIntegerReply("APPEND", key, value);
}

auth(password: string) {
return this.execStatusReply("AUTH", password);
auth(param1: string, param2?: string) {
if (typeof param2 === "string") {
return this.execStatusReply("AUTH", param1, param2);
}
return this.execStatusReply("AUTH", param1);
}

bgrewriteaof() {
Expand Down Expand Up @@ -662,6 +724,18 @@ class RedisImpl implements RedisCommands {
return this.execStatusReply("MIGRATE", ...args);
}

module_list() {
return this.execArrayReply<BulkString>("MODULE", "LIST");
}

module_load(path: string, args: string) {
return this.execStatusReply("MODULE", "LOAD", path, args);
}

module_unload(name: string) {
return this.execStatusReply("MODULE", "UNLOAD", name);
}

monitor() {
throw new Error("not supported yet");
}
Expand Down
1 change: 1 addition & 0 deletions redis_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ import "./tests/set_test.ts";
import "./tests/sorted_set_test.ts";
import "./tests/string_test.ts";
import "./tests/key_test.ts";
import "./tests/acl_cmd_test.ts";
120 changes: 120 additions & 0 deletions tests/acl_cmd_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { makeTest } from "./test_util.ts";
import {
assertEquals,
} from "../vendor/https/deno.land/std/testing/asserts.ts";

const { test, client } = await makeTest("acl_cmd");

Xirui marked this conversation as resolved.
Show resolved Hide resolved
test("whoami", async () => {
assertEquals(await client.acl_whoami(), "default");
});

test("list", async () => {
assertEquals(await client.acl_list(), ["user default on nopass ~* +@all"]);
});

test("getuser", async () => {
assertEquals(await client.acl_getuser("default"),
[ "flags",[ "on", "allkeys", "allcommands", "nopass" ],
"passwords", [], "commands", "+@all", "keys", [ "*" ]
]);
});

test("cat", async () => {
assertEquals((await client.acl_cat()).sort(),
[
"keyspace",
"read",
"write",
"set",
"sortedset",
"list",
"hash",
"string",
"bitmap",
"hyperloglog",
"geo",
"stream",
"pubsub",
"admin",
"fast",
"slow",
"blocking",
"dangerous",
"connection",
"transaction",
"scripting"
].sort());
assertEquals((await client.acl_cat("dangerous")).sort(),
[
"lastsave",
"shutdown",
"module",
"monitor",
"role",
"client",
"replconf",
"config",
"pfselftest",
"save",
"replicaof",
"restore-asking",
"restore",
"latency",
"swapdb",
"slaveof",
"bgsave",
"debug",
"bgrewriteaof",
"sync",
"flushdb",
"keys",
"psync",
"pfdebug",
"flushall",
"cluster",
"info",
"migrate",
"acl",
"sort",
"slowlog"
].sort());
});

test("users", async () => {
assertEquals(await client.acl_users(), ["default"])
});

test("acl_setuser", async () => {
assertEquals(await client.acl_setuser("alan", "+get"), "OK")
assertEquals(await client.acl_deluser("alan"), 1);
});

test("deluser", async () => {
assertEquals(await client.acl_deluser("alan"), 0);
});

test("genpass", async () => {
assertEquals((await client.acl_genpass()).length, 64);
let testlen = 32
assertEquals((await client.acl_genpass(testlen)).length, testlen / 4);
});

test("aclauth", async () => {
assertEquals(await client.auth("default", ""), "OK")
});

test("log", async () => {
let randString = "balh"
try {
await client.auth(randString, randString)
} catch (error) {
// skip invalid username-password pair error
}
assertEquals((await client.acl_log(1))[0][9], randString);
assertEquals((await client.acl_log("RESET")), "OK");
});

test("module_list", async () => {
assertEquals(await client.module_list(), []);
});