From 89ff2610fb19bc5d4536a58753fd831b6522fdd3 Mon Sep 17 00:00:00 2001 From: Robin Brabants Date: Wed, 20 Nov 2024 21:37:57 +0100 Subject: [PATCH] refactor getting authentication details for the Redis cluster Signed-off-by: Robin Brabants --- ...alidRedisClusterConfigurationException.php | 5 +++++ src/RedisClusterOptions.php | 22 +++++-------------- src/RedisClusterResourceManager.php | 20 ++++++++++++++--- test/unit/RedisClusterOptionsTest.php | 6 ++--- 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/Exception/InvalidRedisClusterConfigurationException.php b/src/Exception/InvalidRedisClusterConfigurationException.php index 735093c..3909523 100644 --- a/src/Exception/InvalidRedisClusterConfigurationException.php +++ b/src/Exception/InvalidRedisClusterConfigurationException.php @@ -42,4 +42,9 @@ public static function fromInvalidSeedsConfiguration(string $seed): self ) ); } + + public static function fromMissingRequiredPassword(): self + { + return new self('If a Redis user is provided, a password has to be configured as well.'); + } } diff --git a/src/RedisClusterOptions.php b/src/RedisClusterOptions.php index 683c683..0d09fe7 100644 --- a/src/RedisClusterOptions.php +++ b/src/RedisClusterOptions.php @@ -64,7 +64,7 @@ final class RedisClusterOptions extends AdapterOptions /** @psalm-var array */ private array $libOptions = []; - private ?string $username = null; + private string $user = ''; private string $password = ''; @@ -250,17 +250,17 @@ public function getLibOption(int $option, mixed $default = null): mixed return $this->libOptions[$option] ?? $default; } - public function getUsername(): ?string + public function getUser(): string { - return $this->username; + return $this->user; } /** * @psalm-param non-empty-string $username */ - public function setUsername(string $username): void + public function setUser(string $user): void { - $this->username = $username; + $this->user = $user; } public function getPassword(): string @@ -276,18 +276,6 @@ public function setPassword(string $password): void $this->password = $password; } - /** - * @return array|string|null - */ - public function getAuthentication(): array|string|null - { - $password = $this->password === '' ? null : $this->password; - if ($this->username !== null && $this->username !== '') { - return [$this->username, $password]; - } - return $password; - } - public function getSslContext(): ?SslContext { return $this->sslContext; diff --git a/src/RedisClusterResourceManager.php b/src/RedisClusterResourceManager.php index 7b5114f..bea3857 100644 --- a/src/RedisClusterResourceManager.php +++ b/src/RedisClusterResourceManager.php @@ -48,13 +48,26 @@ public function getResource(): RedisClusterFromExtension private function createRedisResource(RedisClusterOptions $options): RedisClusterFromExtension { + $user = $options->getUser(); + $password = $options->getPassword(); + + if ($user !== '' && $password !== '') { + $authentication = [$user, $password]; + } elseif ($user !== '' && $password === '') { + throw InvalidRedisClusterConfigurationException::fromMissingRequiredPassword(); + } elseif ($user === '' && $password !== '') { + $authentication = [$password]; + } else { + $authentication = null; + } + if ($options->hasName()) { return $this->createRedisResourceFromName( $options->getName(), $options->getTimeout(), $options->getReadTimeout(), $options->isPersistent(), - $options->getAuthentication(), + $authentication, $options->getSslContext() ); } @@ -72,20 +85,21 @@ private function createRedisResource(RedisClusterOptions $options): RedisCluster $options->getTimeout(), $options->getReadTimeout(), $options->isPersistent(), - $options->getAuthentication(), + $authentication, $options->getSslContext()?->toSslContextArray() ); } /** * @psalm-param non-empty-string $name + * @psalm-param array{0: non-empty-string, 1?: non-empty-string}|null $fallbackAuthentication */ private function createRedisResourceFromName( string $name, float $fallbackTimeout, float $fallbackReadTimeout, bool $persistent, - array|string|null $fallbackAuthentication, + array|null $fallbackAuthentication, ?SslContext $sslContext ): RedisClusterFromExtension { try { diff --git a/test/unit/RedisClusterOptionsTest.php b/test/unit/RedisClusterOptionsTest.php index c884956..62699e5 100644 --- a/test/unit/RedisClusterOptionsTest.php +++ b/test/unit/RedisClusterOptionsTest.php @@ -72,7 +72,7 @@ public function testCanHandleOptionsWithNodename(): void self::assertEquals(2.0, $options->getReadTimeout()); self::assertFalse($options->isPersistent()); self::assertEquals('1.0', $options->getRedisVersion()); - self::assertNull($options->getUsername()); + self::assertEmpty($options->getUser()); self::assertEquals('secret', $options->getPassword()); } @@ -84,7 +84,7 @@ public function testCanHandleOptionsWithSeeds(): void 'read_timeout' => 2.0, 'persistent' => false, 'redis_version' => '1.0', - 'username' => 'user', + 'user' => 'user', ]); self::assertEquals(['localhost:1234'], $options->getSeeds()); @@ -92,7 +92,7 @@ public function testCanHandleOptionsWithSeeds(): void self::assertEquals(2.0, $options->getReadTimeout()); self::assertFalse($options->isPersistent()); self::assertEquals('1.0', $options->getRedisVersion()); - self::assertEquals('user', $options->getUsername()); + self::assertEquals('user', $options->getUser()); self::assertEquals('', $options->getPassword()); }