From bca1f261af427d8511f1d5a46a664e9d6c76fcf5 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Thu, 11 Mar 2021 08:03:15 -0300 Subject: [PATCH] Add health check --- lib/Handler/CfsslHandler.php | 23 +++++++++++++++++++++++ lib/Service/AdminSignatureService.php | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Handler/CfsslHandler.php b/lib/Handler/CfsslHandler.php index 8d82b7777c..c9cc558826 100644 --- a/lib/Handler/CfsslHandler.php +++ b/lib/Handler/CfsslHandler.php @@ -75,4 +75,27 @@ private function newCert( return $responseDecoded['result']; } + + public function health(string $cfsslUri) { + try { + $response = (new Client(['base_uri' => $cfsslUri])) + ->request( + 'GET', + 'health' + ) + ; + } catch (TransferException $th) { + if ($th->getHandlerContext() && $th->getHandlerContext()['error']) { + throw new \Exception($th->getHandlerContext()['error'], 1); + } + throw new LibresignException($th->getMessage(), 500); + } + + $responseDecoded = json_decode($response->getBody(), true); + if (!$responseDecoded['success']) { + throw new LibresignException('Error while check cfssl API health!', 500); + } + + return $responseDecoded['result']; + } } diff --git a/lib/Service/AdminSignatureService.php b/lib/Service/AdminSignatureService.php index 90ad74f770..37e01239b3 100644 --- a/lib/Service/AdminSignatureService.php +++ b/lib/Service/AdminSignatureService.php @@ -3,17 +3,25 @@ namespace OCA\Libresign\Service; use OCA\Libresign\AppInfo\Application; +use OCA\Libresign\Handler\CfsslHandler; use OCA\Libresign\Handler\CfsslServerHandler; use OCP\IConfig; class AdminSignatureService { /** @var CfsslServerHandler */ + private $cfsslServerHandler; + /** @var CfsslHandler */ private $cfsslHandler; /** @var IConfig */ private $config; - public function __construct(CfsslServerHandler $cfsslHandler, IConfig $config) { + public function __construct( + CfsslServerHandler $cfsslServerHandler, + CfsslHandler $cfsslHandler, + IConfig $config + ) { + $this->cfsslServerHandler = $cfsslServerHandler; $this->cfsslHandler = $cfsslHandler; $this->config = $config; } @@ -36,6 +44,12 @@ public function generate( $key, $configPath ); + for ($i = 1;$i <= 2;$i++) { + sleep($i); + if ($this->cfsslHandler->health($cfsslUri)) { + break; + } + } $this->config->setAppValue(Application::APP_ID, 'authkey', $key); $this->config->setAppValue(Application::APP_ID, 'commonName', $commonName);