Db: clean up methods related to binary PG data

This commit is contained in:
Thomas Gelf 2021-08-16 06:14:34 +02:00
parent 62a31c7858
commit 5de3a6a497
4 changed files with 21 additions and 35 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Data\Db; namespace Icinga\Module\Director\Data\Db;
use Icinga\Data\Db\DbConnection as IcingaDbConnection; use Icinga\Data\Db\DbConnection as IcingaDbConnection;
use RuntimeException;
use Zend_Db_Expr; use Zend_Db_Expr;
class DbConnection extends IcingaDbConnection class DbConnection extends IcingaDbConnection
@ -19,6 +20,10 @@ class DbConnection extends IcingaDbConnection
public function quoteBinary($binary) public function quoteBinary($binary)
{ {
if ($binary instanceof Zend_Db_Expr) {
throw new RuntimeException('Trying to escape binary twice');
}
if ($this->isPgsql()) { if ($this->isPgsql()) {
return new Zend_Db_Expr("'\\x" . bin2hex($binary) . "'"); return new Zend_Db_Expr("'\\x" . bin2hex($binary) . "'");
} }
@ -26,6 +31,15 @@ class DbConnection extends IcingaDbConnection
return $binary; return $binary;
} }
public function binaryDbResult($value)
{
if (is_resource($value)) {
return stream_get_contents($value);
}
return $value;
}
public function hasPgExtension($name) public function hasPgExtension($name)
{ {
$db = $this->db(); $db = $this->db();

View File

@ -760,12 +760,9 @@ abstract class DbObject
unset($properties[$this->autoincKeyName]); unset($properties[$this->autoincKeyName]);
} }
} }
// TODO: Remove this!
if ($this->connection->isPgsql()) {
foreach ($properties as $key => $value) { foreach ($properties as $key => $value) {
if ($this->isBinaryColumn($key)) { if ($this->isBinaryColumn($key)) {
$properties[$key] = Util::pgBinEscape($value); $properties[$key] = $this->getConnection()->quoteBinary($value);
}
} }
} }

View File

@ -11,7 +11,6 @@ use Icinga\Module\Director\Db\Cache\PrefetchCache;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Module\Director\Hook\ShipConfigFilesHook; use Icinga\Module\Director\Hook\ShipConfigFilesHook;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaZone; use Icinga\Module\Director\Objects\IcingaZone;
use InvalidArgumentException; use InvalidArgumentException;
@ -270,7 +269,7 @@ class IcingaConfig
$checksum = $this->calculateChecksum(); $checksum = $this->calculateChecksum();
$activity = $this->getLastActivityChecksum(); $activity = $this->getLastActivityChecksum();
$lastActivity = $this->binFromDb( $lastActivity = $this->connection->binaryDbResult(
$this->db->fetchOne( $this->db->fetchOne(
$this->db->select()->from( $this->db->select()->from(
self::$table, self::$table,
@ -310,20 +309,7 @@ class IcingaConfig
protected function dbBin($binary) protected function dbBin($binary)
{ {
if ($this->connection->isPgsql()) { return $this->connection->quoteBinary($binary);
return Util::pgBinEscape($binary);
} else {
return $binary;
}
}
protected function binFromDb($value)
{
if (is_resource($value)) {
return stream_get_contents($value);
}
return $value;
} }
protected function calculateChecksum() protected function calculateChecksum()
@ -599,9 +585,9 @@ if (! globals.contains(DirectorOverrideTemplate)) {
throw new NotFoundError('Got no config for %s', bin2hex($checksum)); throw new NotFoundError('Got no config for %s', bin2hex($checksum));
} }
$this->checksum = $this->binFromDb($result->checksum); $this->checksum = $this->connection->binaryDbResult($result->checksum);
$this->generationTime = $result->duration; $this->generationTime = $result->duration;
$this->lastActivityChecksum = $this->binFromDb($result->last_activity_checksum); $this->lastActivityChecksum = $this->connection->binaryDbResult($result->last_activity_checksum);
$query = $this->db->select()->from( $query = $this->db->select()->from(
array('cf' => 'director_generated_config_file'), array('cf' => 'director_generated_config_file'),

View File

@ -9,8 +9,6 @@ use Icinga\Exception\NotImplementedError;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use ipl\Html\Html; use ipl\Html\Html;
use gipfl\IcingaWeb2\Link; use gipfl\IcingaWeb2\Link;
use RuntimeException;
use Zend_Db_Expr;
class Util class Util
{ {
@ -18,15 +16,6 @@ class Util
protected static $allowedResources; protected static $allowedResources;
public static function pgBinEscape($binary)
{
if ($binary instanceof Zend_Db_Expr) {
throw new RuntimeException('Trying to escape binary twice');
}
return new Zend_Db_Expr("'\\x" . bin2hex($binary) . "'");
}
/** /**
* PBKDF2 - Password-Based Cryptography Specification (RFC2898) * PBKDF2 - Password-Based Cryptography Specification (RFC2898)
* *