From bb231e668b53ffa98ce620c613fee502243c900f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 19 Feb 2020 18:58:22 +0100 Subject: [PATCH 1/4] Registered middleware generating request ID --- composer.json | 2 ++ .../autoload/middleware-pipeline.global.php | 2 ++ config/autoload/request_id.global.php | 33 +++++++++++++++++++ module/Core/src/Options/AppOptions.php | 3 -- module/Rest/src/Entity/ApiKey.php | 12 ++----- 5 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 config/autoload/request_id.global.php diff --git a/composer.json b/composer.json index 52681a0e..7b8bf39e 100644 --- a/composer.json +++ b/composer.json @@ -45,8 +45,10 @@ "nikolaposa/monolog-factory": "^3.0", "ocramius/proxy-manager": "^2.7.0", "phly/phly-event-dispatcher": "^1.0", + "php-middleware/request-id": "^4.0", "predis/predis": "^1.1", "pugx/shortid-php": "^0.5", + "ramsey/uuid": "^3.9", "shlinkio/shlink-common": "^2.7.0", "shlinkio/shlink-event-dispatcher": "^1.3", "shlinkio/shlink-installer": "^4.2.0", diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index 2ace0700..45abf30e 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -7,6 +7,7 @@ namespace Shlinkio\Shlink; use Laminas\Stratigility\Middleware\ErrorHandler; use Mezzio; use Mezzio\ProblemDetails; +use PhpMiddleware\RequestId\RequestIdMiddleware; return [ @@ -21,6 +22,7 @@ return [ 'path' => '/rest', 'middleware' => [ Rest\Middleware\CrossDomainMiddleware::class, + RequestIdMiddleware::class, ProblemDetails\ProblemDetailsMiddleware::class, ], ], diff --git a/config/autoload/request_id.global.php b/config/autoload/request_id.global.php new file mode 100644 index 00000000..87d1c72f --- /dev/null +++ b/config/autoload/request_id.global.php @@ -0,0 +1,33 @@ + [ + 'allow_override' => true, + 'header_name' => 'X-Request-Id', + ], + + 'dependencies' => [ + 'factories' => [ + RequestId\Generator\RamseyUuid4StaticGenerator::class => InvokableFactory::class, + RequestId\RequestIdProviderFactory::class => ConfigAbstractFactory::class, + RequestId\RequestIdMiddleware::class => ConfigAbstractFactory::class, + ], + ], + + ConfigAbstractFactory::class => [ + RequestId\RequestIdProviderFactory::class => [ + RequestId\Generator\RamseyUuid4StaticGenerator::class, + 'config.request_id.allow_override', + 'config.request_id.header_name', + ], + RequestId\RequestIdMiddleware::class => [RequestId\RequestIdProviderFactory::class], + ], + +]; diff --git a/module/Core/src/Options/AppOptions.php b/module/Core/src/Options/AppOptions.php index aa51b871..66d76126 100644 --- a/module/Core/src/Options/AppOptions.php +++ b/module/Core/src/Options/AppOptions.php @@ -5,14 +5,11 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Options; use Laminas\Stdlib\AbstractOptions; -use Shlinkio\Shlink\Common\Util\StringUtilsTrait; use function sprintf; class AppOptions extends AbstractOptions { - use StringUtilsTrait; - private string $name = ''; private string $version = '1.0'; private ?string $disableTrackParam = null; diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 8c6d3aeb..f23d22e1 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -5,20 +5,18 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Entity; use Cake\Chronos\Chronos; +use Ramsey\Uuid\Uuid; use Shlinkio\Shlink\Common\Entity\AbstractEntity; -use Shlinkio\Shlink\Common\Util\StringUtilsTrait; class ApiKey extends AbstractEntity { - use StringUtilsTrait; - private string $key; private ?Chronos $expirationDate; private bool $enabled; public function __construct(?Chronos $expirationDate = null) { - $this->key = $this->generateV4Uuid(); + $this->key = Uuid::uuid4()->toString(); $this->expirationDate = $expirationDate; $this->enabled = true; } @@ -30,11 +28,7 @@ class ApiKey extends AbstractEntity public function isExpired(): bool { - if ($this->expirationDate === null) { - return false; - } - - return $this->expirationDate->lt(Chronos::now()); + return $this->expirationDate !== null && $this->expirationDate->lt(Chronos::now()); } public function isEnabled(): bool From d0a986dd5ae39f2ec907f43320e0f2641974469e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 19 Feb 2020 19:37:47 +0100 Subject: [PATCH 2/4] Added request ID to logs with monolog --- config/autoload/logger.global.php | 4 +++- config/autoload/request_id.global.php | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index 879f700a..16515c9e 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -9,6 +9,7 @@ use Monolog\Handler; use Monolog\Logger; use Monolog\Processor; use MonologFactory\DiContainerLoggerFactory; +use PhpMiddleware\RequestId; use Psr\Log\LoggerInterface; use const PHP_EOL; @@ -20,11 +21,12 @@ $processors = [ 'psr3' => [ 'name' => Processor\PsrLogMessageProcessor::class, ], + 'request_id' => RequestId\MonologProcessor::class, ]; $formatter = [ 'name' => Formatter\LineFormatter::class, 'params' => [ - 'format' => '[%datetime%] %channel%.%level_name% - %message%' . PHP_EOL, + 'format' => '[%datetime%] [%extra.request_id%] %channel%.%level_name% - %message%' . PHP_EOL, 'allow_inline_line_breaks' => true, ], ]; diff --git a/config/autoload/request_id.global.php b/config/autoload/request_id.global.php index 87d1c72f..0a9ed6ce 100644 --- a/config/autoload/request_id.global.php +++ b/config/autoload/request_id.global.php @@ -18,6 +18,7 @@ return [ RequestId\Generator\RamseyUuid4StaticGenerator::class => InvokableFactory::class, RequestId\RequestIdProviderFactory::class => ConfigAbstractFactory::class, RequestId\RequestIdMiddleware::class => ConfigAbstractFactory::class, + RequestId\MonologProcessor::class => ConfigAbstractFactory::class, ], ], @@ -28,6 +29,7 @@ return [ 'config.request_id.header_name', ], RequestId\RequestIdMiddleware::class => [RequestId\RequestIdProviderFactory::class], + RequestId\MonologProcessor::class => [RequestId\RequestIdMiddleware::class], ], ]; From fb89cb80ac1352323d0ce0261ec8a8f052bbb164 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 19 Feb 2020 19:48:48 +0100 Subject: [PATCH 3/4] Added formatting to swoole logs, to avoid duplicating the date --- config/autoload/logger.global.php | 1 + 1 file changed, 1 insertion(+) diff --git a/config/autoload/logger.global.php b/config/autoload/logger.global.php index 16515c9e..7c0e4f00 100644 --- a/config/autoload/logger.global.php +++ b/config/autoload/logger.global.php @@ -82,6 +82,7 @@ return [ 'swoole-http-server' => [ 'logger' => [ 'logger-name' => 'Logger_Access', + 'format' => '%h %l %u "%r" %>s %b', ], ], ], From b728a7867393ca56ccc938dc813c8072e8c5a2ec Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 21 Feb 2020 19:47:30 +0100 Subject: [PATCH 4/4] Updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 139a5877..30c989a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this * [#626](https://github.com/shlinkio/shlink/issues/626) Added support for Microsoft SQL Server. * [#556](https://github.com/shlinkio/shlink/issues/556) Short code lengths can now be customized, both globally and on a per-short URL basis. +* [#541](https://github.com/shlinkio/shlink/issues/541) Added a request ID that is returned on `X-Request-Id` header, can be provided from outside and is set in log entries. #### Changed