IcingaService: enforce zone for apply rules

fixes #1621
fixes #1634
This commit is contained in:
Thomas Gelf 2019-09-25 13:28:59 +02:00
parent a540a716c2
commit 95d68aefeb
3 changed files with 45 additions and 4 deletions

View File

@ -480,14 +480,24 @@ class IcingaService extends IcingaObject implements ExportInterface
/** /**
* @return string * @return string
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\NestingError
*/ */
protected function renderSuffix() protected function renderSuffix()
{ {
if ($this->isApplyRule() || $this->usesVarOverrides()) { $suffix = '';
return $this->renderImportHostVarOverrides() . parent::renderSuffix(); if ($this->isApplyRule()) {
} else { $zoneName = $this->getRenderingZone();
return parent::renderSuffix(); if (!IcingaZone::zoneNameIsGlobal($zoneName, $this->connection)) {
$suffix .= c::renderKeyValue('zone', c::renderString($zoneName));
}
} }
if ($this->isApplyRule() || $this->usesVarOverrides()) {
$suffix .= $this->renderImportHostVarOverrides();
}
return $suffix . parent::renderSuffix();
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\IcingaConfig\IcingaConfig; use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c; use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
@ -29,6 +30,8 @@ class IcingaZone extends IcingaObject
protected $supportsImports = true; protected $supportsImports = true;
protected static $globalZoneNames;
private $endpointList; private $endpointList;
protected function renderCustomExtensions() protected function renderCustomExtensions()
@ -41,6 +44,28 @@ class IcingaZone extends IcingaObject
return c::renderKeyValue('endpoints', c::renderArray($endpoints)); return c::renderKeyValue('endpoints', c::renderArray($endpoints));
} }
public function isGlobal()
{
return $this->get('is_global') === 'y';
}
public static function zoneNameIsGlobal($name, Db $connection)
{
if (self::$globalZoneNames === null) {
$db = $connection->getDbAdapter();
self::setCachedGlobalZoneNames($db->fetchCol(
$db->select()->from('icinga_zone', 'object_name')->where('is_global = ?', 'y')
));
}
return \in_array($name, self::$globalZoneNames);
}
public static function setCachedGlobalZoneNames($names)
{
self::$globalZoneNames = $names;
}
public function getRenderingZone(IcingaConfig $config = null) public function getRenderingZone(IcingaConfig $config = null)
{ {
// If the zone has a parent zone... // If the zone has a parent zone...

View File

@ -9,6 +9,7 @@ use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\Migrations; use Icinga\Module\Director\Db\Migrations;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaZone;
use PHPUnit_Framework_TestCase; use PHPUnit_Framework_TestCase;
abstract class BaseTestCase extends PHPUnit_Framework_TestCase abstract class BaseTestCase extends PHPUnit_Framework_TestCase
@ -77,6 +78,11 @@ abstract class BaseTestCase extends PHPUnit_Framework_TestCase
self::$db = new Db($dbConfig); self::$db = new Db($dbConfig);
$migrations = new Migrations(self::$db); $migrations = new Migrations(self::$db);
$migrations->applyPendingMigrations(); $migrations->applyPendingMigrations();
IcingaZone::create([
'object_name' => 'director-global',
'object_type' => 'external_object',
'is_global' => 'y'
])->store(self::$db);
} }
return self::$db; return self::$db;