8000 [Cache] Send Predis SSL options in the $hosts parameter by magnusnordlander · Pull Request #50074 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Send Predis SSL options in the $hosts parameter #50074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000
Failed to load files.
Loading
Diff view
Diff view
[Cache] Send Predis SSL options in the $hosts parameter
  • Loading branch information
magnusnordlander authored and nicolas-grekas committed Apr 21, 2023
commit cd7cd2ff9791df47f419118a86b2bb1c337c7613
25 changes: 25 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/PredisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,29 @@ public function testCreateConnection()
];
$this->assertSame($params, $connection->getParameters()->toArray());
}

public function testCreateSslConnection()
{
$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection('rediss://'.$redisHost.'/1?ssl[verify_peer]=0', ['class' => \Predis\Client::class, 'timeout' => 3]);
$this->assertInstanceOf(\Predis\Client::class, $redis);

$connection = $redis->getConnection();
$this->assertInstanceOf(StreamConnection::class, $connection);

$redisHost = explode(':', $redisHost);
$params = [
'scheme' => 'tls',
'host' => $redisHost[0],
'port' => (int) ($redisHost[1] ?? 6379),
'ssl' => ['verify_peer' => '0'],
'persistent' => 0,
'timeout' => 3,
'read_write_timeout' => 0,
'tcp_nodelay' => true,
'database' => '1',
];
$this->assertSame($params, $connection->getParameters()->toArray());
}
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,15 @@ public static function createConnection(string $dsn, array $options = [])
if (null !== $auth) {
$params['parameters']['password'] = $auth;
}

if (isset($params['ssl'])) {
foreach ($hosts as $i => $host) {
if (!isset($host['ssl'])) {
$hosts[$i]['ssl'] = $params['ssl'];
}
}
}

if (1 === \count($hosts) && !($params['redis_cluster'] || $params['redis_sentinel'])) {
$hosts = $hosts[0];
} elseif (\in_array($params['failover'], ['slaves', 'distribute'], true) && !isset($params['replication'])) {
Expand All @@ -340,7 +349,7 @@ public static function createConnection(string $dsn, array $options = [])
}
$params['exceptions'] = false;

$redis = new $class($hosts, array_diff_key($params, array_diff_key(self::$defaultConnectionOptions, ['ssl' => null])));
$redis = new $class($hosts, array_diff_key($params, self::$defaultConnectionOptions));
if (isset($params['redis_sentinel'])) {
$redis->getConnection()->setSentinelTimeout($params['timeout']);
}
Expand Down
0