Skip to content

Commit

Permalink
Add support AUTH2 argument to MIGRATE (#2430)
Browse files Browse the repository at this point in the history
* Add support AUTH2 argument to MIGRATE

* fix review

* fix test

* Update migrateAuth2

Co-authored-by: M Sazzadul Hoque <7600764+sazzad16@users.noreply.github.com>
  • Loading branch information
dengliming and sazzad16 authored Mar 14, 2021
1 parent dbfc929 commit 99e9836
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/redis/clients/jedis/params/MigrateParams.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package redis.clients.jedis.params;

import redis.clients.jedis.util.SafeEncoder;

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

public class MigrateParams extends Params {

private static final String COPY = "COPY";
private static final String REPLACE = "REPLACE";
private static final String AUTH = "AUTH";
private static final String AUTH2 = "AUTH2";

public MigrateParams() {
}
Expand All @@ -27,4 +33,32 @@ public MigrateParams auth(String password) {
addParam(AUTH, password);
return this;
}

public MigrateParams auth2(String username, String password) {
addParam(AUTH2, new String[] { username, password });
return this;
}

@Override
public byte[][] getByteParams() {
List<byte[]> byteParams = new ArrayList<>();

if (contains(COPY)) {
byteParams.add(SafeEncoder.encode(COPY));
}
if (contains(REPLACE)) {
byteParams.add(SafeEncoder.encode(REPLACE));
}
if (contains(AUTH)) {
byteParams.add(SafeEncoder.encode(AUTH));
byteParams.add(SafeEncoder.encode((String) getParam(AUTH)));
} else if (contains(AUTH2)) {
byteParams.add(SafeEncoder.encode(AUTH2));
String[] nameAndPass = (String[]) getParam(AUTH2);
byteParams.add(SafeEncoder.encode(nameAndPass[0]));
byteParams.add(SafeEncoder.encode(nameAndPass[1]));
}

return byteParams.toArray(new byte[byteParams.size()][]);
}
}
16 changes: 16 additions & 0 deletions src/test/java/redis/clients/jedis/tests/commands/MigrateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ public void migrateAuth() {
assertNull(jedis.get(bfoo));
}

@Test
public void migrateAuth2() {
destAuth.set("foo", "bar");
assertEquals("OK", destAuth.migrate(host, hnp.getPort(), 0, timeout,
new MigrateParams().auth2("acljedis", "fizzbuzz"), "foo"));
assertEquals("bar", jedis.get("foo"));
assertNull(destAuth.get("foo"));

// binary
dest.set(bfoo1, bbar1);
assertEquals("OK", dest.migrate(host, hnp.getPort(), 0, timeout,
new MigrateParams().auth2("acljedis", "fizzbuzz"), bfoo1));
assertArrayEquals(bbar1, jedis.get(bfoo1));
assertNull(dest.get(bfoo1));
}

@Test
public void migrateCopyReplaceAuth() {
jedis.set("foo", "bar1");
Expand Down

0 comments on commit 99e9836

Please sign in to comment.