Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect protected custom vars #25

Merged
merged 1 commit into from Apr 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 45 additions & 2 deletions library/Cube/Ido/IdoHostStatusCube.php
Expand Up @@ -2,8 +2,17 @@

namespace Icinga\Module\Cube\Ido;

use Icinga\Application\Config;

class IdoHostStatusCube extends IdoCube
{
/**
* Cache for {@link filterProtectedCustomvars()}
*
* @var string|null
*/
protected $protectedCustomvars;

public function getRenderer()
{
return new IdoHostStatusCubeRenderer($this);
Expand Down Expand Up @@ -34,7 +43,11 @@ public function getAvailableFactColumns()
*/
public function addDimensionByName($name)
{
return $this->addDimension(new CustomVarDimension($name));
if (count($this->filterProtectedCustomvars(array($name))) === 1) {
$this->addDimension(new CustomVarDimension($name));
}

return $this;
}

/**
Expand All @@ -57,7 +70,7 @@ public function listAvailableDimensions()
$select->where('cv.is_json = 0');
}

return $this->db()->fetchCol($select);
return $this->filterProtectedCustomvars($this->db()->fetchCol($select));
}

public function prepareInnerQuery()
Expand All @@ -77,4 +90,34 @@ public function prepareInnerQuery()

return $select;
}

/**
* Return the given array without values matching the custom variables protected by the monitoring module
*
* @param string[] $customvars
*
* @return string[]
*/
protected function filterProtectedCustomvars(array $customvars)
{
if ($this->protectedCustomvars === null) {
$config = Config::module('monitoring')->get('security', 'protected_customvars');
$protectedCustomvars = array();

foreach (preg_split('~,~', $config, -1, PREG_SPLIT_NO_EMPTY) as $pattern) {
$regex = array();
foreach (explode('*', $pattern) as $literal) {
$regex[] = preg_quote($literal, '/');
}

$protectedCustomvars[] = implode('.*', $regex);
}

$this->protectedCustomvars = empty($protectedCustomvars)
? '/^$/'
: '/^(?:' . implode('|', $protectedCustomvars) . ')$/';
}

return preg_grep($this->protectedCustomvars, $customvars, PREG_GREP_INVERT);
}
}