From 694b46516a5650f15efdd1fd34cfa8b8c4e3f068 Mon Sep 17 00:00:00 2001 From: Josan Ruslan Date: Tue, 8 Feb 2022 11:43:56 +0300 Subject: [PATCH] add possibility to use password protected redis servers (#5) Co-authored-by: Ruslan Zhosan --- src/RedisConfig.php | 10 +++++++ src/RedisLocator.php | 14 +++++++++- src/WithPasswordClientFactoryDecorator.php | 32 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/WithPasswordClientFactoryDecorator.php diff --git a/src/RedisConfig.php b/src/RedisConfig.php index 012bcc9..b861a30 100644 --- a/src/RedisConfig.php +++ b/src/RedisConfig.php @@ -21,6 +21,9 @@ final class RedisConfig /** @var string|null */ private $connectionName; + /** @var string|null */ + private $password; + public function __construct(array $options = []) { $options = array_merge([ @@ -29,6 +32,7 @@ public function __construct(array $options = []) 'protocol' => 'tcp', 'dbIndex' => 0, 'connectionName' => null, + 'password' => null, ], $options); $this->host = $options['host']; @@ -36,6 +40,7 @@ public function __construct(array $options = []) $this->protocol = $options['protocol']; $this->dbIndex = (int) $options['dbIndex']; $this->connectionName = $options['connectionName']; + $this->password = $options['password']; } public function getHost(): string @@ -62,4 +67,9 @@ public function getConnectionName(): ?string { return $this->connectionName; } + + public function getPassword(): ?string + { + return $this->password; + } } diff --git a/src/RedisLocator.php b/src/RedisLocator.php index 08fd1c9..4efaca7 100644 --- a/src/RedisLocator.php +++ b/src/RedisLocator.php @@ -5,6 +5,8 @@ namespace Lamoda\RedisSentinel; use PSRedis\Client; +use PSRedis\Client\Adapter\Predis\PredisClientCreator; +use PSRedis\Client\Adapter\PredisClientAdapter; use PSRedis\MasterDiscovery; class RedisLocator @@ -59,10 +61,20 @@ private function initMasterDiscovery(): ?MasterDiscovery } $host = $url['host'] ?? $url['path']; $port = $url['port'] ?? 26379; - $sentinel = new Client($host, $port); + + $redisClientAdapter = $this->getRedisClientAdapter(); + $sentinel = new Client($host, $port, $redisClientAdapter); + $masterDiscovery->addSentinel($sentinel); } return $masterDiscovery; } + + private function getRedisClientAdapter(): PredisClientAdapter + { + $clientFactory = new WithPasswordClientFactoryDecorator($this->redisConfig, new PredisClientCreator()); + + return new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); + } } diff --git a/src/WithPasswordClientFactoryDecorator.php b/src/WithPasswordClientFactoryDecorator.php new file mode 100644 index 0000000..6b3f045 --- /dev/null +++ b/src/WithPasswordClientFactoryDecorator.php @@ -0,0 +1,32 @@ +config = $config; + $this->decorated = $decorated; + } + + public function createClient($clientType, array $parameters = []): \Predis\Client + { + if ($clientType === Client::TYPE_REDIS && isset($this->config['password'])) { + $parameters['password'] = $this->config['password']; + } + + return $this->decorated->createClient($clientType, $parameters); + } +}