Skip to content

Commit

Permalink
Address ACL V2 changes (#2841)
Browse files Browse the repository at this point in the history
* Address ACL V2 changes

* Address ACL V2 changes (approach 2)
  • Loading branch information
sazzad16 authored Jan 26, 2022
1 parent c27794f commit 1a451e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
45 changes: 31 additions & 14 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,42 +699,59 @@ public AccessControlUser build(Object data) {
return null;
}

List<List<Object>> objectList = (List<List<Object>>) data;
List<Object> objectList = (List<Object>) data;
if (objectList.isEmpty()) {
return null;
}

AccessControlUser accessControlUser = new AccessControlUser();

// flags
List<Object> flags = objectList.get(1);
List<Object> flags = (List<Object>) objectList.get(1);
for (Object f : flags) {
accessControlUser.addFlag(SafeEncoder.encode((byte[]) f));
}

// passwords
List<Object> passwords = objectList.get(3);
List<Object> passwords = (List<Object>) 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<Object> 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<Object> keys = (List<Object>) 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<Object> channels = objectList.get(9);
for (Object channel : channels) {
accessControlUser.addChannel(SafeEncoder.encode((byte[]) channel));
// Redis 6.2 -->
// channels
if (objectList.size() >= 10) {
List<Object> channels = (List<Object>) 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;
}

Expand Down
19 changes: 16 additions & 3 deletions src/main/java/redis/clients/jedis/resps/AccessControlUser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis.clients.jedis.resps;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AccessControlUser {
Expand Down Expand Up @@ -30,6 +31,12 @@ public List<String> 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);
}
Expand All @@ -39,13 +46,19 @@ public List<String> getPassword() {
}

public void addChannel(String channel) {
channels.add(channel);
channels.add(channel);
}

public List<String> getChannels() {
return channels;
}

public void addChannels(String channels) {
if (!channels.isEmpty()) {
this.channels.addAll(Arrays.asList(channels.split(" ")));
}
}

public String getCommands() {
return commands;
}
Expand All @@ -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 + "'}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 1a451e4

Please sign in to comment.