|
12 | 12 | import java.util.ArrayList;
|
13 | 13 | import java.util.Arrays;
|
14 | 14 | import java.util.List;
|
| 15 | +import java.util.Map; |
15 | 16 | import java.util.function.Supplier;
|
16 | 17 |
|
17 | 18 | import redis.clients.jedis.Protocol.Command;
|
@@ -41,6 +42,8 @@ public class Connection implements Closeable {
|
41 | 42 | private boolean broken = false;
|
42 | 43 | private boolean strValActive;
|
43 | 44 | private String strVal;
|
| 45 | + protected String server; |
| 46 | + protected String version; |
44 | 47 |
|
45 | 48 | public Connection() {
|
46 | 49 | this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT);
|
@@ -453,12 +456,12 @@ protected void initializeFromClientConfig(final JedisClientConfig config) {
|
453 | 456 | final RedisCredentialsProvider redisCredentialsProvider = (RedisCredentialsProvider) credentialsProvider;
|
454 | 457 | try {
|
455 | 458 | redisCredentialsProvider.prepare();
|
456 |
| - helloOrAuth(protocol, redisCredentialsProvider.get()); |
| 459 | + helloAndAuth(protocol, redisCredentialsProvider.get()); |
457 | 460 | } finally {
|
458 | 461 | redisCredentialsProvider.cleanUp();
|
459 | 462 | }
|
460 | 463 | } else {
|
461 |
| - helloOrAuth(protocol, credentialsProvider != null ? credentialsProvider.get() |
| 464 | + helloAndAuth(protocol, credentialsProvider != null ? credentialsProvider.get() |
462 | 465 | : new DefaultRedisCredentials(config.getUser(), config.getPassword()));
|
463 | 466 | }
|
464 | 467 |
|
@@ -517,50 +520,56 @@ protected void initializeFromClientConfig(final JedisClientConfig config) {
|
517 | 520 | }
|
518 | 521 | }
|
519 | 522 |
|
520 |
| - private void helloOrAuth(final RedisProtocol protocol, final RedisCredentials credentials) { |
521 |
| - |
522 |
| - if (credentials == null || credentials.getPassword() == null) { |
523 |
| - if (protocol != null) { |
524 |
| - sendCommand(Command.HELLO, encode(protocol.version())); |
525 |
| - getOne(); |
| 523 | + private void helloAndAuth(final RedisProtocol protocol, final RedisCredentials credentials) { |
| 524 | + Map<String, Object> helloResult = null; |
| 525 | + if (protocol != null && credentials != null && credentials.getUser() != null) { |
| 526 | + byte[] rawPass = encodeToBytes(credentials.getPassword()); |
| 527 | + try { |
| 528 | + helloResult = hello(encode(protocol.version()), Keyword.AUTH.getRaw(), encode(credentials.getUser()), rawPass); |
| 529 | + } finally { |
| 530 | + Arrays.fill(rawPass, (byte) 0); // clear sensitive data |
526 | 531 | }
|
527 |
| - return; |
| 532 | + } else { |
| 533 | + auth(credentials); |
| 534 | + helloResult = protocol == null ? null : hello(encode(protocol.version())); |
| 535 | + } |
| 536 | + if (helloResult != null) { |
| 537 | + server = (String) helloResult.get("server"); |
| 538 | + version = (String) helloResult.get("version"); |
528 | 539 | }
|
529 | 540 |
|
530 |
| - // Source: https://stackoverflow.com/a/9670279/4021802 |
531 |
| - ByteBuffer passBuf = Protocol.CHARSET.encode(CharBuffer.wrap(credentials.getPassword())); |
532 |
| - byte[] rawPass = Arrays.copyOfRange(passBuf.array(), passBuf.position(), passBuf.limit()); |
533 |
| - Arrays.fill(passBuf.array(), (byte) 0); // clear sensitive data |
| 541 | + // clearing 'char[] credentials.getPassword()' should be |
| 542 | + // handled in RedisCredentialsProvider.cleanUp() |
| 543 | + } |
534 | 544 |
|
| 545 | + private void auth(RedisCredentials credentials) { |
| 546 | + if (credentials == null || credentials.getPassword() == null) { |
| 547 | + return; |
| 548 | + } |
| 549 | + byte[] rawPass = encodeToBytes(credentials.getPassword()); |
535 | 550 | try {
|
536 |
| - /// actual HELLO or AUTH --> |
537 |
| - if (protocol != null) { |
538 |
| - if (credentials.getUser() != null) { |
539 |
| - sendCommand(Command.HELLO, encode(protocol.version()), |
540 |
| - Keyword.AUTH.getRaw(), encode(credentials.getUser()), rawPass); |
541 |
| - getOne(); // Map |
542 |
| - } else { |
543 |
| - sendCommand(Command.AUTH, rawPass); |
544 |
| - getStatusCodeReply(); // OK |
545 |
| - sendCommand(Command.HELLO, encode(protocol.version())); |
546 |
| - getOne(); // Map |
547 |
| - } |
548 |
| - } else { // protocol == null |
549 |
| - if (credentials.getUser() != null) { |
550 |
| - sendCommand(Command.AUTH, encode(credentials.getUser()), rawPass); |
551 |
| - } else { |
552 |
| - sendCommand(Command.AUTH, rawPass); |
553 |
| - } |
554 |
| - getStatusCodeReply(); // OK |
| 551 | + if (credentials.getUser() == null) { |
| 552 | + sendCommand(Command.AUTH, rawPass); |
| 553 | + } else { |
| 554 | + sendCommand(Command.AUTH, encode(credentials.getUser()), rawPass); |
555 | 555 | }
|
556 |
| - /// <-- actual HELLO or AUTH |
557 | 556 | } finally {
|
558 |
| - |
559 | 557 | Arrays.fill(rawPass, (byte) 0); // clear sensitive data
|
560 | 558 | }
|
| 559 | + getStatusCodeReply(); |
| 560 | + } |
561 | 561 |
|
562 |
| - // clearing 'char[] credentials.getPassword()' should be |
563 |
| - // handled in RedisCredentialsProvider.cleanUp() |
| 562 | + protected Map<String, Object> hello(byte[]... args) { |
| 563 | + sendCommand(Command.HELLO, args); |
| 564 | + return BuilderFactory.ENCODED_OBJECT_MAP.build(getOne()); |
| 565 | + } |
| 566 | + |
| 567 | + protected byte[] encodeToBytes(char[] chars) { |
| 568 | + // Source: https://stackoverflow.com/a/9670279/4021802 |
| 569 | + ByteBuffer passBuf = Protocol.CHARSET.encode(CharBuffer.wrap(chars)); |
| 570 | + byte[] rawPass = Arrays.copyOfRange(passBuf.array(), passBuf.position(), passBuf.limit()); |
| 571 | + Arrays.fill(passBuf.array(), (byte) 0); // clear sensitive data |
| 572 | + return rawPass; |
564 | 573 | }
|
565 | 574 |
|
566 | 575 | public String select(final int index) {
|
|
0 commit comments