Skip to content

Commit 6f91478

Browse files
Add ability to auth with username + password to sentinel (#25)
* Add support for username + password auth * Lint * Refactor auth in PhpRedisSentinelConnector * Try setting auth only if used * Remove whitespace --------- Co-authored-by: Namoshek <[email protected]>
1 parent ac644ca commit 6f91478

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ To use the Redis Sentinel driver, the `redis` section in `config/database.php` n
5454
'sentinel_persistent' => env('REDIS_SENTINEL_PERSISTENT'),
5555
'sentinel_retry_interval' => env('REDIS_SENTINEL_RETRY_INTERVAL', 0),
5656
'sentinel_read_timeout' => env('REDIS_SENTINEL_READ_TIMEOUT', 0),
57+
'sentinel_username' => env('REDIS_SENTINEL_USERNAME'),
5758
'sentinel_password' => env('REDIS_SENTINEL_PASSWORD'),
5859
'password' => env('REDIS_PASSWORD'),
5960
'database' => env('REDIS_DB', 0),

src/Connectors/PhpRedisSentinelConnector.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,20 @@ private function connectToSentinel(array $config): RedisSentinel
8989
$persistent = $config['sentinel_persistent'] ?? null;
9090
$retryInterval = $config['sentinel_retry_interval'] ?? 0;
9191
$readTimeout = $config['sentinel_read_timeout'] ?? 0;
92+
$username = $config['sentinel_username'] ?? '';
9293
$password = $config['sentinel_password'] ?? '';
9394

9495
if (strlen(trim($host)) === 0) {
9596
throw new ConfigurationException('No host has been specified for the Redis Sentinel connection.');
9697
}
9798

99+
$auth = null;
100+
if (strlen(trim($username)) !== 0 && strlen(trim($password)) !== 0) {
101+
$auth = [$username, $password];
102+
} elseif (strlen(trim($password)) !== 0) {
103+
$auth = $password;
104+
}
105+
98106
if (version_compare(phpversion('redis'), '6.0', '>=')) {
99107
$options = [
100108
'host' => $host,
@@ -105,18 +113,14 @@ private function connectToSentinel(array $config): RedisSentinel
105113
'readTimeout' => $readTimeout,
106114
];
107115

108-
if (strlen(trim($password)) !== 0) {
109-
$options['auth'] = $password;
116+
if ($auth !== null) {
117+
$options['auth'] = $auth;
110118
}
111119

112120
return new RedisSentinel($options);
113-
} else {
114-
if (strlen(trim($password)) !== 0) {
115-
/** @noinspection PhpMethodParametersCountMismatchInspection */
116-
return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, $password);
117-
}
118-
119-
return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout);
120121
}
122+
123+
/** @noinspection PhpMethodParametersCountMismatchInspection */
124+
return new RedisSentinel($host, $port, $timeout, $persistent, $retryInterval, $readTimeout, $auth);
121125
}
122126
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ protected function getEnvironmentSetUp($app)
4141
$app['config']->set('database.redis.default', [
4242
'sentinel_host' => env('REDIS_SENTINEL_HOST', '127.0.0.1'),
4343
'sentinel_port' => (int) env('REDIS_SENTINEL_PORT', 6379),
44+
'sentinel_username' => env('REDIS_SENTINEL_USERNAME'),
4445
'sentinel_password' => env('REDIS_SENTINEL_PASSWORD'),
4546
'sentinel_service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'),
4647
]);

0 commit comments

Comments
 (0)