Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1688e5d

Browse filesBrowse files
bug #39599 [Cache] Fix Redis TLS scheme rediss for Redis connection (misaert)
This PR was submitted for the 5.x branch but it was merged into the 4.4 branch instead. Discussion ---------- [Cache] Fix Redis TLS scheme `rediss` for Redis connection | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#14728 Like #35503 on Symfony Messenger, this will enable TLS support for Redis adapter. The implementation just prefix the host with `tls://` as described here: https://github.com/phpredis/phpredis#connect-open I don't know how to test it because I guess I need a TLS Redis in `src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php`. Commits ------- 3288897 [Cache] Fix Redis TLS scheme `rediss` for Redis connection
2 parents f3529fd + 3288897 commit 1688e5d
Copy full SHA for 1688e5d

File tree

2 files changed

+45
-25
lines changed
Filter options

2 files changed

+45
-25
lines changed

‎src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
+20-18Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,46 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac
3636
return $adapter;
3737
}
3838

39-
/**
40-
* @dataProvider provideValidSchemes
41-
*/
42-
public function testCreateConnection(string $dsnScheme)
39+
public function testCreateConnection()
4340
{
44-
$redis = RedisAdapter::createConnection($dsnScheme.':?host[h1]&host[h2]&host[/foo:]');
41+
$redis = RedisAdapter::createConnection('redis:?host[h1]&host[h2]&host[/foo:]');
4542
$this->assertInstanceOf(\RedisArray::class, $redis);
4643
$this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts());
4744
@$redis = null; // some versions of phpredis connect on destruct, let's silence the warning
4845

4946
$redisHost = getenv('REDIS_HOST');
5047

51-
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost);
48+
$redis = RedisAdapter::createConnection('redis://'.$redisHost);
5249
$this->assertInstanceOf(\Redis::class, $redis);
5350
$this->assertTrue($redis->isConnected());
5451
$this->assertSame(0, $redis->getDbNum());
5552

56-
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'/2');
53+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
5754
$this->assertSame(2, $redis->getDbNum());
5855

59-
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['timeout' => 3]);
56+
$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['timeout' => 3]);
6057
$this->assertEquals(3, $redis->getTimeout());
6158

62-
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'?timeout=4');
59+
$redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
6360
$this->assertEquals(4, $redis->getTimeout());
6461

65-
$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['read_timeout' => 5]);
62+
$redis = RedisAdapter::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
6663
$this->assertEquals(5, $redis->getReadTimeout());
6764
}
6865

66+
public function testCreateTlsConnection()
67+
{
68+
$redis = RedisAdapter::createConnection('rediss:?host[h1]&host[h2]&host[/foo:]');
69+
$this->assertInstanceOf(\RedisArray::class, $redis);
70+
$this->assertSame(['tls://h1:6379', 'tls://h2:6379', '/foo'], $redis->_hosts());
71+
@$redis = null; // some versions of phpredis connect on destruct, let's silence the warning
72+
73+
$redisHost = getenv('REDIS_HOST');
74+
75+
$redis = RedisAdapter::createConnection('rediss://'.$redisHost.'?lazy=1');
76+
$this->assertInstanceOf(RedisProxy::class, $redis);
77+
}
78+
6979
/**
7080
* @dataProvider provideFailedCreateConnection
7181
*/
@@ -95,14 +105,6 @@ public function testInvalidCreateConnection(string $dsn)
95105
RedisAdapter::createConnection($dsn);
96106
}
97107

98-
public function provideValidSchemes(): array
99-
{
100-
return [
101-
['redis'],
102-
['rediss'],
103-
];
104-
}
105-
106108
public function provideInvalidCreateConnection(): array
107109
{
108110
return [

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Traits/RedisTrait.php
+25-7Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public static function createConnection($dsn, array $options = [])
118118

119119
$query = $hosts = [];
120120

121+
$tls = 'rediss' === $scheme;
122+
$tcpScheme = $tls ? 'tls' : 'tcp';
123+
121124
if (isset($params['query'])) {
122125
parse_str($params['query'], $query);
123126

@@ -130,9 +133,9 @@ public static function createConnection($dsn, array $options = [])
130133
parse_str($parameters, $parameters);
131134
}
132135
if (false === $i = strrpos($host, ':')) {
133-
$hosts[$host] = ['scheme' => 'tcp', 'host' => $host, 'port' => 6379] + $parameters;
136+
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => $host, 'port' => 6379] + $parameters;
134137
} elseif ($port = (int) substr($host, 1 + $i)) {
135-
$hosts[$host] = ['scheme' => 'tcp', 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
138+
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
136139
} else {
137140
$hosts[$host] = ['scheme' => 'unix', 'path' => substr($host, 0, $i)] + $parameters;
138141
}
@@ -148,7 +151,7 @@ public static function createConnection($dsn, array $options = [])
148151
}
149152

150153
if (isset($params['host'])) {
151-
array_unshift($hosts, ['scheme' => 'tcp', 'host' => $params['host'], 'port' => $params['port'] ?? 6379]);
154+
array_unshift($hosts, ['scheme' => $tcpScheme, 'host' => $params['host'], 'port' => $params['port'] ?? 6379]);
152155
} else {
153156
array_unshift($hosts, ['scheme' => 'unix', 'path' => $params['path']]);
154157
}
@@ -174,9 +177,16 @@ public static function createConnection($dsn, array $options = [])
174177
$connect = $params['persistent'] || $params['persistent_id'] ? 'pconnect' : 'connect';
175178
$redis = new $class();
176179

177-
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
180+
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) {
181+
$host = $hosts[0]['host'] ?? $hosts[0]['path'];
182+
$port = $hosts[0]['port'] ?? null;
183+
184+
if (isset($hosts[0]['host']) && $tls) {
185+
$host = 'tls://'.$host;
186+
}
187+
178188
try {
179-
@$redis->{$connect}($hosts[0]['host'] ?? $hosts[0]['path'], $hosts[0]['port'] ?? null, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
189+
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
180190

181191
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
182192
$isConnected = $redis->isConnected();
@@ -210,7 +220,11 @@ public static function createConnection($dsn, array $options = [])
210220
}
211221
} elseif (is_a($class, \RedisArray::class, true)) {
212222
foreach ($hosts as $i => $host) {
213-
$hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
223+
switch ($host['scheme']) {
224+
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
225+
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
226+
default: $hosts[$i] = $host['path'];
227+
}
214228
}
215229
$params['lazy_connect'] = $params['lazy'] ?? true;
216230
$params['connect_timeout'] = $params['timeout'];
@@ -227,7 +241,11 @@ public static function createConnection($dsn, array $options = [])
227241
} elseif (is_a($class, \RedisCluster::class, true)) {
228242
$initializer = static function () use ($class, $params, $dsn, $hosts) {
229243
foreach ($hosts as $i => $host) {
230-
$hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
244+
switch ($host['scheme']) {
245+
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
246+
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
247+
default: $hosts[$i] = $host['path'];
248+
}
231249
}
232250

233251
try {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.