Skip to content
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

feat: add auth to healthcheck #294

Merged
merged 5 commits into from
May 23, 2024
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
15 changes: 12 additions & 3 deletions controllers/front/apiHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,41 @@

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Controller\AbstractApiController;
use PrestaShop\Module\PsEventbus\Exception\UnauthorizedException;
use PrestaShop\Module\PsEventbus\Repository\ServerInformationRepository;

class ps_EventbusApiHealthCheckModuleFrontController extends AbstractApiController
{
public $type = Config::COLLECTION_SHOPS;

private $isAuthentifiedCall = true;

/**
* Override default method from AbstractApiController: skip jobId verification
* Override default method from AbstractApiController
* Get another behavior for healthcheck
*
* @return void
*/
public function init()
{
try {
parent::init();
} catch (UnauthorizedException $exception) {
$this->isAuthentifiedCall = false;
}
}

/**
* @return void
*
* @throws\PrestaShopException
* @throws \PrestaShopException
*/
public function postProcess()
{
/** @var ServerInformationRepository $serverInformationRepository */
$serverInformationRepository = $this->module->getService(ServerInformationRepository::class);

$status = $serverInformationRepository->getHealthCheckData();
$status = $serverInformationRepository->getHealthCheckData($this->isAuthentifiedCall);

$this->exitWithResponse($status);
}
Expand Down
29 changes: 19 additions & 10 deletions src/Controller/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PrestaShop\Module\PsEventbus\Exception\EnvVarException;
use PrestaShop\Module\PsEventbus\Exception\FirebaseException;
use PrestaShop\Module\PsEventbus\Exception\QueryParamsException;
use PrestaShop\Module\PsEventbus\Exception\UnauthorizedException;
use PrestaShop\Module\PsEventbus\Handler\ErrorHandler\ErrorHandler;
use PrestaShop\Module\PsEventbus\Provider\PaginatedApiDataProviderInterface;
use PrestaShop\Module\PsEventbus\Repository\EventbusSyncRepository;
Expand Down Expand Up @@ -99,23 +100,31 @@ public function __construct()
}

/**
* @return void
* @return bool|void
*/
public function init()
{
$this->startTime = time();

try {
$this->authorize();
} catch (\PrestaShopDatabaseException $exception) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
} catch (EnvVarException $exception) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
} catch (FirebaseException $exception) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
} catch (\Exception $exception) {
// For ApiHealthCheck, handle the error, and throw UnauthorizedException directly, to catch-up at top level.
if (str_contains($this->page_name, 'apiHealthCheck')) {
$this->errorHandler->handle($exception);
throw new UnauthorizedException('You are not allowed to access to this resource');
}

if ($exception instanceof \PrestaShopDatabaseException) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
} elseif ($exception instanceof EnvVarException) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
} elseif ($exception instanceof FirebaseException) {
$this->errorHandler->handle($exception);
$this->exitWithExceptionMessage($exception);
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/Repository/ServerInformationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function getServerInformation($langIso = '')
/**
* @return array
*/
public function getHealthCheckData()
public function getHealthCheckData(bool $isAuthentifiedCall)
{
$tokenValid = false;
$tokenIsSet = true;
Expand Down Expand Up @@ -171,11 +171,15 @@ public function getHealthCheckData()
$phpVersion = (string) explode('-', (string) phpversion())[0];
}

return [
$sensibleInformation = [
'prestashop_version' => _PS_VERSION_,
'ps_eventbus_version' => \Ps_eventbus::VERSION,
'ps_accounts_version' => defined('Ps_accounts::VERSION') ? \Ps_accounts::VERSION : false, /* @phpstan-ignore-line */
'php_version' => $phpVersion,
'shop_id' => $this->psAccountsAdapterService->getShopUuid(),
];

$serverInformation = [
'ps_account' => $tokenIsSet,
'is_valid_jwt' => $tokenValid,
'ps_eventbus' => $allTablesInstalled,
Expand All @@ -185,6 +189,12 @@ public function getHealthCheckData()
'EVENT_BUS_LIVE_SYNC_API_URL' => isset($this->configuration['EVENT_BUS_LIVE_SYNC_API_URL']) ? $this->configuration['EVENT_BUS_LIVE_SYNC_API_URL'] : null,
],
];

if ($isAuthentifiedCall) {
$serverInformation = array_merge($sensibleInformation, $serverInformation);
}

return $serverInformation;
}

/**
Expand Down
Loading