Skip to content

Commit

Permalink
ACL patterns for Pub/Sub channels (#2750)
Browse files Browse the repository at this point in the history
* ACL patterns for Pub/Sub channels

* ACL patterns for Pub/Sub channels

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
  • Loading branch information
zeekling and sazzad16 committed Jan 19, 2022
1 parent cb11ac1 commit dc6a891
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/BuilderFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,7 @@ public String toString() {
* Create a AccessControlUser object from the ACL GETUSER < > result
*/
public static final Builder<AccessControlUser> ACCESS_CONTROL_USER = new Builder<AccessControlUser>() {
@SuppressWarnings("unchecked")
@Override
public AccessControlUser build(Object data) {
if (data == null) {
Expand Down Expand Up @@ -726,6 +727,14 @@ public AccessControlUser build(Object data) {
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));
}
}

return accessControlUser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public interface AccessControlLogBinaryCommands {
*/
List<byte[]> aclUsersBinary();

/**
* The command returns all the rules defined for an existing ACL user.
* @param name username
* @return a list of ACL rule definitions for the user.
*/
AccessControlUser aclGetUser(byte[] name);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public interface AccessControlLogCommands {
*/
List<String> aclUsers();

/**
* The command returns all the rules defined for an existing ACL user.
* @param name username
* @return a list of ACL rule definitions for the user.
*/
AccessControlUser aclGetUser(String name);

/**
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/redis/clients/jedis/resps/AccessControlUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AccessControlUser {
private final List<String> flags = new ArrayList<>();
private final List<String> keys = new ArrayList<>();
private final List<String> passwords = new ArrayList<>();
private final List<String> channels = new ArrayList<>();
private String commands;

public AccessControlUser() {
Expand Down Expand Up @@ -37,6 +38,14 @@ public List<String> getPassword() {
return passwords;
}

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

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

public String getCommands() {
return commands;
}
Expand All @@ -48,6 +57,6 @@ public void setCommands(String commands) {
@Override
public String toString() {
return "AccessControlUser{" + "flags=" + flags + ", keys=" + keys + ", passwords=" + passwords
+ ", commands='" + commands + '\'' + '}';
+ ", commands='" + commands + ", channels='" + channels + '\'' + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,15 @@ public void aclBinaryCommandsTest() {
assertEquals(1L, jedis.aclDelUser(USER_ZZZ.getBytes()));

jedis.aclSetUser(USER_ZZZ.getBytes(), "reset".getBytes(), "+@all".getBytes(), "~*".getBytes(),
"-@string".getBytes(), "+incr".getBytes(), "-debug".getBytes(), "+debug|digest".getBytes());
"-@string".getBytes(), "+incr".getBytes(), "-debug".getBytes(), "+debug|digest".getBytes(),
"resetchannels".getBytes(), "&testchannel:*".getBytes());

AccessControlUser userInfo = jedis.aclGetUser(USER_ZZZ.getBytes());

assertThat(userInfo.getCommands(), containsString("+@all"));
assertThat(userInfo.getCommands(), containsString("-@string"));
assertThat(userInfo.getCommands(), containsString("+debug|digest"));
assertEquals("testchannel:*", userInfo.getChannels().get(0));

jedis.aclDelUser(USER_ZZZ.getBytes());

Expand Down

0 comments on commit dc6a891

Please sign in to comment.