Merge branch 'feature/add-inspectable-api-to-db-connections-9641'
resolves #9641
This commit is contained in:
commit
67f46bab27
|
@ -129,16 +129,13 @@ class DbResourceForm extends Form
|
||||||
*/
|
*/
|
||||||
public static function isValidResource(Form $form)
|
public static function isValidResource(Form $form)
|
||||||
{
|
{
|
||||||
try {
|
$result = ResourceFactory::createResource(new ConfigObject($form->getValues()))->inspect();
|
||||||
$resource = ResourceFactory::createResource(new ConfigObject($form->getValues()));
|
if ($result->hasError()) {
|
||||||
$resource->getConnection()->getConnection();
|
$form->addError(sprintf($form->translate('Connectivity validation failed: %s'), $result->getError()));
|
||||||
} catch (Exception $e) {
|
|
||||||
$form->addError(
|
|
||||||
$form->translate('Connectivity validation failed, connection to the given resource not possible.')
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// TODO: display diagnostics in $result->toArray() to the user
|
||||||
|
|
||||||
|
return ! $result->hasError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,18 +105,15 @@ class DbBackendForm extends Form
|
||||||
*/
|
*/
|
||||||
public static function isValidUserBackend(Form $form)
|
public static function isValidUserBackend(Form $form)
|
||||||
{
|
{
|
||||||
try {
|
$backend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig()));
|
||||||
$dbUserBackend = new DbUserBackend(ResourceFactory::createResource($form->getResourceConfig()));
|
$result = $backend->inspect();
|
||||||
if ($dbUserBackend->select()->where('is_active', true)->count() < 1) {
|
if ($result->hasError()) {
|
||||||
$form->addError($form->translate('No active users found under the specified database backend'));
|
$form->addError(sprintf($form->translate('Using the specified backend failed: %s'), $result->getError()));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$form->addError(sprintf($form->translate('Using the specified backend failed: %s'), $e->getMessage()));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
// TODO: display diagnostics in $result->toArray() to the user
|
||||||
|
|
||||||
|
return ! $result->hasError();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -4,13 +4,15 @@
|
||||||
namespace Icinga\Authentication\User;
|
namespace Icinga\Authentication\User;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Icinga\Data\Inspectable;
|
||||||
|
use Icinga\Data\Inspection;
|
||||||
use PDO;
|
use PDO;
|
||||||
use Icinga\Data\Filter\Filter;
|
use Icinga\Data\Filter\Filter;
|
||||||
use Icinga\Exception\AuthenticationException;
|
use Icinga\Exception\AuthenticationException;
|
||||||
use Icinga\Repository\DbRepository;
|
use Icinga\Repository\DbRepository;
|
||||||
use Icinga\User;
|
use Icinga\User;
|
||||||
|
|
||||||
class DbUserBackend extends DbRepository implements UserBackendInterface
|
class DbUserBackend extends DbRepository implements UserBackendInterface, Inspectable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The algorithm to use when hashing passwords
|
* The algorithm to use when hashing passwords
|
||||||
|
@ -246,4 +248,26 @@ class DbUserBackend extends DbRepository implements UserBackendInterface
|
||||||
{
|
{
|
||||||
return crypt($password, self::HASH_ALGORITHM . ($salt !== null ? $salt : $this->generateSalt()));
|
return crypt($password, self::HASH_ALGORITHM . ($salt !== null ? $salt : $this->generateSalt()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inspect this object to gain extended information about its health
|
||||||
|
*
|
||||||
|
* @return Inspection The inspection result
|
||||||
|
*/
|
||||||
|
public function inspect()
|
||||||
|
{
|
||||||
|
$insp = new Inspection('Db User Backend');
|
||||||
|
$insp->write($this->ds->inspect());
|
||||||
|
try {
|
||||||
|
$users = $this->select()->where('is_active', true)->count();
|
||||||
|
if ($users > 1) {
|
||||||
|
$insp->write(sprintf('%s active users', $users));
|
||||||
|
} else {
|
||||||
|
return $insp->error('0 active users', $users);
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$insp->error(sprintf('Query failed: %s', $e->getMessage()));
|
||||||
|
}
|
||||||
|
return $insp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
|
|
||||||
namespace Icinga\Data\Db;
|
namespace Icinga\Data\Db;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Icinga\Data\Inspectable;
|
||||||
|
use Icinga\Data\Inspection;
|
||||||
use PDO;
|
use PDO;
|
||||||
use Iterator;
|
use Iterator;
|
||||||
use Zend_Db;
|
use Zend_Db;
|
||||||
|
@ -23,7 +26,7 @@ use Icinga\Exception\ProgrammingError;
|
||||||
/**
|
/**
|
||||||
* Encapsulate database connections and query creation
|
* Encapsulate database connections and query creation
|
||||||
*/
|
*/
|
||||||
class DbConnection implements Selectable, Extensible, Updatable, Reducible
|
class DbConnection implements Selectable, Extensible, Updatable, Reducible, Inspectable
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Connection config
|
* Connection config
|
||||||
|
@ -435,4 +438,42 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible
|
||||||
return $column . ' ' . $sign . ' ' . $this->dbAdapter->quote($value);
|
return $column . ' ' . $sign . ' ' . $this->dbAdapter->quote($value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function inspect()
|
||||||
|
{
|
||||||
|
$insp = new Inspection('Db Connection');
|
||||||
|
try {
|
||||||
|
$this->getDbAdapter()->getConnection();
|
||||||
|
$config = $this->dbAdapter->getConfig();
|
||||||
|
$insp->write(sprintf(
|
||||||
|
'Connection to %s as %s on %s:%s successful',
|
||||||
|
$config['dbname'],
|
||||||
|
$config['username'],
|
||||||
|
$config['host'],
|
||||||
|
$config['port']
|
||||||
|
));
|
||||||
|
switch ($this->dbType) {
|
||||||
|
case 'mysql':
|
||||||
|
$rows = $this->dbAdapter->query(
|
||||||
|
'SHOW VARIABLES WHERE variable_name ' .
|
||||||
|
'IN (\'version\', \'protocol_version\', \'version_compile_os\');'
|
||||||
|
)->fetchAll();
|
||||||
|
$sqlinsp = new Inspection('MySQL');
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
$sqlinsp->write($row->variable_name . ': ' . $row->value);
|
||||||
|
}
|
||||||
|
$insp->write($sqlinsp);
|
||||||
|
break;
|
||||||
|
case 'pgsql':
|
||||||
|
$row = $this->dbAdapter->query('SELECT version();')->fetchAll();
|
||||||
|
$sqlinsp = new Inspection('PostgreSQL');
|
||||||
|
$sqlinsp->write($row[0]->version);
|
||||||
|
$insp->write($sqlinsp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception $e) {
|
||||||
|
return $insp->error(sprintf('Connection failed %s', $e->getMessage()));
|
||||||
|
}
|
||||||
|
return $insp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ class DbResourceFormTest extends BaseTestCase
|
||||||
public function testValidDbResourceIsValid()
|
public function testValidDbResourceIsValid()
|
||||||
{
|
{
|
||||||
$this->setUpResourceFactoryMock(
|
$this->setUpResourceFactoryMock(
|
||||||
Mockery::mock()->shouldReceive('getConnection')->atMost()->twice()->andReturn(Mockery::self())->getMock()
|
Mockery::mock()->shouldReceive('inspect')->andReturn(self::createInspector(false))->getMock()
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertTrue(
|
$this->assertTrue(
|
||||||
|
@ -42,7 +42,7 @@ class DbResourceFormTest extends BaseTestCase
|
||||||
public function testInvalidDbResourceIsNotValid()
|
public function testInvalidDbResourceIsNotValid()
|
||||||
{
|
{
|
||||||
$this->setUpResourceFactoryMock(
|
$this->setUpResourceFactoryMock(
|
||||||
Mockery::mock()->shouldReceive('getConnection')->once()->andThrow('\Exception')->getMock()
|
Mockery::mock()->shouldReceive('inspect')->andReturn(self::createInspector(true))->getMock()
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertFalse(
|
$this->assertFalse(
|
||||||
|
@ -58,4 +58,21 @@ class DbResourceFormTest extends BaseTestCase
|
||||||
->with(Mockery::type('Icinga\Data\ConfigObject'))
|
->with(Mockery::type('Icinga\Data\ConfigObject'))
|
||||||
->andReturn($resourceMock);
|
->andReturn($resourceMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function createInspector($error = false, $log = array('log'))
|
||||||
|
{
|
||||||
|
if (! $error) {
|
||||||
|
$calls = array(
|
||||||
|
'hasError' => false,
|
||||||
|
'toArray' => $log
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$calls = array(
|
||||||
|
'hasError' => true,
|
||||||
|
'getError' => 'Error',
|
||||||
|
'toArray' => $log
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Mockery::mock('Icinga\Data\Inspection', $calls);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,14 +28,16 @@ class DbBackendFormTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
$this->setUpResourceFactoryMock();
|
$this->setUpResourceFactoryMock();
|
||||||
Mockery::mock('overload:Icinga\Authentication\User\DbUserBackend')
|
Mockery::mock('overload:Icinga\Authentication\User\DbUserBackend')
|
||||||
->shouldReceive('select->where->count')
|
->shouldReceive('inspect')
|
||||||
->andReturn(2);
|
->andReturn(self::createInspector(false));
|
||||||
|
|
||||||
// Passing array(null) is required to make Mockery call the constructor...
|
// Passing array(null) is required to make Mockery call the constructor...
|
||||||
$form = Mockery::mock('Icinga\Forms\Config\UserBackend\DbBackendForm[getView]', array(null));
|
$form = Mockery::mock('Icinga\Forms\Config\UserBackend\DbBackendForm[getView]', array(null));
|
||||||
$form->shouldReceive('getView->escape')
|
$form->shouldReceive('getView->escape')
|
||||||
->with(Mockery::type('string'))
|
->with(Mockery::type('string'))
|
||||||
->andReturnUsing(function ($s) { return $s; });
|
->andReturnUsing(function ($s) {
|
||||||
|
return $s;
|
||||||
|
});
|
||||||
$form->setTokenDisabled();
|
$form->setTokenDisabled();
|
||||||
$form->setResources(array('test_db_backend'));
|
$form->setResources(array('test_db_backend'));
|
||||||
$form->populate(array('resource' => 'test_db_backend'));
|
$form->populate(array('resource' => 'test_db_backend'));
|
||||||
|
@ -54,14 +56,16 @@ class DbBackendFormTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
$this->setUpResourceFactoryMock();
|
$this->setUpResourceFactoryMock();
|
||||||
Mockery::mock('overload:Icinga\Authentication\User\DbUserBackend')
|
Mockery::mock('overload:Icinga\Authentication\User\DbUserBackend')
|
||||||
->shouldReceive('count')
|
->shouldReceive('inspect')
|
||||||
->andReturn(0);
|
->andReturn(self::createInspector(true));
|
||||||
|
|
||||||
// Passing array(null) is required to make Mockery call the constructor...
|
// Passing array(null) is required to make Mockery call the constructor...
|
||||||
$form = Mockery::mock('Icinga\Forms\Config\UserBackend\DbBackendForm[getView]', array(null));
|
$form = Mockery::mock('Icinga\Forms\Config\UserBackend\DbBackendForm[getView]', array(null));
|
||||||
$form->shouldReceive('getView->escape')
|
$form->shouldReceive('getView->escape')
|
||||||
->with(Mockery::type('string'))
|
->with(Mockery::type('string'))
|
||||||
->andReturnUsing(function ($s) { return $s; });
|
->andReturnUsing(function ($s) {
|
||||||
|
return $s;
|
||||||
|
});
|
||||||
$form->setTokenDisabled();
|
$form->setTokenDisabled();
|
||||||
$form->setResources(array('test_db_backend'));
|
$form->setResources(array('test_db_backend'));
|
||||||
$form->populate(array('resource' => 'test_db_backend'));
|
$form->populate(array('resource' => 'test_db_backend'));
|
||||||
|
@ -80,4 +84,21 @@ class DbBackendFormTest extends BaseTestCase
|
||||||
->shouldReceive('getResourceConfig')
|
->shouldReceive('getResourceConfig')
|
||||||
->andReturn(new ConfigObject());
|
->andReturn(new ConfigObject());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function createInspector($error = false, $log = array('log'))
|
||||||
|
{
|
||||||
|
if (! $error) {
|
||||||
|
$calls = array(
|
||||||
|
'hasError' => false,
|
||||||
|
'toArray' => $log
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$calls = array(
|
||||||
|
'hasError' => true,
|
||||||
|
'getError' => 'Error',
|
||||||
|
'toArray' => $log
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Mockery::mock('Icinga\Data\Inspection', $calls);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue