Skip to content

Commit

Permalink
Make Database trait a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Nov 16, 2023
1 parent b6d2c31 commit 7b2c234
Show file tree
Hide file tree
Showing 25 changed files with 76 additions and 62 deletions.
4 changes: 2 additions & 2 deletions application/clicommands/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateTimeInterface;
use Icinga\Application\Logger;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\Model\X509Target;
use ipl\Sql\Expression;
Expand Down Expand Up @@ -67,8 +68,7 @@ public function hostAction()
exit(3);
}

$conn = $this->getDb();
$targets = X509Target::on($conn)->with([
$targets = X509Target::on(Database::get())->with([
'chain',
'chain.certificate',
'chain.certificate.issuer_certificate'
Expand Down
3 changes: 2 additions & 1 deletion application/clicommands/CleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Icinga\Application\Logger;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use InvalidArgumentException;
use Throwable;

Expand Down Expand Up @@ -62,7 +63,7 @@ public function indexAction()
}

try {
$conn = $this->getDb();
$conn = Database::get();
$query = $conn->delete(
'x509_target',
['last_scan < ?' => $sinceLastScan->format('Uv')]
Expand Down
5 changes: 2 additions & 3 deletions application/clicommands/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Application\Logger;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use ipl\Sql\Connection;
use ipl\Sql\Expression;

Expand All @@ -32,13 +33,11 @@ public function indexAction()
exit(1);
}

$db = $this->getDb();

$bundle = CertificateUtils::parseBundle($file);

$count = 0;

$db->transaction(function (Connection $db) use ($bundle, &$count) {
Database::get()->transaction(function (Connection $db) use ($bundle, &$count) {
foreach ($bundle as $data) {
$cert = openssl_x509_read($data);

Expand Down
11 changes: 9 additions & 2 deletions application/clicommands/JobsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Icinga\Data\ResourceFactory;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Common\JobUtils;
use Icinga\Module\X509\Hook\SniHook;
use Icinga\Module\X509\Job;
Expand Down Expand Up @@ -152,7 +153,13 @@ public function runAction(): void
*/
protected function fetchSchedules(?string $jobName, ?string $scheduleName): array
{
$jobs = X509Job::on($this->getDb());
$conn = Database::get();
// Even if the Job class regularly pings the same connection whenever its frequency becomes due and is run by
// the scheduler, we need to explicitly ping that same connection here, as the interval of the schedule jobs
// could be larger than the daemon configuration reload interval (5m).
$conn->ping();

$jobs = X509Job::on($conn);
if ($jobName) {
$jobs->filter(Filter::equal('name', $jobName));
}
Expand Down Expand Up @@ -227,7 +234,7 @@ protected function attachJobsLogging(Scheduler $scheduler): void
);

try {
$verified = CertificateUtils::verifyCertificates($this->getDb());
$verified = CertificateUtils::verifyCertificates(Database::get());

Logger::info('Checked %d certificate chain(s)', $verified);
} catch (Exception $err) {
Expand Down
3 changes: 2 additions & 1 deletion application/clicommands/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Icinga\Application\Logger;
use Icinga\Authentication\Auth;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Job;
use Icinga\Repository\IniRepository;
use Icinga\User;
Expand Down Expand Up @@ -64,7 +65,7 @@ protected function migrateJobs(): void
];
};

$conn = $this->getDb();
$conn = Database::get();
$conn->transaction(function (Connection $conn) use ($repo) {
/** @var User $user */
$user = Auth::getInstance()->getUser();
Expand Down
5 changes: 3 additions & 2 deletions application/clicommands/ScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Icinga\Application\Logger;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Common\JobUtils;
use Icinga\Module\X509\Hook\SniHook;
use Icinga\Module\X509\Job;
Expand Down Expand Up @@ -101,7 +102,7 @@ public function indexAction(): void
}

/** @var X509Job $jobConfig */
$jobConfig = X509Job::on($this->getDb())
$jobConfig = X509Job::on(Database::get())
->filter(Filter::equal('name', $name))
->first();
if ($jobConfig === null) {
Expand Down Expand Up @@ -142,7 +143,7 @@ public function indexAction(): void
Logger::info('Scanned %d target(s) from job %s', $targets, $job->getName());

try {
$verified = CertificateUtils::verifyCertificates($this->getDb());
$verified = CertificateUtils::verifyCertificates(Database::get());

Logger::info('Checked %d certificate chain(s)', $verified);
} catch (Exception $err) {
Expand Down
5 changes: 2 additions & 3 deletions application/clicommands/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Icinga\Application\Logger;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Command;
use Icinga\Module\X509\Common\Database;

class VerifyCommand extends Command
{
Expand All @@ -19,9 +20,7 @@ class VerifyCommand extends Command
*/
public function indexAction()
{
$db = $this->getDb();

$verified = CertificateUtils::verifyCertificates($db);
$verified = CertificateUtils::verifyCertificates(Database::get());

Logger::info("Checked %d certificate chain%s.", $verified, $verified !== 1 ? 's' : '');
}
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/CertificateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\CertificateDetails;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Model\X509Certificate;
use ipl\Stdlib\Filter;
Expand All @@ -20,7 +21,7 @@ public function indexAction()
$certId = $this->params->getRequired('cert');

try {
$conn = $this->getDb();
$conn = Database::get();
} catch (ConfigurationError $_) {
$this->render('missing-resource', null, true);

Expand Down
5 changes: 3 additions & 2 deletions application/controllers/CertificatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\CertificatesTable;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\Web\Control\SearchBar\ObjectSuggestions;
Expand All @@ -21,7 +22,7 @@ public function indexAction()
$this->getTabs()->enableDataExports();

try {
$conn = $this->getDb();
$conn = Database::get();
} catch (ConfigurationError $_) {
$this->render('missing-resource', null, true);

Expand Down Expand Up @@ -105,7 +106,7 @@ public function completeAction()

public function searchEditorAction()
{
$editor = $this->createSearchEditor(X509Certificate::on($this->getDb()), [
$editor = $this->createSearchEditor(X509Certificate::on(Database::get()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM
]);
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/ChainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\ChainDetails;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\Model\X509CertificateChain;
Expand All @@ -23,7 +24,7 @@ public function indexAction()
$id = $this->params->getRequired('id');

try {
$conn = $this->getDb();
$conn = Database::get();
} catch (ConfigurationError $_) {
$this->render('missing-resource', null, true);
return;
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\CertificateUtils;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Donut;
use Icinga\Module\X509\Model\X509Certificate;
Expand All @@ -22,7 +23,7 @@ public function indexAction()
$this->getTabs()->disableLegacyExtensions();

try {
$db = $this->getDb();
$db = Database::get();
} catch (ConfigurationError $_) {
$this->render('missing-resource', null, true);
return;
Expand Down
6 changes: 2 additions & 4 deletions application/controllers/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@

class JobController extends CompatController
{
use Database;

/** @var X509Job */
protected $job;

Expand All @@ -41,7 +39,7 @@ public function init()
$jobId = $this->params->getRequired('id');

/** @var X509Job $job */
$job = X509Job::on($this->getDb())
$job = X509Job::on(Database::get())
->filter(Filter::equal('id', $jobId))
->first();

Expand Down Expand Up @@ -164,7 +162,7 @@ public function updateScheduleAction(): void
/** @var int $id */
$id = $this->params->getRequired('scheduleId');
/** @var X509Schedule $schedule */
$schedule = X509Schedule::on($this->getDb())
$schedule = X509Schedule::on(Database::get())
->filter(Filter::equal('id', $id))
->first();
if ($schedule === null) {
Expand Down
4 changes: 1 addition & 3 deletions application/controllers/JobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

class JobsController extends CompatController
{
use Database;

/**
* List all jobs
*/
Expand All @@ -29,7 +27,7 @@ public function indexAction()
'baseTarget' => '_main'
]);

$jobs = X509Job::on($this->getDb());
$jobs = X509Job::on(Database::get());
if ($this->hasPermission('config/x509')) {
$this->addControl(
(new ButtonLink($this->translate('New Job'), Url::fromPath('x509/jobs/new'), 'plus'))
Expand Down
5 changes: 3 additions & 2 deletions application/controllers/UsageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\X509\Controllers;

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\UsageTable;
Expand All @@ -22,7 +23,7 @@ public function indexAction()
$this->getTabs()->enableDataExports();

try {
$conn = $this->getDb();
$conn = Database::get();
} catch (ConfigurationError $_) {
$this->render('missing-resource', null, true);
return;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function completeAction()

public function searchEditorAction()
{
$editor = $this->createSearchEditor(X509Certificate::on($this->getDb()), [
$editor = $this->createSearchEditor(X509Certificate::on(Database::get()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM
]);
Expand Down
4 changes: 1 addition & 3 deletions application/forms/Jobs/JobConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
class JobConfigForm extends CompatForm
{
use Database;

/** @var ?X509Job */
protected $job;

Expand Down Expand Up @@ -110,7 +108,7 @@ protected function assemble(): void

protected function onSuccess(): void
{
$conn = $this->getDb();
$conn = Database::get();
/** @var FormSubmitElement $submitElement */
$submitElement = $this->getPressedSubmitElement();
if ($submitElement->getName() === 'btn_remove') {
Expand Down
4 changes: 1 addition & 3 deletions application/forms/Jobs/ScheduleForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

class ScheduleForm extends CompatForm
{
use Database;

/** @var int */
protected $jobId;

Expand Down Expand Up @@ -160,7 +158,7 @@ protected function onSuccess(): void
{
/** @var X509Schedule $schedule */
$schedule = $this->schedule;
$conn = $this->getDb();
$conn = Database::get();
/** @var FormSubmitElement $submitElement */
$submitElement = $this->getPressedSubmitElement();
if ($submitElement->getName() === 'btn_remove') {
Expand Down
5 changes: 0 additions & 5 deletions library/X509/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
namespace Icinga\Module\X509;

use Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
use Icinga\Module\X509\Common\Database;
use ipl\Sql;

class Command extends \Icinga\Cli\Command
{
use Database;

// Fix Web 2 issue where $configs is not properly initialized
protected $configs = [];

Expand Down
25 changes: 23 additions & 2 deletions library/X509/Common/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,35 @@
use ipl\Sql;
use PDO;

trait Database
final class Database
{
/** @var Sql\Connection Database connection */
private static $instance;

private function __construct()
{
}

/**
* Get the database connection
*
* @return Sql\Connection
*/
public static function get(): Sql\Connection
{
if (self::$instance === null) {
self::$instance = self::getDb();
}

return self::$instance;
}

/**
* Get the connection to the X.509 database
*
* @return Sql\Connection
*/
protected function getDb(): Sql\Connection
private static function getDb(): Sql\Connection
{
$config = new Sql\Config(ResourceFactory::getResourceConfig(
Config::module('x509')->get('backend', 'resource', 'x509')
Expand Down
Loading

0 comments on commit 7b2c234

Please sign in to comment.