From 677f5dc72a848bf898dd246d1b42993019492f57 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 17 Nov 2023 14:13:03 +0100 Subject: [PATCH] Monitoring/IcingadbBackend: Handle if $hostName or $serviceName is null --- .../Director/Integration/BackendInterface.php | 24 ++++++------- .../Integration/Icingadb/IcingadbBackend.php | 36 ++++++++++++++----- .../MonitoringModule/Monitoring.php | 26 ++++++++------ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/library/Director/Integration/BackendInterface.php b/library/Director/Integration/BackendInterface.php index 4af6ae8d..e89e1392 100644 --- a/library/Director/Integration/BackendInterface.php +++ b/library/Director/Integration/BackendInterface.php @@ -16,47 +16,47 @@ interface BackendInterface /** * Whether the backend has the given host * - * @param string $hostname + * @param ?string $hostName * * @return bool */ - public function hasHost(string $hostname): bool; + public function hasHost(?string $hostName): bool; /** * Whether the backend has the given service of the specified host * - * @param string $hostname - * @param string $service + * @param ?string $hostName + * @param ?string $serviceName * * @return bool */ - public function hasService(string $hostname, string $service): bool; + public function hasService(?string $hostName, ?string $serviceName): bool; /** * Whether an authenticated user has the permission (is not restricted) to modify given host * - * @param string $hostName + * @param ?string $hostName * * @return bool */ - public function canModifyHost(string $hostName): bool; + public function canModifyHost(?string $hostName): bool; /** * Whether an authenticated user has the permission (is not restricted) to modify given service of specified host * - * @param string $hostName - * @param string $serviceName + * @param ?string $hostName + * @param ?string $serviceName * * @return bool */ - public function canModifyService(string $hostName, string $serviceName): bool; + public function canModifyService(?string $hostName, ?string $serviceName): bool; /** * Get the url of given host * - * @param string $hostname + * @param ?string $hostName * * @return Url */ - public function getHostUrl(string $hostname): Url; + public function getHostUrl(?string $hostName): ?Url; } diff --git a/library/Director/Integration/Icingadb/IcingadbBackend.php b/library/Director/Integration/Icingadb/IcingadbBackend.php index 5279652c..299b9ff9 100644 --- a/library/Director/Integration/Icingadb/IcingadbBackend.php +++ b/library/Director/Integration/Icingadb/IcingadbBackend.php @@ -21,22 +21,30 @@ class IcingadbBackend implements BackendInterface return Module::exists('icingadb'); } - public function hasHost($hostname): bool + public function hasHost(?string $hostName): bool { + if ($hostName === null) { + return false; + } + $query = Host::on($this->getDb()) - ->filter(Filter::equal('host.name', $hostname)); + ->filter(Filter::equal('host.name', $hostName)); $this->applyRestrictions($query); return $query->first() !== null; } - public function hasService($hostname, $service): bool + public function hasService(?string $hostName, ?string $serviceName): bool { + if ($hostName === null || $serviceName === null) { + return false; + } + $query = Service::on($this->getDb()) ->filter(Filter::all( - Filter::equal('service.name', $service), - Filter::equal('host.name', $hostname) + Filter::equal('service.name', $serviceName), + Filter::equal('host.name', $hostName) )); $this->applyRestrictions($query); @@ -44,19 +52,29 @@ class IcingadbBackend implements BackendInterface return $query->first() !== null; } - public function getHostUrl(string $hostname): Url + public function getHostUrl(?string $hostName): ?Url { - return Url::fromPath('icingadb/host', ['name' => $hostname]); + if ($hostName === null) { + return null; + } + + return Url::fromPath('icingadb/host', ['name' => $hostName]); } - public function canModifyHost(string $hostName): bool + public function canModifyHost(?string $hostName): bool { + if ($hostName === null) { + return false; + } // TODO: Implement canModifyService() method. return false; } - public function canModifyService(string $hostName, string $serviceName): bool + public function canModifyService(?string $hostName, ?string $serviceName): bool { + if ($hostName === null || $serviceName === null) { + return false; + } // TODO: Implement canModifyService() method. return false; } diff --git a/library/Director/Integration/MonitoringModule/Monitoring.php b/library/Director/Integration/MonitoringModule/Monitoring.php index 6d9fa201..82ea9968 100644 --- a/library/Director/Integration/MonitoringModule/Monitoring.php +++ b/library/Director/Integration/MonitoringModule/Monitoring.php @@ -33,38 +33,42 @@ class Monitoring implements BackendInterface return $this->backend !== null; } - public function getHostUrl(string $hostname): Url + public function getHostUrl(?string $hostName): ?Url { - return Url::fromPath('monitoring/host/show', ['host' => $hostname]); + if ($hostName === null) { + return null; + } + + return Url::fromPath('monitoring/host/show', ['host' => $hostName]); } - public function hasHost($hostname): bool + public function hasHost(?string $hostName): bool { - if (! $this->isAvailable()) { + if ($hostName === null || ! $this->isAvailable()) { return false; } try { - return $this->selectHost($hostname)->fetchOne() === $hostname; + return $this->selectHost($hostName)->fetchOne() === $hostName; } catch (Exception $_) { return false; } } - public function hasService($hostname, $service): bool + public function hasService(?string $hostName, ?string $serviceName): bool { - if (! $this->isAvailable()) { + if ($hostName === null || $serviceName === null || ! $this->isAvailable()) { return false; } try { - return $this->rowIsService($this->selectService($hostname, $service)->fetchRow(), $hostname, $service); + return $this->rowIsService($this->selectService($hostName, $serviceName)->fetchRow(), $hostName, $serviceName); } catch (Exception $_) { return false; } } - public function canModifyService(string $hostName, string $serviceName): bool + public function canModifyService(?string $hostName, ?string $serviceName): bool { if (! $this->isAvailable() || $hostName === null || $serviceName === null) { return false; @@ -84,9 +88,9 @@ class Monitoring implements BackendInterface return false; } - public function canModifyHost(string $hostName): bool + public function canModifyHost(?string $hostName): bool { - if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) { + if ($hostName !== null && $this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) { $restriction = null; foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) { if ($this->hasHostWithFilter($hostName, Filter::fromQueryString($restriction))) {