Skip to content

Commit

Permalink
Merge pull request #524 from php-enqueue/pr-515
Browse files Browse the repository at this point in the history
[Redis] Add support of secure\TLS connections (based on PR 515)
  • Loading branch information
makasim authored Sep 7, 2018
2 parents 3f98fd3 + 856968c commit 8a64437
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ $psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('redis:')->creat
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
$redis->connect();

// Secure\TLS connection. Works only with predis library. Note second "S" in scheme.
$factory = new RedisConnectionFactory('rediss://user:pass@host/0?vendor=predis');

$factory = new RedisConnectionFactory($redis);
```

Expand Down
1 change: 1 addition & 0 deletions pkg/redis/PRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PRedis implements Redis
public function __construct(array $config)
{
$this->config = $this->config = array_replace([
'scheme' => null,
'host' => null,
'port' => null,
'pass' => null,
Expand Down
5 changes: 5 additions & 0 deletions pkg/redis/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class PhpRedis implements Redis
public function __construct(array $config)
{
$this->config = array_replace([
'scheme' => null,
'host' => null,
'port' => null,
'pass' => null,
Expand Down Expand Up @@ -73,6 +74,10 @@ public function connect(): void
return;
}

if ('rediss' == $this->config['scheme']) {
throw new \LogicException('The phpredis extension does not support secured connections. Try to use predis library as vendor.');
}

$this->redis = new \Redis();

if ($this->config['persisted']) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/redis/RedisConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ private function createRedis(): Redis

private function parseDsn(string $dsn): array
{
if (false === strpos($dsn, 'redis:')) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "redis:".', $dsn));
if ((false === strpos($dsn, 'redis:')) and (false === strpos($dsn, 'rediss:'))) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "redis:" or "rediss:".', $dsn));
}

if (false === $config = parse_url($dsn)) {
Expand All @@ -140,7 +140,7 @@ private function parseDsn(string $dsn): array
$config = array_replace($queryConfig, $config);
}

unset($config['query'], $config['scheme']);
unset($config['query']);

$config['lazy'] = empty($config['lazy']) ? false : true;
$config['persisted'] = empty($config['persisted']) ? false : true;
Expand All @@ -152,6 +152,7 @@ private function defaultConfig(): array
{
return [
'host' => 'localhost',
'scheme' => 'redis',
'port' => 6379,
'timeout' => .0,
'reserved' => null,
Expand Down
55 changes: 54 additions & 1 deletion pkg/redis/Tests/RedisConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
public function testThrowIfSchemeIsNotAmqp()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "redis:".');
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "redis:" or "rediss:".');

new RedisConnectionFactory('http://example.com');
}
Expand Down Expand Up @@ -57,6 +57,15 @@ public function testCouldBeCreatedWithRedisInstance()
$this->assertSame($redisMock, $context->getRedis());
}

public function testThrowIfRedissConnectionUsedWithPhpRedisExtension()
{
$factory = new RedisConnectionFactory('rediss:?vendor=phpredis');

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The phpredis extension does not support secured connections. Try to use predis library as vendor.');
$factory->createContext();
}

/**
* @dataProvider provideConfigs
*
Expand All @@ -76,6 +85,7 @@ public static function provideConfigs()
null,
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 6379,
'timeout' => null,
'reserved' => null,
Expand All @@ -92,6 +102,7 @@ public static function provideConfigs()
'redis:',
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 6379,
'timeout' => null,
'reserved' => null,
Expand All @@ -108,6 +119,7 @@ public static function provideConfigs()
[],
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 6379,
'timeout' => null,
'reserved' => null,
Expand All @@ -124,6 +136,7 @@ public static function provideConfigs()
'redis://localhost:1234?foo=bar&lazy=0&persisted=true&database=5',
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 1234,
'timeout' => null,
'reserved' => null,
Expand All @@ -137,10 +150,49 @@ public static function provideConfigs()
],
];

//check normal redis connection for predis library
yield [
'redis://localhost:1234?foo=bar&lazy=0&vendor=predis',
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 1234,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'vendor' => 'predis',
'persisted' => false,
'lazy' => false,
'foo' => 'bar',
'database' => 0,
'redis' => null,
],
];

//check tls connection for predis library
yield [
'rediss://localhost:1234?foo=bar&lazy=0&vendor=predis',
[
'host' => 'localhost',
'scheme' => 'rediss',
'port' => 1234,
'timeout' => null,
'reserved' => null,
'retry_interval' => null,
'vendor' => 'predis',
'persisted' => false,
'lazy' => false,
'foo' => 'bar',
'database' => 0,
'redis' => null,
],
];

yield [
['host' => 'localhost', 'port' => 1234, 'foo' => 'bar'],
[
'host' => 'localhost',
'scheme' => 'redis',
'port' => 1234,
'timeout' => null,
'reserved' => null,
Expand All @@ -159,6 +211,7 @@ public static function provideConfigs()
'redis://h:[email protected]:111',
[
'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com',
'scheme' => 'redis',
'port' => 111,
'timeout' => null,
'reserved' => null,
Expand Down

0 comments on commit 8a64437

Please sign in to comment.