Skip to content

Commit 3be5e3e

Browse files
committed
Merge remote-tracking branch 'origin/master' into v5.5
2 parents 3fa3d3c + 409872c commit 3be5e3e

File tree

7 files changed

+150
-4
lines changed

7 files changed

+150
-4
lines changed

.travis.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
language: php
1+
dist: xenial
22

3-
sudo: false
3+
language: php
44

55
php:
66
- 7.0
77
- 7.1
88

9+
env:
10+
- RABBITMQ_CONFIG_FILE="/tmp/rabbitmq.config" SSL_CAFILE="/tmp/rootCA.pem
11+
912
services:
1013
- rabbitmq
1114

1215
before_script:
1316
- COMPOSER_DISCARD_CHANGES=1 composer update --prefer-dist --no-interaction --no-suggest
17+
- sed -i 's/<env name="RABBITMQ_SSL_CAFILE" value="\/path_to_your_ca_file"\/>/<env name="RABBITMQ_SSL_CAFILE" value="\/tmp\/rootCA.pem"\/>/g' phpunit.xml.dist
18+
- openssl genrsa -out /tmp/rootCA.key 2048
19+
- openssl req -x509 -new -nodes -key /tmp/rootCA.key -sha256 -days 1024 -out /tmp/rootCA.pem -subj "/C=US/ST=Arizona/L=Scottsdale/O=Example Company Inc./CN=127.0.0.1"
20+
- openssl genrsa -out /tmp/device.key 2048
21+
- openssl req -new -key /tmp/device.key -out /tmp/device.csr -subj "/C=US/ST=Arizona/L=Scottsdale/O=Example Company Inc./CN=127.0.0.1"
22+
- openssl x509 -req -in /tmp/device.csr -CA /tmp/rootCA.pem -CAkey /tmp/rootCA.key -CAcreateserial -out /tmp/device.crt -days 500 -sha256
23+
- sudo service rabbitmq-server stop
24+
- echo "[{rabbit,[{ssl_listeners, [5671]},{ssl_options,[{cacertfile,\"/tmp/rootCA.pem\"},{certfile,\"/tmp/rootCA.pem\"},{keyfile,\"/tmp/rootCA.key\"},{verify,verify_none},{fail_if_no_peer_cert,false}]}]}]." > /tmp/rabbitmq.config
25+
- cp /tmp/rabbitmq.config /tmp/rabbitmq.config.config
26+
- sudo rabbitmq-server &
27+
- sleep 10
28+
1429
script:
1530
- composer test

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ RABBITMQ_PASSWORD=guest
2727
RABBITMQ_QUEUE=queue_name
2828
```
2929

30+
3. Optionally: if you want to to use an SSL connection, add these properties to the `.env` with proper values:
31+
```
32+
RABBITMQ_SSL=true
33+
RABBITMQ_SSL_CAFILE=/path_to_your_ca_file
34+
RABBITMQ_SSL_LOCALCERT=
35+
RABBITMQ_SSL_PASSPHRASE=
36+
RABBITMQ_SSL_KEY=
37+
```
38+
39+
Using an SSL connection will also require to configure your RabbitMQ to enable SSL. More details can be founds here: https://www.rabbitmq.com/ssl.html
40+
3041
#### Usage
3142

3243
Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues

config/rabbitmq.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@
5555
*/
5656
'sleep_on_error' => env('RABBITMQ_ERROR_SLEEP', 5),
5757

58+
/*
59+
* Optional SSL params if an SSL connection is used
60+
*/
61+
'ssl_params' => [
62+
'ssl_on' => env('RABBITMQ_SSL', false),
63+
'cafile' => env('RABBITMQ_SSL_CAFILE', null),
64+
'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
65+
'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
66+
'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
67+
]
68+
5869
];

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
<php>
2222
<env name="HOST" value="127.0.0.1"/>
2323
<env name="PORT" value="5672"/>
24+
<env name="PORT_SSL" value="5671"/>
25+
<env name="RABBITMQ_SSL_CAFILE" value="/path_to_your_ca_file"/>
2426
</php>
2527
<filter>
2628
<whitelist>

src/LaravelQueueRabbitMQServiceProvider.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Illuminate\Queue\QueueManager;
66
use Illuminate\Support\ServiceProvider;
7+
use PhpAmqpLib\Connection\AMQPStreamConnection;
78
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnector;
9+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnectorSSL;
810

911
class LaravelQueueRabbitMQServiceProvider extends ServiceProvider
1012
{
@@ -29,10 +31,17 @@ public function boot()
2931
{
3032
/** @var QueueManager $queue */
3133
$queue = $this->app['queue'];
32-
$connector = new RabbitMQConnector();
34+
35+
if ($this->app['config']['rabbitmq']['ssl_params']['ssl_on'] === true) {
36+
$connector = new RabbitMQConnectorSSL();
37+
} else {
38+
$connector = new RabbitMQConnector();
39+
}
3340

3441
$queue->stopping(function () use ($connector) {
35-
$connector->connection()->close();
42+
if ($connector->connection() instanceof AMQPStreamConnection) {
43+
$connector->connection()->close();
44+
}
3645
});
3746

3847
$queue->addConnector('rabbitmq', function () use ($connector) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors;
4+
5+
use Illuminate\Queue\Connectors\ConnectorInterface;
6+
use PhpAmqpLib\Connection\AMQPSSLConnection;
7+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
8+
9+
class RabbitMQConnectorSSL implements ConnectorInterface
10+
{
11+
/** @var AMQPSSLConnection */
12+
private $connection;
13+
14+
/**
15+
* Establish a queue connection.
16+
*
17+
* @param array $config
18+
*
19+
* @return \Illuminate\Contracts\Queue\Queue
20+
*/
21+
public function connect(array $config)
22+
{
23+
// Remove null values from the SSL config
24+
foreach ($config['ssl_params'] as $idx => $option) {
25+
if ($option === null || empty($option)) {
26+
unset($config['ssl_params'][$idx]);
27+
}
28+
}
29+
30+
// Create connection with AMQP
31+
$this->connection = new AMQPSSLConnection(
32+
$config['host'],
33+
$config['port'],
34+
$config['login'],
35+
$config['password'],
36+
$config['vhost'],
37+
$config['ssl_params']
38+
);
39+
40+
return new RabbitMQQueue(
41+
$this->connection,
42+
$config
43+
);
44+
}
45+
46+
public function connection()
47+
{
48+
return $this->connection;
49+
}
50+
}

tests/RabbitMQConnectorSSLTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use PhpAmqpLib\Connection\AMQPSSLConnection;
4+
use PHPUnit\Framework\TestCase;
5+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Connectors\RabbitMQConnectorSSL;
6+
use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\RabbitMQQueue;
7+
8+
class RabbitMQConnectorSSLTest extends TestCase
9+
{
10+
public function test_connect()
11+
{
12+
$config = [
13+
'host' => getenv('HOST'),
14+
'port' => getenv('PORT_SSL'),
15+
'login' => 'guest',
16+
'password' => 'guest',
17+
'vhost' => '/',
18+
19+
'queue' => 'queue_name',
20+
'exchange_declare' => true,
21+
'queue_declare_bind' => true,
22+
23+
'queue_params' => [
24+
'passive' => false,
25+
'durable' => true,
26+
'exclusive' => false,
27+
'auto_delete' => false,
28+
'arguments' => null,
29+
],
30+
'exchange_params' => [
31+
'name' => null,
32+
'type' => 'direct',
33+
'passive' => false,
34+
'durable' => true,
35+
'auto_delete' => false,
36+
],
37+
'ssl_params' => [
38+
'cafile' => getenv('RABBITMQ_SSL_CAFILE')
39+
]
40+
];
41+
42+
$connector = new RabbitMQConnectorSSL();
43+
$queue = $connector->connect($config);
44+
45+
$this->assertInstanceOf(RabbitMQQueue::class, $queue);
46+
$this->assertInstanceOf(AMQPSSLConnection::class, $connector->connection());
47+
}
48+
}

0 commit comments

Comments
 (0)