Skip to content

Commit

Permalink
Add support for ACL DRYRUN command #1992
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming authored and mp911de committed Jul 1, 2022
1 parent b46dfd5 commit 6e5018b
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public RedisFuture<Long> aclDeluser(String... usernames) {
return dispatch(commandBuilder.aclDeluser(usernames));
}

@Override
public RedisFuture<String> aclDryRun(String username, String command, V... args) {
return dispatch(commandBuilder.aclDryRun(username, command, args));
}

@Override
public RedisFuture<String> aclGenpass() {
return dispatch(commandBuilder.aclGenpass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public Mono<Long> aclDeluser(String... usernames) {
return createMono(() -> commandBuilder.aclDeluser(usernames));
}

@Override
public Mono<String> aclDryRun(String username, String command, V... args) {
return createMono(() -> commandBuilder.aclDryRun(username, command, args));
}

@Override
public Mono<String> aclGenpass() {
return createMono(commandBuilder::aclGenpass);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/lettuce/core/RedisCommandBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ Command<K, V, Long> aclDeluser(String... usernames) {
return createCommand(ACL, new IntegerOutput<>(codec), args);
}

Command<K, V, String> aclDryRun(String username, String command, V... commandArgs) {
LettuceAssert.notNull(username, "username " + MUST_NOT_BE_NULL);
LettuceAssert.notNull(command, "command " + MUST_NOT_BE_NULL);

CommandArgs<K, V> args = new CommandArgs<>(codec);
args.add(DRYRUN).add(username).add(command).addValues(commandArgs);
return createCommand(ACL, new StatusOutput<>(codec), args);
}

Command<K, V, String> aclGenpass() {
CommandArgs<K, V> args = new CommandArgs<>(codec);
args.add(GENPASS);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/lettuce/core/api/async/RedisAclAsyncCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public interface RedisAclAsyncCommands<K, V> {
*/
RedisFuture<Long> aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
RedisFuture<String> aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ public interface RedisAclReactiveCommands<K, V> {
*/
Mono<Long> aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
Mono<String> aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/lettuce/core/api/sync/RedisAclCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public interface RedisAclCommands<K, V> {
*/
Long aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
String aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public interface NodeSelectionAclAsyncCommands<K, V> {
*/
AsyncExecutions<Long> aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
AsyncExecutions<String> aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ public interface NodeSelectionAclCommands<K, V> {
*/
Executions<Long> aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
Executions<String> aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum CommandKeyword implements ProtocolKeyword {

ABSTTL, ADDR, ADDSLOTS, ADDSLOTSRANGE, AFTER, AGGREGATE, ALLCHANNELS, ALLCOMMANDS, ALLKEYS, ALPHA, AND, ASK, ASC, ASYNC, BEFORE, BLOCK, BUMPEPOCH,

BY, BYLEX, BYSCORE, CACHING, CAT, CH, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELSLOTSRANGE, DELUSER, DESC, SOFT, HARD, ENCODING,
BY, BYLEX, BYSCORE, CACHING, CAT, CH, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELSLOTSRANGE, DELUSER, DESC, DRYRUN, SOFT, HARD, ENCODING,

FAILOVER, FORGET, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ interface RedisAclCoroutinesCommands<K : Any, V : Any> {
*/
suspend fun aclDeluser(vararg usernames: String): Long?

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
suspend fun aclDryRun(username: String, command: String, vararg args: V): String?

/**
* The command generates a password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ internal class RedisAclCoroutinesCommandsImpl<K : Any, V : Any>(internal val ops

override suspend fun aclDeluser(vararg usernames: String): Long? = ops.aclDeluser(*usernames).awaitFirstOrNull()

override suspend fun aclDryRun(username: String, command: String, vararg args: V): String? = ops.aclDryRun(username, command, *args).awaitFirstOrNull()

override suspend fun aclGenpass(): String? = ops.aclGenpass().awaitFirstOrNull()

override suspend fun aclGenpass(bits: Int): String? = ops.aclGenpass(bits).awaitFirstOrNull()
Expand Down
11 changes: 11 additions & 0 deletions src/main/templates/io/lettuce/core/api/RedisAclCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public interface RedisAclCommands<K, V> {
*/
Long aclDeluser(String... usernames);

/**
* Simulate the execution of a given command by a given user.
*
* @param username the specified username
* @param command the specified command
* @param args the specified args of command
* @return String reply: OK on success.
* @since 6.2
*/
String aclDryRun(String username, String command, V... args);

/**
* The command generates a password.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ void aclDeluser() {
assertThat(redis.aclDeluser("non-existing")).isZero();
}

@Test
@EnabledOnCommand("EVAL_RO") // Redis 7.0
void aclDryRun() {
assertThatThrownBy(() -> redis.aclDryRun("non-existing", "GET", "foo", "bar"))
.isInstanceOf(RedisCommandExecutionException.class).hasMessageContaining("ERR User 'non-existing' not found");
assertThat(redis.aclDryRun("default", "GET", "foo", "bar")).isEqualTo("OK");
}

@Test
void aclGenpass() {
assertThat(redis.aclGenpass()).hasSize(64);
Expand Down

0 comments on commit 6e5018b

Please sign in to comment.