From 06d2a7202d4da75dd061a551c729c0d9850be3f0 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Tue, 2 Aug 2022 17:59:52 +0200 Subject: [PATCH 1/9] test --- config/packages/security.yaml | 8 ++++ config/services.yaml | 7 +++ .../UpdateVersionsController.php | 46 +++++++++++++++++++ src/Security/TokenAPIAuthenticator.php | 3 +- 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 6c2d84fedef..7ce332d86a4 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -17,6 +17,14 @@ security: authentication: pattern: ^.*(?findCurrentVersion(); + if ($currentVersion === null || version_compare($currentVersion, self::MINIMAL_INSTALLED_VERSION, '<')) { + $presenter->setResponseStatus( + new ForbiddenResponse( + sprintf('Centreon installed version %s required', self::MINIMAL_INSTALLED_VERSION) + . ($currentVersion !== null ? sprintf(' (%s installed)', $currentVersion) : ''), + ), + ); + + return $presenter->show(); + } + + $this->denyAccessUnlessGrantedForApiConfiguration(); + + //dump($this->token->getToken()); + + //dump(get_class($this->authenticatorManager)); + //dump($request->attributes->get('_security_authenticators')); + //dump($request->attributes->get('_security_authenticators')); + //dump($request->attributes->get('_security_skipped_authenticators')); + //dump($request->attributes); + dump($this->authenticatorManager->authenticateRequest($request)); + //dump($this->authenticatorManager); + + $contact = $this->getUser(); + dump($contact); + $this->denyAccessUnlessGrantedForApiConfiguration(); /** diff --git a/src/Security/TokenAPIAuthenticator.php b/src/Security/TokenAPIAuthenticator.php index d12b2156922..993ace9e688 100644 --- a/src/Security/TokenAPIAuthenticator.php +++ b/src/Security/TokenAPIAuthenticator.php @@ -36,6 +36,7 @@ use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; @@ -46,7 +47,7 @@ * * @package Security */ -class TokenAPIAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface +class TokenAPIAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface, AuthenticatorInterface { /** * @var AuthenticationRepositoryInterface From 5b2ea5801e768b3c4d1ae6e369d42a64b4882a1e Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 15:31:43 +0200 Subject: [PATCH 2/9] fix --- config/packages/security.yaml | 8 -- config/services.yaml | 7 -- .../UpdateVersionsController.php | 46 --------- src/EventSubscriber/UpdateEventSubscriber.php | 98 +++++++++++++++++++ 4 files changed, 98 insertions(+), 61 deletions(-) create mode 100644 src/EventSubscriber/UpdateEventSubscriber.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 7ce332d86a4..6c2d84fedef 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -17,14 +17,6 @@ security: authentication: pattern: ^.*(?findCurrentVersion(); - if ($currentVersion === null || version_compare($currentVersion, self::MINIMAL_INSTALLED_VERSION, '<')) { - $presenter->setResponseStatus( - new ForbiddenResponse( - sprintf('Centreon installed version %s required', self::MINIMAL_INSTALLED_VERSION) - . ($currentVersion !== null ? sprintf(' (%s installed)', $currentVersion) : ''), - ), - ); - - return $presenter->show(); - } - - $this->denyAccessUnlessGrantedForApiConfiguration(); - - //dump($this->token->getToken()); - - //dump(get_class($this->authenticatorManager)); - //dump($request->attributes->get('_security_authenticators')); - //dump($request->attributes->get('_security_authenticators')); - //dump($request->attributes->get('_security_skipped_authenticators')); - //dump($request->attributes); - dump($this->authenticatorManager->authenticateRequest($request)); - //dump($this->authenticatorManager); - - $contact = $this->getUser(); - dump($contact); - $this->denyAccessUnlessGrantedForApiConfiguration(); /** diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php new file mode 100644 index 00000000000..79d55659826 --- /dev/null +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -0,0 +1,98 @@ + [ + ['checkCentreonWebVersion', 35], + ], + ]; + } + + /** + * validation centreon web installed version + * + * @param RequestEvent $event + * @throws \Exception + */ + public function validateCentreonWebVersionOrFail(RequestEvent $event) + { + $this->info('Checking if route matches updates endpoint'); + if ( + preg_match( + '#^.*/api/(?:latest|beta|v[0-9]+|v[0-9]+\.[0-9]+)/platform/updates$#', + $event->getRequest()->getPathInfo(), + ) + ) { + $this->info('Getting Centreon web current version'); + $currentVersion = $this->readVersionRepository->findCurrentVersion(); + + if ($currentVersion === null) { + throw new \Exception( + sprintf('Required Centreon installed version is %s', self::MINIMAL_INSTALLED_VERSION), + ); + } + + $this->info( + sprintf( + 'Comparing installed version %s to required version %s', + $currentVersion, + self::MINIMAL_INSTALLED_VERSION, + ), + ); + if (version_compare($currentVersion, self::MINIMAL_INSTALLED_VERSION, '<')) { + $errorMessage = sprintf( + 'Required Centreon installed version is %s (%s installed)', + self::MINIMAL_INSTALLED_VERSION, + $currentVersion, + ); + $this->error($errorMessage); + throw new \Exception($errorMessage); + } + } + } +} From 972b1a22f26cbd89c20762671a24b5108ee92ac7 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 15:37:50 +0200 Subject: [PATCH 3/9] fix --- src/EventSubscriber/UpdateEventSubscriber.php | 4 ++-- src/Security/TokenAPIAuthenticator.php | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 79d55659826..245c093316e 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -31,7 +31,7 @@ class UpdateEventSubscriber implements EventSubscriberInterface { use LoggerTrait; - private const MINIMAL_INSTALLED_VERSION = '22.11.0'; + private const MINIMAL_INSTALLED_VERSION = '22.04.0'; /** * @param ReadVersionRepositoryInterface $readVersionRepository @@ -48,7 +48,7 @@ public static function getSubscribedEvents(): array { return [ KernelEvents::REQUEST => [ - ['checkCentreonWebVersion', 35], + ['validateCentreonWebVersionOrFail', 35], ], ]; } diff --git a/src/Security/TokenAPIAuthenticator.php b/src/Security/TokenAPIAuthenticator.php index 993ace9e688..d12b2156922 100644 --- a/src/Security/TokenAPIAuthenticator.php +++ b/src/Security/TokenAPIAuthenticator.php @@ -36,7 +36,6 @@ use Symfony\Component\Security\Core\Exception\UserNotFoundException; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; -use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; @@ -47,7 +46,7 @@ * * @package Security */ -class TokenAPIAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface, AuthenticatorInterface +class TokenAPIAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface { /** * @var AuthenticationRepositoryInterface From ac3a8c5ac00d5947388b0b28950c0220c18ec0d7 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 15:39:37 +0200 Subject: [PATCH 4/9] fix --- src/EventSubscriber/UpdateEventSubscriber.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 245c093316e..78ece8e1d0f 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -72,9 +72,9 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event) $currentVersion = $this->readVersionRepository->findCurrentVersion(); if ($currentVersion === null) { - throw new \Exception( - sprintf('Required Centreon installed version is %s', self::MINIMAL_INSTALLED_VERSION), - ); + $errorMessage = sprintf('Required Centreon installed version is %s', self::MINIMAL_INSTALLED_VERSION); + $this->error($errorMessage); + throw new \Exception($errorMessage); } $this->info( From ec1ec7d7f4a029ef53c1b34ac671f2c8e84e93e4 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 15:52:25 +0200 Subject: [PATCH 5/9] replace info by debug --- src/EventSubscriber/UpdateEventSubscriber.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 78ece8e1d0f..fa79007218a 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -61,14 +61,14 @@ public static function getSubscribedEvents(): array */ public function validateCentreonWebVersionOrFail(RequestEvent $event) { - $this->info('Checking if route matches updates endpoint'); + $this->debug('Checking if route matches updates endpoint'); if ( preg_match( '#^.*/api/(?:latest|beta|v[0-9]+|v[0-9]+\.[0-9]+)/platform/updates$#', $event->getRequest()->getPathInfo(), ) ) { - $this->info('Getting Centreon web current version'); + $this->debug('Getting Centreon web current version'); $currentVersion = $this->readVersionRepository->findCurrentVersion(); if ($currentVersion === null) { @@ -77,7 +77,7 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event) throw new \Exception($errorMessage); } - $this->info( + $this->debug( sprintf( 'Comparing installed version %s to required version %s', $currentVersion, @@ -90,7 +90,7 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event) self::MINIMAL_INSTALLED_VERSION, $currentVersion, ); - $this->error($errorMessage); + $this->debug($errorMessage); throw new \Exception($errorMessage); } } From eed86cdf4095258045f7ff21df75d894b3eabb16 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 16:21:20 +0200 Subject: [PATCH 6/9] add translation --- lang/fr_FR.UTF-8/LC_MESSAGES/messages.po | 6 ++++++ src/EventSubscriber/UpdateEventSubscriber.php | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po index bac219049df..9459121147b 100644 --- a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po +++ b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po @@ -16963,3 +16963,9 @@ msgstr "Le module \"%s\" est manquant" msgid "An error occured while retrieving details of module \"%s\"" msgstr "Une erreur s'est produite lors de la récupération des informations détaillées du module \"%s\"" + +msgid "Required Centreon installed version is %s" +msgstr "La version installée de Centreon requise est %s" + +msgid "Required Centreon installed version is %s (%s installed)" +msgstr "La version installée de Centreon requise est %s (%s installée)" diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index fa79007218a..133828be0de 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -74,7 +74,7 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event) if ($currentVersion === null) { $errorMessage = sprintf('Required Centreon installed version is %s', self::MINIMAL_INSTALLED_VERSION); $this->error($errorMessage); - throw new \Exception($errorMessage); + throw new \Exception(_($errorMessage)); } $this->debug( @@ -91,7 +91,7 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event) $currentVersion, ); $this->debug($errorMessage); - throw new \Exception($errorMessage); + throw new \Exception(_($errorMessage)); } } } From 42a5560982b72db92142ba94090be721c6ba9f04 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Wed, 3 Aug 2022 16:32:46 +0200 Subject: [PATCH 7/9] compare http method --- src/EventSubscriber/UpdateEventSubscriber.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 133828be0de..d3c75f7cb83 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -23,6 +23,7 @@ use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Centreon\Domain\Log\LoggerTrait; use Core\Platform\Application\Repository\ReadVersionRepositoryInterface; @@ -59,11 +60,12 @@ public static function getSubscribedEvents(): array * @param RequestEvent $event * @throws \Exception */ - public function validateCentreonWebVersionOrFail(RequestEvent $event) + public function validateCentreonWebVersionOrFail(RequestEvent $event): void { $this->debug('Checking if route matches updates endpoint'); if ( - preg_match( + $event->getRequest()->getMethod() === Request::METHOD_PATCH + && preg_match( '#^.*/api/(?:latest|beta|v[0-9]+|v[0-9]+\.[0-9]+)/platform/updates$#', $event->getRequest()->getPathInfo(), ) From c927b1ab3197ff5acbe7c4f9d0bd45ab13101d44 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 4 Aug 2022 07:27:29 +0200 Subject: [PATCH 8/9] improve error message --- lang/fr_FR.UTF-8/LC_MESSAGES/messages.po | 14 ++++++++++---- src/EventSubscriber/UpdateEventSubscriber.php | 11 ++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po index 9459121147b..19b0adfc5a8 100644 --- a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po +++ b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po @@ -16964,8 +16964,14 @@ msgstr "Le module \"%s\" est manquant" msgid "An error occured while retrieving details of module \"%s\"" msgstr "Une erreur s'est produite lors de la récupération des informations détaillées du module \"%s\"" -msgid "Required Centreon installed version is %s" -msgstr "La version installée de Centreon requise est %s" +msgid "Centreon database schema seems not installed." +msgstr "Le schema de base de données de Centreon ne semble pas installé." -msgid "Required Centreon installed version is %s (%s installed)" -msgstr "La version installée de Centreon requise est %s (%s installée)" +msgid "Centreon database schema version is \"%s\" (\"%s\" required)." +msgstr "La version du schema de base de données de Centreon est \"%s\" (\"%s\" requise)." + +msgid "Please use Web UI to install Centreon." +msgstr "Veuillez utiliser l'interface Web pour installer Centreon." + +msgid "Please use Web UI to update Centreon." +msgstr "Veuillez utiliser l'interface Web pour mettre à jour Centreon." diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index d3c75f7cb83..41199879990 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -74,7 +74,10 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event): void $currentVersion = $this->readVersionRepository->findCurrentVersion(); if ($currentVersion === null) { - $errorMessage = sprintf('Required Centreon installed version is %s', self::MINIMAL_INSTALLED_VERSION); + $errorMessage = + _('Centreon database schema seems not installed.') + . ' ' + . _('Please use Web UI to install Centreon.'); $this->error($errorMessage); throw new \Exception(_($errorMessage)); } @@ -88,9 +91,11 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event): void ); if (version_compare($currentVersion, self::MINIMAL_INSTALLED_VERSION, '<')) { $errorMessage = sprintf( - 'Required Centreon installed version is %s (%s installed)', - self::MINIMAL_INSTALLED_VERSION, + _('Centreon database schema version is "%s" ("%s" required).') + . ' ' + . _('Please use Web UI to update Centreon.'), $currentVersion, + self::MINIMAL_INSTALLED_VERSION, ); $this->debug($errorMessage); throw new \Exception(_($errorMessage)); From c0d8620ddda2bbfb956445ac0fbe12f764816e82 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 4 Aug 2022 11:06:06 +0200 Subject: [PATCH 9/9] apply suggestions --- lang/fr_FR.UTF-8/LC_MESSAGES/messages.po | 2 +- src/EventSubscriber/UpdateEventSubscriber.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po index 19b0adfc5a8..0d03b0103ce 100644 --- a/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po +++ b/lang/fr_FR.UTF-8/LC_MESSAGES/messages.po @@ -16964,7 +16964,7 @@ msgstr "Le module \"%s\" est manquant" msgid "An error occured while retrieving details of module \"%s\"" msgstr "Une erreur s'est produite lors de la récupération des informations détaillées du module \"%s\"" -msgid "Centreon database schema seems not installed." +msgid "Centreon database schema does not seem to be installed." msgstr "Le schema de base de données de Centreon ne semble pas installé." msgid "Centreon database schema version is \"%s\" (\"%s\" required)." diff --git a/src/EventSubscriber/UpdateEventSubscriber.php b/src/EventSubscriber/UpdateEventSubscriber.php index 41199879990..386bbe07db1 100644 --- a/src/EventSubscriber/UpdateEventSubscriber.php +++ b/src/EventSubscriber/UpdateEventSubscriber.php @@ -75,7 +75,7 @@ public function validateCentreonWebVersionOrFail(RequestEvent $event): void if ($currentVersion === null) { $errorMessage = - _('Centreon database schema seems not installed.') + _('Centreon database schema does not seem to be installed.') . ' ' . _('Please use Web UI to install Centreon.'); $this->error($errorMessage);