Skip to content

Commit 6ede44a

Browse files
authored
Merge pull request #5 from krakphp/support-tls
Support TLS
2 parents 0cfc405 + 454d76f commit 6ede44a

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Here are the available options that can be provided to the transport options arr
7474
- *default:* N/A
7575
- *description:* The redis database to select when performing redis operations. This value can also be set from the dsn via a query parameter or by setting the path in the dsn (`redis//redis:6379?db=1` or `redis://redis:6379/1`).
7676

77+
### TLS Support
78+
79+
To enable TLS support, make the DSN schema `rediss://` instead of `redis://` to note that the redis client should connect with TLS.
80+
7781
### Processed Queue
7882

7983
This library uses the [rpoplpush reliable queue pattern](https://redis.io/commands/rpoplpush#pattern-reliable-queue). It's important to note however that this library does not make any attempt to clean up that processed queue.

src/Transport/RedisTransport.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,16 @@ public static function fromDsn(SerializerInterface $serializer, string $dsn, arr
4848
throw new InvalidArgumentException('The queue option must be included in the query parameters or configuration options.');
4949
}
5050

51+
$host = $parsedUrl['host'] ?? '127.0.0.1';
52+
if ($parsedUrl['scheme'] === 'rediss') {
53+
$host = 'tls://'.$host;
54+
}
55+
5156
return new self(
5257
$serializer,
5358
new Redis(),
5459
$options['queue'] ?? $query['queue'],
55-
[$parsedUrl['host'] ?? '127.0.0.1', intval($parsedUrl['port'] ?? 6379)],
60+
[$host, intval($parsedUrl['port'] ?? 6379)],
5661
function(Redis $conn) use ($query, $parsedUrl, $options) {
5762
$auth = $options['password'] ?? $parsedUrl['pass'] ?? null;
5863
if ($auth) {

src/Transport/RedisTransportFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function createTransport(string $dsn, array $options, SerializerInterface
2020
* of this implementation.
2121
*/
2222
public function supports(string $dsn, array $options): bool {
23-
return strpos($dsn, 'redis://') === 0 && boolval($options['use_krak_redis'] ?? true);
23+
return (strpos($dsn, 'redis://') === 0 || strpos($dsn, 'rediss://') === 0) && boolval($options['use_krak_redis'] ?? true);
2424
}
2525
}

tests/Feature/TransportTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function provide_custom_redis_urls_with_db() {
4848
yield 'db in options' => ['options'];
4949
}
5050

51+
/** @test */
52+
public function supports_tls() {
53+
$this->given_the_redis_transport_is_setup_from_dsn_and_options('rediss://redis?queue=messenger');
54+
$this->then_the_redis_transport_connect_params_use_tls();
55+
}
56+
5157
/** @test */
5258
public function can_send_a_message() {
5359
$this->given_there_is_a_wrapped_message();
@@ -220,18 +226,26 @@ private function then_the_queue_contains_the_message() {
220226
);
221227
}
222228

223-
public function then_the_queue_has_size(int $size): void {
229+
private function then_the_queue_has_size(int $size): void {
224230
$this->assertEquals($size, $this->redis->lLen('messenger') + $this->redis->zCard('messenger:delayed'));
225231
}
226232

227-
public function then_the_message_is_not_available_immediately() {
233+
private function then_the_message_is_not_available_immediately() {
228234
$res = $this->transport->get();
229235
$this->assertCount(0, $res);
230236
}
231237

232-
public function then_the_message_is_available_after(int $delayMs) {
238+
private function then_the_message_is_available_after(int $delayMs) {
233239
usleep($delayMs * 1000);
234240
$res = $this->transport->get();
235241
$this->assertCount(1, $res);
236242
}
243+
244+
private function then_the_redis_transport_connect_params_use_tls() {
245+
// this is NOT a great test, but setting up a redis TLS server is quite a pain, so this is just hack to give
246+
// some piece of mind regarding the code, but isn't a good test because it's asserting private functionality.
247+
\Closure::bind(function() {
248+
TransportTest::assertEquals(['tls://redis', 6379], $this->connectParams);
249+
}, $this->transport, RedisTransport::class)();
250+
}
237251
}

0 commit comments

Comments
 (0)