8000 bug #40882 [Cache] phpredis: Added full TLS support for RedisCluster … · symfony/symfony@f8518ca · GitHub
[go: up one dir, main page]

Skip to content

Commit f8518ca

Browse files
bug #40882 [Cache] phpredis: Added full TLS support for RedisCluster (jackthomasatl)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Cache] phpredis: Added full TLS support for RedisCluster | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | n/a This Pr bridges the gap for full TLS support when using phpredis driver implementation of TLS. Adds the 'ssl' options array for cache configuration when using RedisCluster https://www.php.net/manual/en/context.ssl.php Switches directed node commands from using individual \Redis connections to using the recommended implementation from the phpredis documentation: https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#directed-node-commands This pr will enable compatibility with Amazon ElastiCache redis cluster mode using In Transit encryption (TLS) using the phpredis driver, Supports tagging & binary data types. Commits ------- a1e0408 [Cache] phpredis: Added full TLS support for RedisCluster
2 parents 2dd8445 + a1e0408 commit f8518ca

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Traits;
13+
14+
/**
15+
* This file acts as a wrapper to the \RedisCluster implementation so it can accept the same type of calls as
16+
* individual \Redis objects.
17+
*
18+
* Calls are made to individual nodes via: RedisCluster->{method}($host, ...args)'
19+
* according to https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#directed-node-commands
20+
*
21+
* @author Jack Thomas <jack.thomas@solidalpha.com>
22+
*
23+
* @internal
24+
*/
25+
class RedisClusterNodeProxy
26+
{
27+
private $host;
28+
private $redis;
29+
30+
/**
31+
* @param \RedisCluster|RedisClusterProxy $redis
32+
*/
33+
public function __construct(array $host, $redis)
34+
{
35+
$this->host = $host;
36+
$this->redis = $redis;
37+
}
38+
39+
public function __call(string $method, array $args)
40+
{
41+
return $this->redis->{$method}($this->host, ...$args);
42+
}
43+
44+
public function scan(&$iIterator, $strPattern = null, $iCount = null)
45+
{
46+
return $this->redis->scan($iIterator, $this->host, $strPattern, $iCount);
47+
}
48+
}

src/Symfony/Component/Cache/Traits/RedisTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ trait RedisTrait
4141
'redis_sentinel' => null,
4242
'dbindex' => 0,
4343
'failover' => 'none',
44+
'ssl' => null, // see https://php.net/context.ssl
4445
];
4546
private $redis;
4647
private $marshaller;
@@ -187,7 +188,7 @@ public static function createConnection($dsn, array $options = [])
187188
}
188189

189190
8000 try {
190-
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
191+
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout'], ['stream' => $params['ssl'] ?? null]);
191192

192193
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
193194
$isConnected = $redis->isConnected();
@@ -250,7 +251,7 @@ public static function createConnection($dsn, array $options = [])
250251
}
251252

252253
try {
253-
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '');
254+
$redis = new $class(null, $hosts, $params['timeout'], $params['read_timeout'], (bool) $params['persistent'], $params['auth'] ?? '', $params['ssl'] ?? null);
254255
} catch (\RedisClusterException $e) {
255256
throw new InvalidArgumentException(sprintf('Redis connection "%s" failed: ', $dsn).$e->getMessage());
256257
}
@@ -299,7 +300,7 @@ public static function createConnection($dsn, array $options = [])
299300
}
300301
$params['exceptions'] = false;
301302

302-
$redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
303+
$redis = new $class($hosts, array_diff_key($params, array_diff_key(self::$defaultConnectionOptions, ['ssl' => null])));
303304
if (isset($params['redis_sentinel'])) {
304305
$redis->getConnection()->setSentinelTimeout($params['timeout']);
305306
}
@@ -530,8 +531,7 @@ private function getHosts(): array
530531
} elseif ($this->redis instanceof RedisClusterProxy || $this->redis instanceof \RedisCluster) {
531532
$hosts = [];
532533
foreach ($this->redis->_masters() as $host) {
533-
$hosts[] = $h = new \Redis();
534-
$h->connect($host[0], $host[1]);
534+
$hosts[] = new RedisClusterNodeProxy($host, $this->redis);
535535
}
536536
}
537537

0 commit comments

Comments
 (0)
0