Skip to content

Provide extra info when pushing tasks to non-https URL #82

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
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
12 changes: 12 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public static function getHandler($handler): string
));
}

// When the application is running behind a proxy and the TrustedProxy middleware has not been set up yet,
// an error like [HttpRequest.url must start with 'https'] could be thrown. Since the handler URL must
// always be https, we will provide a little extra information on how to fix this.
if ($parse['scheme'] !== 'https') {
throw new Exception(sprintf(
'Unable to push task to Cloud Tasks because the hander URL is not https. Google Cloud Tasks ' .
'will only call safe (https) URLs. If you are running Laravel behind a proxy (e.g. Ngrok, Expose), make sure it is ' .
'as a trusted proxy. To quickly fix this, add the following to the [app/Http/Middleware/TrustProxies] middleware: ' .
'protected $proxies = \'*\';'
));
}

// Versions 1.x and 2.x required the full path (e.g. my-app.com/handle-task). In 3.x and beyond
// it is no longer necessary to also include the path and simply setting the handler
// URL is enough. If someone upgrades and forgets we will warn them here.
Expand Down
6 changes: 3 additions & 3 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function a_http_request_with_the_handler_url_is_made()

// Assert
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8080/handle-task';
return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8080/handle-task';
});
}

Expand All @@ -69,15 +69,15 @@ public function it_posts_to_the_handler()
public function it_posts_to_the_correct_handler_url()
{
// Arrange
$this->setConfigValue('handler', 'http://docker.for.mac.localhost:8081');
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081');
CloudTasksApi::fake();

// Act
$this->dispatch(new SimpleJob());

// Assert
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8081/handle-task';
return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8081/handle-task';
});
}

Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function getEnvironmentSetUp($app)
'queue' => 'barbequeue',
'project' => 'my-test-project',
'location' => 'europe-west6',
'handler' => env('CLOUD_TASKS_HANDLER', 'http://docker.for.mac.localhost:8080'),
'handler' => env('CLOUD_TASKS_HANDLER', 'https://docker.for.mac.localhost:8080'),
'service_account_email' => 'info@stackkit.io',
]);
$app['config']->set('queue.failed.driver', 'database-uuids');
Expand Down Expand Up @@ -216,7 +216,7 @@ protected function addIdTokenToHeader(?Closure $closure = null): void
{
$base = [
'iss' => 'https://accounts.google.com',
'aud' => 'http://docker.for.mac.localhost:8080',
'aud' => 'https://docker.for.mac.localhost:8080',
'exp' => time() + 10,
];

Expand Down