FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Encrypt all pushed Closures regardless of driver. · JavaDevl/framework@cbaf65e · GitHub

Commit cbaf65e

Browse files
committed
Encrypt all pushed Closures regardless of driver.
1 parent dbad947 commit cbaf65e

7 files changed

Lines changed: 83 additions & 22 deletions

File tree

‎src/Illuminate/Queue/IlluminateQueueClosure.php‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
<?php
22

3+
use Illuminate\Encryption\Encrypter;
4+
35
class IlluminateQueueClosure {
46

7+
/**
8+
* The encrypter instance.
9+
*
10+
* @var \Illuminate\Encryption\Encrypter $crypt
11+
*/
12+
protected $crypt;
13+
14+
/**
15+
* Create a new queued Closure job.
16+
*
17+
* @param \Illuminate\Encryption\Encrypter $crypt
18+
* @return void
19+
*/
20+
public function __construct(Encrypter $crypt)
21+
{
22+
$this->crypt = $crypt;
23+
}
24+
525
/**
626
* Fire the Closure based queue job.
727
*
@@ -11,7 +31,7 @@ class IlluminateQueueClosure {
1131
*/
1232
public function fire($job, $data)
1333
{
14-
$closure = unserialize($data['closure']);
34+
$closure = unserialize($this->crypt->decrypt($data['closure']));
1535

1636
$closure($job);
1737
}

‎src/Illuminate/Queue/IronQueue.php‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Illuminate\Http\Request;
55
use Illuminate\Http\Response;
66
use Illuminate\Queue\Jobs\IronJob;
7-
use Illuminate\Encryption\Encrypter;
87

98
class IronQueue extends Queue implements QueueInterface {
109

@@ -15,13 +14,6 @@ class IronQueue extends Queue implements QueueInterface {
1514
*/
1615
protected $iron;
1716

18-
/**
19-
* The encrypter instance.
20-
*
21-
* @var \Illuminate\Encryption\Encrypter
22-
*/
23-
protected $crypt;
24-
2517
/**
2618
* The current request instance.
2719
*
@@ -47,16 +39,14 @@ class IronQueue extends Queue implements QueueInterface {
4739
* Create a new IronMQ queue instance.
4840
*
4941
* @param \IronMQ $iron
50-
* @param \Illuminate\Encryption\Encrypter $crypt
5142
* @param \Illuminate\Http\Request $request
5243
* @param string $default
5344
* @param bool $shouldEncrypt
5445
* @return void
5546
*/
56-
public function __construct(IronMQ $iron, Encrypter $crypt, Request $request, $default, $shouldEncrypt = false)
47+
public function __construct(IronMQ $iron, Request $request, $default, $shouldEncrypt = false)
5748
{
5849
$this->iron = $iron;
59-
$this->crypt = $crypt;
6050
$this->request = $request;
6151
$this->default = $default;
6252
$this->shouldEncrypt = $shouldEncrypt;

‎src/Illuminate/Queue/Queue.php‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Closure;
44
use DateTime;
55
use Illuminate\Container\Container;
6+
use Illuminate\Encryption\Encrypter;
67
use Illuminate\Support\SerializableClosure;
78

89
abstract class Queue {
@@ -69,7 +70,7 @@ protected function createPayload($job, $data = '', $queue = null)
6970
*/
7071
protected function createClosurePayload($job, $data)
7172
{
72-
$closure = serialize(new SerializableClosure($job));
73+
$closure = $this->crypt->encrypt(serialize(new SerializableClosure($job)));
7374

7475
return array('job' => 'IlluminateQueueClosure', 'data' => compact('closure'));
7576
}
@@ -128,4 +129,15 @@ public function setContainer(Container $container)
128129
$this->container = $container;
129130
}
130131

132+
/**
133+
* Set the encrypter instance.
134+
*
135+
* @param \Illuminate\Encryption\Encrypter $crypt
136+
* @return void
137+
*/
138+
public function setEncrypter(Encrypter $crypt)
139+
{
140+
$this->crypt = $crypt;
141+
}
142+
131143
}

‎src/Illuminate/Queue/QueueManager.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public function connection($name = null)
6969
$this->connections[$name] = $this->resolve($name);
7070

7171
$this->connections[$name]->setContainer($this->app);
72+
73+
$this->connections[$name]->setEncrypter($this->app['encrypter']);
7274
}
7375

7476
return $this->connections[$name];

‎src/Illuminate/Queue/QueueServiceProvider.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Illuminate\Queue;
22

3+
use IlluminateQueueClosure;
34
use Illuminate\Support\ServiceProvider;
45
use Illuminate\Queue\Console\WorkCommand;
56
use Illuminate\Queue\Console\ListenCommand;
@@ -36,6 +37,8 @@ public function register()
3637
$this->registerSubscriber();
3738

3839
$this->registerFailedJobServices();
40+
41+
$this->registerQueueClosure();
3942
}
4043

4144
/**
@@ -254,6 +257,19 @@ protected function registerFailedJobServices()
254257
});
255258
}
256259

260+
/**
261+
* Register the Illuminate queued closure job.
262+
*
263+
* @return void
264+
*/
265+
protected function registerQueueClosure()
266+
{
267+
$this->app->bindShared('IlluminateQueueClosure', function($app)
268+
{
269+
return new IlluminateQueueClosure($app['encrypter']);
270+
});
271+
}
272+
257273
/**
258274
* Get the services provided by the provider.
259275
*

‎tests/Queue/QueueIronQueueTest.php‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public function tearDown()
1212

1313
public function testPushProperlyPushesJobOntoIron()
1414
{
15-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default', true);
15+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default', true);
16+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
17+
$queue->setEncrypter($crypt);
1618
$crypt->shouldReceive('encrypt')->once()->with(json_encode(array('job' => 'foo', 'data' => array(1, 2, 3), 'attempts' => 1, 'queue' => 'default')))->andReturn('encrypted');
1719
$iron->shouldReceive('postMessage')->once()->with('default', 'encrypted', array())->andReturn((object) array('id' => 1));
1820
$queue->push('foo', array(1, 2, 3));
@@ -21,7 +23,9 @@ public function testPushProperlyPushesJobOntoIron()
2123

2224
public function testPushProperlyPushesJobOntoIronWithoutEncryption()
2325
{
24-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default');
26+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default');
27+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
28+
$queue->setEncrypter($crypt);
2529
$crypt->shouldReceive('encrypt')->never();
2630
$iron->shouldReceive('postMessage')->once()->with('default', json_encode(['job' => 'foo', 'data' => [1, 2, 3], 'attempts' => 1, 'queue' => 'default']), array())->andReturn((object) array('id' => 1));
2731
$queue->push('foo', array(1, 2, 3));
@@ -30,11 +34,14 @@ public function testPushProperlyPushesJobOntoIronWithoutEncryption()
3034

3135
public function testPushProperlyPushesJobOntoIronWithClosures()
3236
{
33-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default', true);
37+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default', true);
38+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
39+
$queue->setEncrypter($crypt);
3440
$name = 'Foo';
3541
$closure = new Illuminate\Support\SerializableClosure($innerClosure = function() use ($name) { return $name; });
42+
$crypt->shouldReceive('encrypt')->once()->with(serialize($closure))->andReturn('serial_closure');
3643
$crypt->shouldReceive('encrypt')->once()->with(json_encode(array(
37-
'job' => 'IlluminateQueueClosure', 'data' => array('closure' => serialize($closure)), 'attempts' => 1, 'queue' => 'default'
44+
'job' => 'IlluminateQueueClosure', 'data' => array('closure' => 'serial_closure'), 'attempts' => 1, 'queue' => 'default'
3845
)))->andReturn('encrypted');
3946
$iron->shouldReceive('postMessage')->once()->with('default', 'encrypted', array())->andReturn((object) array('id' => 1));
4047
$queue->push($innerClosure);
@@ -43,7 +50,9 @@ public function testPushProperlyPushesJobOntoIronWithClosures()
4350

4451
public function testDelayedPushProperlyPushesJobOntoIron()
4552
{
46-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default', true);
53+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default', true);
54+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
55+
$queue->setEncrypter($crypt);
4756
$crypt->shouldReceive('encrypt')->once()->with(json_encode(array(
4857
'job' => 'foo', 'data' => array(1, 2, 3), 'attempts' => 1, 'queue' => 'default',
4958
)))->andReturn('encrypted');
@@ -55,7 +64,9 @@ public function testDelayedPushProperlyPushesJobOntoIron()
5564
public function testDelayedPushProperlyPushesJobOntoIronWithTimestamp()
5665
{
5766
$now = Carbon\Carbon::now();
58-
$queue = $this->getMock('Illuminate\Queue\IronQueue', array('getTime'), array($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default', true));
67+
$queue = $this->getMock('Illuminate\Queue\IronQueue', array('getTime'), array($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default', true));
68+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
69+
$queue->setEncrypter($crypt);
5970
$queue->expects($this->once())->method('getTime')->will($this->returnValue($now->getTimestamp()));
6071
$crypt->shouldReceive('encrypt')->once()->with(json_encode(array('job' => 'foo', 'data' => array(1, 2, 3), 'attempts' => 1, 'queue' => 'default')))->andReturn('encrypted');
6172
$iron->shouldReceive('postMessage')->once()->with('default', 'encrypted', array('delay' => 5))->andReturn((object) array('id' => 1));
@@ -65,7 +76,9 @@ public function testDelayedPushProperlyPushesJobOntoIronWithTimestamp()
6576

6677
public function testPopProperlyPopsJobOffOfIron()
6778
{
68-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default', true);
79+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default', true);
80+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
81+
$queue->setEncrypter($crypt);
6982
$queue->setContainer(m::mock('Illuminate\Container\Container'));
7083
$iron->shouldReceive('getMessage')->once()->with('default')->andReturn($job = m::mock('IronMQ_Message'));
7184
$job->body = 'foo';
@@ -78,7 +91,9 @@ public function testPopProperlyPopsJobOffOfIron()
7891

7992
public function testPopProperlyPopsJobOffOfIronWithoutEncryption()
8093
{
81-
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), m::mock('Illuminate\Http\Request'), 'default');
94+
$queue = new Illuminate\Queue\IronQueue($iron = m::mock('IronMQ'), m::mock('Illuminate\Http\Request'), 'default');
95+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
96+
$queue->setEncrypter($crypt);
8297
$queue->setContainer(m::mock('Illuminate\Container\Container'));
8398
$iron->shouldReceive('getMessage')->once()->with('default')->andReturn($job = m::mock('IronMQ_Message'));
8499
$job->body = 'foo';
@@ -91,7 +106,9 @@ public function testPopProperlyPopsJobOffOfIronWithoutEncryption()
91106

92107
public function testPushedJobsCanBeMarshaled()
93108
{
94-
$queue = $this->getMock('Illuminate\Queue\IronQueue', array('createPushedIronJob'), array($iron = m::mock('IronMQ'), $crypt = m::mock('Illuminate\Encryption\Encrypter'), $request = m::mock('Illuminate\Http\Request'), 'default', true));
109+
$queue = $this->getMock('Illuminate\Queue\IronQueue', array('createPushedIronJob'), array($iron = m::mock('IronMQ'), $request = m::mock('Illuminate\Http\Request'), 'default', true));
110+
$crypt = m::mock('Illuminate\Encryption\Encrypter');
111+
$queue->setEncrypter($crypt);
95112
$request->shouldReceive('header')->once()->with('iron-message-id')->andReturn('message-id');
96113
$request->shouldReceive('getContent')->once()->andReturn($content = json_encode(array('foo' => 'bar')));
97114
$crypt->shouldReceive('decrypt')->once()->with($content)->andReturn($content);

‎tests/Queue/QueueManagerTest.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function testDefaultConnectionCanBeResolved()
1818
'queue.default' => 'sync',
1919
'queue.connections.sync' => array('driver' => 'sync'),
2020
),
21+
'encrypter' => $encrypter = m::mock('Illuminate\Encryption\Encrypter'),
2122
);
2223

2324
$manager = new QueueManager($app);
@@ -26,6 +27,7 @@ public function testDefaultConnectionCanBeResolved()
2627
$connector->shouldReceive('connect')->once()->with(array('driver' => 'sync'))->andReturn($queue);
2728
$manager->addConnector('sync', function() use ($connector) { return $connector; });
2829
$queue->shouldReceive('setContainer')->once()->with($app);
30+
$queue->shouldReceive('setEncrypter')->once()->with($encrypter);
2931

3032
$this->assertTrue($queue === $manager->connection('sync'));
3133
}
@@ -38,6 +40,7 @@ public function testOtherConnectionCanBeResolved()
3840
'queue.default' => 'sync',
3941
'queue.connections.foo' => array('driver' => 'bar'),
4042
),
43+
'encrypter' => $encrypter = m::mock('Illuminate\Encryption\Encrypter'),
4144
);
4245

4346
$manager = new QueueManager($app);
@@ -46,6 +49,7 @@ public function testOtherConnectionCanBeResolved()
4649
$connector->shouldReceive('connect')->once()->with(array('driver' => 'bar'))->andReturn($queue);
4750
$manager->addConnector('bar', function() use ($connector) { return $connector; });
4851
$queue->shouldReceive('setContainer')->once()->with($app);
52+
$queue->shouldReceive('setEncrypter')->once()->with($encrypter);
4953

5054
$this->assertTrue($queue === $manager->connection('foo'));
5155
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL