Merge branch 'feature/extend-resources-ini-4587'
This commit is contained in:
commit
ea5daf964b
|
@ -36,9 +36,9 @@ use \Icinga\Application\Modules\Manager as ModuleManager;
|
|||
use \Icinga\Application\Platform;
|
||||
use \Icinga\Application\Config;
|
||||
use \Icinga\Exception\ProgrammingError;
|
||||
use \Icinga\Application\DbAdapterFactory;
|
||||
use \Icinga\Exception\ConfigurationError;
|
||||
use \Icinga\Util\DateTimeFactory;
|
||||
|
||||
use Icinga\Data\ResourceFactory;
|
||||
|
||||
/**
|
||||
|
@ -393,7 +393,6 @@ abstract class ApplicationBootstrap
|
|||
protected function setupResourceFactory()
|
||||
{
|
||||
$config = Config::app('resources');
|
||||
DbAdapterFactory::setConfig($config);
|
||||
ResourceFactory::setConfig($config);
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -1,300 +0,0 @@
|
|||
<?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\Application;
|
||||
|
||||
use \PDO;
|
||||
use \Zend_Config;
|
||||
use \Zend_Db;
|
||||
use \Zend_Db_Adapter_Abstract;
|
||||
use \Icinga\Application\Logger;
|
||||
use \Icinga\Util\ConfigAwareFactory;
|
||||
use \Icinga\Exception\ConfigurationError;
|
||||
use \Icinga\Exception\ProgrammingError;
|
||||
|
||||
/**
|
||||
* Create resources using short identifiers referring to configuration entries
|
||||
*/
|
||||
class DbAdapterFactory implements ConfigAwareFactory
|
||||
{
|
||||
/**
|
||||
* Resource definitions
|
||||
*
|
||||
* @var Zend_Config
|
||||
*/
|
||||
private static $resources;
|
||||
|
||||
/**
|
||||
* The factory class used to create instances of Zend_Db_Adapter
|
||||
*
|
||||
* @var String
|
||||
*/
|
||||
private static $factoryClass = 'Zend_Db';
|
||||
|
||||
/**
|
||||
* Resource cache to allow multiple use
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $resourceCache = array();
|
||||
|
||||
/**
|
||||
* Array of PDO driver options
|
||||
*
|
||||
* @see http://www.php.net/manual/en/pdo.constants.php
|
||||
* @var array
|
||||
*/
|
||||
private static $defaultPdoDriverOptions = array(
|
||||
PDO::ATTR_TIMEOUT => 2,
|
||||
PDO::ATTR_CASE => PDO::CASE_LOWER
|
||||
);
|
||||
|
||||
/**
|
||||
* Array of Zend_Db adapter options
|
||||
*
|
||||
* @see http://framework.zend.com/manual/1.12/en/zend.db.html
|
||||
* @var array
|
||||
*/
|
||||
private static $defaultZendDbAdapterOptions = array(
|
||||
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false,
|
||||
Zend_Db::CASE_FOLDING => Zend_Db::CASE_LOWER,
|
||||
// Zend_Db::FETCH_MODE => Zend_Db::FETCH_OBJ
|
||||
'fetchMode' => Zend_Db::FETCH_OBJ
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the configuration that stores the available resources
|
||||
*
|
||||
* @param mixed $config The configuration containing the resources
|
||||
*
|
||||
* @param array $options Additional options that affect the factories behaviour:
|
||||
* * factory : Set the factory class that creates instances
|
||||
* of Zend_Db_Adapter for the different database types
|
||||
* (used for testing)
|
||||
*/
|
||||
public static function setConfig($config, array $options = null)
|
||||
{
|
||||
if (is_array($config)) {
|
||||
$config = new Zend_Config($config);
|
||||
}
|
||||
self::$resources = $config;
|
||||
if (isset($options['factory'])) {
|
||||
self::$factoryClass = $options['factory'];
|
||||
} else {
|
||||
self::$factoryClass = 'Zend_Db';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the factory configuration back to the default state
|
||||
*/
|
||||
public static function resetConfig()
|
||||
{
|
||||
self::$resources = null;
|
||||
self::$factoryClass = 'Zend_Db';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all resources available to this factory
|
||||
*
|
||||
* @return array An array containing all resources compatible to this factory
|
||||
*/
|
||||
public static function getResources()
|
||||
{
|
||||
$resources = self::$resources->toArray();
|
||||
foreach ($resources as $identifier => $resource) {
|
||||
if ($resource['type'] !== 'db') {
|
||||
unset($resources[$identifier]);
|
||||
}
|
||||
}
|
||||
return $resources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if a resource with the given identifier exists
|
||||
*
|
||||
* @param $identifier The name of the resource
|
||||
*
|
||||
* @return boolean If the resource exists and is compatible
|
||||
*/
|
||||
public static function resourceExists($identifier)
|
||||
{
|
||||
return isset(self::$resources->{$identifier})
|
||||
&& (self::$resources->{$identifier}->type === 'db');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the resource with the given $identifier
|
||||
*
|
||||
* @param string $identifier The name of the resource
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract
|
||||
* @throws ConfigurationError
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public static function getDbAdapter($identifier)
|
||||
{
|
||||
if (!isset(self::$resources)) {
|
||||
$msg = 'Creation of resource ' . $identifier . ' not possible, because there is no configuration present.'
|
||||
. ' Make shure this factory class was initialised correctly during the application bootstrap.';
|
||||
Logger::error($msg);
|
||||
throw new ProgrammingError($msg);
|
||||
}
|
||||
if (!isset(self::$resources->{$identifier})) {
|
||||
$msg = 'Creation of resource "'
|
||||
. $identifier
|
||||
. '" not possible, because there is no matching resource present in the configuration ';
|
||||
Logger::error($msg);
|
||||
throw new ConfigurationError($msg);
|
||||
}
|
||||
if (array_key_exists($identifier, self::$resourceCache)) {
|
||||
return self::$resourceCache[$identifier];
|
||||
} else {
|
||||
$res = self::createDbAdapter(self::$resources->{$identifier});
|
||||
self::$resourceCache[$identifier] = $res;
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a db adapter directly from a configuration instead of a resource identifier
|
||||
*
|
||||
* @param Zend_Config $config The resource configuration that will be used.
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created DbAdapter
|
||||
* @throws ProgrammingError
|
||||
*/
|
||||
public static function createDbAdapterFromConfig(Zend_Config $config)
|
||||
{
|
||||
return self::createDbAdapter($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the Db_Adapter for the given configuration section
|
||||
*
|
||||
* @param Zend_Config $config The configuration section containing the db information
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created Zend_Db_Adapter
|
||||
* @throws ConfigurationError When the specified db type is invalid
|
||||
*/
|
||||
public static function createDbAdapter(Zend_Config $config)
|
||||
{
|
||||
if ($config->type !== 'db') {
|
||||
$msg = 'Resource type must be "db" but is "' . $config->type . '"';
|
||||
Logger::error($msg);
|
||||
throw new ConfigurationError($msg);
|
||||
}
|
||||
$options = array(
|
||||
'dbname' => $config->dbname,
|
||||
'host' => $config->host,
|
||||
'username' => $config->username,
|
||||
'password' => $config->password,
|
||||
'options' => self::$defaultZendDbAdapterOptions,
|
||||
'driver_options' => self::$defaultPdoDriverOptions
|
||||
);
|
||||
switch ($config->db) {
|
||||
case 'mysql':
|
||||
$options['port'] = $config->get('port', 3306);
|
||||
return self::callFactory('Pdo_Mysql', $options);
|
||||
case 'pgsql':
|
||||
$options['port'] = $config->get('port', 5432);
|
||||
return self::callFactory('Pdo_Pgsql', $options);
|
||||
default:
|
||||
if (!$config->db) {
|
||||
$msg = 'Database type is missing (e.g. db=mysql).';
|
||||
} else {
|
||||
$msg = 'Unsupported db type ' . $config->db . '.';
|
||||
}
|
||||
Logger::error($msg);
|
||||
throw new ConfigurationError($msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the currently set factory class
|
||||
*
|
||||
* @param string $adapter The name of the used db adapter
|
||||
* @param mixed $options An array or Zend_Config object with adapter
|
||||
* parameters
|
||||
*
|
||||
* @return Zend_Db_Adapter_Abstract The created adapter
|
||||
*/
|
||||
private static function callFactory($adapter, $options)
|
||||
{
|
||||
$factory = self::$factoryClass;
|
||||
$optionModifierCallback = __CLASS__ . '::get' . ucfirst(str_replace('_', '', $adapter)) . 'Options';
|
||||
if (is_callable($optionModifierCallback)) {
|
||||
$options = call_user_func($optionModifierCallback, $options);
|
||||
}
|
||||
return $factory::factory($adapter, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get modified attributes for driver PDO_Mysql
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function getPdoMysqlOptions(array $options)
|
||||
{
|
||||
/*
|
||||
* Set MySQL server SQL modes to behave as closely as possible to Oracle and PostgreSQL. Note that the
|
||||
* ONLY_FULL_GROUP_BY mode is left on purpose because MySQL requires you to specify all non-aggregate columns
|
||||
* in the group by list even if the query is grouped by the master table's primary key which is valid
|
||||
* ANSI SQL though. Further in that case the query plan would suffer if you add more columns to the group by
|
||||
* list.
|
||||
*/
|
||||
$options['driver_options'][PDO::MYSQL_ATTR_INIT_COMMAND] =
|
||||
'SET SESSION SQL_MODE=\'STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,'
|
||||
. 'NO_AUTO_CREATE_USER,ANSI_QUOTES,PIPES_AS_CONCAT,NO_ENGINE_SUBSTITUTION\';';
|
||||
|
||||
if (!isset($options['port'])) {
|
||||
$options['port'] = 3306;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get modified attributes for driver PDO_PGSQL
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function getPdoPgsqlOptions(array $options)
|
||||
{
|
||||
if (!isset($options['port'])) {
|
||||
$options['port'] = 5432;
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
|
@ -34,13 +34,14 @@ use \stdClass;
|
|||
use \Zend_Config;
|
||||
use \Zend_Db;
|
||||
use \Zend_Db_Adapter_Abstract;
|
||||
use \Icinga\Application\DbAdapterFactory;
|
||||
use \Icinga\Exception\ProgrammingError;
|
||||
use \Icinga\Data\ResourceFactory;
|
||||
use \Icinga\Data\Db\Connection as DbConnection;
|
||||
use \Icinga\User;
|
||||
use \Icinga\Authentication\UserBackend;
|
||||
use \Icinga\Authentication\Credential;
|
||||
use \Icinga\Authentication;
|
||||
use \Icinga\Application\Logger;
|
||||
use \Icinga\Exception\ProgrammingError;
|
||||
use \Icinga\Exception\ConfigurationError;
|
||||
|
||||
/**
|
||||
|
@ -103,22 +104,22 @@ class DbUserBackend implements UserBackend
|
|||
private $name;
|
||||
|
||||
/**
|
||||
* Create a DbUserBackend
|
||||
* Create a new DbUserBackend
|
||||
*
|
||||
* @param Zend_Config $config The database that provides the authentication data
|
||||
* @throws ConfigurationError
|
||||
* @param DbConnection $resource The db connection to use for the authentication.
|
||||
* @param Zend_Config $config The configuration for this authentication backend.
|
||||
*
|
||||
* @throws Exception When connection to the resource is not possible.
|
||||
*/
|
||||
public function __construct(Zend_Config $config)
|
||||
public function __construct(DbConnection $resource = null, Zend_Config $config)
|
||||
{
|
||||
$this->name = $config->name;
|
||||
|
||||
if ($config->resource instanceof Zend_Db_Adapter_Abstract) {
|
||||
$this->db = $config->resource;
|
||||
} else {
|
||||
$this->db = DbAdapterFactory::getDbAdapter($config->resource);
|
||||
$this->db = $resource->getConnection();
|
||||
}
|
||||
|
||||
// Throw any errors for Authentication/Manager
|
||||
// will throw an exception when connecting is not possible
|
||||
$this->db->getConnection();
|
||||
}
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ use \Icinga\User;
|
|||
use \Icinga\Authentication\UserBackend;
|
||||
use \Icinga\Authentication\Credential;
|
||||
use \Icinga\Protocol\Ldap;
|
||||
use \Icinga\Protocol\Ldap\Connection;
|
||||
use \Icinga\Protocol\Ldap\Connection as LdapConnection;
|
||||
use \Icinga\Application\Config as IcingaConfig;
|
||||
|
||||
/**
|
||||
|
@ -67,13 +67,19 @@ class LdapUserBackend implements UserBackend
|
|||
/**
|
||||
* Create new Ldap User backend
|
||||
*
|
||||
* @param Zend_Config $config Configuration to create instance
|
||||
* @param Zend_Config $connection Connection to use
|
||||
* @param Zend_Config $config Configuration for authentication
|
||||
*
|
||||
* @throws Exception When connection to the resource is not possible.
|
||||
*/
|
||||
public function __construct(Zend_Config $config)
|
||||
public function __construct(LdapConnection $connection, Zend_Config $config)
|
||||
{
|
||||
$this->connection = new Connection($config);
|
||||
$this->connection = $connection;
|
||||
$this->config = $config;
|
||||
$this->name = $config->name;
|
||||
|
||||
// will throw an exception, when the connection is not possible.
|
||||
$connection->connect();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -170,6 +176,5 @@ class LdapUserBackend implements UserBackend
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,13 +30,19 @@
|
|||
namespace Icinga\Authentication;
|
||||
|
||||
use \Exception;
|
||||
use Icinga\Exception\ConfigurationError;
|
||||
use \Zend_Config;
|
||||
use \Icinga\User;
|
||||
use \Icinga\Data\ResourceFactory;
|
||||
use \Icinga\Data\Db\Connection as DbConnection;
|
||||
use \Icinga\Application\Logger;
|
||||
use \Icinga\Application\Config as IcingaConfig;
|
||||
use \Icinga\Application\DbAdapterFactory;
|
||||
use \Icinga\Protocol\Ldap\Connection as LdapConnection;
|
||||
use \Icinga\Authentication\Backend\DbUserBackend;
|
||||
use \Icinga\Authentication\Backend\LdapUserBackend;
|
||||
use \Icinga\Exception\ProgrammingError;
|
||||
use \Icinga\Exception\ConfigurationError as ConfigError;
|
||||
use \Icinga\User;
|
||||
use \Icinga\Exception\ConfigurationError;
|
||||
|
||||
|
||||
/**
|
||||
* The authentication manager allows to identify users and
|
||||
|
@ -56,20 +62,6 @@ use \Icinga\Exception\ConfigurationError;
|
|||
**/
|
||||
class Manager
|
||||
{
|
||||
/**
|
||||
* Backend type user
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const BACKEND_TYPE_USER = 'user';
|
||||
|
||||
/**
|
||||
* Backend type group
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const BACKEND_TYPE_GROUP = 'group';
|
||||
|
||||
/**
|
||||
* Singleton instance
|
||||
*
|
||||
|
@ -172,11 +164,11 @@ class Manager
|
|||
if ($backendConfig->name === null) {
|
||||
$backendConfig->name = $name;
|
||||
}
|
||||
|
||||
$backend = $this->createBackend($backendConfig);
|
||||
|
||||
if ($backend instanceof UserBackend) {
|
||||
$this->userBackends[$backend->getName()] = $backend;
|
||||
|
||||
} elseif ($backend instanceof GroupBackend) {
|
||||
$this->groupBackends[$backend->getName()] = $backend;
|
||||
}
|
||||
|
@ -192,38 +184,39 @@ class Manager
|
|||
*/
|
||||
private function createBackend(Zend_Config $backendConfig)
|
||||
{
|
||||
$type = ucwords(strtolower($backendConfig->backend));
|
||||
$target = ucwords(strtolower($backendConfig->target));
|
||||
$name = $backendConfig->name;
|
||||
|
||||
if (!$type && !$backendConfig->class) {
|
||||
Logger::warn('AuthManager: Backend "%s" has no backend type configuration. (e.g. backend=ldap)', $name);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!$target && !$backendConfig->class) {
|
||||
// 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);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// Allow vendor and test classes in configuration
|
||||
if ($backendConfig->class) {
|
||||
$class = $backendConfig->class;
|
||||
} else {
|
||||
$class = '\\Icinga\\Authentication\\Backend\\' . $type . $target . 'Backend';
|
||||
}
|
||||
if (isset($backendConfig->class)) {
|
||||
// use custom backend class, this is only useful for testing
|
||||
if (!class_exists($backendConfig->class)) {
|
||||
Logger::error('AuthManager: Class not found (%s) for backend %s', $backendConfig->class, $name);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
Logger::error('AuthManager: Class not found (%s) for backend %s', $class, $name);
|
||||
return null;
|
||||
} else {
|
||||
$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.');
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Logger::warn('AuthManager: Not able to create backend. Exception was thrown: %s', $e->getMessage());
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -303,7 +296,6 @@ class Manager
|
|||
);
|
||||
|
||||
$authErrors++;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -38,13 +38,6 @@ use Icinga\Authentication\Credential;
|
|||
*/
|
||||
interface UserBackend
|
||||
{
|
||||
/**
|
||||
* Create a new object
|
||||
*
|
||||
* @param Zend_Config $config Object to configure instance
|
||||
*/
|
||||
public function __construct(Zend_Config $config);
|
||||
|
||||
/**
|
||||
* Test if the username exists
|
||||
*
|
||||
|
|
|
@ -29,12 +29,14 @@
|
|||
|
||||
namespace Icinga\Data;
|
||||
|
||||
use Icinga\Exception\ProgrammingError;
|
||||
use Zend_Config;
|
||||
use Icinga\Util\ConfigAwareFactory;
|
||||
use Icinga\Exception\ConfigurationError;
|
||||
use Icinga\Data\Db\Connection as DbConnection;
|
||||
use Icinga\Protocol\Livestatus\Connection as LivestatusConnection;
|
||||
use Icinga\Protocol\Statusdat\Reader as StatusdatReader;
|
||||
use Icinga\Protocol\Ldap\Connection as LdapConnection;
|
||||
|
||||
class ResourceFactory implements ConfigAwareFactory
|
||||
{
|
||||
|
@ -50,6 +52,11 @@ class ResourceFactory implements ConfigAwareFactory
|
|||
|
||||
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"
|
||||
);
|
||||
}
|
||||
if (($resourceConfig = self::$resources->get($resourceName)) === null) {
|
||||
throw new ConfigurationError('Resource "' . $resourceName . '" couldn\'t be retrieved');
|
||||
}
|
||||
|
@ -62,6 +69,9 @@ class ResourceFactory implements ConfigAwareFactory
|
|||
case 'db':
|
||||
$resource = new DbConnection($config);
|
||||
break;
|
||||
case 'ldap':
|
||||
$resource = new LdapConnection($config);
|
||||
break;
|
||||
case 'statusdat':
|
||||
$resource = new StatusdatReader($config);
|
||||
break;
|
||||
|
|
|
@ -60,6 +60,7 @@ namespace Icinga\Test {
|
|||
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;
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
@ -209,7 +210,8 @@ namespace Icinga\Test {
|
|||
$this->requireDbLibraries();
|
||||
|
||||
try {
|
||||
$adapter = DbAdapterFactory::createDbAdapter($this->createDbConfigFor($name));
|
||||
//$adapter = DbAdapterFactory::createDbAdapter($this->createDbConfigFor($name));
|
||||
$adapter = ResourceFactory::createResource($this->createDbConfigFor($name))->getConnection();
|
||||
} catch (Exception $e) {
|
||||
$adapter = $e->getMessage();
|
||||
}
|
||||
|
@ -385,7 +387,9 @@ namespace Icinga\Test {
|
|||
|
||||
require_once self::$libDir . '/Exception/ConfigurationError.php';
|
||||
require_once self::$libDir . '/Util/ConfigAwareFactory.php';
|
||||
require_once self::$libDir . '/Application/DbAdapterFactory.php';
|
||||
require_once self::$libDir . '/Data/DatasourceInterface.php';
|
||||
require_once self::$libDir . '/Data/ResourceFactory.php';
|
||||
require_once self::$libDir . '/Data/Db/Connection.php';
|
||||
require_once self::$libDir . '/Application/Logger.php';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -176,7 +176,6 @@ abstract class MonitoringControllerTest extends Zend_Test_PHPUnit_ControllerTest
|
|||
*/
|
||||
private function requireIDOQueries()
|
||||
{
|
||||
require_once('Application/DbAdapterFactory.php');
|
||||
require_once('library/Monitoring/Backend/Ido/Query/IdoQuery.php');
|
||||
$this->requireFolder('library/Monitoring/Backend/Ido/Query');
|
||||
}
|
||||
|
|
|
@ -1,216 +0,0 @@
|
|||
<?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 Tests\Icinga\Application;
|
||||
|
||||
require_once 'Zend/Db.php';
|
||||
require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
|
||||
require_once 'Zend/Config.php';
|
||||
require_once 'Zend/Log.php';
|
||||
require_once 'Zend/Config.php';
|
||||
require_once realpath(__DIR__. '/../../../library/Icinga/Application/ZendDbMock.php');
|
||||
require_once realpath(__DIR__. '/../../../../../library/Icinga/Application/Logger.php');
|
||||
require_once realpath(__DIR__. '/../../../../../library/Icinga/Exception/ConfigurationError.php');
|
||||
require_once realpath(__DIR__. '/../../../../../library/Icinga/Exception/ProgrammingError.php');
|
||||
require_once realpath(__DIR__. '/../../../../../library/Icinga/Util/ConfigAwareFactory.php');
|
||||
require_once realpath(__DIR__. '/../../../../../library/Icinga/Application/DbAdapterFactory.php');
|
||||
|
||||
use \PDO;
|
||||
use \Zend_Db;
|
||||
use \Tests\Icinga\Application\ZendDbMock;
|
||||
use \Icinga\Application\DbAdapterFactory;
|
||||
|
||||
/*
|
||||
* Unit test for the class DbAdapterFactory
|
||||
*/
|
||||
class DbAdapterFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* The resources used for this test
|
||||
*/
|
||||
private $resources;
|
||||
|
||||
/**
|
||||
* Set up the test fixture
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$this->resources = array(
|
||||
/*
|
||||
* PostgreSQL database
|
||||
*/
|
||||
'resource1' => array(
|
||||
'type' => 'db',
|
||||
'db' => 'pgsql',
|
||||
'dbname' => 'resource1',
|
||||
'host' => 'host1',
|
||||
'username' => 'username1',
|
||||
'password' => 'password1',
|
||||
'options' => array(
|
||||
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false,
|
||||
Zend_Db::CASE_FOLDING => Zend_Db::CASE_LOWER,
|
||||
Zend_Db::FETCH_MODE => Zend_Db::FETCH_OBJ
|
||||
),
|
||||
'driver_options' => array(
|
||||
PDO::ATTR_TIMEOUT => 2,
|
||||
PDO::ATTR_CASE => PDO::CASE_LOWER
|
||||
),
|
||||
'port' => 5432
|
||||
),
|
||||
/*
|
||||
* MySQL database
|
||||
*/
|
||||
'resource2' => array(
|
||||
'type' => 'db',
|
||||
'db' => 'mysql',
|
||||
'dbname' => 'resource2',
|
||||
'host' => 'host2',
|
||||
'username' => 'username2',
|
||||
'password' => 'password2',
|
||||
'options' => array(
|
||||
Zend_Db::AUTO_QUOTE_IDENTIFIERS => false,
|
||||
Zend_Db::CASE_FOLDING => Zend_Db::CASE_LOWER,
|
||||
Zend_Db::FETCH_MODE => Zend_Db::FETCH_OBJ
|
||||
),
|
||||
'driver_options' => array(
|
||||
PDO::ATTR_TIMEOUT => 2,
|
||||
PDO::ATTR_CASE => PDO::CASE_LOWER,
|
||||
PDO::MYSQL_ATTR_INIT_COMMAND =>
|
||||
'SET SESSION SQL_MODE=\'STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,'
|
||||
. 'ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,ANSI_QUOTES,PIPES_AS_CONCAT,'
|
||||
. 'NO_ENGINE_SUBSTITUTION\';'
|
||||
),
|
||||
'port' => 3306
|
||||
),
|
||||
/*
|
||||
* Unsupported database type
|
||||
*/
|
||||
'resource3' => array(
|
||||
'type' => 'db',
|
||||
'db' => 'mssql',
|
||||
'dbname' => 'resource3',
|
||||
'host' => 'host3',
|
||||
'username' => 'username3',
|
||||
'password' => 'password3'
|
||||
),
|
||||
/*
|
||||
* Unsupported resource type
|
||||
*/
|
||||
'resource4' => array(
|
||||
'type' => 'ldap',
|
||||
),
|
||||
);
|
||||
DbAdapterFactory::setConfig(
|
||||
$this->resources,
|
||||
array(
|
||||
'factory' => '\Tests\Icinga\Application\ZendDbMock'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetValidResource()
|
||||
{
|
||||
DbAdapterFactory::getDbAdapter('resource2');
|
||||
$this->assertEquals(
|
||||
'Pdo_Mysql',
|
||||
ZendDbMock::getAdapter(),
|
||||
'The db adapter name must be Pdo_Mysql.'
|
||||
);
|
||||
$this->assertEquals(
|
||||
$this->getOptions($this->resources['resource2']),
|
||||
ZendDbMock::getConfig(),
|
||||
'The options must match the original config file content'
|
||||
);
|
||||
}
|
||||
|
||||
public function testResourceExists()
|
||||
{
|
||||
$this->assertTrue(
|
||||
DbAdapterFactory::resourceExists('resource2'),
|
||||
'resourceExists() called with an existing resource should return true'
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
DbAdapterFactory::resourceExists('not existing'),
|
||||
'resourceExists() called with an existing resource should return false'
|
||||
);
|
||||
|
||||
$this->assertFalse(
|
||||
DbAdapterFactory::resourceExists('resource4'),
|
||||
'resourceExists() called with an incompatible resource should return false'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetResources()
|
||||
{
|
||||
$withoutIncompatible = array_merge(array(), $this->resources);
|
||||
unset($withoutIncompatible['resource4']);
|
||||
$this->assertEquals(
|
||||
$withoutIncompatible,
|
||||
DbAdapterFactory::getResources(),
|
||||
'getResources should return an array of all existing resources that are compatible'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an exception is thrown, when an invalid database is used.
|
||||
*
|
||||
* @expectedException Icinga\Exception\ConfigurationError
|
||||
*/
|
||||
public function testGetInvalidDatabase()
|
||||
{
|
||||
DbAdapterFactory::getDbAdapter('resource3');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if an exception is thrown, when an invalid type is used.
|
||||
*
|
||||
* @expectedException Icinga\Exception\ConfigurationError
|
||||
*/
|
||||
public function testGetInvalidType()
|
||||
{
|
||||
DbAdapterFactory::getDbAdapter('resource4');
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the options object for assertions
|
||||
*
|
||||
* @param Zend_Config $config The configuration to prepare
|
||||
*
|
||||
* @return array The prepared options object
|
||||
*/
|
||||
private function getOptions($config)
|
||||
{
|
||||
$options = array_merge(array(), $config);
|
||||
unset($options['type']);
|
||||
unset($options['db']);
|
||||
return $options;
|
||||
}
|
||||
}
|
|
@ -45,7 +45,6 @@ require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
|
|||
require_once BaseTestCase::$libDir . '/Util/ConfigAwareFactory.php';
|
||||
require_once BaseTestCase::$libDir . '/Authentication/UserBackend.php';
|
||||
require_once BaseTestCase::$libDir . '/Protocol/Ldap/Exception.php';
|
||||
require_once BaseTestCase::$libDir . '/Application/DbAdapterFactory.php';
|
||||
require_once BaseTestCase::$libDir . '/Application/Config.php';
|
||||
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
|
||||
require_once BaseTestCase::$libDir . '/Authentication/Backend/DbUserBackend.php';
|
||||
|
@ -132,7 +131,7 @@ class DbUserBackendTest extends BaseTestCase
|
|||
public function testCorrectUserLoginForPgsql($db)
|
||||
{
|
||||
$this->setupDbProvider($db);
|
||||
$backend = new DbUserBackend($this->createDbBackendConfig($db));
|
||||
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db));
|
||||
$this->runBackendAuthentication($backend);
|
||||
$this->runBackendUsername($backend);
|
||||
}
|
||||
|
@ -145,7 +144,7 @@ class DbUserBackendTest extends BaseTestCase
|
|||
public function testCorrectUserLoginForMySQL($db)
|
||||
{
|
||||
$this->setupDbProvider($db);
|
||||
$backend = new DbUserBackend($this->createDbBackendConfig($db));
|
||||
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db));
|
||||
$this->runBackendAuthentication($backend);
|
||||
$this->runBackendUsername($backend);
|
||||
}
|
||||
|
@ -282,7 +281,7 @@ class DbUserBackendTest extends BaseTestCase
|
|||
$this->setupDbProvider($db);
|
||||
|
||||
$testName = 'test-name-123123';
|
||||
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
|
||||
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
|
||||
|
||||
$this->assertSame($testName, $backend->getName());
|
||||
}
|
||||
|
@ -294,7 +293,7 @@ class DbUserBackendTest extends BaseTestCase
|
|||
{
|
||||
$this->setupDbProvider($db);
|
||||
$testName = 'test-name-123123';
|
||||
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
|
||||
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
|
||||
|
||||
$this->assertGreaterThan(0, $backend->getUserCount());
|
||||
}
|
||||
|
@ -306,7 +305,7 @@ class DbUserBackendTest extends BaseTestCase
|
|||
{
|
||||
$this->setupDbProvider($db);
|
||||
$testName = 'test-name-123123';
|
||||
$backend = new DbUserBackend($this->createDbBackendConfig($db, $testName));
|
||||
$backend = new DbUserBackend(null, $this->createDbBackendConfig($db, $testName));
|
||||
|
||||
$this->assertGreaterThan(0, $backend->getUserCount());
|
||||
}
|
||||
|
|
|
@ -48,6 +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;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -183,7 +184,8 @@ class LdapUserBackendTest extends BaseTestCase
|
|||
**/
|
||||
public function testHasUsername()
|
||||
{
|
||||
$backend = new LdapUserBackend($this->createBackendConfig());
|
||||
$config = $this->createBackendConfig();
|
||||
$backend = new LdapUserBackend(new Connection($config), $config);
|
||||
$this->assertTrue($backend->hasUsername(new Credential('jwoe')));
|
||||
$this->assertTrue($backend->hasUsername(new Credential('rmiles')));
|
||||
$this->assertFalse($backend->hasUsername(new Credential('DoesNotExist')));
|
||||
|
@ -194,7 +196,8 @@ class LdapUserBackendTest extends BaseTestCase
|
|||
*/
|
||||
public function testAuthenticate()
|
||||
{
|
||||
$backend = new LdapUserBackend($this->createBackendConfig());
|
||||
$config = $this->createBackendConfig();
|
||||
$backend = new LdapUserBackend(new Connection($config), $config);
|
||||
|
||||
$this->assertInstanceOf(
|
||||
'\Icinga\User',
|
||||
|
@ -217,7 +220,8 @@ class LdapUserBackendTest extends BaseTestCase
|
|||
*/
|
||||
public function testAuthenticateUnknownUser()
|
||||
{
|
||||
$backend = new LdapUserBackend($this->createBackendConfig());
|
||||
$config = $this->createBackendConfig();
|
||||
$backend = new LdapUserBackend(new Connection($config), $config);
|
||||
$this->assertFalse($backend->authenticate(new Credential('unknown123', 'passunknown123')));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ require_once BaseTestCase::$libDir . '/Application/Logger.php';
|
|||
require_once BaseTestCase::$libDir . '/Authentication/Manager.php';
|
||||
require_once BaseTestCase::$libDir . '/Authentication/Credential.php';
|
||||
require_once BaseTestCase::$libDir . '/Exception/ConfigurationError.php';
|
||||
require_once BaseTestCase::$libDir . '/Exception/ProgrammingError.php';
|
||||
require_once 'BackendMock.php';
|
||||
require_once 'ErrorProneBackendMock.php';
|
||||
require_once 'SessionMock.php';
|
||||
|
|
Loading…
Reference in New Issue