webui: Added Oxidized config validator (#7983)

* webui: Added Oxidized config validator

* final updates

* small changes like box height + indicate config is valid

* docs + small changes
This commit is contained in:
Neil Lathwood
2018-01-11 13:21:05 +00:00
committed by Tony Murray
parent e11e84f2ee
commit d26d142700
4 changed files with 195 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ $pagetitle[] = 'Oxidized';
<ul class="nav nav-tabs">
<li class="active"><a href="#list" data-toggle="tab">Node List</a></li>
<li><a href="#search" data-toggle="tab">Config Search</a></li>
<li><a href="<?php echo generate_url(array('page' => 'tools', 'tool' => 'oxidized-cfg-check')); ?>">Oxidized config validation</a></li>
</ul>
</div>
<div class="panel with-nav-tabs panel-default">

39
html/pages/tools.inc.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
$no_refresh = true;
$pagetitle[] = 'Tools';
$sections = array(
'oxidized-cfg-check' => 'Oxidized Config Checker',
);
print_optionbar_start('', '');
echo '<span style="font-weight: bold;">Tools</span> &#187; ';
unset($sep);
foreach ($sections as $type => $texttype) {
echo $sep;
if ($vars['search'] == $type) {
echo "<span class='pagemenu-selected'>";
}
echo generate_link($texttype, array('page' => 'tools', 'tool' => $type));
if ($vars['search'] == $type) {
echo '</span>';
}
$sep = ' | ';
}
unset($sep);
print_optionbar_end();
if (file_exists('pages/tools/'.$vars['tool'].'.inc.php')) {
include 'pages/tools/'.$vars['tool'].'.inc.php';
} else {
echo report_this('Unknown tool type '.$vars['tool']);
}

View File

@@ -0,0 +1,148 @@
<?php
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
if (isset($_POST['config'])) {
try {
$oxidized_cfg = Yaml::parse($_POST['config']);
$validate_cfg = validate_oxidized_cfg($oxidized_cfg);
foreach ($validate_cfg as $error) {
echo "<div class='alert alert-danger'>$error</div>";
}
if (empty($validate_cfg)) {
echo '<div class="alert alert-success">Config has validated ok</div>';
}
} catch (ParseException $e) {
echo "<div class='alert alert-danger'>{$e->getMessage()}</div>";
}
}
?>
<form method="post">
<div class="form-group">
<label for="exampleInputEmail1">Paste your Oxidized yaml config:</label>
<textarea name="config" value="config" rows="20" class="form-control" placeholder="Paste your Oxidized yaml config"><?php echo $_POST['config']; ?></textarea>
</div>
<button type="submit" class="btn btn-default btn-primary">Validate YAML</button>
</form>
<?php
function validate_oxidized_cfg($tree, $wanted_leaf = false)
{
$valid_config = array(
'username' => 'string',
'password' => 'string',
'model' => 'string',
'interval' => 'int',
'use_syslog' => 'boolean',
'log' => 'string',
'debug' => 'boolean',
'threads' => 'int',
'timeout' => 'int',
'retries' => 'int',
'prompt' => 'string',
'models' => 'array',
'vars' => array(
'enable' => 'boolean',
'ssh_no_exec' => 'boolean',
'remove_secret' => 'boolean',
),
'groups' => 'array',
'rest' => 'string',
'pid' => 'string',
'input' => array(
'default' => 'string',
'debug' => 'boolean',
'ssh' => array(
'secure' => 'boolean',
),
),
'output' => array(
'default' => 'string',
'git' => array(
'user' => 'string',
'email' => 'string',
'repo' => 'string',
),
),
'source' => array(
'default' => 'string',
'csv' => array(
'file' => 'string',
'delimiter' => 'string',
'map' => array(
'name' => 'string',
'model' => 'string',
'username' => 'string',
'password' => 'string',
'group' => 'group',
),
'vars_map' => array(
'enable' => 'string',
),
),
'http' => array(
'url' => 'string',
'scheme' => 'string',
'secure' => 'boolean',
'delimiter' => 'string',
'user' => 'string',
'pass' => 'string',
'map' => array(
'name' => 'string',
'model' => 'string',
'username' => 'string',
'password' => 'string',
'group' => 'group',
),
'vars_map' => 'array',
'headers' => array(
'X-Auth-Token' => 'string',
),
),
'mysql' => array(
'adapter' => 'string',
'database' => 'string',
'table' => 'string',
'user' => 'string',
'password' => 'password',
'map' => array(
'name' => 'string',
'model' => 'string',
'username' => 'string',
'password' => 'string',
'group' => 'group',
),
'vars_map' => 'array',
),
),
'model_map' => 'array',
'next_adds_job' => 'boolean',
'hooks' => 'array',
);
if ($wanted_leaf !== false) {
$valid_config_tmp = $wanted_leaf;
} else {
$valid_config_tmp = $valid_config;
}
$output = array();
foreach ($tree as $leaf => $value) {
if (is_array($tree[$leaf]) && ($valid_config_tmp !== 'array' && $valid_config_tmp[$leaf] !== 'array')) {
$tmp_output = validate_oxidized_cfg($tree[$leaf], $valid_config_tmp[$leaf]);
if (is_array($tmp_output)) {
$output = array_merge($output, $tmp_output);
}
} else {
if (!isset($valid_config_tmp[$leaf]) && ($valid_config_tmp !== 'array' && $valid_config_tmp[$leaf] !== 'array')) {
$output[] = "$leaf - is not valid";
}
}
}
if (!empty($output)) {
return $output;
}
}