Skip to content

Commit

Permalink
Merge pull request #113 from LibreSign/add_health_check
Browse files Browse the repository at this point in the history
Add health check
  • Loading branch information
vitormattos authored Mar 11, 2021
2 parents 182a385 + bca1f26 commit 51ae58b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/Handler/CfsslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,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'];
}
}
16 changes: 15 additions & 1 deletion lib/Service/AdminSignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down

0 comments on commit 51ae58b

Please sign in to comment.