Skip to content

Support encryption, avoid double __unserialize call #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/CloudTasksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function __construct(array $job, CloudTasksQueue $cloudTasksQueue)
$this->job = $job;
$this->container = Container::getInstance();
$this->cloudTasksQueue = $cloudTasksQueue;
/** @var \stdClass $command */
$command = unserialize($job['data']['command']);
$this->queue = $command->queue;

$command = TaskHandler::getCommandProperties($job['data']['command']);
$this->queue = $command['queue'] ?? config('queue.connections.' .config('queue.default') . '.queue');
}

public function getJobId(): string
Expand Down
19 changes: 17 additions & 2 deletions src/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Google\Cloud\Tasks\V2\CloudTasksClient;
use Google\Cloud\Tasks\V2\RetryConfig;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Queue\Jobs\Job;
use Illuminate\Queue\WorkerOptions;
use Illuminate\Support\Str;
use stdClass;
use UnexpectedValueException;
use function Safe\json_decode;
Expand Down Expand Up @@ -56,8 +58,8 @@ private function loadQueueConnectionConfiguration(array $task): void
/**
* @var stdClass $command
*/
$command = unserialize($task['data']['command']);
$connection = $command->connection ?? config('queue.default');
$command = self::getCommandProperties($task['data']['command']);
$connection = $command['connection'] ?? config('queue.default');
$this->config = array_merge(
(array) config("queue.connections.{$connection}"),
['connection' => $connection]
Expand Down Expand Up @@ -131,4 +133,17 @@ private function loadQueueRetryConfig(CloudTasksJob $job): void

$this->retryConfig = CloudTasksApi::getRetryConfig($queueName);
}

public static function getCommandProperties(string $command): array
{
if (Str::startsWith($command, 'O:')) {
return (array) unserialize($command, ['allowed_classes' => false]);
}

if (app()->bound(Encrypter::class)) {
return (array) unserialize(app(Encrypter::class)->decrypt($command), ['allowed_classes' => false]);
}

return [];
}
}
9 changes: 5 additions & 4 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Google\Cloud\Tasks\V2\HttpMethod;
use Google\Cloud\Tasks\V2\Task;
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
use Tests\Support\FailingJob;
use Tests\Support\SimpleJob;

Expand Down Expand Up @@ -137,19 +138,19 @@ public function it_posts_the_task_the_correct_queue()
// Assert
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
$command = unserialize($decoded['data']['command']);
$command = TaskHandler::getCommandProperties($decoded['data']['command']);

return $decoded['displayName'] === SimpleJob::class
&& $command->queue === null
&& $command['queue'] === null
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
});

CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
$command = unserialize($decoded['data']['command']);
$command = TaskHandler::getCommandProperties($decoded['data']['command']);

return $decoded['displayName'] === FailingJob::class
&& $command->queue === 'my-special-queue'
&& $command['queue'] === 'my-special-queue'
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
});
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Support/EncryptedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Support;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class EncryptedJob implements ShouldQueue, ShouldBeEncrypted
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle()
{
logger('EncryptedJob:success');
}
}
25 changes: 19 additions & 6 deletions tests/TaskHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,16 @@
use Firebase\JWT\ExpiredException;
use Google\Cloud\Tasks\V2\RetryConfig;
use Google\Protobuf\Duration;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\Events\JobExceptionOccurred;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Queue;
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksException;
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksJob;
use Stackkit\LaravelGoogleCloudTasksQueue\LogFake;
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
use Stackkit\LaravelGoogleCloudTasksQueue\StackkitCloudTask;
use Tests\Support\EncryptedJob;
use Tests\Support\FailingJob;
use Tests\Support\SimpleJob;
use UnexpectedValueException;
Expand Down Expand Up @@ -272,4 +267,22 @@ public function test_max_attempts_in_combination_with_retry_until()

$this->assertEquals('failed', $task->fresh()->status);
}

/**
* @test
*/
public function it_can_handle_encrypted_jobs()
{
// Arrange
OpenIdVerificator::fake();
Log::swap(new LogFake());

// Act
$job = $this->dispatch(new EncryptedJob());
$job->run();

// Assert
$this->assertEquals('O:26:"Tests\Support\EncryptedJob":0:{}', decrypt($job->payload['data']['command']));
Log::assertLogged('EncryptedJob:success');
}
}