feature #35503 [Messenger] Add TLS option to Redis transport's DSN (Nyholm)

This PR was squashed before being merged into the 5.1-dev branch (closes #35503).

Discussion
----------

[Messenger] Add TLS option to Redis transport's DSN

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #33025
| License       | MIT
| Doc PR        |

As suggested by @matas-valuzis, this will enable TLS support for Redis transport.

Configure it with the following DSN

```
redis://127.0.0.1?tls=1
```

The implementation just prefix the host with `tls://` as described here: https://github.com/phpredis/phpredis#connect-open

It is already possible to use TLS with the Redis transport, but there are not support in the DSN until now.

Commits
-------

09ec907a7e [Messenger] Add TLS option to Redis transport's DSN
This commit is contained in:
Fabien Potencier 2020-01-29 11:43:52 +01:00
commit 8769c7a356
3 changed files with 33 additions and 1 deletions

View File

@ -5,3 +5,4 @@ CHANGELOG
-----
* Introduced the Redis bridge.
* Added TLS option in the DSN. Example: `redis://127.0.0.1?tls=1`

View File

@ -12,8 +12,8 @@
namespace Symfony\Component\Messenger\Bridge\Redis\Tests\Transport;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Exception\TransportException;
use Symfony\Component\Messenger\Bridge\Redis\Transport\Connection;
use Symfony\Component\Messenger\Exception\TransportException;
/**
* @requires extension redis >= 4.3.0
@ -73,6 +73,28 @@ class ConnectionTest extends TestCase
);
}
public function testFromDsnWithTls()
{
$redis = $this->createMock(\Redis::class);
$redis->expects($this->once())
->method('connect')
->with('tls://127.0.0.1', 6379)
->willReturn(null);
Connection::fromDsn('redis://127.0.0.1?tls=1', [], $redis);
}
public function testFromDsnWithTlsOption()
{
$redis = $this->createMock(\Redis::class);
$redis->expects($this->once())
->method('connect')
->with('tls://127.0.0.1', 6379)
->willReturn(null);
Connection::fromDsn('redis://127.0.0.1', ['tls' => true], $redis);
}
public function testFromDsnWithQueryOptions()
{
$this->assertEquals(

View File

@ -104,6 +104,12 @@ class Connection
unset($redisOptions['dbindex']);
}
$tls = false;
if (\array_key_exists('tls', $redisOptions)) {
$tls = filter_var($redisOptions['tls'], FILTER_VALIDATE_BOOLEAN);
unset($redisOptions['tls']);
}
$configuration = [
'stream' => $redisOptions['stream'] ?? null,
'group' => $redisOptions['group'] ?? null,
@ -125,6 +131,9 @@ class Connection
$configuration['stream'] = $pathParts[1] ?? $configuration['stream'];
$configuration['group'] = $pathParts[2] ?? $configuration['group'];
$configuration['consumer'] = $pathParts[3] ?? $configuration['consumer'];
if ($tls) {
$connectionCredentials['host'] = 'tls://'.$connectionCredentials['host'];
}
} else {
$connectionCredentials = [
'host' => $parsedUrl['path'],