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

[Cache] Fix Redis TLS scheme rediss for Redis connection #39599

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
Feb 22, 2021
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
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions 38 src/Symfony/Component/Cache/Tests/Adapter/RedisAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,46 @@ public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterfac
return $adapter;
}

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

$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost);
$redis = RedisAdapter::createConnection('redis://'.$redisHost);
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());

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

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

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

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

public function testCreateTlsConnection()
{
$redis = RedisAdapter::createConnection('rediss:?host[h1]&host[h2]&host[/foo:]');
$this->assertInstanceOf(\RedisArray::class, $redis);
$this->assertSame(['tls://h1:6379', 'tls://h2:6379', '/foo'], $redis->_hosts());
@$redis = null; // some versions of phpredis connect on destruct, let's silence the warning

$redisHost = getenv('REDIS_HOST');

$redis = RedisAdapter::createConnection('rediss://'.$redisHost.'?lazy=1');
$this->assertInstanceOf(RedisProxy::class, $redis);
}

/**
* @dataProvider provideFailedCreateConnection
*/
Expand Down Expand Up @@ -95,14 +105,6 @@ public function testInvalidCreateConnection(string $dsn)
RedisAdapter::createConnection($dsn);
}

public function provideValidSchemes(): array
{
return [
['redis'],
['rediss'],
];
}

public function provideInvalidCreateConnection(): array
{
return [
Expand Down
32 changes: 25 additions & 7 deletions 32 src/Symfony/Component/Cache/Traits/RedisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public static function createConnection($dsn, array $options = [])

$query = $hosts = [];
misaert marked this conversation as resolved.
Show resolved Hide resolved

$tls = 'rediss' === $scheme;
$tcpScheme = $tls ? 'tls' : 'tcp';

if (isset($params['query'])) {
parse_str($params['query'], $query);

Expand All @@ -130,9 +133,9 @@ public static function createConnection($dsn, array $options = [])
parse_str($parameters, $parameters);
}
if (false === $i = strrpos($host, ':')) {
$hosts[$host] = ['scheme' => 'tcp', 'host' => $host, 'port' => 6379] + $parameters;
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => $host, 'port' => 6379] + $parameters;
} elseif ($port = (int) substr($host, 1 + $i)) {
$hosts[$host] = ['scheme' => 'tcp', 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
$hosts[$host] = ['scheme' => $tcpScheme, 'host' => substr($host, 0, $i), 'port' => $port] + $parameters;
} else {
$hosts[$host] = ['scheme' => 'unix', 'path' => substr($host, 0, $i)] + $parameters;
}
Expand All @@ -148,7 +151,7 @@ public static function createConnection($dsn, array $options = [])
}

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

$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts) {
$initializer = static function ($redis) use ($connect, $params, $dsn, $auth, $hosts, $tls) {
$host = $hosts[0]['host'] ?? $hosts[0]['path'];
$port = $hosts[0]['port'] ?? null;

if (isset($hosts[0]['host']) && $tls) {
$host = 'tls://'.$host;
jderusse marked this conversation as resolved.
Show resolved Hide resolved
}

try {
@$redis->{$connect}($hosts[0]['host'] ?? $hosts[0]['path'], $hosts[0]['port'] ?? null, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);
@$redis->{$connect}($host, $port, $params['timeout'], (string) $params['persistent_id'], $params['retry_interval'], $params['read_timeout']);

set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
$isConnected = $redis->isConnected();
Expand Down Expand Up @@ -210,7 +220,11 @@ public static function createConnection($dsn, array $options = [])
}
} elseif (is_a($class, \RedisArray::class, true)) {
foreach ($hosts as $i => $host) {
$hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
switch ($host['scheme']) {
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
default: $hosts[$i] = $host['path'];
}
}
$params['lazy_connect'] = $params['lazy'] ?? true;
$params['connect_timeout'] = $params['timeout'];
Expand All @@ -227,7 +241,11 @@ public static function createConnection($dsn, array $options = [])
} elseif (is_a($class, \RedisCluster::class, true)) {
$initializer = static function () use ($class, $params, $dsn, $hosts) {
foreach ($hosts as $i => $host) {
$hosts[$i] = 'tcp' === $host['scheme'] ? $host['host'].':'.$host['port'] : $host['path'];
switch ($host['scheme']) {
case 'tcp': $hosts[$i] = $host['host'].':'.$host['port']; break;
case 'tls': $hosts[$i] = 'tls://'.$host['host'].':'.$host['port']; break;
default: $hosts[$i] = $host['path'];
}
}

try {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.