From fd24b115b35d54ec2f0f356e36e793933ee57bb8 Mon Sep 17 00:00:00 2001 From: Jan Kramer Date: Tue, 31 Jul 2018 10:05:29 +0200 Subject: [PATCH] Fix connection via TLS (rediss://) (#444) (#445) --- .../Configuration/RedisDsn.php | 19 +++++++++++++-- Factory/PredisParametersFactory.php | 2 +- .../Configuration/RedisDsnTest.php | 23 +++++++++++++++++++ Tests/Factory/PredisParametersFactoryTest.php | 11 +++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/Configuration/RedisDsn.php b/DependencyInjection/Configuration/RedisDsn.php index 2dc3a397..62ce2dbb 100644 --- a/DependencyInjection/Configuration/RedisDsn.php +++ b/DependencyInjection/Configuration/RedisDsn.php @@ -38,6 +38,11 @@ class RedisDsn */ protected $socket; + /** + * @var bool + */ + protected $tls; + /** * @var int */ @@ -114,6 +119,14 @@ public function getSocket() return $this->socket; } + /** + * @return bool + */ + public function getTls() + { + return $this->tls; + } + /** * @return string */ @@ -135,7 +148,7 @@ public function getPersistentId() */ public function isValid() { - if (0 !== strpos($this->dsn, 'redis://')) { + if (0 !== strpos($this->dsn, 'redis://') && 0 !== strpos($this->dsn, 'rediss://')) { return false; } @@ -155,7 +168,7 @@ public function isValid() */ protected function parseDsn($dsn) { - $dsn = str_replace('redis://', '', $dsn); // remove "redis://" + $dsn = preg_replace('#rediss?://#', '', $dsn); // remove "redis://" and "rediss://" if (false !== $pos = strrpos($dsn, '@')) { // parse password $password = substr($dsn, 0, $pos); @@ -195,6 +208,8 @@ protected function parseDsn($dsn) $this->port = (int) $matches[3]; } } + + $this->tls = 0 === strpos($this->dsn, 'rediss://'); } /** diff --git a/Factory/PredisParametersFactory.php b/Factory/PredisParametersFactory.php index 58a789ea..9e7604a1 100644 --- a/Factory/PredisParametersFactory.php +++ b/Factory/PredisParametersFactory.php @@ -37,7 +37,7 @@ private static function parseDsn(RedisDsn $dsn) $options['scheme'] = 'unix'; $options['path'] = $dsn->getSocket(); } else { - $options['scheme'] = 'tcp'; + $options['scheme'] = $dsn->getTls() ? 'tls' : 'tcp'; $options['host'] = $dsn->getHost(); $options['port'] = $dsn->getPort(); if (null !== $dsn->getDatabase()) { diff --git a/Tests/DependencyInjection/Configuration/RedisDsnTest.php b/Tests/DependencyInjection/Configuration/RedisDsnTest.php index 51b6e6d1..809383d7 100644 --- a/Tests/DependencyInjection/Configuration/RedisDsnTest.php +++ b/Tests/DependencyInjection/Configuration/RedisDsnTest.php @@ -62,6 +62,7 @@ public function hostValues() array('redis://%redis_host%:%redis_port%', '%redis_host%'), array('redis://%redis_host%:%redis_port%/%redis_db%', '%redis_host%'), array('redis://%redis_pass%@%redis_host%:%redis_port%/%redis_db%', '%redis_host%'), + array('rediss://localhost', 'localhost'), ); } @@ -110,6 +111,26 @@ public function testSocket($dsn, $socket) $this->assertSame($socket, $dsn->getSocket()); } + public function tlsValues() + { + return array( + array('redis://localhost', false), + array('rediss://localhost', true), + ); + } + + /** + * @param string $dsn DSN + * @param string $tls TLS + * + * @dataProvider tlsValues + */ + public function testTls($dsn, $tls) + { + $dsn = new RedisDsn($dsn); + $this->assertSame($tls, $dsn->getTls()); + } + /** * @static * @@ -120,6 +141,7 @@ public static function portValues() return array( array('redis://localhost', 6379), array('redis://localhost/1', 6379), + array('rediss://localhost:6380', 6380), array('redis://localhost:63790', 63790), array('redis://localhost:63790/10', 63790), array('redis://pw@localhost:63790/10', 63790), @@ -250,6 +272,7 @@ public static function isValidValues() { return array( array('redis://localhost', true), + array('rediss://localhost', true), array('redis://localhost/1', true), array('redis://pw@localhost:63790/10', true), array('redis://127.0.0.1', true), diff --git a/Tests/Factory/PredisParametersFactoryTest.php b/Tests/Factory/PredisParametersFactoryTest.php index 656f52f0..1440f1de 100644 --- a/Tests/Factory/PredisParametersFactoryTest.php +++ b/Tests/Factory/PredisParametersFactoryTest.php @@ -61,6 +61,17 @@ public function createDp() 'password' => 'pw', 'database' => 10, ), + ), + array( + 'rediss://pw@localhost:6380', + 'Predis\Connection\Parameters', + array(), + array( + 'scheme' => 'tls', + 'host' => 'localhost', + 'port' => 6380, + 'password' => 'pw' + ) ) ); }