Added testing with SSL authentication using CN (#1627) · predis/predis@d8a99ef · GitHub
Skip to content

Commit d8a99ef

Browse files
authored
Added testing with SSL authentication using CN (#1627)
* Added testing with SSL authentication using CN * Update CHANGELOG.md * Codestyle changes * Fixed broken tests * Fixed hybdrid tests * Fixed SSL tests * Fixed array merging * Updated README.md * Updated spelling list * Added support for Redis 8.6 * Codestyle changes and test fixes * Fixed hybdrid tests * Updated CHANGELOG.md and composer.json * Fixed XINFO tests
1 parent a24a7be commit d8a99ef

10 files changed

Lines changed: 150 additions & 6 deletions

File tree

‎.github/docker-compose.yml‎

Lines changed: 2 additions & 0 deletions

‎.github/wordlist.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
ACLs
2+
AUTH
23
Autoloading
4+
cafile
35
CAS
6+
CN
47
Customizable
58
ElastiCache
69
FPM
710
GC
811
IANA
12+
mTLS
913
Lua
1014
PSR
1115
Packagist

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### Maintenance
1515
- Added testing with SSL connection (#1624)
16+
- Added testing with SSL authentication using CN (#1627)
1617
- Added support for Redis 8.6 (#1631)
1718

1819
## v3.3.0 (2025-11-24)

‎README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ The connection schemes [`redis`](http://www.iana.org/assignments/uri-schemes/pro
114114
also supported, with the difference that URI strings containing these schemes are parsed following
115115
the rules described on their respective IANA provisional registration documents.
116116

117+
Since Redis 8.6, you can authenticate a client using the Subject CN from its TLS client certificate (mTLS).
118+
When this is enabled on the server, the client is authenticated during the TLS handshake, so you don’t need
119+
to send an AUTH command.
120+
121+
To use this, configure:
122+
123+
- a CA certificate used to verify the server certificate (cafile),
124+
- a client certificate (local_cert) signed by a CA trusted by the Redis server for client authentication,
125+
- the corresponding private key (local_pk).
126+
127+
Make sure:
128+
129+
- the Redis server certificate is signed by a CA trusted by the client, and
130+
- the client certificate is signed by a CA trusted by the Redis server (mTLS).
131+
132+
```php
133+
// Named array of connection parameters:
134+
$client = new Predis\Client([
135+
'scheme' => 'tls',
136+
'ssl' => [
137+
'cafile' => 'ca.pem', // CA used to verify the server certificate
138+
'local_cert' => 'client.crt', // client certificate (Subject CN maps to ACL user)
139+
'local_pk' => 'client.key', // client private key
140+
'verify_peer' => true,
141+
],
142+
]);
143+
144+
// ACL user must exist and match the certificate Subject CN (example: CN=CN_NAME).
145+
// Enable the user and grant permissions as needed:
146+
$client->acl->setUser('CN_NAME', 'on', '>clientpass', 'allcommands', 'allkeys')
147+
148+
echo $client->acl->whoami() // CN_NAME
149+
```
150+
117151
The actual list of supported connection parameters can vary depending on each connection backend so
118152
it is recommended to refer to their specific documentation or implementation for details.
119153

‎phpunit.xml.dist‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@
5757

5858
<!-- SSL -->
5959
<env name="STANDALONE_CA_CERT_PATH" value=".github/dockers/standalone/tls/ca.crt" />
60+
<env name="STANDALONE_LOCAL_CERT_PATH" value=".github/dockers/standalone/tls/test_user.crt" />
61+
<env name="STANDALONE_LOCAL_PK_PATH" value=".github/dockers/standalone/tls/test_user.key" />
62+
<env name="CN_USER_NAME" value="test_user" />
6063
<env name="REDIS_SSL_PORT" value="6666" />
6164
<env name="CLUSTER_CA_CERT_PATH" value=".github/dockers/cluster/tls/ca.crt" />
65+
<env name="CLUSTER_LOCAL_CERT_PATH" value=".github/dockers/cluster/tls/test_user.crt" />
66+
<env name="CLUSTER_LOCAL_PK_PATH" value=".github/dockers/cluster/tls/test_user.key" />
6267
<const
6368
name="SSL_REDIS_CLUSTER_ENDPOINTS"
6469
value="127.0.0.1:27379?password=foobar,127.0.0.1:27380?password=foobar,127.0.0.1:27381?password=foobar"

‎src/Cluster/ClusterStrategy.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ protected function getDefaultCommands()
189189

190190
/* cluster */
191191
'CLUSTER' => [$this, 'getFakeKey'],
192+
193+
/* control */
194+
'ACL' => [$this, 'getFakeKey'],
192195
];
193196
}
194197

‎tests/PHPUnit/PredisTestCase.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,31 @@ public function createClient(?array $parameters = null, ?array $options = null,
286286
);
287287

288288
if ($this->isSSLTest()) {
289-
$options = array_merge($options, [
289+
$options = $options + [
290290
'parameters' => [
291291
'ssl' => [
292292
'cafile' => getenv('CLUSTER_CA_CERT_PATH'),
293+
'local_cert' => getenv('CLUSTER_LOCAL_CERT_PATH'),
294+
'local_pk' => getenv('CLUSTER_LOCAL_PK_PATH'),
293295
'verify_peer' => true,
294296
'verify_peer_name' => false,
295297
'allow_self_signed' => false,
296298
],
297299
],
298-
]);
300+
];
299301
}
300302
} else {
301303
if ($this->isSSLTest()) {
302-
$parameters = array_merge($parameters, [
304+
$parameters = $parameters + [
303305
'ssl' => [
304306
'cafile' => getenv('STANDALONE_CA_CERT_PATH'),
307+
'local_cert' => getenv('STANDALONE_LOCAL_CERT_PATH'),
308+
'local_pk' => getenv('STANDALONE_LOCAL_PK_PATH'),
305309
'verify_peer' => true,
306310
'verify_peer_name' => false,
307311
'allow_self_signed' => false,
308312
],
309-
]);
313+
];
310314
}
311315
}
312316

‎tests/Predis/Cluster/PredisStrategyTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ protected function getExpectedCommands(?string $type = null): array
508508

509509
/* cluster */
510510
'CLUSTER' => 'keys-fake',
511+
512+
/* control */
513+
'ACL' => 'keys-fake',
511514
];
512515

513516
if (isset($type)) {

‎tests/Predis/Cluster/RedisStrategyTest.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,9 @@ protected function getExpectedCommands(?string $type = null): array
531531

532532
/* cluster */
533533
'CLUSTER' => 'keys-fake',
534+
535+
/* control */
536+
'ACL' => 'keys-fake',
534537
];
535538

536539
if (isset($type)) {

‎tests/Predis/SSLTest.php‎

Lines changed: 87 additions & 2 deletions

0 commit comments

Comments
 (0)