Merge branch 'feature/resource-config-frontend-4786'

This commit is contained in:
Matthias Jentsch 2013-11-13 18:35:47 +01:00
commit 9886df95a4
29 changed files with 976 additions and 218 deletions

View File

@ -31,13 +31,16 @@
use \Icinga\Web\Controller\BaseConfigController;
use \Icinga\Web\Widget\Tab;
use \Icinga\Web\Url;
use \Icinga\Web\Widget\Tabs;
use \Icinga\Web\Hook\Configuration\ConfigurationTabBuilder;
use \Icinga\Application\Icinga;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Data\ResourceFactory;
use \Icinga\Form\Config\GeneralForm;
use \Icinga\Form\Config\Authentication\ReorderForm;
use \Icinga\Form\Config\Authentication\LdapBackendForm;
use \Icinga\Form\Config\Authentication\DbBackendForm;
use \Icinga\Form\Config\Resource\EditResourceForm;
use \Icinga\Form\Config\LoggingForm;
use \Icinga\Form\Config\ConfirmRemovalForm;
use \Icinga\Config\PreservingIniWriter;
@ -47,6 +50,12 @@ use \Icinga\Config\PreservingIniWriter;
*/
class ConfigController extends BaseConfigController
{
/**
* The resource types that are available.
*
* @var array
*/
private $resourceTypes = array('livestatus', 'ido', 'statusdat', 'ldap');
/**
* Create tabs for this configuration controller
@ -72,7 +81,13 @@ class ConfigController extends BaseConfigController
'url' => Url::fromPath('/config/authentication')
)
),
'resources' => new Tab(
array(
'name' => 'resource',
'title' => 'Resources',
'url' => Url::fromPath('/config/resource')
)
),
'logging' => new Tab(
array(
'name' => 'logging',
@ -210,40 +225,6 @@ class ConfigController extends BaseConfigController
$this->render('authentication');
}
/**
* Action for removing a backend from the authentication list.
*
* Redirects to the overview after removal is finished
*/
public function removeauthenticationbackendAction()
{
$configArray = IcingaConfig::app('authentication', true)->toArray();
$authBackend = $this->getParam('auth_backend');
if (!isset($configArray[$authBackend])) {
$this->view->errorMessage = 'Can\'t perform removal: Unknown Authentication Backend Provided';
$this->authenticationAction(true);
return;
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('auth_backend', $authBackend);
if ($form->isSubmittedAndValid()) {
unset($configArray[$authBackend]);
if ($this->writeAuthenticationFile($configArray)) {
$this->view->successMessage = 'Authentication Backend "' . $authBackend . '" Removed';
$this->authenticationAction(true);
}
return;
}
$this->view->form = $form;
$this->view->name = $authBackend;
$this->render('authentication/remove');
}
/**
* Action for creating a new authentication backend
*/
@ -261,7 +242,11 @@ class ConfigController extends BaseConfigController
if ($form->isSubmittedAndValid()) {
$backendCfg = IcingaConfig::app('authentication')->toArray();
foreach ($backendCfg as $backendName => $settings) {
unset($backendCfg[$backendName]['name']);
}
foreach ($form->getConfig() as $backendName => $settings) {
unset($settings->{'name'});
if (isset($backendCfg[$backendName])) {
$this->view->errorMessage = 'Backend name already exists';
$this->view->form = $form;
@ -281,6 +266,7 @@ class ConfigController extends BaseConfigController
$this->render('authentication/create');
}
/**
* Form for editing backends
*
@ -296,11 +282,24 @@ class ConfigController extends BaseConfigController
$this->authenticationAction(true);
return;
}
if (!array_key_exists('resource', $configArray[$authBackend])) {
$this->view->errorMessage = 'Configuration error: Backend "' . $authBackend . '" has no Resource';
$this->authenticationAction(true);
return;
}
if ($configArray[$authBackend]['backend'] === 'ldap') {
$form = new LdapBackendForm();
} else {
$form = new DbBackendForm();
$type = ResourceFactory::getResourceConfig($configArray[$authBackend]['resource'])->type;
switch ($type) {
case 'ldap':
$form = new LdapBackendForm();
break;
case 'db':
$form = new DbBackendForm();
break;
default:
$this->view->errorMessage = 'Can\'t edit: backend type "' . $type . '" of given resource not supported.';
$this->authenticationAction(true);
return;
}
$form->setBackendName($this->getParam('auth_backend'));
@ -315,6 +314,7 @@ class ConfigController extends BaseConfigController
if ($backendName != $authBackend) {
unset($backendCfg[$authBackend]);
}
unset($settings['name']);
}
if ($this->writeAuthenticationFile($backendCfg)) {
// redirect to overview with success message
@ -329,6 +329,137 @@ class ConfigController extends BaseConfigController
$this->render('authentication/modify');
}
/**
* Action for removing a backend from the authentication list.
*
* Redirects to the overview after removal is finished
*/
public function removeauthenticationbackendAction()
{
$configArray = IcingaConfig::app('authentication', true)->toArray();
$authBackend = $this->getParam('auth_backend');
if (!isset($configArray[$authBackend])) {
$this->view->errorMessage = 'Can\'t perform removal: Unknown Authentication Backend Provided';
$this->authenticationAction(true);
return;
}
if (!array_key_exists('resource', $configArray[$authBackend])) {
$this->view->errorMessage = 'Configuration error: Backend "' . $authBackend . '" has no Resource';
$this->authenticationAction(true);
return;
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('auth_backend', $authBackend);
if ($form->isSubmittedAndValid()) {
unset($configArray[$authBackend]);
if ($this->writeAuthenticationFile($configArray)) {
$this->view->successMessage = 'Authentication Backend "' . $authBackend . '" Removed';
$this->authenticationAction(true);
}
return;
}
$this->view->form = $form;
$this->view->name = $authBackend;
$this->render('authentication/remove');
}
public function resourceAction($showOnly = false)
{
$this->view->resources = IcingaConfig::app('resources', true)->toArray();
$this->render('resource');
}
public function createresourceAction()
{
$this->view->resourceTypes = $this->resourceTypes;
$resources = IcingaConfig::app('resources', true);
$form = new EditResourceForm();
$form->setRequest($this->_request);
if ($form->isSubmittedAndValid()) {
$name = $form->getName();
if (isset($resources->{$name})) {
$this->view->errorMessage = 'Resource name "' . $name .'" already in use.';
$this->view->form = $form;
$this->render('resource/create');
return;
}
$resources->{$name} = $form->getConfig();
if ($this->writeConfigFile($resources, 'resources')) {
$this->view->successMessage = 'Resource "' . $name . '" created.';
$this->resourceAction(true);
}
return;
}
$this->view->form = $form;
$this->render('resource/create');
}
public function editresourceAction()
{
$resources = ResourceFactory::getResourceConfigs();
$name = $this->getParam('resource');
if ($resources->get($name) === null) {
$this->view->errorMessage = 'Can\'t edit: Unknown Resource Provided';
$this->resourceAction(true);
return;
}
$form = new EditResourceForm();
if ($this->_request->isPost() === false) {
$form->setOldName($name);
$form->setName($name);
}
$form->setRequest($this->_request);
$form->setResource($resources->get($name));
if ($form->isSubmittedAndValid()) {
$oldName = $form->getOldName();
$name = $form->getName();
if ($oldName !== $name) {
unset($resources->{$oldName});
}
$resources->{$name} = $form->getConfig();
if ($this->writeConfigFile($resources, 'resources')) {
$this->view->successMessage = 'Resource "' . $name . '" created.';
$this->resourceAction(true);
}
return;
}
$this->view->form = $form;
$this->view->name = $name;
$this->render('resource/modify');
}
public function removeresourceAction()
{
$resources = ResourceFactory::getResourceConfigs()->toArray();
$name = $this->getParam('resource');
if (!isset($resources[$name])) {
$this->view->errorMessage = 'Can\'t remove: Unknown resource provided';
$this->resourceAction(true);
return;
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('resource', $name);
if ($form->isSubmittedAndValid()) {
unset($resources[$name]);
if ($this->writeConfigFile($resources, 'resources')) {
$this->view->successMessage = 'Resource "' . $name . '" removed';
$this->resourceAction(true);
}
return;
}
$this->view->name = $name;
$this->view->form = $form;
$this->render('resource/remove');
}
/**
* Write changes to an authentication file
*

View File

@ -31,7 +31,7 @@ namespace Icinga\Form\Config\Authentication;
use \Zend_Config;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Data\ResourceFactory;
use \Icinga\Web\Form;
/**
@ -121,7 +121,7 @@ abstract class BaseBackendForm extends Form
public function getResources()
{
if ($this->resources === null) {
return DbAdapterFactory::getResources();
return ResourceFactory::getResourceConfigs()->toArray();
} else {
return $this->resources;
}

View File

@ -30,7 +30,6 @@
namespace Icinga\Form\Config\Authentication;
use \Icinga\Authentication\Backend\DbUserBackend;
use \Icinga\Application\DbAdapterFactory;
use \Zend_Config;
/**

View File

@ -29,10 +29,12 @@
namespace Icinga\Form\Config\Authentication;
use \Icinga\Authentication\Backend\LdapUserBackend;
use \Exception;
use Icinga\Data\ResourceFactory;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Authentication\Backend\LdapUserBackend;
use \Icinga\Protocol\Ldap\Connection as LdapConnection;
/**
* Form for adding or modifying LDAP authentication backends
@ -42,9 +44,12 @@ class LdapBackendForm extends BaseBackendForm
/**
* Create this form and add all required elements
*
* @param $options Only useful for testing purposes:
* 'resources' => All available resources.
*
* @see Form::create()
*/
public function create()
public function create($options = array())
{
$this->setName('form_modify_backend');
$name = $this->filterName($this->getBackendName());
@ -63,48 +68,16 @@ class LdapBackendForm extends BaseBackendForm
);
$this->addElement(
'text',
'backend_' . $name . '_hostname',
'select',
'backend_' . $name . '_resource',
array(
'label' => 'LDAP Server Host',
'label' => 'Database Connection',
'required' => true,
'allowEmpty' => false,
'value' => $backend->get('hostname', 'localhost'),
'helptext' => 'The hostname or address of the LDAP server to use for authentication',
'required' => true
)
);
$this->addElement(
'text',
'backend_' . $name . '_root_dn',
array(
'label' => 'LDAP Root DN',
'value' => $backend->get('root_dn', 'ou=people,dc=icinga,dc=org'),
'helptext' => 'The path where users can be found on the ldap server',
'required' => true
)
);
$this->addElement(
'text',
'backend_' . $name . '_bind_dn',
array(
'label' => 'LDAP Bind DN',
'value' => $backend->get('bind_dn', 'cn=admin,cn=config'),
'helptext' => 'The user dn to use for querying the ldap server',
'required' => true
)
);
$this->addElement(
'password',
'backend_' . $name . '_bind_pw',
array(
'label' => 'LDAP Bind Password',
'renderPassword' => true,
'value' => $backend->get('bind_pw', 'admin'),
'helptext' => 'The password to use for querying the ldap server',
'required' => true
'helptext' => 'The database connection to use for authenticating with this provider',
'value' => $this->getBackend()->get('resource'),
'multiOptions' => array_key_exists('resources', $options) ?
$options['resources'] : $this->getLdapResources()
)
);
@ -157,14 +130,10 @@ class LdapBackendForm extends BaseBackendForm
$section = $this->getValue($prefix . 'name');
$cfg = array(
'backend' => 'ldap',
'target' => 'user',
'hostname' => $this->getValue($prefix . 'hostname'),
'root_dn' => $this->getValue($prefix . 'root_dn'),
'bind_dn' => $this->getValue($prefix . 'bind_dn'),
'bind_pw' => $this->getValue($prefix . 'bind_pw'),
'user_class' => $this->getValue($prefix . 'user_class'),
'user_name_attribute' => $this->getValue($prefix . 'user_name_attribute')
'target' => 'user',
'resource' => $this->getValue($prefix . 'resource'),
'user_class' => $this->getValue($prefix . 'user_class'),
'user_name_attribute' => $this->getValue($prefix . 'user_name_attribute')
);
return array(
$section => $cfg
@ -182,21 +151,26 @@ class LdapBackendForm extends BaseBackendForm
try {
$cfg = $this->getConfig();
$backendName = 'backend_' . $this->filterName($this->getBackendName()) . '_name';
$testConn = new LdapUserBackend(
new Zend_Config($cfg[$this->getValue($backendName)])
);
$backendConfig = new Zend_Config($cfg[$this->getValue($backendName)]);
$testConn = new LdapUserBackend($backendConfig);
if ($testConn->getUserCount() === 0) {
throw new Exception('No Users Found On Directory Server');
}
} catch (Exception $exc) {
$this->addErrorMessage(
'Connection Validation Failed:'.
$exc->getMessage()
'Connection Validation Failed:' . $exc->getMessage()
);
return false;
}
return true;
}
private function getLdapResources()
{
$res = ResourceFactory::getResourceConfigs('ldap')->toArray();
foreach ($res as $key => $value) {
$res[$key] = $key;
}
return $res;
}
}

View File

@ -35,7 +35,7 @@ use \Zend_Form_Element_Text;
use \Zend_Form_Element_Select;
use \Zend_View_Helper_DateFormat;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Data\ResourceFactory;
use \Icinga\Web\Form;
use \Icinga\Web\Form\Validator\WritablePathValidator;
use \Icinga\Web\Form\Validator\TimeFormatValidator;
@ -132,7 +132,7 @@ class GeneralForm extends Form
public function getResources()
{
if ($this->resources === null) {
return DbAdapterFactory::getResources();
return ResourceFactory::getResourceConfigs()->toArray();
} else {
return $this->resources;
}

View File

@ -0,0 +1,12 @@
<?php
namespace Icinga\Module\Monitoring\Form\Config\Backend;
use Icinga\Module\Monitoring\Form\Config\Backend\EditResourceForm;
class CreateResourceForm extends EditResourceForm {
public function __construct()
{
}
}

View File

@ -0,0 +1,457 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Config\Resource;
use Icinga\Exception\ProgrammingError;
use Icinga\Protocol\Ldap\Exception;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Web\Form\Decorator\HelpText;
use \Icinga\Application\Logger;
use \Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
/**
* Form for modifying a monitoring backend
*/
class EditResourceForm extends Form
{
/**
* The currently edited resource.
*
* @var Zend_Config
*/
private $resource;
/**
* @var string
*/
private $name = '';
/**
* @var string
*/
private $oldName = '';
/**
* Return the current resource name.
*
* @param string $name
*
* @return void|\Zend_Form
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return null|string
*/
public function getName()
{
return $this->getValue('resource_all_name');
}
/**
* Set the original name of the resource. This value is persisted using
* a hidden field.
*
* @param $name
*/
public function setOldName($name)
{
$this->oldName = $name;
}
/**
* Get the resource name that was initially set.
*/
public function getOldName()
{
return $this->getValue('resource_all_name_old');
}
private function addDbForm()
{
$this->addElement(
'select',
'resource_db_db',
array(
'label' => 'Database Type',
'value' => $this->getResource()->get('db', 'mysql'),
'required' => true,
'helptext' => 'The type of SQL database you want to create.',
'multiOptions' => array(
'mysql' => 'MySQL',
'pgsql' => 'PostgreSQL'
//'oracle' => 'Oracle'
)
)
);
$this->addElement(
'text',
'resource_db_host',
array (
'label' => 'Host',
'value' => $this->getResource()->get('host', 'localhost'),
'required' => true,
'helptext' => 'The hostname of the database.'
)
);
$this->addElement(
'text',
'resource_db_port',
array(
'label' => 'Port',
'value' => $this->getResource()->get('port', 3306),
'required' => true,
'validators' => array(
array('regex', false, '/^[0-9]+$/')
),
'helptext' => 'The port number to use.'
)
);
$this->addElement(
'text',
'resource_db_username',
array (
'label' => 'Username',
'value' => $this->getResource()->get('username', ''),
'required' => true,
'helptext' => 'The user name to use for authentication.'
)
);
$this->addElement(
'password',
'resource_db_password',
array(
'label' => 'Password',
'renderPassword' => true,
'value' => $this->getResource()->get('password', ''),
'helptext' => 'The password to use for authentication',
'required' => true
)
);
}
private function addStatusdatForm()
{
$this->addElement(
'text',
'resource_statusdat_status_file',
array(
'label' => 'Status.dat File',
'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/status.dat'),
'required' => true,
'helptext' => 'Location of your icinga status.dat file'
)
);
$this->addElement(
'text',
'resource_statusdat_object_file',
array(
'label' => 'Objects.cache File',
'value' => $this->getResource()->get('status_file', '/usr/local/icinga/var/objects.cache'),
'required' => true,
'helptext' => 'Location of your icinga objects.cache file'
)
);
}
private function addLivestatusForm()
{
$this->addElement(
'text',
'resource_livestatus_socket',
array(
'label' => 'Livestatus Socket Location',
'required' => true,
'helptext' => 'The path to your livestatus socket used for querying monitoring data',
'value' => $this->getResource()->socket,
)
);
}
private function addLdapForm()
{
$this->addElement(
'text',
'resource_ldap_hostname',
array(
'label' => 'LDAP Server Host',
'allowEmpty' => false,
'value' => $this->getResource()->get('hostname', 'localhost'),
'helptext' => 'The hostname or address of the LDAP server to use for authentication',
'required' => true
)
);
$this->addElement(
'text',
'resource_ldap_root_dn',
array(
'label' => 'LDAP Root DN',
'value' => $this->getResource()->get('root_dn', 'ou=people,dc=icinga,dc=org'),
'helptext' => 'The path where users can be found on the ldap server',
'required' => true
)
);
$this->addElement(
'text',
'resource_ldap_bind_dn',
array(
'label' => 'LDAP Bind DN',
'value' => $this->getResource()->get('bind_dn', 'cn=admin,cn=config'),
'helptext' => 'The user dn to use for querying the ldap server',
'required' => true
)
);
$this->addElement(
'password',
'resource_ldap_bind_pw',
array(
'label' => 'LDAP Bind Password',
'renderPassword' => true,
'value' => $this->getResource()->get('bind_pw', ''),
'helptext' => 'The password to use for querying the ldap server',
'required' => true
)
);
}
/**
* Set the resource configuration to edit.
*
* @param Zend_Config $resource
*/
public function setResource(Zend_Config $resource)
{
$this->resource = $resource;
}
/**
* Get the current resource configuration.
*
* @return Zend_Config
*/
public function getResource()
{
if (!isset($this->resource)) {
// Init empty resource
$this->resource = new Zend_Config(
array('type' => 'db')
);
}
return $this->resource;
}
/**
* Add a field to change the resource name and one hidden field
* to save the previous resource name.
*/
private function addNameFields()
{
$this->addElement(
'text',
'resource_all_name',
array(
'label' => 'Resource Name',
'value' => $this->name,
'helptext' => 'The unique name of this resource',
'required' => true
)
);
$this->addElement(
'hidden',
'resource_all_name_old',
array(
'value' => $this->oldName
)
);
}
/**
* Add checkbox at the beginning of the form which allows to skip logic connection validation
*/
private function addForceCreationCheckbox()
{
$checkbox = new \Zend_Form_Element_Checkbox(
array(
'name' => 'backend_force_creation',
'label' => 'Force Changes',
'helptext' => 'Check this box to enforce changes without connectivity validation',
'order' => 0
)
);
$checkbox->addDecorator(new HelpText());
$this->addElement($checkbox);
}
/**
* Add a select box for choosing the type to use for this backend
*/
private function addTypeSelectionBox()
{
$this->addElement(
'select',
'resource_type',
array(
'label' => 'Resource Type',
'value' => $this->getResource()->type,
'required' => true,
'helptext' => 'The type of resource.',
'multiOptions' => array(
'db' => 'SQL Database',
'ldap' => 'Ldap',
'statusdat' => 'Status.dat',
'livestatus' => 'Livestatus'
)
)
);
$this->enableAutoSubmit(array('resource_type'));
}
/**
* Validate this form with the Zend validation mechanism and perform a validation of the connection.
*
* If validation fails, the 'backend_force_creation' checkbox is prepended to the form to allow users to
* skip the logic connection validation.
*
* @param array $data The form input to validate
*
* @return bool True when validation succeeded, false if not
*/
public function isValid($data)
{
if (!parent::isValid($data)) {
return false;
}
if ($this->getRequest()->getPost('backend_force_creation')) {
return true;
}
if (!$this->isValidResource()) {
$this->addForceCreationCheckbox();
return false;
}
return true;
}
/**
* Test if the changed resource is a valid resource, by instantiating it and
* checking if connection is possible.
*
* @return bool True when connection to the resource is possible.
*/
private function isValidResource()
{
try {
$config = $this->getConfig();
switch ($config->type) {
case 'db':
$resource = ResourceFactory::createResource($config);
$resource->getConnection()->getConnection();
break;
case 'statusdat':
if (
!file_exists($config->object_file) ||
!file_exists($config->status_file)
) {
return false;
}
break;
case 'livestatus':
// TODO: Implement check
break;
case 'ldap':
$resource = ResourceFactory::createResource($config);
$resource->connect();
break;
}
} catch (\Exception $exc) {
return false;
}
return true;
}
public function create()
{
$this->addNameFields();
$this->addTypeSelectionBox();
switch ($this->getRequest()->getParam('resource_type', $this->getResource()->type)) {
case 'db':
$this->addDbForm();
break;
case 'statusdat':
$this->addStatusdatForm();
break;
case 'livestatus':
$this->addLivestatusForm();
break;
case 'ldap':
$this->addLdapForm();
break;
}
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
}
/**
* Return a configuration containing the backend settings entered in this form
*
* @return Zend_Config The updated configuration for this backend
*/
public function getConfig()
{
$values = $this->getValues();
$type = $values['resource_type'];
$result = array('type' => $type);
foreach ($values as $key => $value) {
if (
$key !== 'resource_type' &&
$key !== 'resource_all_name' &&
$key !== 'resource_all_name_old'
) {
$configKey = explode('_', $key, 3);
if (sizeof($configKey) < 3) {
Logger::warn('EditResourceForm: invalid form key "' . $key . '" was ignored.');
continue;
}
$result[$configKey[2]] = $value;
}
}
return new Zend_Config($result);
}
}

View File

@ -1,5 +1,3 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-create"></i>
Create New Authentication Backend

View File

@ -1,5 +1,3 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-edit"></i>
Edit Backend "<?= $this->escape($this->name); ?>"

View File

@ -1,5 +1,3 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-remove"></i>
Remove Backend "<?= $this->escape($this->name); ?>"

View File

@ -0,0 +1,61 @@
<?php
use Icinga\Web\Url;
$createResource = $this->href('/config/createresource');
?>
<?= $this->tabs->render($this); ?>
<?php if ($this->errorMessage): ?>
<div class="alert alert-danger">
<i class="icinga-icon-error"></i>
<strong><?= $this->escape($this->errorMessage); ?></strong>
</div>
<?php endif; ?>
<?php if ($this->successMessage): ?>
<div class="alert alert-success">
<i class="icinga-icon-success"></i>
<strong><?= $this->escape($this->successMessage); ?></strong>
</div>
<?php elseif ($this->flashMessages): ?>
<div class="alert alert-success">
<i class="icinga-icon-success"></i>
<strong><?= $this->escape($this->flashMessages); ?></strong>
</div>
<?php endif; ?>
<div class="panel panel-default">
<div class="panel-heading panel-title">
Create Resource
</div>
<div class="panel-body">
<a href="<?= $createResource ?>"><i class="icinga-icon-create"></i> Create A New Resource</a><br/>
</div>
</div>
<?php foreach ($this->resources as $name => $resource): ?>
<div class="panel panel-default">
<div class="panel-heading panel-title">
<b>Resource: </b> <?= $this->escape($name); ?>
<br/>
</div>
<div class="panel-body">
<a href="<?= $this->href('config/editresource', array('resource' => $name));?>">
<i class="icinga-icon-edit"></i> Edit This Resource
</a>
<br/>
<?php if (count($this->resources) > 1): ?>
<a href="<?= $this->href('config/removeresource', array('resource' => $name));?>">
<i class="icinga-icon-remove"></i> Remove This Resource
</a>
<br/>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>

View File

@ -0,0 +1,16 @@
<h4>
<i class="icinga-icon-create"></i>
Create New Resource
</h4>
<?php if ($this->errorMessage): ?>
<div class="alert alert-danger">
<?= $this->escape($this->errorMessage); ?>
</div>
<?php endif; ?>
<p>
Create a new resource to describes a data sourc
</p>
<?= $this->form ?>

View File

@ -0,0 +1,15 @@
<h4>
<i class="icinga-icon-edit"></i>
Edit Resource "<?= $this->escape($this->name); ?>"
</h4>
<?php if ($this->errorMessage || $this->form->getErrorMessages()): ?>
<div class="alert alert-danger">
<?= $this->escape($this->errorMessage); ?>
<?php foreach ($this->form->getErrorMessages() as $error): ?>
<?= $this->escape($error); ?><br/>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?= $this->form ?>

View File

@ -0,0 +1,5 @@
<h4>
<i class="icinga-icon-remove"></i>
Remove Resource "<?= $this->escape($this->name); ?>"
</h4>
<?= $this->form ?>

View File

@ -9,13 +9,49 @@ place, avoiding the need to edit several different files, when the connection in
Each section represents a resource, with the section name being the identifier used to
reference this certain section. Depending on the resource type, each section contains different properties.
The property *type* defines the resource type and thus how the properties are going to be interpreted.
Currently only the resource type *db* is available.
The available resource types are 'db', 'statusdat', 'livestatus' and 'ldap' and are
described in detail in the following sections:
### db
This resource type describes a SQL database. The property *db* defines the used database vendor, which
could be a value like *mysql* or *pgsql*. The other properties like *host*, *password*, *username* and
*dbname* are the connection information for the resource.
This resource type describes a SQL database on an SQL server. Databases can contain users and groups
to handle authentication and permissions, or monitoring data using IDO.
- *db*: defines the used database vendor, which could be a value like *mysql* or *pgsql*.
- *host*: The hostname that is used to connect to the database.
- *port*: The port that is used to connect to the database.
- *username*: The user name that is used to authenticate.
- *password*: The password of the user given in *username*.
- *dbname*: The name of the database that contains the resources data.
### ldap
The resource is a tree in a ldap domain. This resource type is usually used to fetch users and groups
to handle authentication and permissions.
- *hostname*: The hostname that is used to connect to the ldap server.
- *port*: The port that is used to connect to the ldap server.
- *root_dn*: The root object of the tree. This is usually an organizational unit like
"ou=people, dc=icinga, dc=org".
- *bind_dn*: The user on the LDAP server that will be used to access it. Usually something
like "cn=admin, cn=config".
- *bind_pw*: The password of the user given in *bind_dn*.
### livestatus
A resource that points to a livestatus socket. This resource type contains monitoring data.
- *socket*: The livestatus socket. Can be either be a path to a domain socket (like
"/usr/local/icinga-mysql/var/rw/live") or to a TCP socket like
(tcp://<domain>:<port>)
### statusdat
A resource that points to statusdat files. This resource type contains monitoring data.
- *status_file*: The path to the *status.dat* file, like "/usr/local/icinga-mysql/var/status.dat"
- *object_file*: The path to *objects.cache*, like "/usr/local/icinga-mysql/var/objects.cache"
## Factory Implementations
@ -26,53 +62,35 @@ factory class, that can be used to comfortably create instances of classes that
to the data of the resources.
### DbAdapterFactory
### ResourceFactory
The DbAdapterFactory can be used to retrieve instances of Zend_Db_Adapter_Abstract for accessing
the data of the SQL database.
The ResourceFactory can be used to retrieve objects to access resources. Lets assume
for the following examples, that we have an *resources.ini* that looks like this:
Lets assume for the following examples, that we have an *resources.ini* that looks like this:
[statusdat]
type = statusdat
status_file = /usr/local/icinga-mysql/var/status.dat
object_file = /usr/local/icinga-mysql/var/objects.cache
[resource1]
type = "db"
db = "mysql"
dbname = "resource1"
host = "host"
username = "username1"
password = "password1"
[resource2]
type = "db"
db = "pgsql"
dbname = "resource2"
host = "host"
username = "username2"
password = "password2"
[resource3]
type = "other"
foo = "foo"
bar = "bar"
[ldap_authentication]
type = "ldap"
hostname = "localhost"
port = "389"
root_dn = "ou=people, dc=icinga, dc=org"
bind_dn = "cn=admin, cn=config"
bind_pw = "admin"
In the most simple use-case you can create an adapter by calling the
*getDbAdapter* function. The created adapter will be an instance of
Zend_Db_Adapter_Pdo_Mysql
$adapter = DbAdapterFactory::getDbAdapter('resource1');
Here is an example of how to retrieve the resource 'statusdat' from the factory.
$resource = ResourceFactory::createResource(
ResourceFactory::getResourceConfig('statusdat')
);
If you specify a resource that does not exist or has the wrong type,
the factory will throw an ConfigurationException. You can make sure
a resource exists and has the right type, by calling the function *resourceExists*:
if (DbAdapterFactory::resourceExists('resource3')) {
$adapter = DbAdapterFactory::getDbAdapter('resource3');
} else {
// This returned false, because resource3 has a different type than "db"
echo 'resource does not exist, adapter could not be created...'
}
the factory will throw an ConfigurationException.
You can retrieve a list of all available resources by calling *getResources*. You will
get an array of all resources that have the type 'db'.
You can also retrieve a list of all available resources by calling *getResourceConfigs*.
$resourceConfigs = ResourceFactory::getResourceConfigs();

View File

@ -106,20 +106,27 @@ class DbUserBackend implements UserBackend
/**
* Create a new DbUserBackend
*
* @param DbConnection $resource The db connection to use for the authentication.
* @param Zend_Config $config The configuration for this authentication backend.
* 'resource' => The name of the resource to use, or an actual
* instance of Zend_Db_Adapter_Abstract
* 'name' => The name of this authentication backend
*
* @throws Exception When connection to the resource is not possible.
* @throws Exception When the connection to the resource is not possible.
*/
public function __construct(DbConnection $resource = null, Zend_Config $config)
public function __construct(Zend_Config $config)
{
if (!isset($config->resource)) {
throw new ConfigurationError('An authentication backend must provide a resource.');
}
$this->name = $config->name;
if ($config->resource instanceof Zend_Db_Adapter_Abstract) {
$this->db = $config->resource;
} else {
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($config->resource));
$this->db = $resource->getConnection();
}
// will throw an exception when connecting is not possible
// test the connection
$this->db->getConnection();
}

View File

@ -29,6 +29,7 @@
namespace Icinga\Authentication\Backend;
use Icinga\Data\ResourceFactory;
use \stdClass;
use \Zend_Config;
use \Icinga\User;
@ -37,6 +38,7 @@ use \Icinga\Authentication\Credential;
use \Icinga\Protocol\Ldap;
use \Icinga\Protocol\Ldap\Connection as LdapConnection;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Exception\ConfigurationError;
/**
* User authentication backend
@ -65,21 +67,32 @@ class LdapUserBackend implements UserBackend
private $name;
/**
* Create new Ldap User backend
* Create a new LdapUserBackend
*
* @param Zend_Config $connection Connection to use
* @param Zend_Config $config Configuration for authentication
* @param Zend_Config $config The configuration for this authentication backend.
* 'resource' => The name of the resource to use, or an actual
* instance of \Icinga\Protocol\Ldap\Connection.
* 'name' => The name of this authentication backend.
*
* @throws Exception When connection to the resource is not possible.
* @throws \Exception When the connection to the resource is not possible.
*/
public function __construct(LdapConnection $connection, Zend_Config $config)
public function __construct(Zend_Config $config)
{
$this->connection = $connection;
if (!isset($config->resource)) {
throw new ConfigurationError('An authentication backend must provide a resource.');
}
$this->config = $config;
$this->name = $config->name;
if ($config->resource instanceof LdapConnection) {
$this->connection = $config->resource;
} else {
$this->connection = ResourceFactory::createResource(
ResourceFactory::getResourceConfig($config->resource)
);
}
// will throw an exception, when the connection is not possible.
$connection->connect();
$this->connection->connect();
}
/**

View File

@ -186,7 +186,6 @@ class Manager
{
$target = ucwords(strtolower($backendConfig->target));
$name = $backendConfig->name;
// TODO: implement support for groups (#4624) and remove OR-Clause
if ((!$target || strtolower($target) != "user") && !$backendConfig->class) {
Logger::warn('AuthManager: Backend "%s" has no target configuration. (e.g. target=user|group)', $name);
@ -194,24 +193,22 @@ class Manager
}
try {
if (isset($backendConfig->class)) {
// use custom backend class, this is only useful for testing
// use a custom backend class, this is probably only useful for testing
if (!class_exists($backendConfig->class)) {
Logger::error('AuthManager: Class not found (%s) for backend %s', $backendConfig->class, $name);
return null;
}
$class = $backendConfig->class;
return new $class($backendConfig);
}
} else {
$resource = ResourceFactory::createResource(ResourceFactory::getResourceConfig($backendConfig->resource));
if ($resource instanceof DbConnection) {
return new DbUserBackend($resource, $backendConfig);
} else if ($resource instanceof LdapConnection) {
return new LdapUserBackend($resource, $backendConfig);
} else {
Logger::warn('AuthManager: Resource class ' . get_class($resource) . ' cannot be used as backend.');
}
switch (ResourceFactory::getResourceConfig($backendConfig->resource)->type) {
case 'db':
return new DbUserBackend($backendConfig);
case 'ldap':
return new LdapUserBackend($backendConfig);
default:
Logger::warn('AuthManager: Resource type ' . $backendConfig->type . ' not available.');
}
} catch (\Exception $e) {
Logger::warn('AuthManager: Not able to create backend. Exception was thrown: %s', $e->getMessage());

View File

@ -50,9 +50,8 @@ class PreservingIniWriter extends Zend_Config_Writer_FileAbstract
/**
* Create a new PreservingIniWriter
*
* @param array $options Contains the options that should be used for the ConfigWriter
* in an associative array. Supports all options of Zend_Config_Writer and additional
* options for setting the formatting for the internal IniEditor:
* @param array $options Supports all options of Zend_Config_Writer and additional
* options for the internal IniEditor:
* * valueIndentation: The indentation level of the values
* * commentIndentation: The indentation level of the comments
* * sectionSeparators: The amount of newlines between sections

View File

@ -50,19 +50,72 @@ class ResourceFactory implements ConfigAwareFactory
self::$resources = $config;
}
/**
* Get the configuration for a specific resource
*
* @param $resourceName String The resources name
*
* @return Zend_Config The configuration of the resource
* @throws \Icinga\Exception\ConfigurationError
*/
public static function getResourceConfig($resourceName)
{
if (!isset(self::$resources)) {
throw new ProgrammingError(
"The ResourceFactory must be initialised by setting a config, before it can be used"
);
}
self::assertResourcesExist();
if (($resourceConfig = self::$resources->get($resourceName)) === null) {
throw new ConfigurationError('Resource "' . $resourceName . '" couldn\'t be retrieved');
}
return $resourceConfig;
}
/**
* Return the configuration of all existing resources, or get all resources of a given type.
*
* @param String|null $type Fetch only resources that have the given type.
*
* @return Zend_Config The configuration containing all resources
*/
public static function getResourceConfigs($type = null)
{
self::assertResourcesExist();
if (!isset($type)) {
return self::$resources;
} else {
$resources = array();
foreach (self::$resources as $name => $resource) {
if (strtolower($resource->type) === $type) {
$resources[$name] = $resource;
}
}
return new Zend_Config($resources);
}
}
/**
* Check if the existing resources are set. If not, throw an error.
*
* @throws \Icinga\Exception\ProgrammingError
*/
private static function assertResourcesExist()
{
if (!isset(self::$resources)) {
throw new ProgrammingError(
"The ResourceFactory must be initialised by setting a config, before it can be used"
);
}
}
/**
* Create a single resource from the given configuration.
*
* NOTE: The factory does not test if the given configuration is valid and the resource is accessible, this
* depends entirely on the implementation of the returned resource.
*
* @param Zend_Config $config The configuration for the created resource.
*
* @return DbConnection|LdapConnection|LivestatusConnection|StatusdatReader An objects that can be used to access
* the given resource. The returned class depends on the configuration property 'type'.
* @throws \Icinga\Exception\ConfigurationError When an unsupported type is given
*/
public static function createResource(Zend_Config $config)
{
switch (strtolower($config->type)) {
@ -80,8 +133,12 @@ class ResourceFactory implements ConfigAwareFactory
break;
default:
throw new ConfigurationError('Unsupported resource type "' . $config->type . '"');
}
return $resource;
}
public static function getBackendType($resource)
{
}
}

View File

@ -123,7 +123,6 @@ class Connection
$this->bind_pw = $config->bind_pw;
$this->root_dn = $config->root_dn;
$this->port = $config->get('port', $this->port);
}
public function getDN()

View File

@ -59,7 +59,6 @@ namespace Icinga\Test {
use Zend_Db_Adapter_Pdo_Mysql;
use Zend_Db_Adapter_Pdo_Pgsql;
use Zend_Db_Adapter_Pdo_Oci;
use Icinga\Application\DbAdapterFactory;
use Icinga\Data\ResourceFactory;
use Icinga\User\Preferences;
use Icinga\Web\Form;
@ -113,7 +112,7 @@ namespace Icinga\Test {
public static $moduleDir;
/**
* DbAdapterFactory configuration for different database types
* Resource configuration for different database types
*
* @var array
*/
@ -210,7 +209,6 @@ namespace Icinga\Test {
$this->requireDbLibraries();
try {
//$adapter = DbAdapterFactory::createDbAdapter($this->createDbConfigFor($name));
$adapter = ResourceFactory::createResource($this->createDbConfigFor($name))->getConnection();
} catch (Exception $e) {
$adapter = $e->getMessage();

View File

@ -32,7 +32,7 @@ namespace Icinga\User\Preferences;
use \Zend_Config;
use \Icinga\User;
use \Icinga\Exception\ProgrammingError;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Data\ResourceFactory;
/**
* Create preference stores from zend config
@ -75,7 +75,7 @@ final class StoreFactory
$items = $config->toArray();
if ($items['type'] == 'db') {
$items['dbAdapter'] = DbAdapterFactory::getDbAdapter($items['resource']);
$items['dbAdapter'] = ResourceFactory::createResource($items['resource']);
}
unset($items['type']);

View File

@ -42,6 +42,11 @@ use \Icinga\Application\Icinga;
*/
class BaseConfigController extends ActionController
{
/**
* @var Zend_Controller_Action_Helper_FlashMessenger
*/
protected $flashManager;
/*
* Return an array of tabs provided by this configuration controller.
*
@ -63,5 +68,6 @@ class BaseConfigController extends ActionController
{
parent::init();
$this->view->tabs = ControllerTabCollector::collectControllerTabs('ConfigController');
$this->view->flashMessages = $this->_request->getParam('flash_message');
}
}

View File

@ -32,7 +32,7 @@ namespace Icinga\Module\Monitoring\Form\Config\Backend;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Application\Icinga;
use \Icinga\Application\DbAdapterFactory;
use Icinga\Data\ResourceFactory;
/**
* Form for modifying a monitoring backend
@ -40,7 +40,7 @@ use \Icinga\Application\DbAdapterFactory;
class EditBackendForm extends Form
{
/**
* Database resources to use instead of the one's from DBAdapterFactory (used for testing)
* Database resources to use instead of the one's from ResourceFactory (used for testing)
*
* @var array
*/
@ -82,7 +82,7 @@ class EditBackendForm extends Form
}
/**
* Set a custom array of resources to be used in this form instead of the ones from DbAdapterFactory
* Set a custom array of resources to be used in this form instead of the ones from ResourceFactory
* (used for testing)
*/
public function setResources($resources)
@ -98,7 +98,7 @@ class EditBackendForm extends Form
public function getResources()
{
if ($this->resources === null) {
return DbAdapterFactory::getResources();
return ResourceFactory::getResourceConfigs()->toArray();
} else {
return $this->resources;
}

View File

@ -37,7 +37,6 @@ use \Zend_Config;
use \Zend_Test_PHPUnit_ControllerTestCase;
use \Icinga\Protocol\Statusdat\Reader;
use \Icinga\Web\Controller\ActionController;
use \Icinga\Application\DbAdapterFactory;
use \Test\Monitoring\Testlib\DataSource\TestFixture;
use \Test\Monitoring\Testlib\DataSource\DataSourceTestSetup;
use Icinga\Module\Monitoring\Backend;

View File

@ -100,17 +100,13 @@ class AuthenticationFormTest extends BaseTestCase
array(
'backend' => 'ldap',
'target' => 'user',
'hostname' => 'test host',
'root_dn' => 'ou=test,dc=icinga,dc=org',
'bind_dn' => 'cn=testuser,cn=config',
'bind_pw' => 'password',
'user_class' => 'testClass',
'user_name_attribute' => 'testAttribute'
)
);
$form->setBackendName('testldap');
$form->setBackend($config);
$form->create();
$form->create(array('resources' => array()));
// parameters to be hidden
$notShown = array('backend', 'target');

View File

@ -56,7 +56,6 @@ use \PDO;
use \Zend_Db_Adapter_Pdo_Abstract;
use \Zend_Config;
use \Icinga\Authentication\Backend\DbUserBackend;
use \Icinga\Application\DbAdapterFactory;
use \Icinga\Authentication\Credential;
use \Icinga\User;
use \Icinga\Application\Config;
@ -131,7 +130,7 @@ class DbUserBackendTest extends BaseTestCase
public function testCorrectUserLoginForPgsql($db)
{
$this->setupDbProvider($db);
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db));
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
@ -144,7 +143,7 @@ class DbUserBackendTest extends BaseTestCase
public function testCorrectUserLoginForMySQL($db)
{
$this->setupDbProvider($db);
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db));
$backend = new DbUserBackend($this->createDbBackendConfig($db));
$this->runBackendAuthentication($backend);
$this->runBackendUsername($backend);
}
@ -281,7 +280,7 @@ class DbUserBackendTest extends BaseTestCase
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$this->assertSame($testName, $backend->getName());
}
@ -293,7 +292,7 @@ class DbUserBackendTest extends BaseTestCase
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$this->assertGreaterThan(0, $backend->getUserCount());
}
@ -305,7 +304,7 @@ class DbUserBackendTest extends BaseTestCase
{
$this->setupDbProvider($db);
$testName = 'test-name-123123';
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
$this->assertGreaterThan(0, $backend->getUserCount());
}

View File

@ -48,7 +48,7 @@ require_once BaseTestCase::$libDir . '/Authentication/Backend/LdapUserBackend.ph
use \Exception;
use \Zend_Config;
use Icinga\Authentication\Backend\LdapUserBackend;
use Icinga\Protocol\Ldap\Connection;
use Icinga\Protocol\Ldap\Connection as LdapConnection;
/**
*
@ -161,22 +161,31 @@ class LdapUserBackendTest extends BaseTestCase
ldap_close($conn);
}
/**
* Create a backend config and initialise the LdapConnection to the testing backend manually,
* to prevent the LdapUserBackend from calling the unitialised ResourceFactory
*
* @return Zend_Config The authentication backend configuration
*/
private function createBackendConfig()
{
$config = new Zend_Config(
$resourceConfig = new Zend_Config(
array(
'backend' => 'ldap',
'target' => 'user',
'hostname' => 'localhost',
'root_dn' => 'ou=icinga-unittest,dc=icinga,dc=org',
'bind_dn' => 'cn=admin,cn=config',
'bind_pw' => 'admin',
'bind_pw' => 'admin'
)
);
$backendConfig = new Zend_Config(
array(
'resource' => new LdapConnection($resourceConfig),
'target' => 'user',
'user_class' => 'inetOrgPerson',
'user_name_attribute' => 'uid'
)
);
return $config;
return $backendConfig;
}
/**
@ -184,8 +193,7 @@ class LdapUserBackendTest extends BaseTestCase
**/
public function testHasUsername()
{
$config = $this->createBackendConfig();
$backend = new LdapUserBackend(new Connection($config), $config);
$backend = new LdapUserBackend($this->createBackendConfig());
$this->assertTrue($backend->hasUsername(new Credential('jwoe')));
$this->assertTrue($backend->hasUsername(new Credential('rmiles')));
$this->assertFalse($backend->hasUsername(new Credential('DoesNotExist')));
@ -196,8 +204,7 @@ class LdapUserBackendTest extends BaseTestCase
*/
public function testAuthenticate()
{
$config = $this->createBackendConfig();
$backend = new LdapUserBackend(new Connection($config), $config);
$backend = new LdapUserBackend($this->createBackendConfig());
$this->assertInstanceOf(
'\Icinga\User',
@ -220,8 +227,7 @@ class LdapUserBackendTest extends BaseTestCase
*/
public function testAuthenticateUnknownUser()
{
$config = $this->createBackendConfig();
$backend = new LdapUserBackend(new Connection($config), $config);
$backend = new LdapUserBackend($this->createBackendConfig());
$this->assertFalse($backend->authenticate(new Credential('unknown123', 'passunknown123')));
}
}