From 5a6bf504f3e7223173ed8d7ec7ded8013da8a494 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 23 Sep 2021 19:00:49 +0200 Subject: [PATCH] [Messenger] [Redis] Allow authentication with user and password --- .../Tests/Transport/RedisExt/ConnectionTest.php | 17 ++++++++++++++--- .../Messenger/Transport/RedisExt/Connection.php | 3 ++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php index 3aa59d69eaeee..bf7ad591e454d 100644 --- a/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php +++ b/src/Symfony/Component/Messenger/Tests/Transport/RedisExt/ConnectionTest.php @@ -113,15 +113,26 @@ public function testKeepGettingPendingMessages() $this->assertNotNull($connection->get()); } - public function testAuth() + /** + * @param string|array $expected + * + * @dataProvider provideAuthDsn + */ + public function testAuth($expected, string $dsn) { $redis = $this->createMock(\Redis::class); $redis->expects($this->exactly(1))->method('auth') - ->with('password') + ->with($expected) ->willReturn(true); - Connection::fromDsn('redis://password@localhost/queue', [], $redis); + Connection::fromDsn($dsn, [], $redis); + } + + public function provideAuthDsn(): \Generator + { + yield 'Password only' => ['password', 'redis://password@localhost/queue']; + yield 'User and password' => [['user', 'password'], 'redis://user:password@localhost/queue']; } public function testNoAuthWithEmptyPassword() diff --git a/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php b/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php index e7c549ecf49f8..88b1a7e5a5f28 100644 --- a/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php +++ b/src/Symfony/Component/Messenger/Transport/RedisExt/Connection.php @@ -97,7 +97,8 @@ public static function fromDsn(string $dsn, array $redisOptions = [], \Redis $re $connectionCredentials = [ 'host' => $parsedUrl['host'] ?? '127.0.0.1', 'port' => $parsedUrl['port'] ?? 6379, - 'auth' => $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null, + // See: https://github.com/phpredis/phpredis/#auth + 'auth' => isset($parsedUrl['pass']) && isset($parsedUrl['user']) ? [$parsedUrl['user'], $parsedUrl['pass']] : $parsedUrl['pass'] ?? $parsedUrl['user'] ?? null, ]; if (isset($parsedUrl['query'])) {