From f7d3c73c4a4746655f931421faa26f0102c5276b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 30 May 2021 12:30:03 +0200 Subject: [PATCH 1/3] Skip downloading GeoLite db if global tracking or IP tracking are disabled --- composer.json | 2 +- docker/config/shlink_in_docker.local.php | 2 +- module/CLI/config/dependencies.config.php | 8 +++++++- module/CLI/src/Util/GeolocationDbUpdater.php | 15 +++++++++++++-- module/CLI/test/Util/GeolocationDbUpdaterTest.php | 2 ++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 7a84c886..22c75221 100644 --- a/composer.json +++ b/composer.json @@ -52,7 +52,7 @@ "shlinkio/shlink-event-dispatcher": "^2.1", "shlinkio/shlink-importer": "^2.3", "shlinkio/shlink-installer": "^6.0", - "shlinkio/shlink-ip-geolocation": "^1.5", + "shlinkio/shlink-ip-geolocation": "^2.0", "symfony/console": "^5.1", "symfony/filesystem": "^5.1", "symfony/lock": "^5.1", diff --git a/docker/config/shlink_in_docker.local.php b/docker/config/shlink_in_docker.local.php index 2a8369d7..2f1c9499 100644 --- a/docker/config/shlink_in_docker.local.php +++ b/docker/config/shlink_in_docker.local.php @@ -167,7 +167,7 @@ return [ ], 'geolite2' => [ - 'license_key' => env('GEOLITE_LICENSE_KEY', 'G4Lm0C60yJsnkdPi'), // Deprecated. Remove the default value + 'license_key' => env('GEOLITE_LICENSE_KEY', 'G4Lm0C60yJsnkdPi'), // Deprecated. Remove hardcoded license on v3 ], 'mercure' => $helper->getMercureConfig(), diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 7d7e2865..5f51d6c2 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -10,6 +10,7 @@ use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Laminas\ServiceManager\Factory\InvokableFactory; use Shlinkio\Shlink\Common\Doctrine\NoDbNameConnectionFactory; use Shlinkio\Shlink\Core\Domain\DomainService; +use Shlinkio\Shlink\Core\Options\TrackingOptions; use Shlinkio\Shlink\Core\Service; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; @@ -64,7 +65,12 @@ return [ ], ConfigAbstractFactory::class => [ - Util\GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, LOCAL_LOCK_FACTORY], + Util\GeolocationDbUpdater::class => [ + DbUpdater::class, + Reader::class, + LOCAL_LOCK_FACTORY, + TrackingOptions::class, + ], Util\ProcessRunner::class => [SymfonyCli\Helper\ProcessHelper::class], ApiKey\RoleResolver::class => [DomainService::class], diff --git a/module/CLI/src/Util/GeolocationDbUpdater.php b/module/CLI/src/Util/GeolocationDbUpdater.php index 6e7c2da2..2c4ef0e2 100644 --- a/module/CLI/src/Util/GeolocationDbUpdater.php +++ b/module/CLI/src/Util/GeolocationDbUpdater.php @@ -8,6 +8,7 @@ use Cake\Chronos\Chronos; use GeoIp2\Database\Reader; use MaxMind\Db\Reader\Metadata; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; +use Shlinkio\Shlink\Core\Options\TrackingOptions; use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException; use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface; use Symfony\Component\Lock\LockFactory; @@ -21,12 +22,18 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface private DbUpdaterInterface $dbUpdater; private Reader $geoLiteDbReader; private LockFactory $locker; + private TrackingOptions $trackingOptions; - public function __construct(DbUpdaterInterface $dbUpdater, Reader $geoLiteDbReader, LockFactory $locker) - { + public function __construct( + DbUpdaterInterface $dbUpdater, + Reader $geoLiteDbReader, + LockFactory $locker, + TrackingOptions $trackingOptions + ) { $this->dbUpdater = $dbUpdater; $this->geoLiteDbReader = $geoLiteDbReader; $this->locker = $locker; + $this->trackingOptions = $trackingOptions; } /** @@ -34,6 +41,10 @@ class GeolocationDbUpdater implements GeolocationDbUpdaterInterface */ public function checkDbUpdate(?callable $beforeDownload = null, ?callable $handleProgress = null): void { + if ($this->trackingOptions->disableTracking() || $this->trackingOptions->disableIpTracking()) { + return; + } + $lock = $this->locker->createLock(self::LOCK_NAME); $lock->acquire(true); // Block until lock is released diff --git a/module/CLI/test/Util/GeolocationDbUpdaterTest.php b/module/CLI/test/Util/GeolocationDbUpdaterTest.php index 54b07f1f..c3897175 100644 --- a/module/CLI/test/Util/GeolocationDbUpdaterTest.php +++ b/module/CLI/test/Util/GeolocationDbUpdaterTest.php @@ -13,6 +13,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; +use Shlinkio\Shlink\Core\Options\TrackingOptions; use Shlinkio\Shlink\IpGeolocation\Exception\RuntimeException; use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdaterInterface; use Symfony\Component\Lock; @@ -45,6 +46,7 @@ class GeolocationDbUpdaterTest extends TestCase $this->dbUpdater->reveal(), $this->geoLiteDbReader->reveal(), $locker->reveal(), + new TrackingOptions(), ); } From bfcccd8c33d4c35ad5c65bd1f12451cceb46b270 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 30 May 2021 12:36:58 +0200 Subject: [PATCH 2/3] Added test to check for GeoLite db update disabling based on tracking options --- CHANGELOG.md | 17 ++++++++++++ .../test/Util/GeolocationDbUpdaterTest.php | 27 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7df04c1..4e22a961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## [Unreleased] +### Added +* *Nothing* + +### Changed +* *Nothing* + +### Deprecated +* *Nothing* + +### Removed +* *Nothing* + +### Fixed +* [#1100](https://github.com/shlinkio/shlink/issues/1100) Fixed Shlink trying to download GeoLite2 db files even when tracking has been disabled. + + ## [2.7.0] - 2021-05-23 ### Added * [#1044](https://github.com/shlinkio/shlink/issues/1044) Added ability to set names on API keys, which helps to identify them when the list grows. diff --git a/module/CLI/test/Util/GeolocationDbUpdaterTest.php b/module/CLI/test/Util/GeolocationDbUpdaterTest.php index c3897175..0a52660f 100644 --- a/module/CLI/test/Util/GeolocationDbUpdaterTest.php +++ b/module/CLI/test/Util/GeolocationDbUpdaterTest.php @@ -29,11 +29,13 @@ class GeolocationDbUpdaterTest extends TestCase private GeolocationDbUpdater $geolocationDbUpdater; private ObjectProphecy $dbUpdater; private ObjectProphecy $geoLiteDbReader; + private TrackingOptions $trackingOptions; public function setUp(): void { $this->dbUpdater = $this->prophesize(DbUpdaterInterface::class); $this->geoLiteDbReader = $this->prophesize(Reader::class); + $this->trackingOptions = new TrackingOptions(); $locker = $this->prophesize(Lock\LockFactory::class); $lock = $this->prophesize(Lock\LockInterface::class); @@ -46,7 +48,7 @@ class GeolocationDbUpdaterTest extends TestCase $this->dbUpdater->reveal(), $this->geoLiteDbReader->reveal(), $locker->reveal(), - new TrackingOptions(), + $this->trackingOptions, ); } @@ -176,4 +178,27 @@ class GeolocationDbUpdaterTest extends TestCase 'record_size' => 4, ]); } + + /** + * @test + * @dataProvider provideTrackingOptions + */ + public function downloadDbIsSkippedIfTrackingIsDisabled(array $props): void + { + foreach ($props as $prop) { + $this->trackingOptions->{$prop} = true; + } + + $this->geolocationDbUpdater->checkDbUpdate(); + + $this->dbUpdater->databaseFileExists(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->geoLiteDbReader->metadata(Argument::cetera())->shouldNotHaveBeenCalled(); + } + + public function provideTrackingOptions(): iterable + { + yield 'disableTracking' => [['disableTracking']]; + yield 'disableIpTracking' => [['disableIpTracking']]; + yield 'both' => [['disableTracking', 'disableIpTracking']]; + } } From aa00e33b6d920973a862ac1c535819de75a03b61 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 30 May 2021 13:25:37 +0200 Subject: [PATCH 3/3] Added v2.7.1 to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e22a961..b6936625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). -## [Unreleased] +## [2.7.1] - 2021-05-30 ### Added * *Nothing*