Skip to content

Commit 638703d

Browse files
bug symfony#50074 [Cache] Send Predis SSL options in the $hosts parameter (magnusnordlander)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Cache] Send Predis SSL options in the $hosts parameter | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#46465 | License | MIT Predis accepts SSL options in the `$hosts` parameter, not in the `$params` parameter. From my perspective, this is really only applicable when using `rediss://host:port` style DSNs, where you might want to add `?ssl[verify_peer]=0` or something similar. I'm unsure how to write a good test for this, since there doesn't seem to be any standard Redis host with TLS that requires additional options in the test runner. Happy to take suggestions on how to approach a test, if that's deemed necessary. Commits ------- cd7cd2f [Cache] Send Predis SSL options in the $hosts parameter
2 parents c9f5f8f + cd7cd2f commit 638703d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 10 additions & 1 deletion
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)