From a05300dc245a9d762556bc6eb0808ab4aab6d68d Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sun, 6 Feb 2022 13:18:58 +0100 Subject: [PATCH] DbObject: trigger 404 where we get no UUID --- library/Director/Data/Db/DbObject.php | 4 ++-- library/Director/Db/Branch/UuidLookup.php | 27 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index 71ab51dc..ea8f2b86 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -1239,7 +1239,7 @@ abstract class DbObject if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) { $table = $obj->getTableName(); assert($connection instanceof Db); - $uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch()); + $uuid = UuidLookup::requireUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch()); return self::$dbObjectStore->load($table, $uuid); } @@ -1268,7 +1268,7 @@ abstract class DbObject if (self::$dbObjectStore !== null && $obj->hasUuidColumn()) { $table = $obj->getTableName(); assert($connection instanceof Db); - $uuid = UuidLookup::findUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch()); + $uuid = UuidLookup::requireUuidForKey($id, $table, $connection, self::$dbObjectStore->getBranch()); return self::$dbObjectStore->load($table, $uuid); } diff --git a/library/Director/Db/Branch/UuidLookup.php b/library/Director/Db/Branch/UuidLookup.php index 097a1c3f..663ebd6b 100644 --- a/library/Director/Db/Branch/UuidLookup.php +++ b/library/Director/Db/Branch/UuidLookup.php @@ -2,10 +2,12 @@ namespace Icinga\Module\Director\Db\Branch; +use Icinga\Exception\NotFoundError; use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaServiceSet; use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidInterface; use RuntimeException; use function is_int; use function is_resource; @@ -57,6 +59,31 @@ class UuidLookup return $uuid; } + /** + * @param int|string|array $key + * @param string $table + * @param Db $connection + * @param Branch $branch + * @return UuidInterface + * @throws NotFoundError + */ + public static function requireUuidForKey($key, $table, Db $connection, Branch $branch) + { + $uuid = self::findUuidForKey($key, $table, $connection, $branch); + if ($uuid === null) { + throw new NotFoundError('No such object available'); + } + + return $uuid; + } + + /** + * @param int|string|array $key + * @param string $table + * @param Db $connection + * @param Branch $branch + * @return ?UuidInterface + */ public static function findUuidForKey($key, $table, Db $connection, Branch $branch) { $db = $connection->getDbAdapter();