Skip to content

Commit

Permalink
Fix connection via TLS (rediss://) (#444) (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
jankramer authored and curry684 committed Jul 31, 2018
1 parent a2d0faa commit fd24b11
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
19 changes: 17 additions & 2 deletions DependencyInjection/Configuration/RedisDsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class RedisDsn
*/
protected $socket;

/**
* @var bool
*/
protected $tls;

/**
* @var int
*/
Expand Down Expand Up @@ -114,6 +119,14 @@ public function getSocket()
return $this->socket;
}

/**
* @return bool
*/
public function getTls()
{
return $this->tls;
}

/**
* @return string
*/
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down Expand Up @@ -195,6 +208,8 @@ protected function parseDsn($dsn)
$this->port = (int) $matches[3];
}
}

$this->tls = 0 === strpos($this->dsn, 'rediss://');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Factory/PredisParametersFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
23 changes: 23 additions & 0 deletions Tests/DependencyInjection/Configuration/RedisDsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
);
}

Expand Down Expand Up @@ -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
*
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions Tests/Factory/PredisParametersFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
)
);
}
Expand Down

0 comments on commit fd24b11

Please sign in to comment.