From e8d421ae0416652b4849b6b0a3f49a1560feb579 Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Mon, 13 Apr 2020 15:40:33 +0200 Subject: [PATCH] added certificates to export --- lam/lib/config.inc | 46 +++++++++++++++ lam/lib/persistence.inc | 72 ++++++++++++++++++++--- lam/templates/config/confImportExport.php | 7 ++- lam/tests/lib/persistenceTest.php | 8 ++- 4 files changed, 122 insertions(+), 11 deletions(-) diff --git a/lam/lib/config.inc b/lam/lib/config.inc index 201ddf2c..06a6eca7 100644 --- a/lam/lib/config.inc +++ b/lam/lib/config.inc @@ -2698,6 +2698,7 @@ class LAMCfgMain { * Imports configuration data. * * @param array $data config data + * @throws LAMException import error */ public function importData($data) { foreach ($data as $dataKey => $dataValue) { @@ -2712,6 +2713,51 @@ class LAMCfgMain { } } + /** + * Returns the content of the server certificates file + * + * @return null|string certificates + */ + public function exportCertificates() { + $fileName = $this->getSSLCaCertPath(); + if ($fileName === null) { + return null; + } + $content = null; + $handle = @fopen($fileName, "r"); + if ($handle) { + $content = fread($handle, 10000000); + fclose($handle); + } + return $content; + } + + /** + * Imports the server certificates. + * + * @param null|string $certsContent certificates + * @throws LAMException write to file failed + */ + public function importCertificates($certsContent) { + $fileName = $this->getSSLCaCertPath(); + if (empty($certsContent)) { + if ($fileName !== null) { + unlink($fileName); + } + return; + } + $fileName = $this->getInternalSSLCaCertFileName(); + $handle = @fopen($fileName, "wb"); + if ($handle) { + fputs($handle, $certsContent); + fclose($handle); + @chmod($fileName, 0600); + } + else { + throw new LAMException(printf(_('Unable to write file %s.'), $fileName)); + } + } + /** * Reloads preferences from config file config.cfg * diff --git a/lam/lib/persistence.inc b/lam/lib/persistence.inc index dadbeb31..46401611 100644 --- a/lam/lib/persistence.inc +++ b/lam/lib/persistence.inc @@ -42,21 +42,52 @@ class ConfigDataExporter { * Exports LAM's configuration data in JSON format. */ public function exportAsJson() { + $mainCfg = $this->_getMainConfiguration(); $jsonData = array(); - $jsonData['mainConfig'] = $this->_getMainConfigData(); + $jsonData['mainConfig'] = $this->_getMainConfigData($mainCfg); + $jsonData['certificates'] = $this->_getCertificates($mainCfg); + /** + * TODO + * + * webauthn + * server profiles + * server profile job database + * account profiles + * PDF profiles + * self service profiles + */ return json_encode($jsonData); } + /** + * Returns the main configuration. + * + * @return LAMCfgMain main config + */ + public function _getMainConfiguration() { + return new LAMCfgMain(); + } + /** * Internal function to read master configuration. * + * @param LAMCfgMain $mainCfg main config * @return array data */ - public function _getMainConfigData() { - $mainCfg = new LAMCfgMain(); + public function _getMainConfigData($mainCfg) { return $mainCfg->exportData(); } + /** + * Returns the certificate file content. + * + * @param LAMCfgMain $mainCfg main config + * @return array data + */ + public function _getCertificates($mainCfg) { + return $mainCfg->exportCertificates(); + } + } /** @@ -82,6 +113,9 @@ class ConfigDataImporter { case 'mainConfig': $steps[] = new ImporterStep(_('General settings'), 'mainConfig', $value); break; + case 'certificates': + $steps[] = new ImporterStep(_('SSL certificates'), 'certificates', $value); + break; default: logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); } @@ -96,7 +130,7 @@ class ConfigDataImporter { * Runs the actual import. * * @param ImporterStep[] $steps import steps - * @throws LAMException if error occured + * @throws LAMException if error occurred */ public function runImport($steps) { foreach ($steps as $step) { @@ -106,9 +140,10 @@ class ConfigDataImporter { $key = $step->getKey(); switch ($key) { case 'mainConfig': - $cfgMain = new LAMCfgMain(); - $cfgMain->importData($step->getValue()); - $cfgMain->save(); + $this->importMainConfig($step->getValue()); + break; + case 'certificates': + $this->importCertificates($step->getValue()); break; default: logNewMessage(LOG_WARNING, 'Unknown import type: ' . $key); @@ -116,6 +151,29 @@ class ConfigDataImporter { } } + /** + * Imports the main configuration. + * + * @param array $data main config data + * @throws LAMException error during import + */ + private function importMainConfig($data) { + $cfgMain = new LAMCfgMain(); + $cfgMain->importData($data); + $cfgMain->save(); + } + + /** + * Imports the SSL certificates. + * + * @param null|string $data file content + * @throws LAMException error during import + */ + private function importCertificates($data) { + $cfgMain = new LAMCfgMain(); + $cfgMain->importCertificates($data); + } + } /** diff --git a/lam/templates/config/confImportExport.php b/lam/templates/config/confImportExport.php index 0f461f71..7c54e8b4 100644 --- a/lam/templates/config/confImportExport.php +++ b/lam/templates/config/confImportExport.php @@ -1,6 +1,7 @@ add(new htmlResponsiveInputCheckbox('step_' . $importStep->getKey(), true, $importStep->getLabel()), 12); } - $content->add(new htmlButton('importConfigConfirm', _('Import')), 12); - $content->add(new htmlButton('importCancel', _('Cancel')), 12); + $buttonGroup = new htmlGroup(); + $buttonGroup->addElement(new htmlButton('importConfigConfirm', _('Import'))); + $buttonGroup->addElement(new htmlButton('importCancel', _('Cancel'))); + $content->add($buttonGroup, 12); } elseif (isset($_POST['importConfigConfirm'])) { $handle = fopen($_SESSION['configImportFile'], "r"); diff --git a/lam/tests/lib/persistenceTest.php b/lam/tests/lib/persistenceTest.php index a02e031d..622b19b2 100644 --- a/lam/tests/lib/persistenceTest.php +++ b/lam/tests/lib/persistenceTest.php @@ -37,12 +37,16 @@ class ConfigDataExporterTest extends TestCase { 'confMainKey2' => 4, 'confMainKey3' => '', ); - $expectedJson = json_encode(array('mainConfig' => $mainData)); + $expectedJson = json_encode(array( + 'mainConfig' => $mainData, + 'certificates' => 'certs' + )); $exporter = $this->getMockBuilder('\LAM\PERSISTENCE\ConfigDataExporter') - ->setMethods(array('_getMainConfigData')) + ->setMethods(array('_getMainConfigData', '_getCertificates')) ->getMock(); $exporter->method('_getMainConfigData')->willReturn($mainData); + $exporter->method('_getCertificates')->willReturn('certs'); $json = $exporter->exportAsJson();