2013-06-28 19:00:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-07-29 11:42:31 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - 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>
|
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Tests\Icinga\Authentication;
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
require_once('Zend/Config.php');
|
2013-07-26 15:01:52 +02:00
|
|
|
require_once('Zend/Config/Ini.php');
|
2013-08-23 15:04:00 +02:00
|
|
|
require_once('Zend/Db/Adapter/Abstract.php');
|
2013-07-26 15:01:52 +02:00
|
|
|
require_once('Zend/Db.php');
|
2013-08-23 15:04:00 +02:00
|
|
|
require_once('Zend/Log.php');
|
|
|
|
require_once('../../library/Icinga/Util/ConfigAwareFactory.php');
|
2013-07-26 15:01:52 +02:00
|
|
|
require_once('../../library/Icinga/Authentication/UserBackend.php');
|
|
|
|
require_once('../../library/Icinga/Protocol/Ldap/Exception.php');
|
2013-08-23 15:04:00 +02:00
|
|
|
require_once('../../library/Icinga/Application/DbAdapterFactory.php');
|
2013-08-12 15:02:25 +02:00
|
|
|
require_once('../../library/Icinga/Application/Config.php');
|
2013-07-26 15:01:52 +02:00
|
|
|
require_once('../../library/Icinga/Authentication/Credentials.php');
|
|
|
|
require_once('../../library/Icinga/Authentication/Backend/DbUserBackend.php');
|
2013-07-30 10:47:50 +02:00
|
|
|
require_once('../../library/Icinga/User.php');
|
2013-08-23 15:04:00 +02:00
|
|
|
require_once('../../library/Icinga/Application/Logger.php');
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
use Zend_Config;
|
|
|
|
use Zend_Db_Adapter_Abstract;
|
2013-06-28 19:00:30 +02:00
|
|
|
use Icinga\Authentication\Backend\DbUserBackend;
|
2013-08-23 15:04:00 +02:00
|
|
|
use Icinga\Application\DbAdapterFactory;
|
2013-06-28 19:00:30 +02:00
|
|
|
use Icinga\Util\Crypto;
|
|
|
|
use Icinga\Authentication\Credentials;
|
2013-07-30 10:47:50 +02:00
|
|
|
use Icinga\User;
|
2013-08-12 15:02:25 +02:00
|
|
|
use \Icinga\Application\Config;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Class fpr DbUserBackend
|
|
|
|
*/
|
|
|
|
class DbUserBackendTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
/*
|
|
|
|
* Mapping of columns
|
|
|
|
*/
|
|
|
|
const USER_NAME_COLUMN = 'username';
|
|
|
|
|
|
|
|
const SALT_COLUMN = 'salt';
|
|
|
|
|
|
|
|
const PASSWORD_COLUMN = 'password';
|
|
|
|
|
|
|
|
const ACTIVE_COLUMN = 'active';
|
|
|
|
|
2013-07-29 12:37:48 +02:00
|
|
|
/**
|
|
|
|
* The table that is used to store the authentication data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-26 15:01:52 +02:00
|
|
|
private $testTable = 'account';
|
2013-07-29 12:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The database that is used to store the authentication data
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-07-26 15:01:52 +02:00
|
|
|
private $testDatabase = 'icinga_unittest';
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-29 12:37:48 +02:00
|
|
|
/**
|
|
|
|
* Example users
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
private $users;
|
2013-07-29 12:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The DbUserBackend configured to use MySQL
|
|
|
|
*
|
|
|
|
* @var DbUserBackend
|
|
|
|
*/
|
2013-07-25 10:05:47 +02:00
|
|
|
private $mysql;
|
2013-07-29 12:37:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The DbUserBackend configured to use PostgreSQL
|
|
|
|
*
|
|
|
|
* @var DbUserBackend
|
|
|
|
*/
|
2013-07-25 10:05:47 +02:00
|
|
|
private $pgsql;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-29 12:37:48 +02:00
|
|
|
/**
|
|
|
|
* Contains the PDO names used for the different SQL databases.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
private $dbTypeMap = Array(
|
|
|
|
'mysql' => 'PDO_MYSQL',
|
|
|
|
'pgsql' => 'PDO_PGSQL'
|
|
|
|
);
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-08-23 15:04:00 +02:00
|
|
|
* Create a preset configuration that can be used to access the database
|
2013-07-25 10:05:47 +02:00
|
|
|
* with the icinga_unittest account.
|
2013-07-29 11:42:31 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
|
|
|
*
|
|
|
|
* @return Zend_Config The created resource configuration
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-08-23 15:04:00 +02:00
|
|
|
private function getResourceConfig($dbType)
|
2013-07-25 10:05:47 +02:00
|
|
|
{
|
2013-08-23 15:04:00 +02:00
|
|
|
return new Zend_Config(
|
|
|
|
array(
|
|
|
|
'type' => 'db',
|
|
|
|
'db' => $dbType,
|
|
|
|
'host' => 'localhost',
|
|
|
|
'username' => 'icinga_unittest',
|
|
|
|
'password' => 'icinga_unittest',
|
|
|
|
'dbname' => $this->testDatabase,
|
|
|
|
'table' => $this->testTable
|
|
|
|
)
|
|
|
|
);
|
2013-07-25 10:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Create a backend with the given database type
|
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param String $dbType The database type as a string, like 'mysql' or 'pgsql'.
|
2013-07-29 12:37:48 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return DbUserBackend|null
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function createBackend($dbType)
|
|
|
|
{
|
|
|
|
try {
|
2013-08-23 15:04:00 +02:00
|
|
|
$db = $this->createDb($this->getResourceConfig($dbType));
|
|
|
|
$this->setUpDb($db,$dbType);
|
2013-08-13 18:08:21 +02:00
|
|
|
return new DbUserBackend($db);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) {
|
2013-07-26 15:01:52 +02:00
|
|
|
echo 'CREATE_BACKEND_ERROR:'.$e->getMessage();
|
2013-07-25 10:05:47 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
/**
|
|
|
|
* Create the db adapter
|
|
|
|
*
|
|
|
|
* @param $config The configuration to use
|
|
|
|
*
|
|
|
|
* @return Zend_Db_Adapter_Abstract The created adabter
|
|
|
|
*/
|
|
|
|
private function createDb($config)
|
|
|
|
{
|
|
|
|
return DbAdapterFactory::createDbAdapterFromConfig($config);
|
|
|
|
}
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Create the backends and fill it with sample-data
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
protected function setUp()
|
|
|
|
{
|
2013-08-23 15:04:00 +02:00
|
|
|
DbAdapterFactory::resetConfig();
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->users = Array(
|
|
|
|
0 => Array(
|
2013-08-23 15:04:00 +02:00
|
|
|
self::USER_NAME_COLUMN => 'user1',
|
|
|
|
self::PASSWORD_COLUMN => 'secret1',
|
|
|
|
self::SALT_COLUMN => '8a7487a539c5d1d6766639d04d1ed1e6',
|
|
|
|
self::ACTIVE_COLUMN => 1
|
2013-06-28 19:00:30 +02:00
|
|
|
),
|
|
|
|
1 => Array(
|
2013-08-23 15:04:00 +02:00
|
|
|
self::USER_NAME_COLUMN => 'user2',
|
|
|
|
self::PASSWORD_COLUMN => 'secret2',
|
|
|
|
self::SALT_COLUMN => '04b5521ddd761b5a5b633be83faa494d',
|
|
|
|
self::ACTIVE_COLUMN => 1
|
2013-06-28 19:00:30 +02:00
|
|
|
),
|
|
|
|
2 => Array(
|
2013-08-23 15:04:00 +02:00
|
|
|
self::USER_NAME_COLUMN => 'user3',
|
|
|
|
self::PASSWORD_COLUMN => 'secret3',
|
|
|
|
self::SALT_COLUMN => '08bb94ba3120338ae56db80ef551d324',
|
|
|
|
self::ACTIVE_COLUMN => 0
|
2013-06-28 19:00:30 +02:00
|
|
|
)
|
|
|
|
);
|
2013-07-26 15:01:52 +02:00
|
|
|
$this->mysql = $this->createBackend('mysql');
|
|
|
|
$this->pgsql = $this->createBackend('pgsql');
|
2013-07-25 10:05:47 +02:00
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-29 11:42:31 +02:00
|
|
|
* Test the authentication functions of the DbUserBackend using PostgreSQL as backend.
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-07-29 11:42:31 +02:00
|
|
|
public function testCorrectUserLoginForPgsql()
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-08-23 15:04:00 +02:00
|
|
|
if (!empty($this->pgsql)) {
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->runBackendAuthentication($this->pgsql);
|
|
|
|
$this->runBackendUsername($this->pgsql);
|
|
|
|
}
|
2013-08-23 15:04:00 +02:00
|
|
|
else {
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-29 11:42:31 +02:00
|
|
|
* Test the authentication functions of the DbUserBackend using MySQL as backend.
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-07-29 11:42:31 +02:00
|
|
|
public function testCorrectUserLoginForMySQL()
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
if(!empty($this->mysql)){
|
|
|
|
$this->runBackendAuthentication($this->mysql);
|
|
|
|
$this->runBackendUsername($this->mysql);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$this->markTestSkipped();
|
|
|
|
}
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Try to drop all databases that may eventually be present
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-06-28 19:00:30 +02:00
|
|
|
public function tearDown()
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
try{
|
2013-08-23 15:04:00 +02:00
|
|
|
$db = $this->createDb($this->getResourceConfig('mysql'));
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->tearDownDb($db);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) { }
|
|
|
|
try {
|
2013-08-23 15:04:00 +02:00
|
|
|
$db = $this->createDb($this->getResourceConfig('pgsql'));
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->tearDownDb($db);
|
2013-07-25 16:47:43 +02:00
|
|
|
} catch(\Exception $e) { }
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Drop the test database in the given db
|
|
|
|
*
|
2013-07-25 10:05:47 +02:00
|
|
|
* @param $db
|
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function tearDownDb($db)
|
|
|
|
{
|
2013-07-25 10:05:47 +02:00
|
|
|
$db->exec('DROP TABLE '.$this->testTable);
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Fill the given database with the sample-data provided in users
|
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $db Zend_Db_Adapter_Abstract The database to set up
|
|
|
|
* @param $type String The database type as a string: 'mysql'|'pgsql'
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-08-23 15:04:00 +02:00
|
|
|
private function setUpDb($db,$type)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-07-26 15:01:52 +02:00
|
|
|
try {
|
|
|
|
$this->tearDownDb($db);
|
2013-08-23 15:04:00 +02:00
|
|
|
} catch (\Exception $e) {}
|
|
|
|
|
|
|
|
$setupScript = file_get_contents('../../etc/schema/accounts.' . $type . '.sql');
|
|
|
|
$db->exec($setupScript);
|
|
|
|
|
2013-07-25 16:47:43 +02:00
|
|
|
for ($i = 0; $i < count($this->users); $i++) {
|
2013-06-28 19:00:30 +02:00
|
|
|
$usr = $this->users[$i];
|
|
|
|
$data = Array(
|
2013-08-23 15:04:00 +02:00
|
|
|
self::USER_NAME_COLUMN => $usr[self::USER_NAME_COLUMN],
|
|
|
|
self::PASSWORD_COLUMN => hash_hmac('sha256',
|
|
|
|
$usr[self::SALT_COLUMN],
|
|
|
|
$usr[self::PASSWORD_COLUMN]
|
|
|
|
),
|
|
|
|
self::ACTIVE_COLUMN => $usr[self::ACTIVE_COLUMN],
|
|
|
|
self::SALT_COLUMN => $usr[self::SALT_COLUMN]
|
2013-06-28 19:00:30 +02:00
|
|
|
);
|
|
|
|
$db->insert($this->testTable,$data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Run the hasUsername test against an instance of DbUserBackend
|
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $backend The backend that will be tested.
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function runBackendUsername($backend)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
// Known user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertTrue($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[0][self::USER_NAME_COLUMN],
|
|
|
|
$this->users[0][self::PASSWORD_COLUMN])
|
|
|
|
), 'Assert that the user is known by the backend');
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
// Unknown user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertFalse($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-07-29 11:42:31 +02:00
|
|
|
'unknown user',
|
2013-06-28 19:00:30 +02:00
|
|
|
'secret')
|
2013-08-23 15:04:00 +02:00
|
|
|
), 'Assert that the user is not known by the backend');
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
// Inactive user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertFalse($backend->hasUsername(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[2][self::USER_NAME_COLUMN],
|
|
|
|
$this->users[2][self::PASSWORD_COLUMN])
|
|
|
|
), 'Assert that the user is inactive and therefore not known by the backend');
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Run the authentication test against an instance of DbUserBackend
|
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $backend The backend that will be tested.
|
2013-07-25 10:05:47 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function runBackendAuthentication($backend)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
// Known user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertNotNull($backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[0][self::USER_NAME_COLUMN],
|
|
|
|
$this->users[0][self::PASSWORD_COLUMN])
|
|
|
|
), 'Assert that an existing, active user with the right credentials can authenticate.');
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
// Wrong password
|
|
|
|
$this->assertNull(
|
2013-07-25 10:05:47 +02:00
|
|
|
$backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[1][self::USER_NAME_COLUMN],
|
2013-06-28 19:00:30 +02:00
|
|
|
'wrongpassword')
|
2013-08-23 15:04:00 +02:00
|
|
|
), 'Assert that an existing user with an invalid password cannot authenticate'
|
2013-06-28 19:00:30 +02:00
|
|
|
);
|
|
|
|
|
2013-07-25 10:05:47 +02:00
|
|
|
// Nonexisting user
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->assertNull(
|
2013-07-25 10:05:47 +02:00
|
|
|
$backend->authenticate(
|
2013-06-28 19:00:30 +02:00
|
|
|
new Credentials(
|
2013-07-25 10:05:47 +02:00
|
|
|
'nonexisting user',
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[1][self::PASSWORD_COLUMN])
|
|
|
|
), 'Assert that a non-existing user cannot authenticate.'
|
2013-06-28 19:00:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Inactive user
|
2013-07-25 10:05:47 +02:00
|
|
|
$this->assertNull(
|
|
|
|
$backend->authenticate(
|
|
|
|
new Credentials(
|
2013-08-23 15:04:00 +02:00
|
|
|
$this->users[2][self::USER_NAME_COLUMN],
|
|
|
|
$this->users[2][self::PASSWORD_COLUMN])
|
|
|
|
), 'Assert that an inactive user cannot authenticate.');
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
}
|