Skip to content

Commit c095a0d

Browse files
vigikaranvigikaran-elogsostrolucky
authored
Add phpredis sentinel auth support
Co-authored-by: Vigikaran <vigikaran.v@elogs.co.uk> Co-authored-by: Gabriel Ostrolucký <gabriel.ostrolucky@gmail.com>
1 parent 01b68b2 commit c095a0d

File tree

11 files changed

+62
-31
lines changed

11 files changed

+62
-31
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
include:
1919
- dependency-versions: "lowest"
2020
php-version: "7.4"
21-
php-extensions: "redis-5.3.0"
21+
php-extensions: "redis-5.3.2"
2222
- symfony-require: "5.4.*"
2323
php-version: "8.2"
2424
php-extensions: "redis"
@@ -31,13 +31,18 @@ jobs:
3131

3232
steps:
3333
- name: "Checkout"
34-
uses: "actions/checkout@v3"
34+
uses: "actions/checkout@v4"
3535
with:
3636
fetch-depth: 2
37-
- name: "Install requirements"
38-
run: sudo apt-get update && sudo apt-get install --no-install-recommends redis-server ruby-foreman
37+
- name: "Install redis server"
38+
uses: shogo82148/actions-setup-redis@v1
39+
with:
40+
redis-version: "7.x"
41+
auto-start: 'false'
42+
- name: "Install cli for Procfile"
43+
run: sudo apt-get update && sudo apt-get install --no-install-recommends ruby-foreman
3944
- name: "Start background services"
40-
run: sudo systemctl stop redis-server && foreman start &
45+
run: foreman start &
4146
- name: "Install PHP"
4247
uses: "shivammathur/setup-php@v2"
4348
with:
@@ -47,7 +52,7 @@ jobs:
4752
tools: "flex"
4853

4954
- name: "Install dependencies with Composer"
50-
uses: "ramsey/composer-install@v2"
55+
uses: "ramsey/composer-install@v3"
5156
with:
5257
dependency-versions: "${{ matrix.dependency-versions }}"
5358

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
sentinel monitor mymaster 127.0.0.1 6379 2
2+
user default on +@all ~* >sentinelauthdefaultpw
3+
user sentinelauth on +@all ~* >sentinelauthpw
24
appendonly no

Procfile

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
redis: redis-server .github/workflows/redis-configs/redis.conf --port 6379
2-
redis-acl: redis-server .github/workflows/redis-configs/redis-acl.conf --port 7099
3-
redis-sentinel: redis-server .github/workflows/redis-configs/redis-sentinel.conf --sentinel
4-
redis-node1: sh -c 'cd .github/workflows/redis-configs/redis-node1 && redis-server redis.conf --port 7079'
5-
redis-node2: sh -c 'cd .github/workflows/redis-configs/redis-node2 && redis-server redis.conf --port 7080'
6-
redis-node3: sh -c 'cd .github/workflows/redis-configs/redis-node3 && redis-server redis.conf --port 7081'
7-
redis-node4: sh -c 'cd .github/workflows/redis-configs/redis-node4 && redis-server redis.conf --port 7082'
8-
redis-node5: sh -c 'cd .github/workflows/redis-configs/redis-node5 && redis-server redis.conf --port 7083'
9-
redis-node6: sh -c 'cd .github/workflows/redis-configs/redis-node6 && redis-server redis.conf --port 7084'
1+
redis: sh -c 'cp .github/workflows/redis-configs/redis.conf /tmp/ && redis-server /tmp/redis.conf --port 6379'
2+
redis-acl: sh -c 'cp .github/workflows/redis-configs/redis-acl.conf /tmp/ && redis-server /tmp/redis-acl.conf --port 7099'
3+
redis-sentinel: sh -c 'cp .github/workflows/redis-configs/redis-sentinel.conf /tmp/ && redis-server /tmp/redis-sentinel.conf --sentinel'
4+
redis-node1: sh -c 'cp -R .github/workflows/redis-configs/redis-node1 /tmp/ && cd /tmp/redis-node1 && redis-server redis.conf --port 7079'
5+
redis-node2: sh -c 'cp -R .github/workflows/redis-configs/redis-node2 /tmp/ && cd /tmp/redis-node2 && redis-server redis.conf --port 7080'
6+
redis-node3: sh -c 'cp -R .github/workflows/redis-configs/redis-node3 /tmp/ && cd /tmp/redis-node3 && redis-server redis.conf --port 7081'
7+
redis-node4: sh -c 'cp -R .github/workflows/redis-configs/redis-node4 /tmp/ && cd /tmp/redis-node4 && redis-server redis.conf --port 7082'
8+
redis-node5: sh -c 'cp -R .github/workflows/redis-configs/redis-node5 /tmp/ && cd /tmp/redis-node5 && redis-server redis.conf --port 7083'
9+
redis-node6: sh -c 'cp -R .github/workflows/redis-configs/redis-node6 /tmp/ && cd /tmp/redis-node6 && redis-server redis.conf --port 7084'

docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ A setup using `predis`, `phpredis` or `relay` sentinel replication could look li
110110
snc_redis:
111111
clients:
112112
default:
113-
type: "predis" # or "phpredis", or "relay"
113+
type: "phpredis" # or "predis", or "relay"
114114
alias: default
115115
dsn:
116116
- redis://localhost:26379
@@ -121,6 +121,8 @@ snc_redis:
121121
parameters:
122122
database: 1
123123
password: pass
124+
sentinel_username: myuser # default to null
125+
sentinel_password: mypass # default to null
124126
```
125127

126128
The `service` is the name of the set of Redis instances.
@@ -131,7 +133,6 @@ If you use a password, it must be in the password parameter and must
131133
be omitted from the DSNs. Also make sure to use the sentinel port number
132134
(26379 by default) in the DSNs, and not the default Redis port.
133135
You can find more information about this on [Configuring Sentinel](https://redis.io/topics/sentinel#configuring-sentinel).
134-
135136
A setup using `RedisCluster` from `phpredis` could look like this:
136137

137138
``` yaml

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
config.allowUnfree = true;
1818
config.allowInsecurePredicate = pkg: pkgs.lib.getName pkg == "openssl";
1919
};
20-
php = pkgs.php82.buildEnv {
20+
php = pkgs.php83.buildEnv {
2121
extensions = (
2222
{
2323
all,

src/DependencyInjection/Configuration/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ private function addClientsSection(ArrayNodeDefinition $rootNode): void
155155
->scalarNode('database')->defaultNull()->end()
156156
->scalarNode('username')->defaultNull()->end()
157157
->scalarNode('password')->defaultNull()->end()
158+
->scalarNode('sentinel_username')->defaultNull()->end()
159+
->scalarNode('sentinel_password')->defaultNull()->end()
158160
->booleanNode('logging')->defaultValue($this->debug)->end()
159161
->variableNode('ssl_context')->defaultNull()->end()
160162
->end()

src/Factory/PhpredisClientFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ public function create(string $class, array $dsns, array $options, string $alias
111111
}
112112

113113
/**
114-
* @param class-string $class
115-
* @param list<RedisDsn> $dsns
116-
* @param array{service: ?string, connection_persistent: ?bool, connection_timeout: ?string, read_write_timeout: ?string} $options
114+
* @param class-string $class
115+
* @param list<RedisDsn> $dsns
116+
* @param array{service: ?string, connection_persistent: ?bool, connection_timeout: ?string, read_write_timeout: ?string, parameters: array{sentinel_username: string, sentinel_password: string}} $options
117117
*
118118
* @return Redis|Relay
119119
*/
@@ -125,6 +125,7 @@ private function createClientFromSentinel(string $class, array $dsns, string $al
125125
$connectionTimeout = $options['connection_timeout'] ?? 0;
126126
$connectionPersistent = $options['connection_persistent'] ? $masterName : null;
127127
$readTimeout = $options['read_write_timeout'] ?? 0;
128+
$parameters = $options['parameters'];
128129

129130
foreach ($dsns as $dsn) {
130131
$args = [
@@ -134,6 +135,7 @@ private function createClientFromSentinel(string $class, array $dsns, string $al
134135
'persistent' => $connectionPersistent,
135136
'retryInterval' => 5,
136137
'readTimeout' => $readTimeout,
138+
'auth' => [$parameters['sentinel_username'], $parameters['sentinel_password']],
137139
];
138140
try {
139141
if ($isRelay || version_compare(phpversion('redis'), '6.0', '<')) {

tests/DependencyInjection/SncRedisExtensionEnvTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function testPredisDefaultParameterConfig(): void
5353
'database' => null,
5454
'username' => null,
5555
'password' => null,
56+
'sentinel_username' => null,
57+
'sentinel_password' => null,
5658
'logging' => false,
5759
],
5860
'commands' => ['foo' => 'Foo\Bar\Baz'],
@@ -132,6 +134,8 @@ public function testPhpredisFullConfig(): void
132134
'database' => null,
133135
'username' => null,
134136
'password' => null,
137+
'sentinel_username' => null,
138+
'sentinel_password' => null,
135139
'logging' => false,
136140
],
137141
'connection_async' => false,
@@ -170,6 +174,8 @@ public function testPhpredisWithAclConfig(): void
170174
'database' => null,
171175
'logging' => false,
172176
'ssl_context' => null,
177+
'sentinel_username' => null,
178+
'sentinel_password' => null,
173179
],
174180
'prefix' => null,
175181
'read_write_timeout' => null,

tests/Factory/PhpredisClientFactoryTest.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,17 @@ public function testCreateMinimalClusterConfig(): void
107107

108108
/**
109109
* @requires extension relay
110-
* @testWith ["RedisSentinel", "Redis"]
111-
* ["Relay\\Sentinel", "Relay\\Relay"]
110+
* @testWith ["RedisSentinel", "Redis", null, "sentinelauthdefaultpw"]
111+
* ["RedisSentinel", "Redis", "sentinelauth", "sentinelauthpw"]
112+
* ["Relay\\Sentinel", "Relay\\Relay", null, "sentinelauthdefaultpw"]
113+
* ["Relay\\Sentinel", "Relay\\Relay", "sentinelauth", "sentinelauthpw"]
112114
*/
113-
public function testCreateSentinelConfig(string $sentinelClass, string $outputClass): void
114-
{
115+
public function testCreateSentinelConfig(
116+
string $sentinelClass,
117+
string $outputClass,
118+
?string $sentinelUser,
119+
?string $sentinelPassword
120+
): void {
115121
$this->logger->method('debug')->with(...$this->withConsecutive(
116122
[$this->stringContains('Executing command "CONNECT 127.0.0.1 6379 5 <null>')],
117123
['Executing command "AUTH sncredis"'],
@@ -130,6 +136,10 @@ public function testCreateSentinelConfig(string $sentinelClass, string $outputCl
130136
'connection_timeout' => 5,
131137
'connection_persistent' => false,
132138
'service' => 'mymaster',
139+
'parameters' => [
140+
'sentinel_username' => $sentinelUser,
141+
'sentinel_password' => $sentinelPassword,
142+
],
133143
],
134144
'phpredissentinel',
135145
true,

0 commit comments

Comments
 (0)