From 80318641a7a85bf29e6c24cf850309bb12df1486 Mon Sep 17 00:00:00 2001 From: Alexis Saettler Date: Sat, 13 Aug 2022 23:35:05 +0200 Subject: [PATCH] feat: add a laravelcloudflare.enabled config (#254) --- README.md | 15 +++++++++++--- config/laravelcloudflare.php | 12 +++++++++++ src/Commands/Reload.php | 4 ++++ src/Http/Middleware/TrustProxies.php | 20 ++++++++++++++++--- tests/Unit/Commands/ReloadTest.php | 20 +++++++++++++++++-- .../Unit/Http/Middleware/TrustProxiesTest.php | 15 ++++++++++++++ 6 files changed, 78 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0c232d3..16b8b2b 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,7 @@ use Monicahq\Cloudflare\Facades\CloudflareProxies; */ public function boot() { - LaravelCloudflare::getProxiesUsing(function() { - return CloudflareProxies::load(); - }); + LaravelCloudflare::getProxiesUsing(fn() => CloudflareProxies::load()); } ``` @@ -117,6 +115,17 @@ php artisan vendor:publish --provider="Monicahq\Cloudflare\TrustedProxyServicePr This file contains some configurations, but you may not need to change them normally. +## Running tests for your package + +When running tests for your package, you generally don't need to get Cloudflare's proxy addresses. +You can deactivate the Laravel Cloudflare middleware by adding the following environment variable in +your `.env` or `phpunit.xml` file: + +``` +LARAVEL_CLOUDFLARE_ENABLED=false +``` + + # Compatibility | Laravel | [monicahq/laravel-cloudflare](https://github.com/monicahq/laravel-cloudflare) | diff --git a/config/laravelcloudflare.php b/config/laravelcloudflare.php index ef744d2..51e5c30 100644 --- a/config/laravelcloudflare.php +++ b/config/laravelcloudflare.php @@ -12,6 +12,18 @@ | */ + 'enabled' => (bool) env('LARAVEL_CLOUDFLARE_ENABLED', true), + + /* + |-------------------------------------------------------------------------- + | Name of the cache to store values of the proxies + |-------------------------------------------------------------------------- + | + | This value is the key used in the cache (table, redis, etc.) to store the + | values. + | + */ + 'cache' => 'cloudflare.proxies', /* diff --git a/src/Commands/Reload.php b/src/Commands/Reload.php index 296792a..868f223 100644 --- a/src/Commands/Reload.php +++ b/src/Commands/Reload.php @@ -32,6 +32,10 @@ class Reload extends Command */ public function handle(Cache $cache, Config $config) { + if (! (bool) $config->get('laravelcloudflare.enabled')) { + return; + } + $proxies = LaravelCloudflare::getProxies(); $cache->store()->forever($config->get('laravelcloudflare.cache'), $proxies); diff --git a/src/Http/Middleware/TrustProxies.php b/src/Http/Middleware/TrustProxies.php index 3bbc1ea..a6670ef 100644 --- a/src/Http/Middleware/TrustProxies.php +++ b/src/Http/Middleware/TrustProxies.php @@ -11,11 +11,27 @@ class TrustProxies extends Middleware { /** - * Sets the trusted proxies on the request to the value of Cloudflare ips. + * Sets the trusted proxies on the request. * * @param \Illuminate\Http\Request $request + * @return void */ protected function setTrustedProxyIpAddresses(Request $request) + { + if ((bool) Config::get('laravelcloudflare.enabled')) { + $this->setTrustedProxyCloudflare($request); + } + + parent::setTrustedProxyIpAddresses($request); + } + + /** + * Sets the trusted proxies on the request to the value of Cloudflare ips. + * + * @param \Illuminate\Http\Request $request + * @return void + */ + protected function setTrustedProxyCloudflare(Request $request): void { $cacheKey = Config::get('laravelcloudflare.cache'); $cachedProxies = Cache::rememberForever($cacheKey, fn () => LaravelCloudflare::getProxies()); @@ -23,7 +39,5 @@ protected function setTrustedProxyIpAddresses(Request $request) if (is_array($cachedProxies) && count($cachedProxies) > 0) { $this->proxies = array_merge((array) $this->proxies, $cachedProxies); } - - parent::setTrustedProxyIpAddresses($request); } } diff --git a/tests/Unit/Commands/ReloadTest.php b/tests/Unit/Commands/ReloadTest.php index b29507d..d6d7d0c 100644 --- a/tests/Unit/Commands/ReloadTest.php +++ b/tests/Unit/Commands/ReloadTest.php @@ -18,8 +18,10 @@ public function it_loads_proxies() $this->artisan('cloudflare:reload') ->expectsOutput('Cloudflare\'s IP blocks have been reloaded.') - ->assertExitCode(0); + ->assertExitCode(0) + ->run(); + $this->assertTrue($this->app['cache']->has('cloudflare.proxies')); $this->assertEquals(['expect'], $this->app['cache']->get('cloudflare.proxies')); } @@ -32,8 +34,22 @@ public function it_saves_address_in_cache() 'https://fake/ips-v6' => Http::response('::1/32', 200), ]); - $this->artisan('cloudflare:reload'); + $this->artisan('cloudflare:reload') + ->run(); $this->assertEquals(['0.0.0.0/20', '::1/32'], $this->app['cache']->get('cloudflare.proxies')); } + + /** @test */ + public function it_deactivate_command() + { + config(['laravelcloudflare.enabled' => false]); + + $this->artisan('cloudflare:reload') + ->doesntExpectOutput('Cloudflare\'s IP blocks have been reloaded.') + ->assertExitCode(0) + ->run(); + + $this->assertFalse($this->app['cache']->has('cloudflare.proxies')); + } } diff --git a/tests/Unit/Http/Middleware/TrustProxiesTest.php b/tests/Unit/Http/Middleware/TrustProxiesTest.php index 207b856..7df0a9c 100644 --- a/tests/Unit/Http/Middleware/TrustProxiesTest.php +++ b/tests/Unit/Http/Middleware/TrustProxiesTest.php @@ -63,4 +63,19 @@ public function it_does_not_sets_trusted_proxies() $this->assertEquals([], $proxies); } + + /** @test */ + public function it_deactivates_middleware() + { + config(['laravelcloudflare.enabled' => false]); + + $request = new Request(); + + $this->app->make(TrustProxies::class)->handle($request, fn () => null); + + $proxies = $request->getTrustedProxies(); + + $this->assertEquals([], $proxies); + $this->assertFalse(Cache::has('cloudflare.proxies')); + } }