Skip to content

Commit

Permalink
Address ACL V2 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Jan 25, 2022
1 parent 427a5e4 commit fe4ea7b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 26 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.setKeys(SafeEncoder.encode((byte[]) objectList.get(7)));

// channels
accessControlUser.setChannels(SafeEncoder.encode((byte[]) objectList.get(9)));
}

// selectors
// TODO: Proper implementation of ACL V2.
return accessControlUser;
}

Expand Down
63 changes: 55 additions & 8 deletions src/main/java/redis/clients/jedis/resps/AccessControlUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
public class AccessControlUser {

private final List<String> flags = new ArrayList<>();
private final List<String> keys = new ArrayList<>();
private final List<String> keysList = new ArrayList<>();
private String keysString;
private final List<String> passwords = new ArrayList<>();
private final List<String> channels = new ArrayList<>();
private final List<String> channelsList = new ArrayList<>();
private String channelsString;
private String commands;

public AccessControlUser() {
Expand All @@ -23,11 +25,33 @@ public List<String> getFlags() {
}

public void addKey(String key) {
keys.add(key);
keysList.add(key);
}

/**
* @deprecated Use {@link AccessControlUser#getKeysList()}.
*/
@Deprecated
public List<String> getKeys() {
return keys;
return keysList;
}

/**
* For Redis version below 7.
*/
public List<String> getKeysList() {
return keysList;
}

/**
* For Redis version from 7.
*/
public String getKeysString() {
return keysString;
}

public void setKeys(String keys) {
this.keysString = keys;
}

public void addPassword(String password) {
Expand All @@ -39,11 +63,33 @@ public List<String> getPassword() {
}

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

/**
* @deprecated Use {@link AccessControlUser#getChannelsList()}.
*/
@Deprecated
public List<String> getChannels() {
return channels;
return channelsList;
}

/**
* For Redis version below 7.
*/
public List<String> getChannelsList() {
return channelsList;
}

/**
* For Redis version from 7.
*/
public String getChannelsString() {
return channelsString;
}

public void setChannels(String channels) {
this.channelsString = channels;
}

public String getCommands() {
Expand All @@ -56,7 +102,8 @@ 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='" + (keysString != null ? keysString : keysList)
+ "', channels='" + (channelsString != null ? channelsString : channelsList) + "'}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ 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));
assertEquals("~*", userInfo.getKeysString());

// 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 +463,8 @@ 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));
assertEquals("&testchannel:*", userInfo.getChannelsString());

jedis.aclDelUser(USER_ZZZ.getBytes());

Expand Down

0 comments on commit fe4ea7b

Please sign in to comment.