Skip to content

Commit af705b0

Browse files
authored
Merge pull request #524 from php-enqueue/pr-515
[Redis] Add support of secure\TLS connections (based on PR 515)
2 parents 83e0ce9 + da1bd58 commit af705b0

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

docs/transport/redis.md

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ $psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('redis:')->creat
6767
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
6868
$redis->connect();
6969

70+
// Secure\TLS connection. Works only with predis library. Note second "S" in scheme.
71+
$factory = new RedisConnectionFactory('rediss://user:pass@host/0?vendor=predis');
72+
7073
$factory = new RedisConnectionFactory($redis);
7174
```
7275

pkg/redis/PRedis.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class PRedis implements Redis
2626
public function __construct(array $config)
2727
{
2828
$this->config = $this->config = array_replace([
29+
'scheme' => null,
2930
'host' => null,
3031
'port' => null,
3132
'pass' => null,

pkg/redis/PhpRedis.php

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class PhpRedis implements Redis
2020
public function __construct(array $config)
2121
{
2222
$this->config = array_replace([
23+
'scheme' => null,
2324
'host' => null,
2425
'port' => null,
2526
'pass' => null,
@@ -73,6 +74,10 @@ public function connect(): void
7374
return;
7475
}
7576

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

7883
if ($this->config['persisted']) {

pkg/redis/RedisConnectionFactory.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ private function createRedis(): Redis
121121

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

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

143-
unset($config['query'], $config['scheme']);
143+
unset($config['query']);
144144

145145
$config['lazy'] = empty($config['lazy']) ? false : true;
146146
$config['persisted'] = empty($config['persisted']) ? false : true;
@@ -152,6 +152,7 @@ private function defaultConfig(): array
152152
{
153153
return [
154154
'host' => 'localhost',
155+
'scheme' => 'redis',
155156
'port' => 6379,
156157
'timeout' => .0,
157158
'reserved' => null,

pkg/redis/Tests/RedisConnectionFactoryConfigTest.php

+54-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
2525
public function testThrowIfSchemeIsNotAmqp()
2626
{
2727
$this->expectException(\LogicException::class);
28-
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "redis:".');
28+
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "redis:" or "rediss:".');
2929

3030
new RedisConnectionFactory('http://example.com');
3131
}
@@ -57,6 +57,15 @@ public function testCouldBeCreatedWithRedisInstance()
5757
$this->assertSame($redisMock, $context->getRedis());
5858
}
5959

60+
public function testThrowIfRedissConnectionUsedWithPhpRedisExtension()
61+
{
62+
$factory = new RedisConnectionFactory('rediss:?vendor=phpredis');
63+
64+
$this->expectException(\LogicException::class);
65+
$this->expectExceptionMessage('The phpredis extension does not support secured connections. Try to use predis library as vendor.');
66+
$factory->createContext();
67+
}
68+
6069
/**
6170
* @dataProvider provideConfigs
6271
*
@@ -76,6 +85,7 @@ public static function provideConfigs()
7685
null,
7786
[
7887
'host' => 'localhost',
88+
'scheme' => 'redis',
7989
'port' => 6379,
8090
'timeout' => null,
8191
'reserved' => null,
@@ -92,6 +102,7 @@ public static function provideConfigs()
92102
'redis:',
93103
[
94104
'host' => 'localhost',
105+
'scheme' => 'redis',
95106
'port' => 6379,
96107
'timeout' => null,
97108
'reserved' => null,
@@ -108,6 +119,7 @@ public static function provideConfigs()
108119
[],
109120
[
110121
'host' => 'localhost',
122+
'scheme' => 'redis',
111123
'port' => 6379,
112124
'timeout' => null,
113125
'reserved' => null,
@@ -124,6 +136,7 @@ public static function provideConfigs()
124136
'redis://localhost:1234?foo=bar&lazy=0&persisted=true&database=5',
125137
[
126138
'host' => 'localhost',
139+
'scheme' => 'redis',
127140
'port' => 1234,
128141
'timeout' => null,
129142
'reserved' => null,
@@ -137,10 +150,49 @@ public static function provideConfigs()
137150
],
138151
];
139152

153+
//check normal redis connection for predis library
154+
yield [
155+
'redis://localhost:1234?foo=bar&lazy=0&vendor=predis',
156+
[
157+
'host' => 'localhost',
158+
'scheme' => 'redis',
159+
'port' => 1234,
160+
'timeout' => null,
161+
'reserved' => null,
162+
'retry_interval' => null,
163+
'vendor' => 'predis',
164+
'persisted' => false,
165+
'lazy' => false,
166+
'foo' => 'bar',
167+
'database' => 0,
168+
'redis' => null,
169+
],
170+
];
171+
172+
//check tls connection for predis library
173+
yield [
174+
'rediss://localhost:1234?foo=bar&lazy=0&vendor=predis',
175+
[
176+
'host' => 'localhost',
177+
'scheme' => 'rediss',
178+
'port' => 1234,
179+
'timeout' => null,
180+
'reserved' => null,
181+
'retry_interval' => null,
182+
'vendor' => 'predis',
183+
'persisted' => false,
184+
'lazy' => false,
185+
'foo' => 'bar',
186+
'database' => 0,
187+
'redis' => null,
188+
],
189+
];
190+
140191
yield [
141192
['host' => 'localhost', 'port' => 1234, 'foo' => 'bar'],
142193
[
143194
'host' => 'localhost',
195+
'scheme' => 'redis',
144196
'port' => 1234,
145197
'timeout' => null,
146198
'reserved' => null,
@@ -159,6 +211,7 @@ public static function provideConfigs()
159211
'redis://h:asdfqwer1234asdf@ec2-111-1-1-1.compute-1.amazonaws.com:111',
160212
[
161213
'host' => 'ec2-111-1-1-1.compute-1.amazonaws.com',
214+
'scheme' => 'redis',
162215
'port' => 111,
163216
'timeout' => null,
164217
'reserved' => null,

0 commit comments

Comments
 (0)