Skip to content

Commit 2c1acdd

Browse files
authored
Merge ca48100 into 2c609cf
2 parents 2c609cf + ca48100 commit 2c1acdd

File tree

1 file changed

+57
-67
lines changed

1 file changed

+57
-67
lines changed

src/main/java/redis/clients/jedis/Connection.java

Lines changed: 57 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package redis.clients.jedis;
22

3+
import static redis.clients.jedis.util.SafeEncoder.encode;
4+
35
import java.io.Closeable;
46
import java.io.IOException;
57
import java.net.Socket;
@@ -9,7 +11,6 @@
911
import java.util.ArrayList;
1012
import java.util.Arrays;
1113
import java.util.List;
12-
import java.util.Map;
1314
import java.util.function.Supplier;
1415

1516
import redis.clients.jedis.Protocol.Command;
@@ -25,7 +26,6 @@
2526
import redis.clients.jedis.util.JedisMetaInfo;
2627
import redis.clients.jedis.util.RedisInputStream;
2728
import redis.clients.jedis.util.RedisOutputStream;
28-
import redis.clients.jedis.util.SafeEncoder;
2929

3030
public class Connection implements Closeable {
3131

@@ -270,14 +270,14 @@ public String getStatusCodeReply() {
270270
if (null == resp) {
271271
return null;
272272
} else {
273-
return SafeEncoder.encode(resp);
273+
return encode(resp);
274274
}
275275
}
276276

277277
public String getBulkReply() {
278278
final byte[] result = getBinaryBulkReply();
279279
if (null != result) {
280-
return SafeEncoder.encode(result);
280+
return encode(result);
281281
} else {
282282
return null;
283283
}
@@ -391,44 +391,27 @@ private static boolean validateClientInfo(String info) {
391391
private void initializeFromClientConfig(JedisClientConfig config) {
392392
try {
393393
connect();
394-
protocol = config.getRedisProtocol();
395-
396-
boolean doClientName = true;
397-
398-
/// HELLO and AUTH -->
399-
if (protocol == RedisProtocol.RESP3 && config.getUser() != null) {
400-
401-
hello(protocol, config.getUser(), config.getPassword(), config.getClientName());
402-
doClientName = false;
403394

404-
} else {
405-
406-
Supplier<RedisCredentials> credentialsProvider = config.getCredentialsProvider();
407-
if (credentialsProvider instanceof RedisCredentialsProvider) {
408-
try {
409-
((RedisCredentialsProvider) credentialsProvider).prepare();
410-
auth(credentialsProvider);
411-
} finally {
412-
((RedisCredentialsProvider) credentialsProvider).cleanUp();
413-
}
414-
} else {
415-
auth(credentialsProvider);
416-
}
395+
protocol = config.getRedisProtocol();
417396

418-
if (protocol != null) {
419-
hello(protocol);
397+
final Supplier<RedisCredentials> credentialsProvider = config.getCredentialsProvider();
398+
if (credentialsProvider instanceof RedisCredentialsProvider) {
399+
final RedisCredentialsProvider redisCredentialsProvider = (RedisCredentialsProvider) credentialsProvider;
400+
try {
401+
redisCredentialsProvider.prepare();
402+
helloOrAuth(protocol, redisCredentialsProvider.get());
403+
} finally {
404+
redisCredentialsProvider.cleanUp();
420405
}
421-
}
422-
423-
int dbIndex = config.getDatabase();
424-
if (dbIndex > 0) {
425-
select(dbIndex);
406+
} else {
407+
helloOrAuth(protocol, credentialsProvider != null ? credentialsProvider.get()
408+
: new DefaultRedisCredentials(config.getUser(), config.getPassword()));
426409
}
427410

428411
List<CommandArguments> fireAndForgetMsg = new ArrayList<>();
429412

430413
String clientName = config.getClientName();
431-
if (doClientName && clientName != null && validateClientInfo(clientName)) {
414+
if (clientName != null && validateClientInfo(clientName)) {
432415
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETNAME).add(clientName));
433416
}
434417

@@ -448,6 +431,12 @@ private void initializeFromClientConfig(JedisClientConfig config) {
448431
sendCommand(arg);
449432
}
450433
getMany(fireAndForgetMsg.size());
434+
435+
int dbIndex = config.getDatabase();
436+
if (dbIndex > 0) {
437+
select(dbIndex);
438+
}
439+
451440
} catch (JedisException je) {
452441
try {
453442
disconnect();
@@ -458,58 +447,59 @@ private void initializeFromClientConfig(JedisClientConfig config) {
458447
}
459448
}
460449

461-
private Map hello(final RedisProtocol protocol) {
462-
sendCommand(Protocol.Command.HELLO, String.valueOf(protocol.version()));
463-
Map reply = BuilderFactory.ENCODED_OBJECT_MAP.build(getOne());
464-
// LoggerFactory.getLogger(Connection.class).info("HELLO reply: {}", reply);
465-
return reply;
466-
}
450+
private void helloOrAuth(final RedisProtocol protocol, final RedisCredentials credentials) {
467451

468-
private Map hello(final RedisProtocol protocol, final String user, final String password,
469-
final String clientName) {
470-
if (clientName == null) {
471-
sendCommand(Protocol.Command.HELLO, String.valueOf(protocol.version()),
472-
Protocol.Keyword.AUTH.name(), user, password);
473-
} else {
474-
sendCommand(Protocol.Command.HELLO, String.valueOf(protocol.version()),
475-
Protocol.Keyword.AUTH.name(), user, password,
476-
Protocol.Keyword.SETNAME.name(), clientName);
452+
if (credentials == null || credentials.getPassword() == null) {
453+
if (protocol != null) {
454+
sendCommand(Command.HELLO, encode(protocol.version()));
455+
getOne();
456+
}
457+
return;
477458
}
478-
Map reply = BuilderFactory.ENCODED_OBJECT_MAP.build(getOne());
479-
// LoggerFactory.getLogger(Connection.class).info("HELLO reply: {}", reply);
480-
return reply;
481-
}
482-
483-
private void auth(final Supplier<RedisCredentials> credentialsProvider) {
484-
RedisCredentials credentials = credentialsProvider.get();
485-
if (credentials == null || credentials.getPassword() == null) return;
486459

487460
// Source: https://stackoverflow.com/a/9670279/4021802
488461
ByteBuffer passBuf = Protocol.CHARSET.encode(CharBuffer.wrap(credentials.getPassword()));
489462
byte[] rawPass = Arrays.copyOfRange(passBuf.array(), passBuf.position(), passBuf.limit());
490463
Arrays.fill(passBuf.array(), (byte) 0); // clear sensitive data
491464

492-
if (credentials.getUser() != null) {
493-
sendCommand(Protocol.Command.AUTH, SafeEncoder.encode(credentials.getUser()), rawPass);
494-
} else {
495-
sendCommand(Protocol.Command.AUTH, rawPass);
496-
}
465+
try {
466+
/// actual HELLO or AUTH -->
467+
if (protocol != null) {
468+
if (credentials.getUser() != null) {
469+
sendCommand(Command.HELLO, encode(protocol.version()),
470+
Keyword.AUTH.getRaw(), encode(credentials.getUser()), rawPass);
471+
getOne(); // Map
472+
} else {
473+
sendCommand(Command.AUTH, rawPass);
474+
getStatusCodeReply(); // OK
475+
sendCommand(Command.HELLO, encode(protocol.version()));
476+
getOne(); // Map
477+
}
478+
} else { // protocol == null
479+
if (credentials.getUser() != null) {
480+
sendCommand(Command.AUTH, encode(credentials.getUser()), rawPass);
481+
} else {
482+
sendCommand(Command.AUTH, rawPass);
483+
}
484+
getStatusCodeReply(); // OK
485+
}
486+
/// <-- actual HELLO or AUTH
487+
} finally {
497488

498-
Arrays.fill(rawPass, (byte) 0); // clear sensitive data
489+
Arrays.fill(rawPass, (byte) 0); // clear sensitive data
490+
}
499491

500492
// clearing 'char[] credentials.getPassword()' should be
501493
// handled in RedisCredentialsProvider.cleanUp()
502-
503-
getStatusCodeReply(); // OK
504494
}
505495

506496
public String select(final int index) {
507-
sendCommand(Protocol.Command.SELECT, Protocol.toByteArray(index));
497+
sendCommand(Command.SELECT, Protocol.toByteArray(index));
508498
return getStatusCodeReply();
509499
}
510500

511501
public boolean ping() {
512-
sendCommand(Protocol.Command.PING);
502+
sendCommand(Command.PING);
513503
String status = getStatusCodeReply();
514504
if (!"PONG".equals(status)) {
515505
throw new JedisException(status);

0 commit comments

Comments
 (0)