From 4b5cf47cce1e445f7acd0f35b0a97beb73e23462 Mon Sep 17 00:00:00 2001 From: Uwe Ebel Date: Wed, 23 May 2018 19:50:31 +0200 Subject: [PATCH 1/2] Introduce SQLite resource type refs #3381 Signed-off-by: Eric Lippmann --- .../forms/Config/Resource/DbResourceForm.php | 196 ++++++++++-------- library/Icinga/Application/Platform.php | 12 ++ library/Icinga/Data/Db/DbConnection.php | 4 + 3 files changed, 122 insertions(+), 90 deletions(-) diff --git a/application/forms/Config/Resource/DbResourceForm.php b/application/forms/Config/Resource/DbResourceForm.php index edf82e487..212305304 100644 --- a/application/forms/Config/Resource/DbResourceForm.php +++ b/application/forms/Config/Resource/DbResourceForm.php @@ -45,6 +45,9 @@ class DbResourceForm extends Form if (Platform::hasOciSupport()) { $dbChoices['oci'] = 'Oracle (OCI8)'; } + if (Platform::hasSqliteSupport()) { + $dbChoices['sqlite'] = 'SQLite'; + } $offerPostgres = false; $offerIbm = false; @@ -89,117 +92,130 @@ class DbResourceForm extends Form 'multiOptions' => $dbChoices ) ); - $this->addElement( - 'text', - 'host', - array ( - 'required' => true, - 'label' => $this->translate('Host'), - 'description' => $this->translate('The hostname of the database') - . ($socketInfo ? '. ' . $socketInfo : ''), - 'value' => 'localhost' - ) - ); - $this->addElement( - 'number', - 'port', - array( - 'description' => $this->translate('The port to use'), - 'label' => $this->translate('Port'), - 'preserveDefault' => true, - 'required' => $offerPostgres, - 'value' => $offerPostgres ? 5432 : null - ) - ); - $this->addElement( - 'text', - 'dbname', - array( - 'required' => true, - 'label' => $this->translate('Database Name'), - 'description' => $this->translate('The name of the database to use') - ) - ); - $this->addElement( - 'text', - 'username', - array ( - 'required' => true, - 'label' => $this->translate('Username'), - 'description' => $this->translate('The user name to use for authentication') - ) - ); - $this->addElement( - 'password', - 'password', - array( - 'renderPassword' => true, - 'label' => $this->translate('Password'), - 'description' => $this->translate('The password to use for authentication') - ) - ); - $this->addElement( - 'text', - 'charset', - array ( - 'description' => $this->translate('The character set for the database'), - 'label' => $this->translate('Character Set') - ) - ); - $this->addElement( - 'checkbox', - 'use_ssl', - array( - 'autosubmit' => true, - 'label' => $this->translate('Use SSL'), - 'description' => $this->translate( - 'Whether to encrypt the connection or to authenticate using certificates' - ) - ) - ); - if (isset($formData['use_ssl']) && $formData['use_ssl']) { + if ($dbChoice === 'sqlite') { $this->addElement( 'text', - 'ssl_key', + 'dbname', array( - 'label' => $this->translate('SSL Key'), - 'description' => $this->translate('The client key file path') + 'required' => true, + 'label' => $this->translate('Database Name'), + 'description' => $this->translate('The name of the database to use') + ) + ); + } else { + $this->addElement( + 'text', + 'host', + array ( + 'required' => true, + 'label' => $this->translate('Host'), + 'description' => $this->translate('The hostname of the database') + . ($socketInfo ? '. ' . $socketInfo : ''), + 'value' => 'localhost' + ) + ); + $this->addElement( + 'number', + 'port', + array( + 'description' => $this->translate('The port to use'), + 'label' => $this->translate('Port'), + 'preserveDefault' => true, + 'required' => $offerPostgres, + 'value' => $offerPostgres ? 5432 : null ) ); $this->addElement( 'text', - 'ssl_cert', + 'dbname', array( - 'label' => $this->translate('SSL Certificate'), - 'description' => $this->translate('The certificate file path') + 'required' => true, + 'label' => $this->translate('Database Name'), + 'description' => $this->translate('The name of the database to use') ) ); $this->addElement( 'text', - 'ssl_ca', + 'username', + array ( + 'required' => true, + 'label' => $this->translate('Username'), + 'description' => $this->translate('The user name to use for authentication') + ) + ); + $this->addElement( + 'password', + 'password', array( - 'label' => $this->translate('SSL CA'), - 'description' => $this->translate('The CA certificate file path') + 'required' => true, + 'renderPassword' => true, + 'label' => $this->translate('Password'), + 'description' => $this->translate('The password to use for authentication') ) ); $this->addElement( 'text', - 'ssl_capath', + 'charset', + array ( + 'description' => $this->translate('The character set for the database'), + 'label' => $this->translate('Character Set') + ) + ); + $this->addElement( + 'checkbox', + 'use_ssl', array( - 'label' => $this->translate('SSL CA Path'), - 'description' => $this->translate( - 'The trusted CA certificates in PEM format directory path' + 'autosubmit' => true, + 'label' => $this->translate('Use SSL'), + 'description' => $this->translate( + 'Whether to encrypt the connection or to authenticate using certificates' ) ) ); - $this->addElement( - 'text', - 'ssl_cipher', - array( - 'label' => $this->translate('SSL Cipher'), - 'description' => $this->translate('The list of permissible ciphers') - ) - ); + if (isset($formData['use_ssl']) && $formData['use_ssl']) { + $this->addElement( + 'text', + 'ssl_key', + array( + 'label' => $this->translate('SSL Key'), + 'description' => $this->translate('The client key file path') + ) + ); + $this->addElement( + 'text', + 'ssl_cert', + array( + 'label' => $this->translate('SSL Certificate'), + 'description' => $this->translate('The certificate file path') + ) + ); + $this->addElement( + 'text', + 'ssl_ca', + array( + 'label' => $this->translate('SSL CA'), + 'description' => $this->translate('The CA certificate file path') + ) + ); + $this->addElement( + 'text', + 'ssl_capath', + array( + 'label' => $this->translate('SSL CA Path'), + 'description' => $this->translate( + 'The trusted CA certificates in PEM format directory path' + ) + ) + ); + $this->addElement( + 'text', + 'ssl_cipher', + array( + 'label' => $this->translate('SSL Cipher'), + 'description' => $this->translate('The list of permissible ciphers') + ) + ); + } } return $this; diff --git a/library/Icinga/Application/Platform.php b/library/Icinga/Application/Platform.php index e05251801..b1b8d906d 100644 --- a/library/Icinga/Application/Platform.php +++ b/library/Icinga/Application/Platform.php @@ -420,4 +420,16 @@ class Platform { return static::extensionLoaded('pdo_pgsql') && static::classExists('Zend_Db_Adapter_Pdo_Pgsql'); } + + /** + * Return whether it's possible to connect to a SQLite database + * + * Checks whether the sqlite pdo extension has been loaded and the Zend framework adapter for SQLite is available + * + * @return bool + */ + public static function hasSqliteSupport() + { + return static::extensionLoaded('pdo_sqlite') && static::classExists('Zend_Db_Adapter_Pdo_Sqlite'); + } } diff --git a/library/Icinga/Data/Db/DbConnection.php b/library/Icinga/Data/Db/DbConnection.php index ffce026e2..701969360 100644 --- a/library/Icinga/Data/Db/DbConnection.php +++ b/library/Icinga/Data/Db/DbConnection.php @@ -224,6 +224,10 @@ class DbConnection implements Selectable, Extensible, Updatable, Reducible, Insp $adapter = 'Pdo_Ibm'; $defaultPort = 50000; break; + case 'sqlite': + $adapter = 'Pdo_Sqlite'; + $defaultPort = 0; // Dummy port because a value is required + break; default: throw new ConfigurationError( 'Backend "%s" is not supported', From f9af22e409d709ac65b867474053a9c63a83d127 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Fri, 8 Jun 2018 14:18:16 +0200 Subject: [PATCH 2/2] Remove unused variable --- application/forms/Config/Resource/DbResourceForm.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/application/forms/Config/Resource/DbResourceForm.php b/application/forms/Config/Resource/DbResourceForm.php index 212305304..518c42280 100644 --- a/application/forms/Config/Resource/DbResourceForm.php +++ b/application/forms/Config/Resource/DbResourceForm.php @@ -50,15 +50,12 @@ class DbResourceForm extends Form } $offerPostgres = false; - $offerIbm = false; $offerMysql = false; $dbChoice = isset($formData['db']) ? $formData['db'] : key($dbChoices); if ($dbChoice === 'pgsql') { $offerPostgres = true; } elseif ($dbChoice === 'mysql') { $offerMysql = true; - } elseif ($dbChoice === 'ibm') { - $offerIbm = true; } $socketInfo = '';