diff --git a/src/Illuminate/Redis/Connectors/PhpRedisConnector.php b/src/Illuminate/Redis/Connectors/PhpRedisConnector.php index ebd6ee11d4ef..5942d7121cb1 100644 --- a/src/Illuminate/Redis/Connectors/PhpRedisConnector.php +++ b/src/Illuminate/Redis/Connectors/PhpRedisConnector.php @@ -56,7 +56,7 @@ public function connectToCluster(array $config, array $clusterOptions, array $op */ protected function buildClusterConnectionString(array $server) { - return $server['host'].':'.$server['port'].'?'.Arr::query(Arr::only($server, [ + return $this->formatHost($server).':'.$server['port'].'?'.Arr::query(Arr::only($server, [ 'database', 'password', 'prefix', 'read_timeout', ])); } @@ -116,7 +116,7 @@ protected function establishConnection($client, array $config) $persistent = $config['persistent'] ?? false; $parameters = [ - $config['host'], + $this->formatHost($config), $config['port'], Arr::get($config, 'timeout', 0.0), $persistent ? Arr::get($config, 'persistent_id', null) : null, @@ -165,4 +165,19 @@ protected function createRedisClusterInstance(array $servers, array $options) } }); } + + /** + * Format the host using the scheme if available. + * + * @param array $options + * @return string + */ + protected function formatHost(array $options) + { + if (isset($options['scheme'])) { + return "{$options['scheme']}://{$options['host']}"; + } + + return $options['host']; + } } diff --git a/src/Illuminate/Redis/RedisManager.php b/src/Illuminate/Redis/RedisManager.php index 144bb413f8cb..b5d98203c180 100644 --- a/src/Illuminate/Redis/RedisManager.php +++ b/src/Illuminate/Redis/RedisManager.php @@ -185,6 +185,12 @@ protected function parseConnectionConfiguration($config) { $parsed = (new ConfigurationUrlParser)->parseConfiguration($config); + $driver = strtolower($parsed['driver'] ?? ''); + + if (in_array($driver, ['tcp', 'tls'])) { + $parsed['scheme'] = $driver; + } + return array_filter($parsed, function ($key) { return ! in_array($key, ['driver', 'username'], true); }, ARRAY_FILTER_USE_KEY); diff --git a/src/Illuminate/Support/ConfigurationUrlParser.php b/src/Illuminate/Support/ConfigurationUrlParser.php index 64518c8c3853..c7861d5c1c47 100644 --- a/src/Illuminate/Support/ConfigurationUrlParser.php +++ b/src/Illuminate/Support/ConfigurationUrlParser.php @@ -17,6 +17,8 @@ class ConfigurationUrlParser 'postgres' => 'pgsql', 'postgresql' => 'pgsql', 'sqlite3' => 'sqlite', + 'redis' => 'tcp', + 'rediss' => 'tls', ]; /** diff --git a/tests/Redis/RedisConnectorTest.php b/tests/Redis/RedisConnectorTest.php new file mode 100644 index 000000000000..599fa2f2aad3 --- /dev/null +++ b/tests/Redis/RedisConnectorTest.php @@ -0,0 +1,163 @@ +setUpRedis(); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->tearDownRedis(); + + m::close(); + } + + public function testDefaultConfiguration() + { + $host = env('REDIS_HOST', '127.0.0.1'); + $port = env('REDIS_PORT', 6379); + + $predisClient = $this->redis['predis']->connection()->client(); + $parameters = $predisClient->getConnection()->getParameters(); + $this->assertEquals('tcp', $parameters->scheme); + $this->assertEquals($host, $parameters->host); + $this->assertEquals($port, $parameters->port); + + $phpRedisClient = $this->redis['phpredis']->connection()->client(); + $this->assertEquals($host, $phpRedisClient->getHost()); + $this->assertEquals($port, $phpRedisClient->getPort()); + } + + public function testUrl() + { + $host = env('REDIS_HOST', '127.0.0.1'); + $port = env('REDIS_PORT', 6379); + + $predis = new RedisManager(new Application, 'predis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'url' => "redis://{$host}:{$port}", + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $predisClient = $predis->connection()->client(); + $parameters = $predisClient->getConnection()->getParameters(); + $this->assertEquals('tcp', $parameters->scheme); + $this->assertEquals($host, $parameters->host); + $this->assertEquals($port, $parameters->port); + + $phpRedis = new RedisManager(new Application, 'phpredis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'url' => "redis://{$host}:{$port}", + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $phpRedisClient = $phpRedis->connection()->client(); + $this->assertEquals("tcp://{$host}", $phpRedisClient->getHost()); + $this->assertEquals($port, $phpRedisClient->getPort()); + } + + public function testUrlWithScheme() + { + $host = env('REDIS_HOST', '127.0.0.1'); + $port = env('REDIS_PORT', 6379); + + $predis = new RedisManager(new Application, 'predis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'url' => "tls://{$host}:{$port}", + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $predisClient = $predis->connection()->client(); + $parameters = $predisClient->getConnection()->getParameters(); + $this->assertEquals('tls', $parameters->scheme); + $this->assertEquals($host, $parameters->host); + $this->assertEquals($port, $parameters->port); + + $phpRedis = new RedisManager(new Application, 'phpredis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'url' => "tcp://{$host}:{$port}", + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $phpRedisClient = $phpRedis->connection()->client(); + $this->assertEquals("tcp://{$host}", $phpRedisClient->getHost()); + $this->assertEquals($port, $phpRedisClient->getPort()); + } + + public function testScheme() + { + $host = env('REDIS_HOST', '127.0.0.1'); + $port = env('REDIS_PORT', 6379); + + $predis = new RedisManager(new Application, 'predis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'scheme' => 'tls', + 'host' => $host, + 'port' => $port, + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $predisClient = $predis->connection()->client(); + $parameters = $predisClient->getConnection()->getParameters(); + $this->assertEquals('tls', $parameters->scheme); + $this->assertEquals($host, $parameters->host); + $this->assertEquals($port, $parameters->port); + + $phpRedis = new RedisManager(new Application, 'phpredis', [ + 'cluster' => false, + 'options' => [ + 'prefix' => 'test_', + ], + 'default' => [ + 'scheme' => 'tcp', + 'host' => $host, + 'port' => $port, + 'database' => 5, + 'timeout' => 0.5, + ], + ]); + $phpRedisClient = $phpRedis->connection()->client(); + $this->assertEquals("tcp://{$host}", $phpRedisClient->getHost()); + $this->assertEquals($port, $phpRedisClient->getPort()); + } +} diff --git a/tests/Support/ConfigurationUrlParserTest.php b/tests/Support/ConfigurationUrlParserTest.php index 5a23513f5b5b..e4cfa0bb0a73 100644 --- a/tests/Support/ConfigurationUrlParserTest.php +++ b/tests/Support/ConfigurationUrlParserTest.php @@ -23,6 +23,8 @@ public function testDriversAliases() 'postgres' => 'pgsql', 'postgresql' => 'pgsql', 'sqlite3' => 'sqlite', + 'redis' => 'tcp', + 'rediss' => 'tls', ], ConfigurationUrlParser::getDriverAliases()); ConfigurationUrlParser::addDriverAlias('some-particular-alias', 'mysql'); @@ -33,6 +35,8 @@ public function testDriversAliases() 'postgres' => 'pgsql', 'postgresql' => 'pgsql', 'sqlite3' => 'sqlite', + 'redis' => 'tcp', + 'rediss' => 'tls', 'some-particular-alias' => 'mysql', ], ConfigurationUrlParser::getDriverAliases()); @@ -355,7 +359,7 @@ public function databaseUrls() 'database' => 0, ], [ - 'driver' => 'redis', + 'driver' => 'tcp', 'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com', 'port' => 111, 'database' => 0, @@ -367,12 +371,12 @@ public function databaseUrls() [ 'url' => 'redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111/', 'host' => '127.0.0.1', - 'password' => null, - 'port' => 6379, + 'password' => null, + 'port' => 6379, 'database' => 2, ], [ - 'driver' => 'redis', + 'driver' => 'tcp', 'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com', 'port' => 111, 'database' => 2, @@ -380,6 +384,40 @@ public function databaseUrls() 'password' => 'asdfqwer1234asdf', ], ], + 'Redis Example with tls scheme' => [ + [ + 'url' => 'tls://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111', + 'host' => '127.0.0.1', + 'password' => null, + 'port' => 6379, + 'database' => 0, + ], + [ + 'driver' => 'tls', + 'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com', + 'port' => 111, + 'database' => 0, + 'username' => 'h', + 'password' => 'asdfqwer1234asdf', + ], + ], + 'Redis Example with rediss scheme' => [ + [ + 'url' => 'rediss://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111', + 'host' => '127.0.0.1', + 'password' => null, + 'port' => 6379, + 'database' => 0, + ], + [ + 'driver' => 'tls', + 'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com', + 'port' => 111, + 'database' => 0, + 'username' => 'h', + 'password' => 'asdfqwer1234asdf', + ], + ], ]; } }