From 7bc489ba4d0d4f7feb7c7376f48a32957b7b65fb Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Fri, 22 Jan 2016 18:37:27 +0100 Subject: [PATCH] MonitoredObject: obfuscate custom variables recursively refs #10640 --- .../Monitoring/Object/MonitoredObject.php | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/modules/monitoring/library/Monitoring/Object/MonitoredObject.php b/modules/monitoring/library/Monitoring/Object/MonitoredObject.php index 4a787d309..37d1d48d7 100644 --- a/modules/monitoring/library/Monitoring/Object/MonitoredObject.php +++ b/modules/monitoring/library/Monitoring/Object/MonitoredObject.php @@ -3,6 +3,7 @@ namespace Icinga\Module\Monitoring\Object; +use stdClass; use InvalidArgumentException; use Icinga\Application\Config; use Icinga\Data\Filter\Filter; @@ -456,18 +457,34 @@ abstract class MonitoredObject implements Filterable $customvars = $this->hostVariables; } - $this->customvars = array(); - foreach ($customvars as $name => $value) { - if ($blacklistPattern && preg_match($blacklistPattern, $name)) { - $this->customvars[$name] = '***'; - } else { - $this->customvars[$name] = $value; - } - } + $this->customvars = $this->obfuscateCustomVars($customvars, $blacklistPattern); return $this; } + /** + * Obfuscate custom variables recursively for $this->customvars. + * + * @param stdClass|array $customvars The custom variables to obfuscate + * @param string $blacklistPattern Which custom variables to obfuscate + * + * @return stdClass|array The obfuscated custom variables + */ + protected function obfuscateCustomVars($customvars, $blacklistPattern) + { + $obfuscatedCustomVars = array(); + foreach ($customvars as $name => $value) { + if ($blacklistPattern && preg_match($blacklistPattern, $name)) { + $obfuscatedCustomVars[$name] = '***'; + } else { + $obfuscatedCustomVars[$name] = $value instanceof stdClass || is_array($value) + ? $this->obfuscateCustomVars($value, $blacklistPattern) + : $value; + } + } + return $customvars instanceof stdClass ? (object) $obfuscatedCustomVars : $obfuscatedCustomVars; + } + /** * Fetch the host custom variables related to this object *