From 1a451e4b888e2fa58be889b7c3a03655946aa5bb Mon Sep 17 00:00:00 2001 From: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com> Date: Wed, 26 Jan 2022 10:49:09 +0600 Subject: [PATCH] Address ACL V2 changes (#2841) * Address ACL V2 changes * Address ACL V2 changes (approach 2) --- .../redis/clients/jedis/BuilderFactory.java | 45 +++++++++++++------ .../jedis/resps/AccessControlUser.java | 19 ++++++-- .../jedis/AccessControlListCommandsTest.java | 8 ++-- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/main/java/redis/clients/jedis/BuilderFactory.java b/src/main/java/redis/clients/jedis/BuilderFactory.java index c5ce38eaf3..afa6032a19 100644 --- a/src/main/java/redis/clients/jedis/BuilderFactory.java +++ b/src/main/java/redis/clients/jedis/BuilderFactory.java @@ -699,7 +699,7 @@ public AccessControlUser build(Object data) { return null; } - List> objectList = (List>) data; + List objectList = (List) data; if (objectList.isEmpty()) { return null; } @@ -707,34 +707,51 @@ public AccessControlUser build(Object data) { AccessControlUser accessControlUser = new AccessControlUser(); // flags - List flags = objectList.get(1); + List flags = (List) objectList.get(1); for (Object f : flags) { accessControlUser.addFlag(SafeEncoder.encode((byte[]) f)); } // passwords - List passwords = objectList.get(3); + List passwords = (List) objectList.get(3); for (Object p : passwords) { accessControlUser.addPassword(SafeEncoder.encode((byte[]) p)); } // commands - accessControlUser.setCommands(SafeEncoder.encode((byte[]) (Object) objectList.get(5))); + accessControlUser.setCommands(SafeEncoder.encode((byte[]) objectList.get(5))); - // keys - List keys = objectList.get(7); - for (Object k : keys) { - accessControlUser.addKey(SafeEncoder.encode((byte[]) k)); - } + // Redis 7 --> + boolean withSelectors = objectList.size() >= 12; + if (!withSelectors) { + + // keys + List keys = (List) objectList.get(7); + for (Object k : keys) { + accessControlUser.addKey(SafeEncoder.encode((byte[]) k)); + } - // before redis 6.2, no channels info - if (objectList.size() > 9) { - List channels = objectList.get(9); - for (Object channel : channels) { - accessControlUser.addChannel(SafeEncoder.encode((byte[]) channel)); + // Redis 6.2 --> + // channels + if (objectList.size() >= 10) { + List channels = (List) objectList.get(9); + for (Object channel : channels) { + accessControlUser.addChannel(SafeEncoder.encode((byte[]) channel)); + } } + + } else { + // TODO: Proper implementation of ACL V2. + + // keys + accessControlUser.addKeys(SafeEncoder.encode((byte[]) objectList.get(7))); + + // channels + accessControlUser.addChannels(SafeEncoder.encode((byte[]) objectList.get(9))); } + // selectors + // TODO: Proper implementation of ACL V2. return accessControlUser; } diff --git a/src/main/java/redis/clients/jedis/resps/AccessControlUser.java b/src/main/java/redis/clients/jedis/resps/AccessControlUser.java index 129d3104a8..6013a8f760 100644 --- a/src/main/java/redis/clients/jedis/resps/AccessControlUser.java +++ b/src/main/java/redis/clients/jedis/resps/AccessControlUser.java @@ -1,6 +1,7 @@ package redis.clients.jedis.resps; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class AccessControlUser { @@ -30,6 +31,12 @@ public List getKeys() { return keys; } + public void addKeys(String keys) { + if (!keys.isEmpty()) { + this.keys.addAll(Arrays.asList(keys.split(" "))); + } + } + public void addPassword(String password) { passwords.add(password); } @@ -39,13 +46,19 @@ public List getPassword() { } public void addChannel(String channel) { - channels.add(channel); + channels.add(channel); } public List getChannels() { return channels; } + public void addChannels(String channels) { + if (!channels.isEmpty()) { + this.channels.addAll(Arrays.asList(channels.split(" "))); + } + } + public String getCommands() { return commands; } @@ -56,7 +69,7 @@ public void setCommands(String commands) { @Override public String toString() { - return "AccessControlUser{" + "flags=" + flags + ", keys=" + keys + ", passwords=" + passwords - + ", commands='" + commands + ", channels='" + channels + '\'' + '}'; + return "AccessControlUser{" + "flags=" + flags + ", passwords=" + passwords + + ", commands='" + commands + "', keys='" + keys + "', channels='" + channels + "'}"; } } diff --git a/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java b/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java index c5d04e0143..cd4dd1197e 100644 --- a/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/commands/jedis/AccessControlListCommandsTest.java @@ -75,16 +75,16 @@ public void aclGetUser() { // get default user information AccessControlUser userInfo = jedis.aclGetUser("default"); - assertEquals(4, userInfo.getFlags().size()); + assertEquals(1, userInfo.getFlags().size()); assertEquals(1, userInfo.getPassword().size()); assertEquals("+@all", userInfo.getCommands()); - assertEquals("*", userInfo.getKeys().get(0)); + assertEquals("~*", userInfo.getKeys().get(0)); // create new user jedis.aclDelUser(USER_ZZZ); jedis.aclSetUser(USER_ZZZ); userInfo = jedis.aclGetUser(USER_ZZZ); - assertEquals(2, userInfo.getFlags().size()); + assertEquals(1, userInfo.getFlags().size()); assertEquals("off", userInfo.getFlags().get(0)); assertTrue(userInfo.getPassword().isEmpty()); assertTrue(userInfo.getKeys().isEmpty()); @@ -462,7 +462,7 @@ public void aclBinaryCommandsTest() { assertThat(userInfo.getCommands(), containsString("+@all")); assertThat(userInfo.getCommands(), containsString("-@string")); assertThat(userInfo.getCommands(), containsString("+debug|digest")); - assertEquals("testchannel:*", userInfo.getChannels().get(0)); + assertEquals("&testchannel:*", userInfo.getChannels().get(0)); jedis.aclDelUser(USER_ZZZ.getBytes());