Skip to content

Commit cd7cd2f

Browse files
magnusnordlandernicolas-grekas
authored andcommitted
[Cache] Send Predis SSL options in the $hosts parameter
1 parent 659000f commit cd7cd2f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,29 @@ public function testCreateConnection()
4848
];
4949
$this->assertSame($params, $connection->getParameters()->toArray());
5050
}
51+
52+
public function testCreateSslConnection()
53+
{
54+
$redisHost = getenv('REDIS_HOST');
55+
56+
$redis = RedisAdapter::createConnection('rediss://'.$redisHost.'/1?ssl[verify_peer]=0', ['class' => \Predis\Client::class, 'timeout' => 3]);
57+
$this->assertInstanceOf(\Predis\Client::class, $redis);
58+
59+
$connection = $redis->getConnection();
60+
$this->assertInstanceOf(StreamConnection::class, $connection);
61+
62+
$redisHost = explode(':', $redisHost);
63+
$params = [
64+
'scheme' => 'tls',
65+
'host' => $redisHost[0],
66+
'port' => (int) ($redisHost[1] ?? 6379),
67+
'ssl' => ['verify_peer' => '0'],
68+
'persistent' => 0,
69+
'timeout' => 3,
70+
'read_write_timeout' => 0,
71+
'tcp_nodelay' => true,
72+
'database' => '1',
73+
];
74+
$this->assertSame($params, $connection->getParameters()->toArray());
75+
}
5176
}

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ public static function createConnection(string $dsn, array $options = [])
332332
if (null !== $auth) {
333333
$params['parameters']['password'] = $auth;
334334
}
335+
336+
if (isset($params['ssl'])) {
337+
foreach ($hosts as $i => $host) {
338+
if (!isset($host['ssl'])) {
339+
$hosts[$i]['ssl'] = $params['ssl'];
340+
}
341+
}
342+
}
343+
335344
if (1 === \count($hosts) && !($params['redis_cluster'] || $params['redis_sentinel'])) {
336345
$hosts = $hosts[0];
337346
} elseif (\in_array($params['failover'], ['slaves', 'distribute'], true) && !isset($params['replication'])) {
@@ -340,7 +349,7 @@ public static function createConnection(string $dsn, array $options = [])
340349
}
341350
$params['exceptions'] = false;
342351

343-
$redis = new $class($hosts, array_diff_key($params, array_diff_key(self::$defaultConnectionOptions, ['ssl' => null])));
352+
$redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
344353
if (isset($params['redis_sentinel'])) {
345354
$redis->getConnection()->setSentinelTimeout($params['timeout']);
346355
}

0 commit comments

Comments
 (0)